Enables tp commands to work from console.

Fixes https://github.com/BentoBoxWorld/BentoBox/issues/1158
This commit is contained in:
tastybento 2020-01-30 10:12:31 -08:00
parent d7b29c350f
commit 07706d78e1
2 changed files with 64 additions and 47 deletions

View File

@ -6,8 +6,9 @@ import java.util.Optional;
import java.util.UUID;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
@ -16,6 +17,9 @@ import world.bentobox.bentobox.util.teleport.SafeSpotTeleport;
public class AdminTeleportCommand extends CompositeCommand {
private @Nullable UUID targetUUID;
private @Nullable User userToTeleport;
/**
* @param parent - parent command
* @param tpCommand - should be "tp", "tpnether" or "tpend"
@ -28,64 +32,75 @@ public class AdminTeleportCommand extends CompositeCommand {
public void setup() {
// Permission
setPermission("admin.tp");
setOnlyPlayer(true);
setParametersHelp("commands.admin.tp.parameters");
setDescription("commands.admin.tp.description");
}
@Override
public boolean execute(User user, String label, List<String> args) {
public boolean canExecute(User user, String label, List<String> args) {
if (args.size() != 1 && args.size() != 2) {
this.showHelp(this, user);
return false;
}
// Check for console or not
if (!user.isPlayer() && args.size() != 2) {
user.sendMessage("general.errors.use-in-game");
return false;
}
// Convert name to a UUID
final UUID targetUUID = getPlayers().getUUID(args.get(0));
targetUUID = getPlayers().getUUID(args.get(0));
if (targetUUID == null) {
user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0));
return false;
} else {
if (getIslands().hasIsland(getWorld(), targetUUID) || getIslands().inTeam(getWorld(), targetUUID)) {
Location warpSpot = getIslands().getIslandLocation(getWorld(), targetUUID).toVector().toLocation(getWorld());
if (getLabel().equals("tpnether")) {
warpSpot = getIslands().getIslandLocation(getWorld(), targetUUID).toVector().toLocation(getPlugin().getIWM().getNetherWorld(getWorld()));
} else if (getLabel().equals("tpend")) {
warpSpot = getIslands().getIslandLocation(getWorld(), targetUUID).toVector().toLocation(getPlugin().getIWM().getEndWorld(getWorld()));
}
// Otherwise, ask the admin to go to a safe spot
String failureMessage = user.getTranslation("commands.admin.tp.manual", "[location]", warpSpot.getBlockX() + " " + warpSpot.getBlockY() + " "
+ warpSpot.getBlockZ());
Player player = user.getPlayer();
if (args.size() == 2) {
// We are trying to teleport another player
UUID playerToTeleportUUID = getPlayers().getUUID(args.get(1));
if (playerToTeleportUUID == null) {
user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(1));
return false;
} else {
User userToTeleport = User.getInstance(playerToTeleportUUID);
if (!userToTeleport.isOnline()) {
user.sendMessage("general.errors.offline-player");
return false;
}
player = userToTeleport.getPlayer();
failureMessage = userToTeleport.getTranslation("general.errors.no-safe-location-found");
}
}
// Teleport
new SafeSpotTeleport.Builder(getPlugin())
.entity(player)
.location(warpSpot)
.failureMessage(failureMessage)
.build();
return true;
}
}
// Check island exists
if (!getIslands().hasIsland(getWorld(), targetUUID) && !getIslands().inTeam(getWorld(), targetUUID)) {
user.sendMessage("general.errors.player-has-no-island");
return false;
}
if (args.size() == 2) {
// We are trying to teleport another player
UUID playerToTeleportUUID = getPlayers().getUUID(args.get(1));
if (playerToTeleportUUID == null) {
user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(1));
return false;
} else {
userToTeleport = User.getInstance(playerToTeleportUUID);
if (!userToTeleport.isOnline()) {
user.sendMessage("general.errors.offline-player");
return false;
}
}
}
return true;
}
@Override
public boolean execute(User user, String label, List<String> args) {
Location warpSpot = getIslands().getIslandLocation(getWorld(), targetUUID).toVector().toLocation(getWorld());
if (getLabel().equals("tpnether")) {
warpSpot = getIslands().getIslandLocation(getWorld(), targetUUID).toVector().toLocation(getPlugin().getIWM().getNetherWorld(getWorld()));
} else if (getLabel().equals("tpend")) {
warpSpot = getIslands().getIslandLocation(getWorld(), targetUUID).toVector().toLocation(getPlugin().getIWM().getEndWorld(getWorld()));
}
// Otherwise, ask the admin to go to a safe spot
String failureMessage = user.getTranslation("commands.admin.tp.manual", "[location]", warpSpot.getBlockX() + " " + warpSpot.getBlockY() + " "
+ warpSpot.getBlockZ());
Player player = user.getPlayer();
if (args.size() == 2) {
player = userToTeleport.getPlayer();
failureMessage = userToTeleport.getTranslation("general.errors.no-safe-location-found");
}
// Teleport
new SafeSpotTeleport.Builder(getPlugin())
.entity(player)
.location(warpSpot)
.failureMessage(failureMessage)
.build();
return true;
}
@Override

View File

@ -82,11 +82,13 @@ public class AdminTeleportCommandTest {
when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p);
when(user.getName()).thenReturn("tastybento");
when(user.isPlayer()).thenReturn(true);
User.setPlugin(plugin);
// Parent command has no aliases
ac = mock(CompositeCommand.class);
when(ac.getSubCommandAliases()).thenReturn(new HashMap<>());
when(ac.getTopLabel()).thenReturn("bskyblock");
// Island World Manager
iwm = mock(IslandWorldManager.class);
@ -160,14 +162,14 @@ public class AdminTeleportCommandTest {
@Test
public void testExecuteUserStringListOfStringEmptyArgs() {
AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp");
assertFalse(atc.execute(user, "tp", new ArrayList<>()));
verify(user).sendMessage(Mockito.anyString());
assertFalse(atc.canExecute(user, "tp", new ArrayList<>()));
verify(user).sendMessage(eq("commands.help.header"), eq(TextVariables.LABEL), eq(null));
}
@Test
public void testExecuteUserStringListOfStringUnknownTarget() {
AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp");
assertFalse(atc.execute(user, "tp", Collections.singletonList("tastybento")));
assertFalse(atc.canExecute(user, "tp", Collections.singletonList("tastybento")));
verify(user).sendMessage(eq("general.errors.unknown-player"), eq(TextVariables.NAME), eq("tastybento"));
}
@ -176,7 +178,7 @@ public class AdminTeleportCommandTest {
when(pm.getUUID(eq("tastybento"))).thenReturn(notUUID);
when(im.hasIsland(any(), any(UUID.class))).thenReturn(false);
AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp");
assertFalse(atc.execute(user, "tp", Collections.singletonList("tastybento")));
assertFalse(atc.canExecute(user, "tp", Collections.singletonList("tastybento")));
verify(user).sendMessage(eq("general.errors.player-has-no-island"));
}