mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-01-25 01:21:21 +01:00
Add command to teleport users. (#2399)
* Add command to teleport users. * Fix bugs
This commit is contained in:
parent
0938df8824
commit
156c3da3bb
@ -46,6 +46,7 @@ public class AdminTeleportCommand extends CompositeCommand {
|
|||||||
setPermission("admin.tp");
|
setPermission("admin.tp");
|
||||||
setParametersHelp("commands.admin.tp.parameters");
|
setParametersHelp("commands.admin.tp.parameters");
|
||||||
setDescription("commands.admin.tp.description");
|
setDescription("commands.admin.tp.description");
|
||||||
|
this.setOnlyPlayer(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -4,12 +4,14 @@ import java.util.ArrayList;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
import org.eclipse.jdt.annotation.NonNull;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
|
|
||||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||||
@ -20,23 +22,21 @@ import world.bentobox.bentobox.util.Util;
|
|||||||
import world.bentobox.bentobox.util.teleport.SafeSpotTeleport;
|
import world.bentobox.bentobox.util.teleport.SafeSpotTeleport;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enables admins to teleport to a player's island, nether or end islands, or to teleport another player
|
* Enables admins to teleport a player to another player's island, nether or end islands,
|
||||||
* to a player's island
|
|
||||||
*
|
*
|
||||||
* For example /acid tp tastybento boxmanager would teleport BoxManager to tastybento's overwold island
|
* For example /acid tp lspvicky tastybento [island name] would teleport lspvicky to tastybento's [named] island
|
||||||
*
|
*
|
||||||
* If the user has multiple islands, then the format is:
|
|
||||||
* [admin_command] [user with island] [island to go to]
|
|
||||||
*/
|
*/
|
||||||
public class AdminTeleportUserCommand extends CompositeCommand {
|
public class AdminTeleportUserCommand extends CompositeCommand {
|
||||||
|
|
||||||
private static final String NOT_SAFE = "general.errors.no-safe-location-found";
|
private static final String NOT_SAFE = "general.errors.no-safe-location-found";
|
||||||
|
private Location warpSpot;
|
||||||
private @Nullable UUID targetUUID;
|
private @Nullable UUID targetUUID;
|
||||||
private @Nullable User userToTeleport;
|
private @NonNull User toBeTeleported;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param parent - parent command
|
* @param parent - parent command
|
||||||
* @param tpCommand - should be "tp", "tpnether" or "tpend"
|
* @param tpCommand - should be "tpuser", "tpusernether" or "tpuserend"
|
||||||
*/
|
*/
|
||||||
public AdminTeleportUserCommand(CompositeCommand parent, String tpCommand) {
|
public AdminTeleportUserCommand(CompositeCommand parent, String tpCommand) {
|
||||||
super(parent, tpCommand);
|
super(parent, tpCommand);
|
||||||
@ -45,57 +45,47 @@ public class AdminTeleportUserCommand extends CompositeCommand {
|
|||||||
@Override
|
@Override
|
||||||
public void setup() {
|
public void setup() {
|
||||||
// Permission
|
// Permission
|
||||||
setPermission("admin.tp");
|
setPermission("admin.tpuser");
|
||||||
setParametersHelp("commands.admin.tp.parameters");
|
setParametersHelp("commands.admin.tpuser.parameters");
|
||||||
setDescription("commands.admin.tp.description");
|
setDescription("commands.admin.tpuser.description");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canExecute(User user, String label, List<String> args) {
|
public boolean canExecute(User user, String label, List<String> args) {
|
||||||
if (args.isEmpty() || args.size() > 3) {
|
if (args.isEmpty() || args.size() == 1) {
|
||||||
this.showHelp(this, user);
|
this.showHelp(this, user);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// Check for console or not
|
// Convert first name to a UUID
|
||||||
if (!user.isPlayer() && args.size() == 1) {
|
UUID teleportee = Util.getUUID(args.get(0));
|
||||||
user.sendMessage("general.errors.use-in-game");
|
if (teleportee == null) {
|
||||||
|
user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// Convert name to a UUID
|
// Check online
|
||||||
targetUUID = Util.getUUID(args.get(0));
|
toBeTeleported = User.getInstance(teleportee);
|
||||||
|
if (!toBeTeleported.isOnline()) {
|
||||||
|
user.sendMessage("general.errors.offline-player");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert second name to a UUID
|
||||||
|
targetUUID = Util.getUUID(args.get(1));
|
||||||
if (targetUUID == null) {
|
if (targetUUID == null) {
|
||||||
user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0));
|
user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check island exists
|
// Check island exists
|
||||||
if (!getIslands().hasIsland(getWorld(), targetUUID) && !getIslands().inTeam(getWorld(), targetUUID)) {
|
if (!getIslands().hasIsland(getWorld(), targetUUID) && !getIslands().inTeam(getWorld(), targetUUID)) {
|
||||||
user.sendMessage("general.errors.player-has-no-island");
|
user.sendMessage("general.errors.player-has-no-island");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args.size() == 2) {
|
|
||||||
// We are trying to teleport another player
|
|
||||||
UUID playerToTeleportUUID = Util.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) {
|
|
||||||
World world = getWorld();
|
World world = getWorld();
|
||||||
if (getLabel().equals("tpnether")) {
|
if (getLabel().equals("tpusernether")) {
|
||||||
world = getPlugin().getIWM().getNetherWorld(getWorld());
|
world = getPlugin().getIWM().getNetherWorld(getWorld());
|
||||||
} else if (getLabel().equals("tpend")) {
|
} else if (getLabel().equals("tpuserend")) {
|
||||||
world = getPlugin().getIWM().getEndWorld(getWorld());
|
world = getPlugin().getIWM().getEndWorld(getWorld());
|
||||||
}
|
}
|
||||||
if (world == null) {
|
if (world == null) {
|
||||||
@ -103,38 +93,45 @@ public class AdminTeleportUserCommand extends CompositeCommand {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// Get default location if there are no arguments
|
// Get default location if there are no arguments
|
||||||
Location warpSpot = getSpot(world);
|
warpSpot = getSpot(world);
|
||||||
if (warpSpot == null) {
|
if (warpSpot == null) {
|
||||||
user.sendMessage(NOT_SAFE);
|
user.sendMessage(NOT_SAFE);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// See if there is a quoted island name
|
|
||||||
if (args.size() == 2) {
|
if (args.size() == 2) {
|
||||||
Map<String, IslandInfo> names = getNameIslandMap(user);
|
return true;
|
||||||
final String name = String.join(" ", args);
|
}
|
||||||
|
|
||||||
|
// They named the island to go to
|
||||||
|
Map<String, IslandInfo> names = getNameIslandMap(User.getInstance(targetUUID));
|
||||||
|
final String name = String.join(" ", args.subList(2, args.size()));
|
||||||
if (!names.containsKey(name)) {
|
if (!names.containsKey(name)) {
|
||||||
// Failed home name check
|
// Failed home name check
|
||||||
user.sendMessage("commands.island.go.unknown-home");
|
user.sendMessage("commands.island.go.unknown-home");
|
||||||
user.sendMessage("commands.island.sethome.homes-are");
|
user.sendMessage("commands.island.sethome.homes-are");
|
||||||
names.keySet().forEach(
|
names.keySet()
|
||||||
n -> user.sendMessage("commands.island.sethome.home-list-syntax", TextVariables.NAME, n));
|
.forEach(n -> user.sendMessage("commands.island.sethome.home-list-syntax", TextVariables.NAME, n));
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else if (names.size() > 1) {
|
||||||
IslandInfo info = names.get(name);
|
IslandInfo info = names.get(name);
|
||||||
Island island = info.island;
|
Island island = info.island;
|
||||||
warpSpot = island.getSpawnPoint(world.getEnvironment()) != null
|
warpSpot = island.getSpawnPoint(world.getEnvironment()) != null
|
||||||
? island.getSpawnPoint(world.getEnvironment())
|
? island.getSpawnPoint(world.getEnvironment())
|
||||||
: island.getProtectionCenter().toVector().toLocation(world);
|
: island.getProtectionCenter().toVector().toLocation(world);
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean execute(User user, String label, List<String> args) {
|
||||||
|
Objects.requireNonNull(warpSpot);
|
||||||
// Otherwise, ask the admin to go to a safe spot
|
// Otherwise, ask the admin to go to a safe spot
|
||||||
String failureMessage = user.getTranslation("commands.admin.tp.manual", "[location]", warpSpot.getBlockX() + " " + warpSpot.getBlockY() + " "
|
String failureMessage = user.getTranslation("commands.admin.tp.manual", "[location]", warpSpot.getBlockX() + " " + warpSpot.getBlockY() + " "
|
||||||
+ warpSpot.getBlockZ());
|
+ warpSpot.getBlockZ());
|
||||||
// Set the player
|
// Set the player
|
||||||
Player player = args.size() == 2 ? userToTeleport.getPlayer() : user.getPlayer();
|
Player player = toBeTeleported.getPlayer();
|
||||||
if (args.size() == 2) {
|
if (args.size() == 2) {
|
||||||
failureMessage = userToTeleport.getTranslation(NOT_SAFE);
|
failureMessage = user.getTranslation(NOT_SAFE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Teleport
|
// Teleport
|
||||||
@ -158,18 +155,18 @@ public class AdminTeleportUserCommand extends CompositeCommand {
|
|||||||
private record IslandInfo(Island island, boolean islandName) {
|
private record IslandInfo(Island island, boolean islandName) {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, IslandInfo> getNameIslandMap(User user) {
|
private Map<String, IslandInfo> getNameIslandMap(User target) {
|
||||||
Map<String, IslandInfo> islandMap = new HashMap<>();
|
Map<String, IslandInfo> islandMap = new HashMap<>();
|
||||||
int index = 0;
|
int index = 0;
|
||||||
for (Island island : getIslands().getIslands(getWorld(), user.getUniqueId())) {
|
for (Island island : getIslands().getIslands(getWorld(), target.getUniqueId())) {
|
||||||
index++;
|
index++;
|
||||||
if (island.getName() != null && !island.getName().isBlank()) {
|
if (island.getName() != null && !island.getName().isBlank()) {
|
||||||
// Name has been set
|
// Name has been set
|
||||||
islandMap.put(island.getName(), new IslandInfo(island, true));
|
islandMap.put(island.getName(), new IslandInfo(island, true));
|
||||||
} else {
|
} else {
|
||||||
// Name has not been set
|
// Name has not been set
|
||||||
String text = user.getTranslation("protection.flags.ENTER_EXIT_MESSAGES.island", TextVariables.NAME,
|
String text = target.getTranslation("protection.flags.ENTER_EXIT_MESSAGES.island", TextVariables.NAME,
|
||||||
user.getName(), TextVariables.DISPLAY_NAME, user.getDisplayName()) + " " + index;
|
target.getName(), TextVariables.DISPLAY_NAME, target.getDisplayName()) + " " + index;
|
||||||
islandMap.put(text, new IslandInfo(island, true));
|
islandMap.put(text, new IslandInfo(island, true));
|
||||||
}
|
}
|
||||||
// Add homes. Homes do not need an island specified
|
// Add homes. Homes do not need an island specified
|
||||||
@ -187,11 +184,15 @@ public class AdminTeleportUserCommand extends CompositeCommand {
|
|||||||
// Don't show every player on the server. Require at least the first letter
|
// Don't show every player on the server. Require at least the first letter
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
if (args.size() == 1) {
|
if (args.size() == 2 || args.size() == 3) {
|
||||||
return Optional.of(Util.tabLimit(new ArrayList<>(Util.getOnlinePlayerList(user)), lastArg));
|
return Optional.of(Util.tabLimit(new ArrayList<>(Util.getOnlinePlayerList(user)), lastArg));
|
||||||
}
|
}
|
||||||
if (args.size() == 2) {
|
|
||||||
return Optional.of(Util.tabLimit(new ArrayList<>(getNameIslandMap(user).keySet()), lastArg));
|
if (args.size() == 4) {
|
||||||
|
UUID target = Util.getUUID(args.get(2));
|
||||||
|
return target == null ? Optional.empty()
|
||||||
|
: Optional
|
||||||
|
.of(Util.tabLimit(new ArrayList<>(getNameIslandMap(User.getInstance(target)).keySet()), lastArg));
|
||||||
}
|
}
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
@ -50,9 +50,14 @@ public abstract class DefaultAdminCommand extends CompositeCommand {
|
|||||||
this.setDescription("commands.admin.help.description");
|
this.setDescription("commands.admin.help.description");
|
||||||
|
|
||||||
new AdminVersionCommand(this);
|
new AdminVersionCommand(this);
|
||||||
|
|
||||||
new AdminTeleportCommand(this, "tp");
|
new AdminTeleportCommand(this, "tp");
|
||||||
new AdminTeleportCommand(this, "tpnether");
|
new AdminTeleportCommand(this, "tpnether");
|
||||||
new AdminTeleportCommand(this, "tpend");
|
new AdminTeleportCommand(this, "tpend");
|
||||||
|
new AdminTeleportUserCommand(this, "tpuser");
|
||||||
|
new AdminTeleportUserCommand(this, "tpusernether");
|
||||||
|
new AdminTeleportUserCommand(this, "tpuserend");
|
||||||
|
|
||||||
new AdminGetrankCommand(this);
|
new AdminGetrankCommand(this);
|
||||||
new AdminSetrankCommand(this);
|
new AdminSetrankCommand(this);
|
||||||
new AdminInfoCommand(this);
|
new AdminInfoCommand(this);
|
||||||
|
@ -271,6 +271,9 @@ commands:
|
|||||||
description: teleport to a player's island
|
description: teleport to a player's island
|
||||||
manual: '&c No safe warp found! Manually tp near to &b [location] &c and check
|
manual: '&c No safe warp found! Manually tp near to &b [location] &c and check
|
||||||
it out'
|
it out'
|
||||||
|
tpuser:
|
||||||
|
parameters: <teleporting player> <island's player> [player's island]
|
||||||
|
description: teleport a player to another player's island
|
||||||
getrank:
|
getrank:
|
||||||
parameters: <player> [island owner]
|
parameters: <player> [island owner]
|
||||||
description: get a player's rank on their island or the island of the owner
|
description: get a player's rank on their island or the island of the owner
|
||||||
|
Loading…
Reference in New Issue
Block a user