From 8276406f9fd132794778785d13f06fa742c98f0a Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 12 Feb 2023 08:22:22 -0800 Subject: [PATCH] Fix runCommands method Note to future self, don't remove Util methods because someone might be using them! --- .../world/bentobox/bentobox/util/Util.java | 19 +++++++++++++++---- .../bentobox/bentobox/util/UtilTest.java | 14 +++++++------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/util/Util.java b/src/main/java/world/bentobox/bentobox/util/Util.java index d76f9c12f..7a633bd31 100644 --- a/src/main/java/world/bentobox/bentobox/util/Util.java +++ b/src/main/java/world/bentobox/bentobox/util/Util.java @@ -357,8 +357,8 @@ public class Util { // Most of passive mobs extends Animals return entity instanceof Animals || entity instanceof IronGolem || entity instanceof Snowman || - entity instanceof WaterMob && !(entity instanceof PufferFish) || entity instanceof Bat || - entity instanceof Allay; + entity instanceof WaterMob && !(entity instanceof PufferFish) || entity instanceof Bat || + entity instanceof Allay; } /* @@ -659,12 +659,23 @@ public class Util { return null; } + /** + * Run a list of commands for a user + * @param user - user affected by the commands + * @param commands - a list of commands + * @param commandType - the type of command being run - used in the console error message + */ + public static void runCommands(User user, @NonNull List commands, String commandType) { + runCommands(user, user.getName(), commands, commandType); + } + /** * Run a list of commands for a user * @param user - user affected by the commands * @param ownerName - name of the island owner, or the user's name if it is the user's island * @param commands - a list of commands * @param commandType - the type of command being run - used in the console error message + * @since 1.22.0 */ public static void runCommands(User user, String ownerName, @NonNull List commands, String commandType) { commands.forEach(command -> { @@ -776,7 +787,7 @@ public class Util { public static String sanitizeInput(String input) { return ChatColor.stripColor( - Util.translateColorCodes(input.replaceAll("[\\\\/:*?\"<>|\s]", "_"))). - toLowerCase(); + Util.translateColorCodes(input.replaceAll("[\\\\/:*?\"<>|\s]", "_"))). + toLowerCase(); } } diff --git a/src/test/java/world/bentobox/bentobox/util/UtilTest.java b/src/test/java/world/bentobox/bentobox/util/UtilTest.java index 6fe17aa38..6b6519388 100644 --- a/src/test/java/world/bentobox/bentobox/util/UtilTest.java +++ b/src/test/java/world/bentobox/bentobox/util/UtilTest.java @@ -406,7 +406,7 @@ public class UtilTest { when(user.getName()).thenReturn("tastybento"); when(user.isOnline()).thenReturn(true); when(user.performCommand(anyString())).thenReturn(true); - Util.runCommands(user, "tasty", Collections.singletonList("[SUDO]help"), "test"); + Util.runCommands(user, Collections.singletonList("[SUDO]help"), "test"); verify(plugin, never()).logError(anyString()); } @@ -418,7 +418,7 @@ public class UtilTest { when(user.getName()).thenReturn("tastybento"); when(user.isOnline()).thenReturn(true); when(user.performCommand(anyString())).thenReturn(false); - Util.runCommands(user, "tasty", Collections.singletonList("[SUDO]help"), "test"); + Util.runCommands(user, Collections.singletonList("[SUDO]help"), "test"); verify(plugin).logError(eq("Could not execute test command for tastybento: help")); } @@ -430,7 +430,7 @@ public class UtilTest { when(user.getName()).thenReturn("tastybento"); when(user.isOnline()).thenReturn(false); when(user.performCommand(anyString())).thenReturn(true); - Util.runCommands(user, "tasty", Collections.singletonList("[SUDO]help"), "test"); + Util.runCommands(user, Collections.singletonList("[SUDO]help"), "test"); verify(plugin).logError(eq("Could not execute test command for tastybento: help")); } @@ -441,13 +441,13 @@ public class UtilTest { public void testRunCommandsConsoleCommand() { when(user.getName()).thenReturn("tastybento"); when(Bukkit.dispatchCommand(eq(sender), anyString())).thenReturn(true); - Util.runCommands(user, "tasty", List.of("replace [player]", "replace owner [owner]", "[owner] [player]"), "test"); + Util.runCommands(user, List.of("replace [player]", "replace owner [owner]", "[owner] [player]"), "test"); PowerMockito.verifyStatic(Bukkit.class); Bukkit.dispatchCommand(sender, "replace tastybento"); PowerMockito.verifyStatic(Bukkit.class); - Bukkit.dispatchCommand(sender, "replace owner tasty"); + Bukkit.dispatchCommand(sender, "replace owner tastybento"); PowerMockito.verifyStatic(Bukkit.class); - Bukkit.dispatchCommand(sender, "tasty tastybento"); + Bukkit.dispatchCommand(sender, "tastybento tastybento"); verify(plugin, never()).logError(anyString()); } @@ -458,7 +458,7 @@ public class UtilTest { public void testRunCommandsConsoleCommandFail() { when(user.getName()).thenReturn("tastybento"); when(Bukkit.dispatchCommand(eq(sender), anyString())).thenReturn(false); - Util.runCommands(user, "", Collections.singletonList("replace [player]"), "test"); + Util.runCommands(user, Collections.singletonList("replace [player]"), "test"); PowerMockito.verifyStatic(Bukkit.class); Bukkit.dispatchCommand(sender, "replace tastybento"); verify(plugin).logError("Could not execute test command as console: replace tastybento");