From 94b7fe8ec8a1988b4cba024f0d52b12d4e0ab407 Mon Sep 17 00:00:00 2001 From: ChomeNS Date: Sat, 25 Mar 2023 13:33:56 +0700 Subject: [PATCH] =?UTF-8?q?add=20multiple=20prefixes=20and=20cspy=20prefix?= =?UTF-8?q?es=20totallynotskidded=E2=84=A2=20some=20codes=20from=20Chipmun?= =?UTF-8?q?kBot=E2=84=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/me/chayapak1/chomens_bot/Bot.java | 4 + .../chayapak1/chomens_bot/Configuration.java | 8 ++ .../chatParsers/ChomeNSCustomChatParser.java | 14 +- .../chatParsers/KaboomChatParser.java | 15 ++- .../chatParsers/MinecraftChatParser.java | 21 ++- .../commandSpy/CommandSpyParser.java | 55 ++++++++ .../data/MutablePlayerListEntry.java | 26 ++++ .../chatParsers/data/PlayerMessage.java | 1 + .../chomens_bot/command/CommandContext.java | 5 +- .../command/ConsoleCommandContext.java | 4 +- .../command/PlayerCommandContext.java | 9 +- .../chomens_bot/commands/HelpCommand.java | 2 +- .../chomens_bot/commands/MusicCommand.java | 2 +- .../plugins/ChatCommandHandlerPlugin.java | 32 ++++- .../chomens_bot/plugins/ChatPlugin.java | 17 ++- .../chomens_bot/plugins/ConsolePlugin.java | 9 +- .../chomens_bot/plugins/PlayersPlugin.java | 122 ++++++++++++++++++ .../plugins/TabCompletePlugin.java | 42 ++++++ src/main/resources/default-config.yml | 14 +- 19 files changed, 376 insertions(+), 26 deletions(-) create mode 100644 src/main/java/me/chayapak1/chomens_bot/chatParsers/commandSpy/CommandSpyParser.java create mode 100644 src/main/java/me/chayapak1/chomens_bot/chatParsers/data/MutablePlayerListEntry.java create mode 100644 src/main/java/me/chayapak1/chomens_bot/plugins/PlayersPlugin.java create mode 100644 src/main/java/me/chayapak1/chomens_bot/plugins/TabCompletePlugin.java diff --git a/src/main/java/me/chayapak1/chomens_bot/Bot.java b/src/main/java/me/chayapak1/chomens_bot/Bot.java index 618875f..75e481f 100644 --- a/src/main/java/me/chayapak1/chomens_bot/Bot.java +++ b/src/main/java/me/chayapak1/chomens_bot/Bot.java @@ -36,6 +36,8 @@ public class Bot { @Getter private final SelfCarePlugin selfCare; @Getter private final PositionPlugin position; @Getter private final CorePlugin core; + @Getter private final PlayersPlugin players; + @Getter private final TabCompletePlugin tabComplete; @Getter private final CommandHandlerPlugin commandHandler; @Getter private final ChatCommandHandlerPlugin chatCommandHandler; @Getter private final HashingPlugin hashing; @@ -54,6 +56,8 @@ public class Bot { this.selfCare = new SelfCarePlugin(this); this.position = new PositionPlugin(this); this.core = new CorePlugin(this); + this.players = new PlayersPlugin(this); + this.tabComplete = new TabCompletePlugin(this); this.commandHandler = new CommandHandlerPlugin(); this.chatCommandHandler = new ChatCommandHandlerPlugin(this); this.hashing = new HashingPlugin(this); diff --git a/src/main/java/me/chayapak1/chomens_bot/Configuration.java b/src/main/java/me/chayapak1/chomens_bot/Configuration.java index da47da3..7e5b8ff 100644 --- a/src/main/java/me/chayapak1/chomens_bot/Configuration.java +++ b/src/main/java/me/chayapak1/chomens_bot/Configuration.java @@ -2,11 +2,19 @@ package me.chayapak1.chomens_bot; import lombok.Getter; +import java.util.List; import java.util.Map; public class Configuration { + @Getter public List prefixes; + @Getter public List commandSpyPrefixes; + + @Getter public Map consolePrefixes; + @Getter public int reconnectDelay = 7000; + @Getter public Map keys; + @Getter public Core core = new Core(); @Getter public SelfCare selfCare = new SelfCare(); @Getter public Bots[] bots = new Bots[]{}; diff --git a/src/main/java/me/chayapak1/chomens_bot/chatParsers/ChomeNSCustomChatParser.java b/src/main/java/me/chayapak1/chomens_bot/chatParsers/ChomeNSCustomChatParser.java index e48143c..c6b12a9 100644 --- a/src/main/java/me/chayapak1/chomens_bot/chatParsers/ChomeNSCustomChatParser.java +++ b/src/main/java/me/chayapak1/chomens_bot/chatParsers/ChomeNSCustomChatParser.java @@ -1,7 +1,10 @@ package me.chayapak1.chomens_bot.chatParsers; +import me.chayapak1.chomens_bot.Bot; import me.chayapak1.chomens_bot.chatParsers.data.ChatParser; +import me.chayapak1.chomens_bot.chatParsers.data.MutablePlayerListEntry; import me.chayapak1.chomens_bot.chatParsers.data.PlayerMessage; +import me.chayapak1.chomens_bot.util.ComponentUtilities; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TranslatableComponent; @@ -12,8 +15,10 @@ import java.util.Map; // Might be a confusing name, but I mean the [Chat] chayapak custom chat thing or any other // custom chat that uses the `[%s] %s › %s` translation public class ChomeNSCustomChatParser implements ChatParser { + private final Bot bot; - public ChomeNSCustomChatParser () { + public ChomeNSCustomChatParser (Bot bot) { + this.bot = bot; } @Override @@ -32,9 +37,14 @@ public class ChomeNSCustomChatParser implements ChatParser { final Component username = args.get(1); final Component contents = args.get(2); + final String stringUsername = ComponentUtilities.stringify(username); + MutablePlayerListEntry sender = bot.players().getEntry(stringUsername); + + if (sender == null) return null; + parameters.put("sender", username); parameters.put("contents", contents); - return new PlayerMessage(parameters); + return new PlayerMessage(parameters, sender); } } diff --git a/src/main/java/me/chayapak1/chomens_bot/chatParsers/KaboomChatParser.java b/src/main/java/me/chayapak1/chomens_bot/chatParsers/KaboomChatParser.java index 0ed2864..5223255 100644 --- a/src/main/java/me/chayapak1/chomens_bot/chatParsers/KaboomChatParser.java +++ b/src/main/java/me/chayapak1/chomens_bot/chatParsers/KaboomChatParser.java @@ -1,6 +1,10 @@ package me.chayapak1.chomens_bot.chatParsers; +import com.github.steveice10.mc.auth.data.GameProfile; +import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode; +import me.chayapak1.chomens_bot.Bot; import me.chayapak1.chomens_bot.chatParsers.data.ChatParser; +import me.chayapak1.chomens_bot.chatParsers.data.MutablePlayerListEntry; import me.chayapak1.chomens_bot.chatParsers.data.PlayerMessage; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; @@ -10,10 +14,13 @@ import net.kyori.adventure.text.format.Style; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.UUID; public class KaboomChatParser implements ChatParser { + private final Bot bot; - public KaboomChatParser () { + public KaboomChatParser (Bot bot) { + this.bot = bot; } private static final Style empty = Style.empty(); @@ -55,11 +62,15 @@ public class KaboomChatParser implements ChatParser { return null; } + MutablePlayerListEntry sender = bot.players().getEntry(Component.empty().append(prefix).append(displayName)); + if (sender == null) sender = bot.players().getEntry(prefix.append(displayName)); // old + if (sender == null) sender = new MutablePlayerListEntry(new GameProfile(new UUID(0L, 0L), null), GameMode.SURVIVAL, 0, displayName, 0L, null, new byte[0]); // new and currently using + parameters.put("sender", displayName); parameters.put("prefix", prefix); parameters.put("contents", contents); - return new PlayerMessage(parameters); + return new PlayerMessage(parameters, sender); } private boolean isSeperatorAt (List children, int start) { diff --git a/src/main/java/me/chayapak1/chomens_bot/chatParsers/MinecraftChatParser.java b/src/main/java/me/chayapak1/chomens_bot/chatParsers/MinecraftChatParser.java index 75fcbca..7abe700 100644 --- a/src/main/java/me/chayapak1/chomens_bot/chatParsers/MinecraftChatParser.java +++ b/src/main/java/me/chayapak1/chomens_bot/chatParsers/MinecraftChatParser.java @@ -1,6 +1,10 @@ package me.chayapak1.chomens_bot.chatParsers; +import com.github.steveice10.mc.auth.data.GameProfile; +import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode; +import me.chayapak1.chomens_bot.Bot; import me.chayapak1.chomens_bot.chatParsers.data.ChatParser; +import me.chayapak1.chomens_bot.chatParsers.data.MutablePlayerListEntry; import me.chayapak1.chomens_bot.chatParsers.data.PlayerMessage; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TranslatableComponent; @@ -9,8 +13,11 @@ import net.kyori.adventure.text.event.HoverEvent; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.UUID; public class MinecraftChatParser implements ChatParser { + private final Bot bot; + // ? Is such a mapping necessary? private static final Map typeMap = new HashMap<>(); static { @@ -21,7 +28,8 @@ public class MinecraftChatParser implements ChatParser { typeMap.put("chat.type.emote", "minecraft:emote_command"); } - public MinecraftChatParser () { + public MinecraftChatParser (Bot bot) { + this.bot = bot; } @Override @@ -40,9 +48,18 @@ public class MinecraftChatParser implements ChatParser { final Component senderComponent = args.get(0); final Component contents = args.get(1); + // try to find the sender then make it a player list entry + final HoverEvent hoverEvent = senderComponent.hoverEvent(); + if (hoverEvent == null || !hoverEvent.action().equals(HoverEvent.Action.SHOW_ENTITY)) return null; + HoverEvent.ShowEntity entityInfo = (HoverEvent.ShowEntity) hoverEvent.value(); + final UUID senderUUID = entityInfo.id(); + + MutablePlayerListEntry sender = bot.players().getEntry(senderUUID); + if (sender == null) sender = new MutablePlayerListEntry(new GameProfile(senderUUID, null), GameMode.SURVIVAL, 0, entityInfo.name(), 0L, null, new byte[0]); + parameters.put("sender", senderComponent); parameters.put("contents", contents); - return new PlayerMessage(parameters); + return new PlayerMessage(parameters, sender); } } diff --git a/src/main/java/me/chayapak1/chomens_bot/chatParsers/commandSpy/CommandSpyParser.java b/src/main/java/me/chayapak1/chomens_bot/chatParsers/commandSpy/CommandSpyParser.java new file mode 100644 index 0000000..51b6af1 --- /dev/null +++ b/src/main/java/me/chayapak1/chomens_bot/chatParsers/commandSpy/CommandSpyParser.java @@ -0,0 +1,55 @@ +package me.chayapak1.chomens_bot.chatParsers.commandSpy; + +import me.chayapak1.chomens_bot.Bot; +import me.chayapak1.chomens_bot.chatParsers.data.ChatParser; +import me.chayapak1.chomens_bot.chatParsers.data.MutablePlayerListEntry; +import me.chayapak1.chomens_bot.chatParsers.data.PlayerMessage; +import me.chayapak1.chomens_bot.util.ComponentUtilities; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; +import net.kyori.adventure.text.format.NamedTextColor; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class CommandSpyParser implements ChatParser { + private final Bot bot; + + public CommandSpyParser (Bot bot) { + this.bot = bot; + } + + @Override + public PlayerMessage parse (Component message) { + if (message instanceof TextComponent) return parse((TextComponent) message); + return null; + } + + public PlayerMessage parse (TextComponent message) { + final List children = message.children(); + + if ( + ( + message.color() != NamedTextColor.AQUA || + message.color() != NamedTextColor.YELLOW + ) && + children.size() < 2 + ) return null; + + final Component username = Component.text(message.content()); + final Component command = children.get(1); + + final String stringUsername = ComponentUtilities.stringify(username); + MutablePlayerListEntry sender = bot.players().getEntry(stringUsername); + + if (sender == null) return null; + + final Map parameters = new HashMap<>(); + + parameters.put("sender", username); + parameters.put("contents", command); + + return new PlayerMessage(parameters, sender); + } +} diff --git a/src/main/java/me/chayapak1/chomens_bot/chatParsers/data/MutablePlayerListEntry.java b/src/main/java/me/chayapak1/chomens_bot/chatParsers/data/MutablePlayerListEntry.java new file mode 100644 index 0000000..9bd40e7 --- /dev/null +++ b/src/main/java/me/chayapak1/chomens_bot/chatParsers/data/MutablePlayerListEntry.java @@ -0,0 +1,26 @@ +package me.chayapak1.chomens_bot.chatParsers.data; + +import com.github.steveice10.mc.auth.data.GameProfile; +import com.github.steveice10.mc.protocol.data.game.PlayerListEntry; +import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode; +import lombok.AllArgsConstructor; +import lombok.Data; +import net.kyori.adventure.text.Component; + +import java.security.PublicKey; + +@Data +@AllArgsConstructor +public class MutablePlayerListEntry { + private GameProfile profile; + private GameMode gamemode; + private int latency; + private Component displayName; + private long expiresAt; + private PublicKey publicKey; + private byte[] keySignature; + + public MutablePlayerListEntry (PlayerListEntry entry) { + this(entry.getProfile(), entry.getGameMode(), entry.getPing(), entry.getDisplayName(), entry.getExpiresAt(), entry.getPublicKey(), entry.getKeySignature()); + } +} diff --git a/src/main/java/me/chayapak1/chomens_bot/chatParsers/data/PlayerMessage.java b/src/main/java/me/chayapak1/chomens_bot/chatParsers/data/PlayerMessage.java index 9944f9d..bea2049 100644 --- a/src/main/java/me/chayapak1/chomens_bot/chatParsers/data/PlayerMessage.java +++ b/src/main/java/me/chayapak1/chomens_bot/chatParsers/data/PlayerMessage.java @@ -10,4 +10,5 @@ import java.util.Map; @AllArgsConstructor public class PlayerMessage { private Map parameters; + private MutablePlayerListEntry sender; } diff --git a/src/main/java/me/chayapak1/chomens_bot/command/CommandContext.java b/src/main/java/me/chayapak1/chomens_bot/command/CommandContext.java index 7eca62b..251c48b 100644 --- a/src/main/java/me/chayapak1/chomens_bot/command/CommandContext.java +++ b/src/main/java/me/chayapak1/chomens_bot/command/CommandContext.java @@ -7,11 +7,14 @@ import net.kyori.adventure.text.Component; public class CommandContext { @Getter public final Bot bot; + @Getter public final String prefix; + @Getter private final String hash; @Getter private final String ownerHash; - public CommandContext(Bot bot, String hash, String ownerHash) { + public CommandContext(Bot bot, String prefix, String hash, String ownerHash) { this.bot = bot; + this.prefix = prefix; this.hash = hash; this.ownerHash = ownerHash; } diff --git a/src/main/java/me/chayapak1/chomens_bot/command/ConsoleCommandContext.java b/src/main/java/me/chayapak1/chomens_bot/command/ConsoleCommandContext.java index bce541f..d39a5c5 100644 --- a/src/main/java/me/chayapak1/chomens_bot/command/ConsoleCommandContext.java +++ b/src/main/java/me/chayapak1/chomens_bot/command/ConsoleCommandContext.java @@ -8,8 +8,8 @@ import net.kyori.adventure.text.format.NamedTextColor; public class ConsoleCommandContext extends CommandContext { private final Bot bot; - public ConsoleCommandContext (Bot bot, String hash, String ownerHash) { - super(bot, hash, ownerHash); + public ConsoleCommandContext (Bot bot, String prefix, String hash, String ownerHash) { + super(bot, prefix, hash, ownerHash); this.bot = bot; } diff --git a/src/main/java/me/chayapak1/chomens_bot/command/PlayerCommandContext.java b/src/main/java/me/chayapak1/chomens_bot/command/PlayerCommandContext.java index 749142d..6b3d2f6 100644 --- a/src/main/java/me/chayapak1/chomens_bot/command/PlayerCommandContext.java +++ b/src/main/java/me/chayapak1/chomens_bot/command/PlayerCommandContext.java @@ -7,17 +7,20 @@ import net.kyori.adventure.text.Component; public class PlayerCommandContext extends CommandContext { @Getter private final String playerName; + @Getter private final String selector; + private final Bot bot; - public PlayerCommandContext (Bot bot, String playerName, String hash, String ownerHash) { - super(bot, hash, ownerHash); + public PlayerCommandContext (Bot bot, String playerName, String prefix, String selector, String hash, String ownerHash) { + super(bot, prefix, hash, ownerHash); this.bot = bot; this.playerName = playerName; + this.selector = selector; } @Override public void sendOutput (Component message) { - bot.chat().tellraw(message); + bot.chat().tellraw(message, selector); } @Override diff --git a/src/main/java/me/chayapak1/chomens_bot/commands/HelpCommand.java b/src/main/java/me/chayapak1/chomens_bot/commands/HelpCommand.java index 103482a..79329f2 100644 --- a/src/main/java/me/chayapak1/chomens_bot/commands/HelpCommand.java +++ b/src/main/java/me/chayapak1/chomens_bot/commands/HelpCommand.java @@ -96,7 +96,7 @@ public class HelpCommand implements Command { public Component sendUsages (CommandContext context, String[] args) { final Bot bot = context.bot(); - final String prefix = bot.chatCommandHandler().prefix(); + final String prefix = context.prefix(); for (Command command : bot.commandHandler().commands()) { if (!command.name().equals(args[0]) && !command.alias().contains(args[0])) continue; diff --git a/src/main/java/me/chayapak1/chomens_bot/commands/MusicCommand.java b/src/main/java/me/chayapak1/chomens_bot/commands/MusicCommand.java index 0600385..069f722 100644 --- a/src/main/java/me/chayapak1/chomens_bot/commands/MusicCommand.java +++ b/src/main/java/me/chayapak1/chomens_bot/commands/MusicCommand.java @@ -148,7 +148,7 @@ public class MusicCommand implements Command { } public Component list (CommandContext context, String[] args) { - final String prefix = context.bot().chatCommandHandler().prefix(); + final String prefix = context.prefix(); final Path _path = Path.of(root.toString(), String.join(" ", args)); final Path path = (args.length < 2) ? root : _path; diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/ChatCommandHandlerPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/ChatCommandHandlerPlugin.java index 0521388..4423fea 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/ChatCommandHandlerPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/ChatCommandHandlerPlugin.java @@ -5,22 +5,34 @@ import me.chayapak1.chomens_bot.Bot; import me.chayapak1.chomens_bot.chatParsers.data.PlayerMessage; import me.chayapak1.chomens_bot.command.PlayerCommandContext; import me.chayapak1.chomens_bot.util.ComponentUtilities; +import me.chayapak1.chomens_bot.util.UUIDUtilities; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; -public class ChatCommandHandlerPlugin extends ChatPlugin.ChatListener { - @Getter private final String prefix = "j*"; +import java.util.List; +public class ChatCommandHandlerPlugin extends ChatPlugin.ChatListener { public final Bot bot; + @Getter private final List prefixes; + @Getter private final List commandSpyPrefixes; + public ChatCommandHandlerPlugin(Bot bot) { this.bot = bot; + this.prefixes = bot.config().prefixes(); + this.commandSpyPrefixes = bot.config().commandSpyPrefixes(); + bot.chat().addListener(this); } @Override - public void playerMessageReceived (PlayerMessage message) { + public void playerMessageReceived (PlayerMessage message) { listener(message, false); } + + @Override + public void commandSpyMessageReceived (PlayerMessage message) { listener(message, true); } + + private void listener (PlayerMessage message, boolean cspy) { final Component displayNameComponent = message.parameters().get("sender"); final Component messageComponent = message.parameters().get("contents"); if (displayNameComponent == null || messageComponent == null) return; @@ -28,10 +40,20 @@ public class ChatCommandHandlerPlugin extends ChatPlugin.ChatListener { final String displayName = ComponentUtilities.stringify(displayNameComponent); final String contents = ComponentUtilities.stringify(messageComponent); - if (!contents.startsWith(prefix)) return; + String prefix = null; + + for (String eachPrefix : (cspy ? commandSpyPrefixes : prefixes)) { + if (!contents.startsWith(eachPrefix)) continue; + prefix = eachPrefix; + } + + if (prefix == null) return; + final String commandString = contents.substring(prefix.length()); - final PlayerCommandContext context = new PlayerCommandContext(bot, displayName, bot.hashing().hash(), bot.hashing().ownerHash()); + final String selector = cspy ? UUIDUtilities.selector(message.sender().profile().getId()) : "@a"; + + final PlayerCommandContext context = new PlayerCommandContext(bot, displayName, prefix, selector, bot.hashing().hash(), bot.hashing().ownerHash()); final Component output = bot.commandHandler().executeCommand(commandString, context, bot.hashing().hash(), bot.hashing().ownerHash()); final String textOutput = ((TextComponent) output).content(); diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/ChatPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/ChatPlugin.java index ec64899..f191c2c 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/ChatPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/ChatPlugin.java @@ -10,6 +10,7 @@ import me.chayapak1.chomens_bot.Bot; import me.chayapak1.chomens_bot.chatParsers.ChomeNSCustomChatParser; import me.chayapak1.chomens_bot.chatParsers.KaboomChatParser; import me.chayapak1.chomens_bot.chatParsers.MinecraftChatParser; +import me.chayapak1.chomens_bot.chatParsers.commandSpy.CommandSpyParser; import me.chayapak1.chomens_bot.chatParsers.data.ChatParser; import me.chayapak1.chomens_bot.chatParsers.data.PlayerMessage; import me.chayapak1.chomens_bot.util.ComponentUtilities; @@ -28,16 +29,21 @@ public class ChatPlugin extends SessionAdapter { private final List chatParsers; + private final CommandSpyParser commandSpyParser; + private final List listeners = new ArrayList<>(); public ChatPlugin (Bot bot) { this.bot = bot; + + this.commandSpyParser = new CommandSpyParser(bot); + bot.addListener(this); chatParsers = new ArrayList<>(); - chatParsers.add(new MinecraftChatParser()); - chatParsers.add(new KaboomChatParser()); - chatParsers.add(new ChomeNSCustomChatParser()); + chatParsers.add(new MinecraftChatParser(bot)); + chatParsers.add(new KaboomChatParser(bot)); + chatParsers.add(new ChomeNSCustomChatParser(bot)); } @Override @@ -66,10 +72,14 @@ public class ChatPlugin extends SessionAdapter { if (playerMessage != null) break; } + PlayerMessage commandSpyMessage; + commandSpyMessage = commandSpyParser.parse(component); + final String message = ComponentUtilities.stringify(component); for (ChatListener listener : listeners) { listener.systemMessageReceived(message, component); if (playerMessage != null) listener.playerMessageReceived(playerMessage); + if (commandSpyMessage != null) listener.commandSpyMessageReceived(commandSpyMessage); } } @@ -109,6 +119,7 @@ public class ChatPlugin extends SessionAdapter { public static class ChatListener { public void playerMessageReceived (PlayerMessage message) {} + public void commandSpyMessageReceived (PlayerMessage message) {} public void systemMessageReceived (String message, Component component) {} } } diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/ConsolePlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/ConsolePlugin.java index 4c3b8e3..287ce1e 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/ConsolePlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/ConsolePlugin.java @@ -20,14 +20,17 @@ public class ConsolePlugin { @Getter private String consoleServer = "all"; - @Getter private final String prefix = "."; - @Getter private final String consoleServerPrefix = "/"; + @Getter private String prefix; + @Getter private String consoleServerPrefix; public ConsolePlugin (List allBots) { this.allBots = allBots; this.reader = LineReaderBuilder.builder().build(); for (Bot bot : allBots) { + prefix = bot.config().consolePrefixes().get("normalCommandsPrefix"); + consoleServerPrefix = bot.config().consolePrefixes().get("consoleServerPrefix"); + bot.console(this); bot.logger(new LoggerPlugin(bot)); } @@ -79,7 +82,7 @@ public class ConsolePlugin { if (!bot.host().equals(consoleServer) && !consoleServer.equals("all")) continue; if (line.startsWith(prefix)) { - final ConsoleCommandContext context = new ConsoleCommandContext(bot, "h", "o"); // ? should the hashes be hardcoded? + final ConsoleCommandContext context = new ConsoleCommandContext(bot, prefix, "h", "o"); // ? should the hashes be hardcoded? final Component output = bot.commandHandler().executeCommand(line.substring(prefix.length()), context, "h", "o"); final String textOutput = ((TextComponent) output).content(); diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/PlayersPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/PlayersPlugin.java new file mode 100644 index 0000000..29cd7a3 --- /dev/null +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/PlayersPlugin.java @@ -0,0 +1,122 @@ +package me.chayapak1.chomens_bot.plugins; + +import com.github.steveice10.mc.protocol.data.game.PlayerListEntry; +import com.github.steveice10.mc.protocol.data.game.PlayerListEntryAction; +import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundPlayerInfoPacket; +import com.github.steveice10.packetlib.Session; +import com.github.steveice10.packetlib.event.session.SessionAdapter; +import com.github.steveice10.packetlib.packet.Packet; +import me.chayapak1.chomens_bot.Bot; +import me.chayapak1.chomens_bot.chatParsers.data.MutablePlayerListEntry; +import net.kyori.adventure.text.Component; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class PlayersPlugin extends SessionAdapter { + private final Bot bot; + public List list = new ArrayList<>(); + + public PlayersPlugin (Bot bot) { + this.bot = bot; + bot.addListener(this); + } + + @Override + public void packetReceived (Session session, Packet packet) { + if (packet instanceof ClientboundPlayerInfoPacket) packetReceived((ClientboundPlayerInfoPacket) packet); + } + + public void packetReceived (ClientboundPlayerInfoPacket packet) { + PlayerListEntryAction action = packet.getAction(); + for (PlayerListEntry entry : packet.getEntries()) { + if (action == PlayerListEntryAction.ADD_PLAYER) addPlayer(entry); + else if (action == PlayerListEntryAction.UPDATE_GAMEMODE) updateGamemode(entry); + else if (action == PlayerListEntryAction.UPDATE_LATENCY) updateLatency(entry); + else if (action == PlayerListEntryAction.UPDATE_DISPLAY_NAME) updateDisplayName(entry); + else if (action == PlayerListEntryAction.REMOVE_PLAYER) removePlayer(entry); + } + } + + public final MutablePlayerListEntry getEntry (UUID uuid) { + for (MutablePlayerListEntry candidate : list) { + if (candidate.profile().getId().equals(uuid)) { + return candidate; + } + } + + return null; + } + + public final MutablePlayerListEntry getEntry (String username) { + for (MutablePlayerListEntry candidate : list) { + if (candidate.profile().getName().equals(username)) { + return candidate; + } + } + + return null; + } + + public final MutablePlayerListEntry getEntry (Component displayName) { + for (MutablePlayerListEntry candidate : list) { + if (candidate.displayName() != null && candidate.displayName().equals(displayName)) { + return candidate; + } + } + + return null; + } + + private MutablePlayerListEntry getEntry (PlayerListEntry other) { + return getEntry(other.getProfile().getId()); + } + + private void addPlayer (PlayerListEntry newEntry) { + final MutablePlayerListEntry duplicate = getEntry(newEntry); + if (duplicate != null) list.remove(duplicate); + + list.add(new MutablePlayerListEntry(newEntry)); + } + + private void updateGamemode (PlayerListEntry newEntry) { + final MutablePlayerListEntry target = getEntry(newEntry); + if (target == null) return; + + target.gamemode(newEntry.getGameMode()); + } + + private void updateLatency (PlayerListEntry newEntry) { + final MutablePlayerListEntry target = getEntry(newEntry); + if (target == null) return; + + target.latency(newEntry.getPing()); + } + + private void updateDisplayName (PlayerListEntry newEntry) { + final MutablePlayerListEntry target = getEntry(newEntry); + if (target == null) return; + + target.displayName(newEntry.getDisplayName()); + } + + private void removePlayer (PlayerListEntry newEntry) { + final MutablePlayerListEntry target = getEntry(newEntry); + if (target == null) return; + + bot.tabComplete().tabComplete("/minecraft:scoreboard players add ").thenApply(packet -> { + final String[] matches = packet.getMatches(); + final Component[] tooltips = packet.getTooltips(); + final String username = target.profile().getName(); + + for (int i = 0; i < matches.length; i++) { + if (tooltips[i] != null || !matches[i].equals(username)) continue; + return packet; + } + + list.remove(target); + return packet; + }); + } +} diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/TabCompletePlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/TabCompletePlugin.java new file mode 100644 index 0000000..18e6194 --- /dev/null +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/TabCompletePlugin.java @@ -0,0 +1,42 @@ +package me.chayapak1.chomens_bot.plugins; + +import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundCommandSuggestionsPacket; +import com.github.steveice10.mc.protocol.packet.ingame.serverbound.ServerboundCommandSuggestionPacket; +import com.github.steveice10.packetlib.Session; +import com.github.steveice10.packetlib.event.session.SessionAdapter; +import com.github.steveice10.packetlib.packet.Packet; +import me.chayapak1.chomens_bot.Bot; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.CompletableFuture; + +public class TabCompletePlugin extends SessionAdapter { + private final Bot bot; + private int nextTransactionId = 0; + private final Map> transactions = new HashMap<>(); + + public TabCompletePlugin (Bot bot) { + this.bot = bot; + bot.addListener(this); + } + + public CompletableFuture tabComplete (String command) { + final int transactionId = nextTransactionId++; + + bot.session().send(new ServerboundCommandSuggestionPacket(transactionId, command)); + + final CompletableFuture future = new CompletableFuture<>(); + transactions.put(transactionId, future); + return future; + } + + @Override + public void packetReceived (Session session, Packet packet) { + if (packet instanceof ClientboundCommandSuggestionsPacket) packetReceived((ClientboundCommandSuggestionsPacket) packet); + } + + public void packetReceived (ClientboundCommandSuggestionsPacket packet) { + transactions.get(packet.getTransactionId()).complete(packet); + } +} diff --git a/src/main/resources/default-config.yml b/src/main/resources/default-config.yml index b566d19..4679afd 100644 --- a/src/main/resources/default-config.yml +++ b/src/main/resources/default-config.yml @@ -1,4 +1,16 @@ +prefixes: + - 'default*' + - 'defaultcbot ' + +commandSpyPrefixes: + - '/defaultcbot ' + +consolePrefixes: + normalCommandsPrefix: '.' + consoleServerPrefix: '/' + reconnectDelay: 7000 + keys: normalKey: 'normal hash key here' ownerKey: 'OwnerHash™ key here' @@ -31,4 +43,4 @@ bots: - host: 'localhost' port: 25565 username: 'ChomeNS_Bot' - kaboom: false \ No newline at end of file + kaboom: false