Skip to content

Commit

Permalink
Fix runCommands method
Browse files Browse the repository at this point in the history
Note to future self, don't remove Util methods because someone might be
using them!
  • Loading branch information
tastybento committed Feb 12, 2023
1 parent 5eaf54e commit 8276406
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
19 changes: 15 additions & 4 deletions src/main/java/world/bentobox/bentobox/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,8 @@ public static boolean isPassiveEntity(Entity entity) {
// 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;
}

/*
Expand Down Expand Up @@ -659,12 +659,23 @@ public static UUID getUUID(@NonNull String nameOrUUID) {
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<String> 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<String> commands, String commandType) {
commands.forEach(command -> {
Expand Down Expand Up @@ -776,7 +787,7 @@ public static int broadcast(String localeKey, String... variables) {
public static String sanitizeInput(String input)
{
return ChatColor.stripColor(
Util.translateColorCodes(input.replaceAll("[\\\\/:*?\"<>|\s]", "_"))).
toLowerCase();
Util.translateColorCodes(input.replaceAll("[\\\\/:*?\"<>|\s]", "_"))).
toLowerCase();
}
}
14 changes: 7 additions & 7 deletions src/test/java/world/bentobox/bentobox/util/UtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public void testRunCommandsSudoUserOnlinePerformCommand() {
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());
}

Expand All @@ -418,7 +418,7 @@ public void testRunCommandsSudoUserOnlineFailCommand() {
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"));
}

Expand All @@ -430,7 +430,7 @@ public void testRunCommandsSudoUserOfflineCommand() {
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"));
}

Expand All @@ -441,13 +441,13 @@ public void testRunCommandsSudoUserOfflineCommand() {
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());
}

Expand All @@ -458,7 +458,7 @@ public void testRunCommandsConsoleCommand() {
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");
Expand Down

0 comments on commit 8276406

Please sign in to comment.