From c7a578f1d0683e5ff8869303ab4b19a50cdb6a95 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Fri, 16 May 2025 16:51:05 +0700 Subject: [PATCH] refactor: improve RandomTeleportCommand --- build-number.txt | 2 +- .../contexts/PlayerCommandContext.java | 10 ++++ .../commands/RandomTeleportCommand.java | 60 +++++++++++++++---- src/main/resources/language-en_us.json | 4 +- 4 files changed, 62 insertions(+), 14 deletions(-) diff --git a/build-number.txt b/build-number.txt index 0f2e8c5a..52e31c72 100644 --- a/build-number.txt +++ b/build-number.txt @@ -1 +1 @@ -3197 \ No newline at end of file +3199 \ No newline at end of file diff --git a/src/main/java/me/chayapak1/chomens_bot/command/contexts/PlayerCommandContext.java b/src/main/java/me/chayapak1/chomens_bot/command/contexts/PlayerCommandContext.java index 302685c3..d30f8963 100644 --- a/src/main/java/me/chayapak1/chomens_bot/command/contexts/PlayerCommandContext.java +++ b/src/main/java/me/chayapak1/chomens_bot/command/contexts/PlayerCommandContext.java @@ -5,6 +5,7 @@ import me.chayapak1.chomens_bot.command.CommandContext; import me.chayapak1.chomens_bot.data.chat.ChatPacketType; import me.chayapak1.chomens_bot.data.player.PlayerEntry; import me.chayapak1.chomens_bot.util.I18nUtilities; +import me.chayapak1.chomens_bot.util.UUIDUtilities; import net.kyori.adventure.text.Component; public class PlayerCommandContext extends CommandContext { @@ -33,7 +34,16 @@ public class PlayerCommandContext extends CommandContext { @Override public void sendOutput (final Component component) { + sendOutput(component, false); + } + + public void sendOutput (final Component component, final boolean onlyToSender) { final Component rendered = I18nUtilities.render(component); + + final String selector = onlyToSender + ? UUIDUtilities.selector(this.sender.profile.getId()) + : this.selector; + bot.chat.tellraw( Component.translatable( "%s", diff --git a/src/main/java/me/chayapak1/chomens_bot/commands/RandomTeleportCommand.java b/src/main/java/me/chayapak1/chomens_bot/commands/RandomTeleportCommand.java index e2cb7835..d519164f 100644 --- a/src/main/java/me/chayapak1/chomens_bot/commands/RandomTeleportCommand.java +++ b/src/main/java/me/chayapak1/chomens_bot/commands/RandomTeleportCommand.java @@ -5,11 +5,14 @@ import me.chayapak1.chomens_bot.command.Command; import me.chayapak1.chomens_bot.command.CommandContext; import me.chayapak1.chomens_bot.command.CommandException; import me.chayapak1.chomens_bot.command.TrustLevel; +import me.chayapak1.chomens_bot.command.contexts.PlayerCommandContext; import me.chayapak1.chomens_bot.data.chat.ChatPacketType; import me.chayapak1.chomens_bot.data.player.PlayerEntry; import me.chayapak1.chomens_bot.util.MathUtilities; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.format.Style; +import net.kyori.adventure.text.format.TextDecoration; public class RandomTeleportCommand extends Command { public RandomTeleportCommand () { @@ -19,7 +22,7 @@ public class RandomTeleportCommand extends Command { new String[] { "randomteleport" }, TrustLevel.PUBLIC, false, - new ChatPacketType[]{ ChatPacketType.DISGUISED } + new ChatPacketType[] { ChatPacketType.DISGUISED } ); } @@ -31,17 +34,50 @@ public class RandomTeleportCommand extends Command { final PlayerEntry sender = context.sender; - final int positionX = MathUtilities.between(-1_000_000, 1_000_000); - final int positionZ = MathUtilities.between(-1_000_000, 1_000_000); - final String stringPosition = positionX + " 100 " + positionZ; // is hardcoding the y to 100 a great idea? - - bot.core.run("essentials:teleport " + sender.profile.getIdAsString() + " " + stringPosition); - - return Component.translatable( - "commands.rtp.output", - bot.colorPalette.defaultColor, - Component.text(sender.profile.getName()).color(bot.colorPalette.username), - Component.text(stringPosition).color(NamedTextColor.GREEN) + final String position = String.format( + "%d %d %d", + MathUtilities.between(-1_000_000, 1_000_000), + 100, + MathUtilities.between(-1_000_000, 1_000_000) ); + + bot.core.run( + String.format( + "essentials:teleport %s %s", + sender.profile.getIdAsString(), + position + ) + ); + + if (context instanceof final PlayerCommandContext playerContext) { + playerContext.sendOutput( + Component.translatable( + "commands.rtp.teleporting_no_location", + bot.colorPalette.defaultColor, + Component.text(sender.profile.getName(), bot.colorPalette.username) + ) + ); + + playerContext.sendOutput( + Component.translatable( + "commands.rtp.to_sender", + Style.style() + .color(NamedTextColor.GRAY) + .decorate(TextDecoration.ITALIC) + .build(), + Component.text(position, NamedTextColor.GREEN) + ), + true + ); + + return null; + } else { + return Component.translatable( + "commands.rtp.teleporting_location", + bot.colorPalette.defaultColor, + Component.text(sender.profile.getName(), bot.colorPalette.username), + Component.text(position, NamedTextColor.GREEN) + ); + } } } diff --git a/src/main/resources/language-en_us.json b/src/main/resources/language-en_us.json index 2455daac..813ac4e4 100644 --- a/src/main/resources/language-en_us.json +++ b/src/main/resources/language-en_us.json @@ -211,7 +211,9 @@ "commands.netmsg.hover.copy_server_to_clipboard": "Click here to copy the server host and port to your clipboard", "commands.rtp.description": "Randomly teleports you", - "commands.rtp.output": "Teleporting %s to %s", + "commands.rtp.teleporting_no_location": "Teleporting %s to a random location...", + "commands.rtp.teleporting_location": "Teleporting %s to %s", + "commands.rtp.to_sender": "You are being teleported to %s, this might take a few seconds depending on the server's performance", "commands.refillcore.description": "Refills and resets the bots command core", "commands.refillcore.output": "Refilled the command core",