From afdb3a0e3e0ac4df00385ea30b9cdd44ae0aea03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=BCseyin=20Serhat=20Han?= Date: Fri, 19 Jul 2024 18:49:25 +0300 Subject: [PATCH] Query command & Worldguard null parameter exception fix (#4) * query command + fix wg exception * remove empty statement and unused imports * added feedback for case when there are no protections in queried location --------- Co-authored-by: YouHaveTrouble --- .../yardwatch/commands/YardWatchCommand.java | 35 +++++++++++++++++++ .../yardwatch/hooks/WorldGuardProtection.java | 4 ++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/main/java/me/youhavetrouble/yardwatch/commands/YardWatchCommand.java b/src/main/java/me/youhavetrouble/yardwatch/commands/YardWatchCommand.java index dd74713..241d2a2 100644 --- a/src/main/java/me/youhavetrouble/yardwatch/commands/YardWatchCommand.java +++ b/src/main/java/me/youhavetrouble/yardwatch/commands/YardWatchCommand.java @@ -2,9 +2,13 @@ package me.youhavetrouble.yardwatch.commands; import me.youhavetrouble.yardwatch.Protection; import me.youhavetrouble.yardwatch.YardWatch; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.event.HoverEvent; +import org.bukkit.Location; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabExecutor; +import org.bukkit.entity.Player; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.util.StringUtil; import org.jetbrains.annotations.NotNull; @@ -34,6 +38,9 @@ public class YardWatchCommand implements TabExecutor { if (args[0].equalsIgnoreCase("hooks")) { sendHooks(sender); return true; + } else if (args[0].equalsIgnoreCase("query")) { + queryProtection(sender); + return true; } return false; @@ -45,6 +52,7 @@ public class YardWatchCommand implements TabExecutor { if (args.length == 1) { completions.add("hooks"); + completions.add("query"); return StringUtil.copyPartialMatches(args[0], completions, new ArrayList<>()); } @@ -72,4 +80,31 @@ public class YardWatchCommand implements TabExecutor { } } + + private void queryProtection(CommandSender sender) { + if (!(sender instanceof Player player)) { + sender.sendMessage(("You must be a player to use this command!")); + return; + } + Location location = player.getLocation(); + sender.sendMessage("Protections at " + location.getBlockX() + ", " + location.getBlockY() + ", " + location.getBlockZ()); + Collection> protections = plugin.getServer().getServicesManager().getRegistrations(Protection.class); + if (protections.isEmpty()) { + sender.sendMessage("No protections registered at " + location.getBlockX() + ", " + location.getBlockY() + ", " + location.getBlockZ()); + return; + } + for (RegisteredServiceProvider protection : protections) { + Component component = Component.text(protection.getPlugin().getName()).append(Component.text(" isProtected " + protection.getProvider().isProtected(location))) + .hoverEvent(HoverEvent.showText( + Component.text(protection.getProvider().toString()) + .append(Component.newline()) + .append(Component.text("canPlaceBlock " + protection.getProvider().canPlaceBlock(player, location)) + .append(Component.newline()) + .append(Component.text("canBreakBlock " + protection.getProvider().canBreakBlock(player, location.getBlock().getState()))) + .append(Component.newline()) + .append(Component.text("canInteract " + protection.getProvider().canInteract(player, location.getBlock().getState()))) + ))); + sender.sendMessage(component); + } + } } diff --git a/src/main/java/me/youhavetrouble/yardwatch/hooks/WorldGuardProtection.java b/src/main/java/me/youhavetrouble/yardwatch/hooks/WorldGuardProtection.java index d8b5916..3315c16 100644 --- a/src/main/java/me/youhavetrouble/yardwatch/hooks/WorldGuardProtection.java +++ b/src/main/java/me/youhavetrouble/yardwatch/hooks/WorldGuardProtection.java @@ -4,6 +4,8 @@ import com.sk89q.worldedit.bukkit.BukkitAdapter; import com.sk89q.worldguard.LocalPlayer; import com.sk89q.worldguard.WorldGuard; import com.sk89q.worldguard.bukkit.WorldGuardPlugin; +import com.sk89q.worldguard.domains.Association; +import com.sk89q.worldguard.protection.association.Associables; import com.sk89q.worldguard.protection.flags.Flags; import com.sk89q.worldguard.protection.regions.RegionContainer; import com.sk89q.worldguard.protection.regions.RegionQuery; @@ -34,7 +36,7 @@ public class WorldGuardProtection implements Protection { com.sk89q.worldedit.util.Location wgLocation = BukkitAdapter.adapt(location); RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer(); RegionQuery query = container.createQuery(); - return query.testBuild(wgLocation, null); + return query.testBuild(wgLocation, Associables.constant(Association.NON_MEMBER)); } @Override