refactor: improve RandomTeleportCommand

This commit is contained in:
ChomeNS
2025-05-16 16:51:05 +07:00
parent 8ab8ecd2f7
commit c7a578f1d0
4 changed files with 62 additions and 14 deletions

View File

@@ -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",

View File

@@ -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)
);
}
}
}