Add [owner] in command placeholders #2080 (#2081)

This commit is contained in:
tastybento 2023-01-31 07:13:01 -08:00 committed by GitHub
parent bbb8207cf0
commit a604d5cf82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 68 additions and 22 deletions

View File

@ -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

View File

@ -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()

View File

@ -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");
}
}

View File

@ -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()

View File

@ -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) {

View File

@ -288,6 +288,8 @@ public interface WorldSettings extends ConfigObject {
* Available placeholders for the commands are the following:
* <ul>
* <li>{@code [player]}: name of the player</li>
* <li>{@code [owner]}: name of the owner of the island. When joining a team, this will be the team leader's name. When
* creating an island, it is the name of the player</li>
* </ul>
* <br/>
* 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:
* <ul>
* <li>{@code [player]}: name of the player</li>
* <li>{@code [owner]}: name of the owner of the island. When joining a team, this will be the team leader's name. When
* creating an island, it is the name of the player</li>
* </ul>
* <br/>
* 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.<br/>
* These commands are executed by the console, unless otherwise stated using the {@code [SUDO]} prefix, in which case they are executed by the player.<br/>
* <br/>
* Available placeholders for the commands are the following:
* <ul>
* <li>{@code [player]}: name of the player</li>
* <li>{@code [owner]}: name of the owner of the island. When joining a team, this will be the team leader's name. When
* creating an island, it is the name of the player</li>
* </ul>
* <br/>
* Here are some examples of valid commands to execute:
* <ul>
* <li>{@code "[SUDO] bbox version"}</li>
* <li>{@code "bsbadmin deaths set [player] 0"}</li>
* </ul>
* <br/>
* 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()

View File

@ -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");
}
}

View File

@ -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());

View File

@ -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()

View File

@ -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<String> commands, String commandType) {
public static void runCommands(User user, String ownerName, @NonNull List<String> 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))) {

View File

@ -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));
}
/**

View File

@ -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"));
}
/**

View File

@ -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<Object> h;
@SuppressWarnings("unchecked")
@ -154,6 +158,9 @@ public class PlayersManagerTest {
notUUID = UUID.randomUUID();
}
// Island
when(island.getOwner()).thenReturn(uuid);
// Player
when(p.getEnderChest()).thenReturn(inv);
when(p.getInventory()).thenReturn(playerInv);
@ -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

View File

@ -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");