From b2fccdd28d6ca6cc1f82bf6502ff8a1e4c1d2cd1 Mon Sep 17 00:00:00 2001 From: ChomeNS Date: Sat, 25 Mar 2023 15:29:44 +0700 Subject: [PATCH] add list --- .../chomens_bot/commands/ListCommand.java | 92 +++++++++++++++++++ .../plugins/CommandHandlerPlugin.java | 1 + .../chomens_bot/plugins/PlayersPlugin.java | 3 +- 3 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 src/main/java/me/chayapak1/chomens_bot/commands/ListCommand.java diff --git a/src/main/java/me/chayapak1/chomens_bot/commands/ListCommand.java b/src/main/java/me/chayapak1/chomens_bot/commands/ListCommand.java new file mode 100644 index 0000000..7680ad1 --- /dev/null +++ b/src/main/java/me/chayapak1/chomens_bot/commands/ListCommand.java @@ -0,0 +1,92 @@ +package me.chayapak1.chomens_bot.commands; + +import me.chayapak1.chomens_bot.Bot; +import me.chayapak1.chomens_bot.chatParsers.data.MutablePlayerListEntry; +import me.chayapak1.chomens_bot.command.Command; +import me.chayapak1.chomens_bot.command.CommandContext; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.JoinConfiguration; +import net.kyori.adventure.text.event.ClickEvent; +import net.kyori.adventure.text.event.HoverEvent; +import net.kyori.adventure.text.format.NamedTextColor; + +import java.util.ArrayList; +import java.util.List; + +public class ListCommand implements Command { + public String name() { return "list"; } + + public String description() { + return "Lists all players in the server (including vanished)"; + } + + public List usage() { + final List usages = new ArrayList<>(); + usages.add(""); + + return usages; + } + + public List alias() { + final List aliases = new ArrayList<>(); + aliases.add(""); + + return aliases; + } + + public int trustLevel() { + return 0; + } + + public Component execute(CommandContext context, String[] args, String[] fullArgs) { + final Bot bot = context.bot(); + + final List list = bot.players().list(); + + final List playersComponent = new ArrayList<>(); + + for (MutablePlayerListEntry entry : list) { + playersComponent.add( + Component.translatable( + "%s › %s", + Component + .text(entry.profile().getName()) + .hoverEvent( + HoverEvent.showText( + Component.text("Click here to copy the username to your clipboard").color(NamedTextColor.GREEN) + ) + ) + .clickEvent( + ClickEvent.copyToClipboard(entry.profile().getName()) + ) + .color(NamedTextColor.YELLOW), + Component + .text(entry.profile().getIdAsString()) + .hoverEvent( + HoverEvent.showText( + Component.text("Click here to copy the UUID to your clipboard").color(NamedTextColor.GREEN) + ) + ) + .clickEvent( + ClickEvent.copyToClipboard(entry.profile().getIdAsString()) + ) + .color(NamedTextColor.AQUA) + ).color(NamedTextColor.DARK_GRAY) + ); + } + + final Component component = Component.empty() + .append(Component.text("Players ").color(NamedTextColor.GREEN)) + .append(Component.text("(").color(NamedTextColor.DARK_GRAY)) + .append(Component.text(list.size()).color(NamedTextColor.GRAY)) + .append(Component.text(")").color(NamedTextColor.DARK_GRAY)) + .append(Component.newline()) + .append( + Component.join(JoinConfiguration.newlines(), playersComponent) + ); + + context.sendOutput(component); + + return Component.text("success"); + } +} diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/CommandHandlerPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/CommandHandlerPlugin.java index 6c9f2b8..dfae2f6 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/CommandHandlerPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/CommandHandlerPlugin.java @@ -36,6 +36,7 @@ public class CommandHandlerPlugin { registerCommand(new WikipediaCommand()); registerCommand(new UrbanCommand()); registerCommand(new ClearChatCommand()); + registerCommand(new ListCommand()); } public void registerCommand (Command command) { diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/PlayersPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/PlayersPlugin.java index 29cd7a3..d45e1c6 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/PlayersPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/PlayersPlugin.java @@ -6,6 +6,7 @@ import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundPl import com.github.steveice10.packetlib.Session; import com.github.steveice10.packetlib.event.session.SessionAdapter; import com.github.steveice10.packetlib.packet.Packet; +import lombok.Getter; import me.chayapak1.chomens_bot.Bot; import me.chayapak1.chomens_bot.chatParsers.data.MutablePlayerListEntry; import net.kyori.adventure.text.Component; @@ -16,7 +17,7 @@ import java.util.UUID; public class PlayersPlugin extends SessionAdapter { private final Bot bot; - public List list = new ArrayList<>(); + @Getter private List list = new ArrayList<>(); public PlayersPlugin (Bot bot) { this.bot = bot;