mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-01-25 01:21:21 +01:00
Replaces command running with single Util method (#1374)
Adds respawn commands.
This commit is contained in:
parent
0fec5aec51
commit
09072282bb
@ -5,7 +5,6 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
@ -88,16 +87,7 @@ public class AdminDeleteCommand extends ConfirmableCommand {
|
||||
getIslands().removePlayer(getWorld(), targetUUID);
|
||||
if (target.isOnline()) {
|
||||
// Execute commands when leaving
|
||||
getIWM().getOnLeaveCommands(getWorld()).forEach(command -> {
|
||||
command = command.replace("[player]", target.getName());
|
||||
if (command.startsWith("[SUDO]")) {
|
||||
// Execute the command by the player
|
||||
target.performCommand(command.substring(6));
|
||||
} else {
|
||||
// Otherwise execute as the server console
|
||||
getPlugin().getServer().dispatchCommand(Bukkit.getConsoleSender(), command);
|
||||
}
|
||||
});
|
||||
Util.runCommands(user, getIWM().getOnLeaveCommands(getWorld()), "leave");
|
||||
// Remove money inventory etc.
|
||||
if (getIWM().isOnLeaveResetEnderChest(getWorld())) {
|
||||
target.getPlayer().getEnderChest().clear();
|
||||
|
@ -3,7 +3,6 @@ package world.bentobox.bentobox.api.commands.island;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.eclipse.jdt.annotation.NonNull;
|
||||
|
||||
import world.bentobox.bentobox.api.addons.GameModeAddon;
|
||||
@ -19,6 +18,7 @@ import world.bentobox.bentobox.managers.BlueprintsManager;
|
||||
import world.bentobox.bentobox.managers.island.NewIsland;
|
||||
import world.bentobox.bentobox.managers.island.NewIsland.Builder;
|
||||
import world.bentobox.bentobox.panels.IslandCreationPanel;
|
||||
import world.bentobox.bentobox.util.Util;
|
||||
|
||||
/**
|
||||
* @author tastybento
|
||||
@ -180,20 +180,7 @@ public class IslandResetCommand extends ConfirmableCommand {
|
||||
getIslands().removePlayer(getWorld(), memberUUID);
|
||||
|
||||
// Execute commands when leaving
|
||||
getIWM().getOnLeaveCommands(island.getWorld()).forEach(command -> {
|
||||
command = command.replace("[player]", member.getName());
|
||||
if (command.startsWith("[SUDO]") && member.isOnline()) {
|
||||
// Execute the command by the player
|
||||
if (!member.performCommand(command.substring(6))) {
|
||||
getPlugin().logError("Could not execute leave command for " + member.getName() + ": " + command.substring(6));
|
||||
}
|
||||
} else {
|
||||
// Otherwise execute as the server console
|
||||
if (!getPlugin().getServer().dispatchCommand(Bukkit.getConsoleSender(), command)) {
|
||||
getPlugin().logError("Could not execute leave command as console: " + command);
|
||||
}
|
||||
}
|
||||
});
|
||||
Util.runCommands(member, getIWM().getOnLeaveCommands(island.getWorld()), "leave");
|
||||
|
||||
// Remove money inventory etc.
|
||||
if (getIWM().isOnLeaveResetEnderChest(getWorld())) {
|
||||
|
@ -4,8 +4,6 @@ import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
|
||||
import world.bentobox.bentobox.api.events.island.IslandEvent;
|
||||
@ -14,6 +12,7 @@ import world.bentobox.bentobox.api.localization.TextVariables;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
import world.bentobox.bentobox.database.objects.Island;
|
||||
import world.bentobox.bentobox.managers.RanksManager;
|
||||
import world.bentobox.bentobox.util.Util;
|
||||
|
||||
|
||||
public class IslandTeamKickCommand extends ConfirmableCommand {
|
||||
@ -82,16 +81,7 @@ public class IslandTeamKickCommand extends ConfirmableCommand {
|
||||
Island oldIsland = getIslands().getIsland(getWorld(), targetUUID);
|
||||
getIslands().removePlayer(getWorld(), targetUUID);
|
||||
// Execute commands when leaving
|
||||
getIWM().getOnLeaveCommands(oldIsland.getWorld()).forEach(command -> {
|
||||
command = command.replace("[player]", target.getName());
|
||||
if (command.startsWith("[SUDO]") && target.isOnline()) {
|
||||
// Execute the command by the player
|
||||
target.performCommand(command.substring(6));
|
||||
} else {
|
||||
// Otherwise execute as the server console
|
||||
getPlugin().getServer().dispatchCommand(Bukkit.getConsoleSender(), command);
|
||||
}
|
||||
});
|
||||
Util.runCommands(target, getIWM().getOnLeaveCommands(oldIsland.getWorld()), "leave");
|
||||
// Remove money inventory etc.
|
||||
if (getIWM().isOnLeaveResetEnderChest(getWorld())) {
|
||||
if (target.isOnline()) {
|
||||
|
@ -3,8 +3,6 @@ package world.bentobox.bentobox.api.commands.island.team;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
|
||||
import world.bentobox.bentobox.api.events.island.IslandEvent;
|
||||
@ -13,6 +11,7 @@ import world.bentobox.bentobox.api.localization.TextVariables;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
import world.bentobox.bentobox.database.objects.Island;
|
||||
import world.bentobox.bentobox.managers.RanksManager;
|
||||
import world.bentobox.bentobox.util.Util;
|
||||
|
||||
public class IslandTeamLeaveCommand extends ConfirmableCommand {
|
||||
|
||||
@ -74,16 +73,7 @@ public class IslandTeamLeaveCommand extends ConfirmableCommand {
|
||||
}
|
||||
getIslands().setLeaveTeam(getWorld(), user.getUniqueId());
|
||||
// Execute commands when leaving
|
||||
getIWM().getOnLeaveCommands(island.getWorld()).forEach(command -> {
|
||||
command = command.replace("[player]", user.getName());
|
||||
if (command.startsWith("[SUDO]") && user.isOnline()) {
|
||||
// Execute the command by the player
|
||||
user.performCommand(command.substring(6));
|
||||
} else {
|
||||
// Otherwise execute as the server console
|
||||
getPlugin().getServer().dispatchCommand(Bukkit.getConsoleSender(), command);
|
||||
}
|
||||
});
|
||||
Util.runCommands(user, getIWM().getOnLeaveCommands(island.getWorld()), "leave");
|
||||
// Remove money inventory etc.
|
||||
if (getIWM().isOnLeaveResetEnderChest(getWorld())) {
|
||||
user.getPlayer().getEnderChest().clear();
|
||||
|
@ -13,6 +13,7 @@ import org.bukkit.entity.EntityType;
|
||||
import org.eclipse.jdt.annotation.NonNull;
|
||||
|
||||
import world.bentobox.bentobox.api.flags.Flag;
|
||||
import world.bentobox.bentobox.lists.Flags;
|
||||
|
||||
/**
|
||||
* Contains world-specific settings that must be provided by the {@link world.bentobox.bentobox.api.addons.GameModeAddon} in order to register its Worlds.
|
||||
@ -315,6 +316,17 @@ public interface WorldSettings extends ConfigObject {
|
||||
*/
|
||||
@NonNull
|
||||
List<String> getOnLeaveCommands();
|
||||
|
||||
/**
|
||||
* Returns a list of commands that should be executed when the player respawns after death if {@link Flags#ISLAND_RESPAWN} is true.<br/>
|
||||
* @return a list of commands.
|
||||
* @since 1.14.0
|
||||
* @see #getOnJoinCommands()
|
||||
*/
|
||||
@NonNull
|
||||
default List<String> getOnRespawnCommands() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if the default world generator should not operate in this world
|
||||
|
@ -65,6 +65,8 @@ public class IslandRespawnListener extends FlagListener {
|
||||
if (respawnLocation != null) {
|
||||
e.setRespawnLocation(respawnLocation);
|
||||
}
|
||||
// Run respawn commands, if any
|
||||
Util.runCommands(User.getInstance(e.getPlayer()), getIWM().getOnRespawnCommands(world), "respawn");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -25,6 +25,7 @@ import world.bentobox.bentobox.api.addons.GameModeAddon;
|
||||
import world.bentobox.bentobox.api.configuration.WorldSettings;
|
||||
import world.bentobox.bentobox.api.flags.Flag;
|
||||
import world.bentobox.bentobox.hooks.MultiverseCoreHook;
|
||||
import world.bentobox.bentobox.lists.Flags;
|
||||
|
||||
/**
|
||||
* Handles registration and management of worlds
|
||||
@ -658,6 +659,18 @@ public class IslandWorldManager {
|
||||
public List<String> getOnLeaveCommands(@NonNull World world) {
|
||||
return gameModes.containsKey(world) ? gameModes.get(world).getWorldSettings().getOnLeaveCommands() : Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of commands to execute when the player respawns and {@link Flags#ISLAND_RESPAWN} is true.
|
||||
* @param world the World
|
||||
* @return a list of commands
|
||||
* @since 1.14.0
|
||||
* @see #getOnJoinCommands(World)
|
||||
*/
|
||||
@NonNull
|
||||
public List<String> getOnRespawnCommands(@NonNull World world) {
|
||||
return gameModes.containsKey(world) ? gameModes.get(world).getWorldSettings().getOnRespawnCommands() : Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get data folder for the addon that registered this world
|
||||
|
@ -913,20 +913,7 @@ public class IslandsManager {
|
||||
// If this is a new island, then run commands and do resets
|
||||
if (newIsland) {
|
||||
// Execute commands
|
||||
plugin.getIWM().getOnJoinCommands(world).forEach(command -> {
|
||||
command = command.replace("[player]", user.getName());
|
||||
if (command.startsWith("[SUDO]") && user.isOnline()) {
|
||||
// Execute the command by the player
|
||||
if (!user.performCommand(command.substring(6))) {
|
||||
plugin.logError("Could not execute join command for " + user.getName() + ": " + command.substring(6));
|
||||
}
|
||||
} else {
|
||||
// Otherwise execute as the server console
|
||||
if (!plugin.getServer().dispatchCommand(Bukkit.getConsoleSender(), command)) {
|
||||
plugin.logError("Could not execute join command as console: " + command);
|
||||
}
|
||||
}
|
||||
});
|
||||
Util.runCommands(user, plugin.getIWM().getOnJoinCommands(world), "join");
|
||||
|
||||
// Remove money inventory etc.
|
||||
if (plugin.getIWM().isOnJoinResetEnderChest(world)) {
|
||||
|
@ -628,4 +628,28 @@ 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<String> commands, String commandType) {
|
||||
commands.forEach(command -> {
|
||||
command = command.replace("[player]", user.getName());
|
||||
if (command.startsWith("[SUDO]")) {
|
||||
// Execute the command by the player
|
||||
if (!user.isOnline() || !user.performCommand(command.substring(6))) {
|
||||
plugin.logError("Could not execute " + commandType + " command for " + user.getName() + ": " + command.substring(6));
|
||||
}
|
||||
} else {
|
||||
// Otherwise execute as the server console
|
||||
if (!Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command)) {
|
||||
plugin.logError("Could not execute " + commandType + " command as console: " + command);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -2,12 +2,14 @@ package world.bentobox.bentobox.listeners.flags.worldsettings;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -213,6 +215,9 @@ public class IslandRespawnListenerTest {
|
||||
PlayerRespawnEvent ev = new PlayerRespawnEvent(player, location, false);
|
||||
l.onPlayerRespawn(ev);
|
||||
assertEquals(safeLocation, ev.getRespawnLocation());
|
||||
// Verify commands
|
||||
PowerMockito.verifyStatic(Util.class);
|
||||
Util.runCommands(any(User.class), eq(Collections.emptyList()), eq("respawn"));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -4,23 +4,32 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.command.ConsoleCommandSender;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
|
||||
import world.bentobox.bentobox.BentoBox;
|
||||
import world.bentobox.bentobox.api.user.User;
|
||||
import world.bentobox.bentobox.managers.IslandWorldManager;
|
||||
|
||||
/**
|
||||
@ -31,10 +40,18 @@ import world.bentobox.bentobox.managers.IslandWorldManager;
|
||||
@PrepareForTest( { Bukkit.class })
|
||||
public class UtilTest {
|
||||
|
||||
@Mock
|
||||
private BentoBox plugin;
|
||||
@Mock
|
||||
private World world;
|
||||
@Mock
|
||||
private IslandWorldManager iwm;
|
||||
@Mock
|
||||
private Location location;
|
||||
@Mock
|
||||
private User user;
|
||||
@Mock
|
||||
private ConsoleCommandSender sender;
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
@ -42,15 +59,11 @@ public class UtilTest {
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
// Set up plugin
|
||||
plugin = mock(BentoBox.class);
|
||||
Util.setPlugin(plugin);
|
||||
// World
|
||||
world = mock(World.class);
|
||||
when(world.getName()).thenReturn("world_name");
|
||||
// Worlds
|
||||
iwm = mock(IslandWorldManager.class);
|
||||
when(plugin.getIWM()).thenReturn(iwm);
|
||||
location = mock(Location.class);
|
||||
when(location.getWorld()).thenReturn(world);
|
||||
when(location.getX()).thenReturn(500D);
|
||||
when(location.getY()).thenReturn(600D);
|
||||
@ -65,6 +78,7 @@ public class UtilTest {
|
||||
Server server = mock(Server.class);
|
||||
when(Bukkit.getServer()).thenReturn(server);
|
||||
when(server.getWorld(Mockito.anyString())).thenReturn(world);
|
||||
when(Bukkit.getConsoleSender()).thenReturn(sender);
|
||||
}
|
||||
|
||||
@After
|
||||
@ -252,4 +266,65 @@ public class UtilTest {
|
||||
// For some reason, Integer#parseInt() does not support this...
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for {@link Util#runCommands(world.bentobox.bentobox.api.user.User, java.util.List, String)}
|
||||
*/
|
||||
@Test
|
||||
public void testRunCommandsSudoUserOnlinePerformCommand() {
|
||||
when(user.getName()).thenReturn("tastybento");
|
||||
when(user.isOnline()).thenReturn(true);
|
||||
when(user.performCommand(anyString())).thenReturn(true);
|
||||
Util.runCommands(user, Collections.singletonList("[SUDO]help"), "test");
|
||||
verify(plugin, never()).logError(anyString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for {@link Util#runCommands(world.bentobox.bentobox.api.user.User, java.util.List, String)}
|
||||
*/
|
||||
@Test
|
||||
public void testRunCommandsSudoUserOnlineFailCommand() {
|
||||
when(user.getName()).thenReturn("tastybento");
|
||||
when(user.isOnline()).thenReturn(true);
|
||||
when(user.performCommand(anyString())).thenReturn(false);
|
||||
Util.runCommands(user, Collections.singletonList("[SUDO]help"), "test");
|
||||
verify(plugin).logError(eq("Could not execute test command for tastybento: help"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for {@link Util#runCommands(world.bentobox.bentobox.api.user.User, java.util.List, String)}
|
||||
*/
|
||||
@Test
|
||||
public void testRunCommandsSudoUserOfflineCommand() {
|
||||
when(user.getName()).thenReturn("tastybento");
|
||||
when(user.isOnline()).thenReturn(false);
|
||||
when(user.performCommand(anyString())).thenReturn(true);
|
||||
Util.runCommands(user, Collections.singletonList("[SUDO]help"), "test");
|
||||
verify(plugin).logError(eq("Could not execute test command for tastybento: help"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for {@link Util#runCommands(world.bentobox.bentobox.api.user.User, java.util.List, String)}
|
||||
*/
|
||||
@Test
|
||||
public void testRunCommandsConsoleCommand() {
|
||||
when(user.getName()).thenReturn("tastybento");
|
||||
when(Bukkit.dispatchCommand(eq(sender), anyString())).thenReturn(true);
|
||||
Util.runCommands(user, Collections.singletonList("replace [player]"), "test");
|
||||
PowerMockito.verifyStatic(Bukkit.class);
|
||||
Bukkit.dispatchCommand(sender, "replace tastybento");
|
||||
verify(plugin, never()).logError(anyString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for {@link Util#runCommands(world.bentobox.bentobox.api.user.User, java.util.List, String)}
|
||||
*/
|
||||
@Test
|
||||
public void testRunCommandsConsoleCommandFail() {
|
||||
when(user.getName()).thenReturn("tastybento");
|
||||
when(Bukkit.dispatchCommand(eq(sender), anyString())).thenReturn(false);
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user