Skip to content

Commit

Permalink
touchups
Browse files Browse the repository at this point in the history
  • Loading branch information
bazke committed Oct 9, 2023
1 parent 3f167cf commit 43b0408
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/main/java/com/lovetropics/extras/command/TpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class TpCommand {

private static final String ARGUMENT_NAME = "name";
private static final DynamicCommandExceptionType PLAYER_NOT_FOUND = new DynamicCommandExceptionType(o -> Component.translatable("commands.tpa.player_not_found"));
private static final DynamicCommandExceptionType REQUEST_NOT_FOUND = new DynamicCommandExceptionType(o -> Component.translatable("commands.tpa.request_not_found"));
private static final DynamicCommandExceptionType GENERAL_ERROR = new DynamicCommandExceptionType(o -> Component.translatable("commands.tpa.general_error"));
private static final DynamicCommandExceptionType TOO_MUCH = new DynamicCommandExceptionType(o -> Component.translatable("commands.tpa.toomuch"));

Expand Down Expand Up @@ -111,24 +112,26 @@ private static int tpHelp(final CommandContext<CommandSourceStack> ctx) throws C
}

private static int tpBack(final CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
final BlockPos blockPos = backCache.getIfPresent(ctx.getSource().getDisplayName().getString().toLowerCase());
final BlockPos blockPos = backCache.getIfPresent(nonNullSourcePlayer(ctx).getName().getString().toLowerCase());
if (blockPos == null) {
throw GENERAL_ERROR.create(null);
}

final ServerPlayer executingPlayer = nonNullSourcePlayer(ctx);

backCache.put(executingPlayer.getName().getString().toLowerCase(), executingPlayer.blockPosition()); //This allows going back and forth using /back
executingPlayer.connection.teleport(blockPos.getX(), blockPos.getY(), blockPos.getZ(), executingPlayer.yya, executingPlayer.xxa, Collections.emptySet());

return Command.SINGLE_SUCCESS;
}

private static int tpAccept(final CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
final String tpRequester = ctx.getArgument(ARGUMENT_NAME, String.class);
final String requestedTarget = requestCache.getIfPresent(tpRequester);
final String requestedTarget = requestCache.getIfPresent(tpRequester.toLowerCase());
final String commandExecutorName = nonNullSourcePlayer(ctx).getName().getString();

if (requestedTarget == null || !requestedTarget.equalsIgnoreCase(ctx.getSource().getDisplayName().getString())) {
throw PLAYER_NOT_FOUND.create(null);
if (requestedTarget == null || !requestedTarget.equalsIgnoreCase(commandExecutorName)) {
throw REQUEST_NOT_FOUND.create(null);
}

final ServerPlayer executingPlayer = nonNullSourcePlayer(ctx);
Expand All @@ -141,7 +144,7 @@ private static int tpAccept(final CommandContext<CommandSourceStack> ctx) throws
private static int acceptAllTpRequests(final CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
final ServerPlayer executingPlayer = nonNullSourcePlayer(ctx);

final String targetPlayerName = ctx.getSource().getDisplayName().getString();
final String targetPlayerName = executingPlayer.getName().getString();
final String targetPlayerNameLowerCase = targetPlayerName.toLowerCase();
final Map<String, String> requesterTargetMap = requestCache.asMap().entrySet().stream()
.filter(entry -> entry.getValue().equalsIgnoreCase(targetPlayerNameLowerCase))
Expand All @@ -157,7 +160,7 @@ private static int acceptAllTpRequests(final CommandContext<CommandSourceStack>

private static int tpRequest(final CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
final ServerPlayer executingPlayer = nonNullSourcePlayer(ctx);
final String requester = executingPlayer.getDisplayName().getString();
final String requester = executingPlayer.getName().getString();
final String requesterLowerCase = requester.toLowerCase();
final String target = ctx.getArgument(ARGUMENT_NAME, String.class);
final String targetLowerCase = target.toLowerCase();
Expand All @@ -178,9 +181,9 @@ private static int tpRequest(final CommandContext<CommandSourceStack> ctx) throw
}

private static void teleportAndSendMessage(ServerPlayer player, ServerPlayer target) {
requestCache.invalidate(player.getDisplayName().getString().toLowerCase());
backCache.put(player.getDisplayName().toString().toLowerCase(), player.blockPosition());
player.sendSystemMessage(Component.translatable("commands.tpa.tp_accepted", target.getDisplayName()));
requestCache.invalidate(player.getName().getString().toLowerCase());
backCache.put(player.getName().getString().toLowerCase(), player.blockPosition());
player.sendSystemMessage(Component.translatable("commands.tpa.tp_accepted", target.getName().getString()));
player.connection.teleport(target.getX(), target.getY(), target.getZ(), target.yya, target.xxa, Collections.emptySet());
}

Expand Down Expand Up @@ -214,6 +217,7 @@ public static void addTranslations(final RegistrateLangProvider provider) {
provider.add("commands.tpa.too_much", "You are doing that too much");
provider.add("commands.tpa.request_sent", "Request sent");
provider.add("commands.tpa.player_not_found", "Player not found");
provider.add("commands.tpa.request_not_found", "No teleport request");
provider.add("commands.tpa.general_error", "Unable to teleport");
provider.add("commands.tpa.help.tpa", "/tpa <player> - Request to teleport to a player");
provider.add("commands.tpa.help.tpaccept", "/tpaccept <player> - Accept a teleport request from a player");
Expand Down

0 comments on commit 43b0408

Please sign in to comment.