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 <youhavetrouble@youhavetrouble.me>
This commit is contained in:
Hüseyin Serhat Han
2024-07-19 18:49:25 +03:00
committed by GitHub
parent 3833c5ce43
commit afdb3a0e3e
2 changed files with 38 additions and 1 deletions
@@ -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<RegisteredServiceProvider<Protection>> 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> 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);
}
}
}
@@ -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