From a604d5cf82e31274f510709e49035abf2a7207b2 Mon Sep 17 00:00:00 2001 From: tastybento Date: Tue, 31 Jan 2023 07:13:01 -0800 Subject: [PATCH] Add [owner] in command placeholders #2080 (#2081) --- .../commands/admin/AdminDeleteCommand.java | 2 +- .../commands/island/IslandResetCommand.java | 2 +- .../team/IslandTeamInviteAcceptCommand.java | 6 ++++-- .../island/team/IslandTeamKickCommand.java | 2 +- .../island/team/IslandTeamLeaveCommand.java | 2 +- .../api/configuration/WorldSettings.java | 20 +++++++++++++++++++ .../worldsettings/IslandRespawnListener.java | 10 +++++++++- .../bentobox/managers/IslandsManager.java | 2 +- .../bentobox/managers/PlayersManager.java | 7 +++++-- .../world/bentobox/bentobox/util/Util.java | 5 +++-- .../team/IslandTeamKickCommandTest.java | 2 +- .../IslandRespawnListenerTest.java | 3 ++- .../bentobox/managers/PlayersManagerTest.java | 13 +++++++++--- .../bentobox/bentobox/util/UtilTest.java | 14 ++++++++----- 14 files changed, 68 insertions(+), 22 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminDeleteCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminDeleteCommand.java index 00e8c2c89..c8e6be0d1 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminDeleteCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminDeleteCommand.java @@ -122,7 +122,7 @@ public class AdminDeleteCommand extends ConfirmableCommand { } // Execute commands when leaving - Util.runCommands(target, getIWM().getOnLeaveCommands(getWorld()), "leave"); + Util.runCommands(target, target.getName(), getIWM().getOnLeaveCommands(getWorld()), "leave"); } @Override diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/IslandResetCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/IslandResetCommand.java index f6c60aa83..128ae6370 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/IslandResetCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/IslandResetCommand.java @@ -187,7 +187,7 @@ public class IslandResetCommand extends ConfirmableCommand { getIslands().removePlayer(getWorld(), memberUUID); // Clean player - getPlayers().cleanLeavingPlayer(getWorld(), member, false); + getPlayers().cleanLeavingPlayer(getWorld(), member, false, island); // Fire event TeamEvent.builder() diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteAcceptCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteAcceptCommand.java index 07e3efba0..8144ed765 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteAcceptCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteAcceptCommand.java @@ -176,6 +176,10 @@ public class IslandTeamInviteAcceptCommand extends ConfirmableCommand { // Put player back into normal mode user.setGameMode(getIWM().getDefaultGameMode(getWorld())); + // Execute commands + String ownerName = this.getPlayers().getName(teamIsland.getOwner()); + Util.runCommands(user, ownerName, getIWM().getOnJoinCommands(getWorld()), "join"); + }); // Reset deaths if (getIWM().isTeamJoinDeathReset(getWorld())) { @@ -228,7 +232,5 @@ public class IslandTeamInviteAcceptCommand extends ConfirmableCommand { user.getPlayer().setTotalExperience(0); } - // Execute commands - Util.runCommands(user, getIWM().getOnJoinCommands(getWorld()), "join"); } } diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommand.java index 778f843fc..8722dfa1f 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommand.java @@ -102,7 +102,7 @@ public class IslandTeamKickCommand extends ConfirmableCommand { getIslands().removePlayer(getWorld(), targetUUID); // Clean the target player - getPlayers().cleanLeavingPlayer(getWorld(), target, true); + getPlayers().cleanLeavingPlayer(getWorld(), target, true, oldIsland); user.sendMessage("commands.island.team.kick.success", TextVariables.NAME, target.getName()); IslandEvent.builder() diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamLeaveCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamLeaveCommand.java index 7fcfa2a60..db1a37379 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamLeaveCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamLeaveCommand.java @@ -82,7 +82,7 @@ public class IslandTeamLeaveCommand extends ConfirmableCommand { } getIslands().setLeaveTeam(getWorld(), user.getUniqueId()); // Clean the player - getPlayers().cleanLeavingPlayer(getWorld(), user, false); + getPlayers().cleanLeavingPlayer(getWorld(), user, false, island); // Add cooldown for this player and target if (getSettings().getInviteCooldown() > 0 && getParent() != null) { diff --git a/src/main/java/world/bentobox/bentobox/api/configuration/WorldSettings.java b/src/main/java/world/bentobox/bentobox/api/configuration/WorldSettings.java index 90f04b054..64a5d5297 100644 --- a/src/main/java/world/bentobox/bentobox/api/configuration/WorldSettings.java +++ b/src/main/java/world/bentobox/bentobox/api/configuration/WorldSettings.java @@ -288,6 +288,8 @@ public interface WorldSettings extends ConfigObject { * Available placeholders for the commands are the following: * *
* Here are some examples of valid commands to execute: @@ -345,6 +347,8 @@ public interface WorldSettings extends ConfigObject { * Available placeholders for the commands are the following: * *
* Here are some examples of valid commands to execute: @@ -363,6 +367,22 @@ public interface WorldSettings extends ConfigObject { /** * Returns a list of commands that should be executed when the player respawns after death if {@link Flags#ISLAND_RESPAWN} is true.
+ * These commands are executed by the console, unless otherwise stated using the {@code [SUDO]} prefix, in which case they are executed by the player.
+ *
+ * Available placeholders for the commands are the following: + * + *
+ * Here are some examples of valid commands to execute: + * + *
+ * Note that player-executed commands might not work, as these commands can be run with said player being offline. * @return a list of commands. * @since 1.14.0 * @see #getOnJoinCommands() diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/IslandRespawnListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/IslandRespawnListener.java index 1c8196716..5f8953dd1 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/IslandRespawnListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/IslandRespawnListener.java @@ -11,8 +11,10 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.player.PlayerRespawnEvent; +import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.flags.FlagListener; import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.util.Util; @@ -61,14 +63,20 @@ public class IslandRespawnListener extends FlagListener { return; // world no longer available } World w = Util.getWorld(world); + String ownerName = e.getPlayer().getName(); if (w != null) { final Location respawnLocation = getIslands().getSafeHomeLocation(w, User.getInstance(e.getPlayer().getUniqueId()), ""); if (respawnLocation != null) { e.setRespawnLocation(respawnLocation); + // Get the island owner name + Island island = BentoBox.getInstance().getIslandsManager().getIsland(w, User.getInstance(e.getPlayer())); + if (island != null) { + ownerName = BentoBox.getInstance().getPlayers().getName(island.getOwner()); + } } } // Run respawn commands, if any - Util.runCommands(User.getInstance(e.getPlayer()), getIWM().getOnRespawnCommands(world), "respawn"); + Util.runCommands(User.getInstance(e.getPlayer()), ownerName, getIWM().getOnRespawnCommands(world), "respawn"); } } diff --git a/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java b/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java index c9a3974c2..a49b3f14b 100644 --- a/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java @@ -1172,7 +1172,7 @@ public class IslandsManager { user.setGameMode(plugin.getIWM().getDefaultGameMode(world)); // Execute commands - Util.runCommands(user, plugin.getIWM().getOnJoinCommands(world), "join"); + Util.runCommands(user, user.getName(), plugin.getIWM().getOnJoinCommands(world), "join"); } // Remove from mid-teleport set goingHome.remove(user.getUniqueId()); diff --git a/src/main/java/world/bentobox/bentobox/managers/PlayersManager.java b/src/main/java/world/bentobox/bentobox/managers/PlayersManager.java index 1ab0b7d86..1cf40a945 100644 --- a/src/main/java/world/bentobox/bentobox/managers/PlayersManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/PlayersManager.java @@ -22,6 +22,7 @@ import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.flags.Flag; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.Database; +import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Names; import world.bentobox.bentobox.database.objects.Players; import world.bentobox.bentobox.util.Util; @@ -538,11 +539,13 @@ public class PlayersManager { * @param world - island world * @param target - target user * @param kicked - true if player is being kicked + * @param island - island being left * @since 1.15.4 */ - public void cleanLeavingPlayer(World world, User target, boolean kicked) { + public void cleanLeavingPlayer(World world, User target, boolean kicked, Island island) { // Execute commands when leaving - Util.runCommands(target, plugin.getIWM().getOnLeaveCommands(world), "leave"); + String ownerName = this.getName(island.getOwner()); + Util.runCommands(target, ownerName, plugin.getIWM().getOnLeaveCommands(world), "leave"); // Remove any tamed animals world.getEntitiesByClass(Tameable.class).stream() diff --git a/src/main/java/world/bentobox/bentobox/util/Util.java b/src/main/java/world/bentobox/bentobox/util/Util.java index b9163ab9a..c24d839c6 100644 --- a/src/main/java/world/bentobox/bentobox/util/Util.java +++ b/src/main/java/world/bentobox/bentobox/util/Util.java @@ -49,7 +49,6 @@ import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.nms.PasteHandler; import world.bentobox.bentobox.nms.WorldRegenerator; -import world.bentobox.bentobox.versions.ServerCompatibility; /** @@ -663,12 +662,14 @@ public class Util { /** * 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 */ - public static void runCommands(User user, @NonNull List commands, String commandType) { + public static void runCommands(User user, String ownerName, @NonNull List commands, String commandType) { commands.forEach(command -> { command = command.replace("[player]", user.getName()); + command = command.replace("[owner]", ownerName); if (command.startsWith("[SUDO]")) { // Execute the command by the player if (!user.isOnline() || !user.performCommand(command.substring(6))) { diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommandTest.java index dff26840c..9468f93ef 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommandTest.java @@ -406,7 +406,7 @@ public class IslandTeamKickCommandTest { verify(im).removePlayer(any(), eq(notUUID)); verify(user).sendMessage("commands.island.team.kick.success", TextVariables.NAME, "poslovitch"); verify(target, Mockito.never()).getInventory(); - verify(pm).cleanLeavingPlayer(any(), any(User.class), eq(true)); + verify(pm).cleanLeavingPlayer(any(), any(User.class), eq(true), eq(island)); } /** diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/IslandRespawnListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/IslandRespawnListenerTest.java index 611de3413..e9ff41aa1 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/IslandRespawnListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/IslandRespawnListenerTest.java @@ -2,6 +2,7 @@ package world.bentobox.bentobox.listeners.flags.worldsettings; import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -217,7 +218,7 @@ public class IslandRespawnListenerTest { assertEquals(safeLocation, ev.getRespawnLocation()); // Verify commands PowerMockito.verifyStatic(Util.class); - Util.runCommands(any(User.class), eq(Collections.emptyList()), eq("respawn")); + Util.runCommands(any(User.class), anyString(), eq(Collections.emptyList()), eq("respawn")); } /** diff --git a/src/test/java/world/bentobox/bentobox/managers/PlayersManagerTest.java b/src/test/java/world/bentobox/bentobox/managers/PlayersManagerTest.java index 033e154ac..cb0691ecd 100644 --- a/src/test/java/world/bentobox/bentobox/managers/PlayersManagerTest.java +++ b/src/test/java/world/bentobox/bentobox/managers/PlayersManagerTest.java @@ -58,6 +58,7 @@ import world.bentobox.bentobox.database.AbstractDatabaseHandler; import world.bentobox.bentobox.database.Database; import world.bentobox.bentobox.database.DatabaseSetup; import world.bentobox.bentobox.database.DatabaseSetup.DatabaseType; +import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Players; import world.bentobox.bentobox.hooks.VaultHook; import world.bentobox.bentobox.util.Util; @@ -95,6 +96,9 @@ public class PlayersManagerTest { private VaultHook vault; @Mock private PlayerInventory playerInv; + @Mock + private Island island; + private static AbstractDatabaseHandler h; @SuppressWarnings("unchecked") @@ -153,6 +157,9 @@ public class PlayersManagerTest { while(notUUID.equals(uuid)) { notUUID = UUID.randomUUID(); } + + // Island + when(island.getOwner()).thenReturn(uuid); // Player when(p.getEnderChest()).thenReturn(inv); @@ -391,7 +398,7 @@ public class PlayersManagerTest { */ @Test public void testCleanLeavingPlayerLeave() { - pm.cleanLeavingPlayer(world, user, false); + pm.cleanLeavingPlayer(world, user, false, island); // Tamed animals verify(tamed).setOwner(eq(null)); // Economy @@ -415,7 +422,7 @@ public class PlayersManagerTest { @Test public void testCleanLeavingPlayerKicked() { // Player is kicked - pm.cleanLeavingPlayer(world, user, true); + pm.cleanLeavingPlayer(world, user, true, island); // Tamed animals verify(tamed).setOwner(eq(null)); // Economy @@ -440,7 +447,7 @@ public class PlayersManagerTest { public void testCleanLeavingPlayerKickedOffline() { when(user.isOnline()).thenReturn(false); // Player is kicked - pm.cleanLeavingPlayer(world, user, true); + pm.cleanLeavingPlayer(world, user, true, island); // Tamed animals verify(tamed).setOwner(eq(null)); // Economy diff --git a/src/test/java/world/bentobox/bentobox/util/UtilTest.java b/src/test/java/world/bentobox/bentobox/util/UtilTest.java index 684a2ff63..6fe17aa38 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, Collections.singletonList("[SUDO]help"), "test"); + Util.runCommands(user, "tasty", 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, Collections.singletonList("[SUDO]help"), "test"); + Util.runCommands(user, "tasty", 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, Collections.singletonList("[SUDO]help"), "test"); + Util.runCommands(user, "tasty", Collections.singletonList("[SUDO]help"), "test"); verify(plugin).logError(eq("Could not execute test command for tastybento: help")); } @@ -441,9 +441,13 @@ public class UtilTest { public void testRunCommandsConsoleCommand() { when(user.getName()).thenReturn("tastybento"); when(Bukkit.dispatchCommand(eq(sender), anyString())).thenReturn(true); - Util.runCommands(user, Collections.singletonList("replace [player]"), "test"); + Util.runCommands(user, "tasty", 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"); + PowerMockito.verifyStatic(Bukkit.class); + Bukkit.dispatchCommand(sender, "tasty tastybento"); verify(plugin, never()).logError(anyString()); } @@ -454,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");