Skip to content

Commit

Permalink
Add command to teleport users. (#2399)
Browse files Browse the repository at this point in the history
* Add command to teleport users.

* Fix bugs
  • Loading branch information
tastybento committed Jun 3, 2024
1 parent 0938df8 commit 156c3da
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public void setup() {
setPermission("admin.tp");
setParametersHelp("commands.admin.tp.parameters");
setDescription("commands.admin.tp.description");
this.setOnlyPlayer(true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;

import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;

import world.bentobox.bentobox.api.commands.CompositeCommand;
Expand All @@ -20,23 +22,21 @@
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
* to a player's island
* Enables admins to teleport a player to another player's island, nether or end islands,
*
* 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 {

private static final String NOT_SAFE = "general.errors.no-safe-location-found";
private Location warpSpot;
private @Nullable UUID targetUUID;
private @Nullable User userToTeleport;
private @NonNull User toBeTeleported;

/**
* @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) {
super(parent, tpCommand);
Expand All @@ -45,96 +45,93 @@ public AdminTeleportUserCommand(CompositeCommand parent, String tpCommand) {
@Override
public void setup() {
// Permission
setPermission("admin.tp");
setParametersHelp("commands.admin.tp.parameters");
setDescription("commands.admin.tp.description");
setPermission("admin.tpuser");
setParametersHelp("commands.admin.tpuser.parameters");
setDescription("commands.admin.tpuser.description");
}

@Override
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);
return false;
}
// Check for console or not
if (!user.isPlayer() && args.size() == 1) {
user.sendMessage("general.errors.use-in-game");
// Convert first name to a UUID
UUID teleportee = Util.getUUID(args.get(0));
if (teleportee == null) {
user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0));
return false;
}
// Check online
toBeTeleported = User.getInstance(teleportee);
if (!toBeTeleported.isOnline()) {
user.sendMessage("general.errors.offline-player");
return false;
}
// Convert name to a UUID
targetUUID = Util.getUUID(args.get(0));

// Convert second name to a UUID
targetUUID = Util.getUUID(args.get(1));
if (targetUUID == null) {
user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0));
return false;
}

// 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 = 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();
if (getLabel().equals("tpnether")) {
if (getLabel().equals("tpusernether")) {
world = getPlugin().getIWM().getNetherWorld(getWorld());
} else if (getLabel().equals("tpend")) {
} else if (getLabel().equals("tpuserend")) {
world = getPlugin().getIWM().getEndWorld(getWorld());
}
if (world == null) {
user.sendMessage(NOT_SAFE);
return false;
}
// Get default location if there are no arguments
Location warpSpot = getSpot(world);
warpSpot = getSpot(world);
if (warpSpot == null) {
user.sendMessage(NOT_SAFE);
return false;
}
// See if there is a quoted island name
if (args.size() == 2) {
Map<String, IslandInfo> names = getNameIslandMap(user);
final String name = String.join(" ", args);
if (!names.containsKey(name)) {
// Failed home name check
user.sendMessage("commands.island.go.unknown-home");
user.sendMessage("commands.island.sethome.homes-are");
names.keySet().forEach(
n -> user.sendMessage("commands.island.sethome.home-list-syntax", TextVariables.NAME, n));
return false;
} else {
IslandInfo info = names.get(name);
Island island = info.island;
warpSpot = island.getSpawnPoint(world.getEnvironment()) != null
? island.getSpawnPoint(world.getEnvironment())
: island.getProtectionCenter().toVector().toLocation(world);
}
return true;
}

// 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)) {
// Failed home name check
user.sendMessage("commands.island.go.unknown-home");
user.sendMessage("commands.island.sethome.homes-are");
names.keySet()
.forEach(n -> user.sendMessage("commands.island.sethome.home-list-syntax", TextVariables.NAME, n));
return false;
} else if (names.size() > 1) {
IslandInfo info = names.get(name);
Island island = info.island;
warpSpot = island.getSpawnPoint(world.getEnvironment()) != null
? island.getSpawnPoint(world.getEnvironment())
: 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
String failureMessage = user.getTranslation("commands.admin.tp.manual", "[location]", warpSpot.getBlockX() + " " + warpSpot.getBlockY() + " "
+ warpSpot.getBlockZ());
// Set the player
Player player = args.size() == 2 ? userToTeleport.getPlayer() : user.getPlayer();
Player player = toBeTeleported.getPlayer();
if (args.size() == 2) {
failureMessage = userToTeleport.getTranslation(NOT_SAFE);
failureMessage = user.getTranslation(NOT_SAFE);
}

// Teleport
Expand All @@ -158,18 +155,18 @@ private Location getSpot(World world) {
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<>();
int index = 0;
for (Island island : getIslands().getIslands(getWorld(), user.getUniqueId())) {
for (Island island : getIslands().getIslands(getWorld(), target.getUniqueId())) {
index++;
if (island.getName() != null && !island.getName().isBlank()) {
// Name has been set
islandMap.put(island.getName(), new IslandInfo(island, true));
} else {
// Name has not been set
String text = user.getTranslation("protection.flags.ENTER_EXIT_MESSAGES.island", TextVariables.NAME,
user.getName(), TextVariables.DISPLAY_NAME, user.getDisplayName()) + " " + index;
String text = target.getTranslation("protection.flags.ENTER_EXIT_MESSAGES.island", TextVariables.NAME,
target.getName(), TextVariables.DISPLAY_NAME, target.getDisplayName()) + " " + index;
islandMap.put(text, new IslandInfo(island, true));
}
// Add homes. Homes do not need an island specified
Expand All @@ -187,11 +184,15 @@ public Optional<List<String>> tabComplete(User user, String alias, List<String>
// Don't show every player on the server. Require at least the first letter
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));
}
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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ public void setup() {
this.setDescription("commands.admin.help.description");

new AdminVersionCommand(this);

new AdminTeleportCommand(this, "tp");
new AdminTeleportCommand(this, "tpnether");
new AdminTeleportCommand(this, "tpend");
new AdminTeleportUserCommand(this, "tpuser");
new AdminTeleportUserCommand(this, "tpusernether");
new AdminTeleportUserCommand(this, "tpuserend");

new AdminGetrankCommand(this);
new AdminSetrankCommand(this);
new AdminInfoCommand(this);
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ commands:
description: teleport to a player's island
manual: '&c No safe warp found! Manually tp near to &b [location] &c and check
it out'
tpuser:
parameters: <teleporting player> <island's player> [player's island]
description: teleport a player to another player's island
getrank:
parameters: <player> [island owner]
description: get a player's rank on their island or the island of the owner
Expand Down

0 comments on commit 156c3da

Please sign in to comment.