From ef9324efd17ef701559fb27c7c8bc8d8ab3549ce Mon Sep 17 00:00:00 2001 From: Hutchy Date: Fri, 31 Jan 2020 10:56:43 -0500 Subject: [PATCH] Improve command execute checks with args. Closes #101 --- .../griefdefender/command/CommandHelper.java | 4 +++ .../listener/CommandEventHandler.java | 36 +++++++++++++++---- .../griefdefender/command/CommandHelper.java | 4 +++ .../listener/PlayerEventHandler.java | 33 +++++++++++++---- 4 files changed, 65 insertions(+), 12 deletions(-) diff --git a/bukkit/src/main/java/com/griefdefender/command/CommandHelper.java b/bukkit/src/main/java/com/griefdefender/command/CommandHelper.java index 54f3bbb..2e5a356 100644 --- a/bukkit/src/main/java/com/griefdefender/command/CommandHelper.java +++ b/bukkit/src/main/java/com/griefdefender/command/CommandHelper.java @@ -953,6 +953,10 @@ public static String handleCommandFlag(CommandSender src, String target) { } private static boolean validateCommandMapping(CommandSender src, String command, String pluginId) { + if (command.equalsIgnoreCase("any")) { + return true; + } + Command commandMapping = NMSUtil.getInstance().getBukkitCommandMap().getCommand(command); if (commandMapping == null) { TextAdapter.sendComponent(src, MessageStorage.MESSAGE_DATA.getMessage(MessageStorage.PLUGIN_COMMAND_NOT_FOUND, diff --git a/bukkit/src/main/java/com/griefdefender/listener/CommandEventHandler.java b/bukkit/src/main/java/com/griefdefender/listener/CommandEventHandler.java index 9a9058d..1414ab2 100644 --- a/bukkit/src/main/java/com/griefdefender/listener/CommandEventHandler.java +++ b/bukkit/src/main/java/com/griefdefender/listener/CommandEventHandler.java @@ -30,6 +30,7 @@ import com.griefdefender.GDTimings; import com.griefdefender.GriefDefenderPlugin; import com.griefdefender.api.Tristate; +import com.griefdefender.api.claim.TrustTypes; import com.griefdefender.api.permission.flag.Flags; import com.griefdefender.api.permission.option.Options; import com.griefdefender.claim.GDClaim; @@ -202,10 +203,21 @@ public void onPlayerCommand(PlayerCommandPreprocessEvent event) { if (GDFlags.COMMAND_EXECUTE && !inPvpCombat && !commandExecuteSourceBlacklisted && !commandExecuteTargetBlacklisted) { // First check base command - Tristate result = GDPermissionManager.getInstance().getFinalPermission(event, player.getLocation(), claim, Flags.COMMAND_EXECUTE, player, commandBaseTarget, player, true); - if (result != Tristate.FALSE) { - // check with args - result = GDPermissionManager.getInstance().getFinalPermission(event, player.getLocation(), claim, Flags.COMMAND_EXECUTE, player, commandTargetWithArgs, player, true); + Tristate result = GDPermissionManager.getInstance().getFinalPermission(event, player.getLocation(), claim, Flags.COMMAND_EXECUTE, player, commandBaseTarget, player, TrustTypes.MANAGER, true); + if (result != Tristate.FALSE && args.length > 0) { + // Check with args + // Test with each arg, break once result returns false + String commandBaseTargetArgCheck = commandBaseTarget; + for (String arg : args) { + if (!arg.isEmpty()) { + commandBaseTargetArgCheck = commandBaseTargetArgCheck + "." + arg; + result = GDPermissionManager.getInstance().getFinalPermission(event, player.getLocation(), claim, Flags.COMMAND_EXECUTE, player, commandBaseTargetArgCheck, player, TrustTypes.MANAGER, true); + if (result == Tristate.FALSE) { + break; + } + + } + } } if (result == Tristate.FALSE) { final Component denyMessage = GriefDefenderPlugin.getInstance().messageData.getMessage(MessageStorage.COMMAND_BLOCKED, @@ -224,9 +236,19 @@ public void onPlayerCommand(PlayerCommandPreprocessEvent event) { if (GDFlags.COMMAND_EXECUTE_PVP && inPvpCombat && !commandExecuteSourceBlacklisted && !commandExecuteTargetBlacklisted) { // First check base command Tristate result = GDPermissionManager.getInstance().getFinalPermission(event, player.getLocation(), claim, Flags.COMMAND_EXECUTE_PVP, player, commandBaseTarget, player, true); - if (result != Tristate.FALSE) { + if (result != Tristate.FALSE && args.length > 0) { // check with args - result = GDPermissionManager.getInstance().getFinalPermission(event, player.getLocation(), claim, Flags.COMMAND_EXECUTE_PVP, player, commandTargetWithArgs, player, true); + // Test with each arg, break once result returns false + String commandBaseTargetArgCheck = commandBaseTarget; + for (String arg : args) { + if (!arg.isEmpty()) { + commandBaseTargetArgCheck = commandBaseTargetArgCheck + "." + arg; + result = GDPermissionManager.getInstance().getFinalPermission(event, player.getLocation(), claim, Flags.COMMAND_EXECUTE_PVP, player, commandBaseTargetArgCheck, player, true); + if (result == Tristate.FALSE) { + break; + } + } + } } if (result == Tristate.FALSE) { final Component denyMessage = GriefDefenderPlugin.getInstance().messageData.getMessage(MessageStorage.COMMAND_BLOCKED, @@ -235,6 +257,8 @@ public void onPlayerCommand(PlayerCommandPreprocessEvent event) { "player", claim.getOwnerName())); GriefDefenderPlugin.sendMessage(player, denyMessage); event.setCancelled(true); + GDTimings.PLAYER_COMMAND_EVENT.stopTiming(); + return; } } GDTimings.PLAYER_COMMAND_EVENT.stopTiming(); diff --git a/sponge/src/main/java/com/griefdefender/command/CommandHelper.java b/sponge/src/main/java/com/griefdefender/command/CommandHelper.java index 4e5b53e..0d146c5 100644 --- a/sponge/src/main/java/com/griefdefender/command/CommandHelper.java +++ b/sponge/src/main/java/com/griefdefender/command/CommandHelper.java @@ -964,6 +964,10 @@ public static String handleCommandFlag(CommandSource src, String target) { } private static boolean validateCommandMapping(CommandSource src, String command, String pluginId) { + if (command.equals("any")) { + return true; + } + CommandMapping commandMapping = Sponge.getCommandManager().get(command).orElse(null); if (commandMapping == null) { TextAdapter.sendComponent(src, MessageStorage.MESSAGE_DATA.getMessage(MessageStorage.PLUGIN_COMMAND_NOT_FOUND, diff --git a/sponge/src/main/java/com/griefdefender/listener/PlayerEventHandler.java b/sponge/src/main/java/com/griefdefender/listener/PlayerEventHandler.java index 9c4e7be..a67130c 100644 --- a/sponge/src/main/java/com/griefdefender/listener/PlayerEventHandler.java +++ b/sponge/src/main/java/com/griefdefender/listener/PlayerEventHandler.java @@ -364,10 +364,21 @@ public void onPlayerCommand(SendCommandEvent event, @First Player player) { if (GDFlags.COMMAND_EXECUTE && !inPvpCombat && !commandExecuteSourceBlacklisted && !commandExecuteTargetBlacklisted) { // First check base command - Tristate result = GDPermissionManager.getInstance().getFinalPermission(event, player.getLocation(), claim, Flags.COMMAND_EXECUTE, event.getSource(), commandBaseTarget, player, true); - if (result != Tristate.FALSE) { - // check with args - result = GDPermissionManager.getInstance().getFinalPermission(event, player.getLocation(), claim, Flags.COMMAND_EXECUTE, event.getSource(), commandTargetWithArgs, player, true); + Tristate result = GDPermissionManager.getInstance().getFinalPermission(event, player.getLocation(), claim, Flags.COMMAND_EXECUTE, event.getSource(), commandBaseTarget, player, TrustTypes.MANAGER, true); + if (result != Tristate.FALSE && args.length > 0) { + // Check with args + // Test with each arg, break once result returns false + String commandBaseTargetArgCheck = commandBaseTarget; + for (String arg : args) { + if (!arg.isEmpty()) { + commandBaseTargetArgCheck = commandBaseTargetArgCheck + "." + arg; + result = GDPermissionManager.getInstance().getFinalPermission(event, player.getLocation(), claim, Flags.COMMAND_EXECUTE, event.getSource(), commandBaseTargetArgCheck, player, TrustTypes.MANAGER, true); + if (result == Tristate.FALSE) { + break; + } + + } + } } if (result == Tristate.FALSE) { final Component denyMessage = GriefDefenderPlugin.getInstance().messageData.getMessage(MessageStorage.COMMAND_BLOCKED, @@ -385,9 +396,19 @@ public void onPlayerCommand(SendCommandEvent event, @First Player player) { if (GDFlags.COMMAND_EXECUTE_PVP && inPvpCombat && !commandExecuteSourceBlacklisted && !commandExecuteTargetBlacklisted) { // First check base command Tristate result = GDPermissionManager.getInstance().getFinalPermission(event, player.getLocation(), claim, Flags.COMMAND_EXECUTE_PVP, event.getSource(), commandBaseTarget, player, true); - if (result != Tristate.FALSE) { + if (result != Tristate.FALSE && args.length > 0) { // check with args - result = GDPermissionManager.getInstance().getFinalPermission(event, player.getLocation(), claim, Flags.COMMAND_EXECUTE_PVP, event.getSource(), commandTargetWithArgs, player, true); + // Test with each arg, break once result returns false + String commandBaseTargetArgCheck = commandBaseTarget; + for (String arg : args) { + if (!arg.isEmpty()) { + commandBaseTargetArgCheck = commandBaseTargetArgCheck + "." + arg; + result = GDPermissionManager.getInstance().getFinalPermission(event, player.getLocation(), claim, Flags.COMMAND_EXECUTE_PVP, event.getSource(), commandBaseTargetArgCheck, player, true); + if (result == Tristate.FALSE) { + break; + } + } + } } if (result == Tristate.FALSE) { final Component denyMessage = GriefDefenderPlugin.getInstance().messageData.getMessage(MessageStorage.COMMAND_BLOCKED,