Add /tp command

This commit is contained in:
William committed 2022-08-18 14:10:37 +01:00
1 parent 25975a4de6
commit 7add61e3cd
5 files changed
+127 -19

No files matched your search

+2
View File
@@ -39,5 +39,7 @@ commands:
usage: /delwarp <name>
editwarp:
usage: /editwarp <name> [rename|description|relocate] [args]
tp:
usage: /tp <name>
back:
usage: /back
@@ -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
}
}
}
@@ -1,10 +0,0 @@
package net.william278.huskhomes.messenger;
import com.google.gson.GsonBuilder;
public class EmptyPayload implements MessageSerializable {
@Override
public String toJson() {
return new GsonBuilder().setPrettyPrinting().create().toJson(this);
}
}
@@ -108,7 +108,7 @@ public abstract class NetworkMessenger {
* @param message The received {@link Message}
*/
protected final void handleMessage(@NotNull OnlineUser receiver, @NotNull Message message) {
switch (message.kind) {
switch (message.relayType) {
// Handle a message and send reply
case MESSAGE -> prepareReply(receiver, message).thenAccept(reply -> sendReply(receiver, message));
// Handle a reply message
@@ -126,15 +126,15 @@ public abstract class NetworkMessenger {
// Handle different message types and apply changes for reply message
switch (message.type) {
case TP_REQUEST -> {
//todo
}
case POSITION_REQUEST -> message.payload = receiver.getPosition().join().toJson();
case POSITION_REQUEST -> message.payload = MessagePayload.withPosition(receiver.getPosition().join());
}
// Prepare reply message
message.targetPlayer = message.sender;
message.sender = receiver.username;
message.kind = Message.MessageKind.REPLY;
message.relayType = Message.RelayType.REPLY;
return message;
});
}
@@ -1,8 +1,8 @@
package net.william278.huskhomes.teleport;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.messenger.EmptyPayload;
import net.william278.huskhomes.messenger.Message;
import net.william278.huskhomes.messenger.MessagePayload;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.player.User;
import net.william278.huskhomes.position.Home;
@@ -35,7 +35,7 @@ public class TeleportManager {
* A set of user UUIDs currently on warmup countdowns for {@link TimedTeleport}
*/
@NotNull
private HashSet<UUID> currentlyOnWarmup = new HashSet<>();
private final HashSet<UUID> currentlyOnWarmup = new HashSet<>();
public TeleportManager(@NotNull HuskHomes implementor) {
this.plugin = implementor;
@@ -136,10 +136,10 @@ public class TeleportManager {
new Message(Message.MessageType.POSITION_REQUEST,
requester.username,
playerName,
new EmptyPayload(),
Message.MessageKind.MESSAGE,
MessagePayload.empty(),
Message.RelayType.MESSAGE,
plugin.getSettings().clusterId))
.thenApply(reply -> Optional.of(Position.fromJson(reply.payload)));
.thenApply(reply -> Optional.ofNullable(reply.payload.position));
}
return CompletableFuture.supplyAsync(Optional::empty);
}