mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-01 22:16:08 +00:00
Add /tp command
This commit is contained in:
1 parent
25975a4de6
commit
7add61e3cd
5 files changed
+127
-19
No files matched your search
@@ -0,0 +1,116 @@
|
||||
package net.william278.huskhomes.command;
|
||||
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.HuskHomesException;
|
||||
import net.william278.huskhomes.player.OnlineUser;
|
||||
import net.william278.huskhomes.position.Position;
|
||||
import net.william278.huskhomes.util.Permission;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class TpCommand extends CommandBase implements TabCompletable, ConsoleExecutable {
|
||||
|
||||
protected TpCommand(@NotNull HuskHomes implementor) {
|
||||
super("tp", Permission.COMMAND_TP, implementor, "tpo");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
|
||||
OnlineUser userToTeleport = onlineUser;
|
||||
TeleportCommandTarget targetPosition;
|
||||
|
||||
// Validate argument length
|
||||
if (args.length > 6 || args.length < 1) {
|
||||
plugin.getLocales().getLocale("error_invalid_syntax", "/tp <target> [destination]")
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
// If the first argument is a player
|
||||
if (args.length == 1) {
|
||||
targetPosition = new TeleportCommandTarget(args[0]);
|
||||
} else {
|
||||
// Determine if a player argument has been passed before the destination argument
|
||||
int coordinatesIndex = (args[0].matches("^[a-zA-Z0-9_.*]{1,17}$") || args.length == 4 || args.length == 6) ? 1 : 0;
|
||||
if (coordinatesIndex == 1) {
|
||||
userToTeleport = plugin.getOnlinePlayers().stream().filter(user -> user.username.equalsIgnoreCase(args[0]))
|
||||
.findFirst().orElse(plugin.getOnlinePlayers().stream().filter(user -> user.username.startsWith(args[0]))
|
||||
.findFirst().orElse(null));
|
||||
if (userToTeleport == null) {
|
||||
plugin.getLocales().getLocale("error_player_not_found", args[0]).ifPresent(onlineUser::sendMessage);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Determine the target position (player or coordinates)
|
||||
if (args.length == 2) {
|
||||
targetPosition = new TeleportCommandTarget(args[1]);
|
||||
} else {
|
||||
final Position userPosition = onlineUser.getPosition().join();
|
||||
final Optional<Position> parsedPosition = Position.parse(userPosition,
|
||||
Arrays.copyOfRange(args, coordinatesIndex, args.length - 1));
|
||||
if (parsedPosition.isEmpty()) {
|
||||
plugin.getLocales().getLocale("error_invalid_syntax", "/tp <target> <x> <y> <z> [world] [server]")
|
||||
.ifPresent(onlineUser::sendMessage);
|
||||
return;
|
||||
}
|
||||
targetPosition = new TeleportCommandTarget(parsedPosition.get());
|
||||
}
|
||||
}
|
||||
|
||||
// Execute the teleport
|
||||
final OnlineUser teleportingUser = userToTeleport;
|
||||
if (targetPosition.targetType == TeleportCommandTarget.TargetType.PLAYER) {
|
||||
assert targetPosition.targetPlayer != null;
|
||||
plugin.getTeleportManager().teleportToPlayer(teleportingUser, targetPosition.targetPlayer);
|
||||
} else if (targetPosition.targetType == TeleportCommandTarget.TargetType.POSITION) {
|
||||
assert targetPosition.targetPosition != null;
|
||||
plugin.getTeleportManager().teleport(teleportingUser, targetPosition.targetPosition).thenAccept(
|
||||
result -> plugin.getTeleportManager().finishTeleport(teleportingUser, result));
|
||||
}
|
||||
throw new HuskHomesException("Attempted to execute invalid teleport command operation");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConsoleExecute(@NotNull String[] args) {
|
||||
//todo
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull List<String> onTabComplete(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Identifies the target of a teleport command operation
|
||||
*/
|
||||
private static class TeleportCommandTarget {
|
||||
|
||||
@NotNull
|
||||
private final TargetType targetType;
|
||||
@Nullable
|
||||
private String targetPlayer;
|
||||
@Nullable
|
||||
private Position targetPosition;
|
||||
|
||||
public TeleportCommandTarget(@NotNull String targetPlayer) {
|
||||
this.targetPlayer = targetPlayer;
|
||||
this.targetType = TargetType.PLAYER;
|
||||
}
|
||||
|
||||
public TeleportCommandTarget(@NotNull Position targetPosition) {
|
||||
this.targetPosition = targetPosition;
|
||||
this.targetType = TargetType.POSITION;
|
||||
}
|
||||
|
||||
public enum TargetType {
|
||||
PLAYER,
|
||||
POSITION
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user