From b340175aab3234db899f89215a709669bbfbe426 Mon Sep 17 00:00:00 2001 From: ChomeNS Date: Mon, 20 Mar 2023 14:10:34 +0700 Subject: [PATCH] add chomens chat parser + merge to 1 line --- .../me/chayapak1/chomensbot_mabe/Main.java | 3 +- .../chatParsers/ChomeNSCustomChatParser.java | 40 +++++++++++++++++++ .../chomensbot_mabe/plugins/ChatPlugin.java | 2 + 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 src/main/java/me/chayapak1/chomensbot_mabe/chatParsers/ChomeNSCustomChatParser.java diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/Main.java b/src/main/java/me/chayapak1/chomensbot_mabe/Main.java index 59c2fa5..10a8bff 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/Main.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/Main.java @@ -41,8 +41,7 @@ public class Main { config = yaml.load(reader); - final Object reconnectDelayObject = config.get("reconnectDelay"); - final int reconnectDelay = (int) reconnectDelayObject; + final int reconnectDelay = (int) ((Object) config.get("reconnectDelay")); List> botsOptions = config.get("bots"); diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/chatParsers/ChomeNSCustomChatParser.java b/src/main/java/me/chayapak1/chomensbot_mabe/chatParsers/ChomeNSCustomChatParser.java new file mode 100644 index 0000000..cad580c --- /dev/null +++ b/src/main/java/me/chayapak1/chomensbot_mabe/chatParsers/ChomeNSCustomChatParser.java @@ -0,0 +1,40 @@ +package me.chayapak1.chomensbot_mabe.chatParsers; + +import me.chayapak1.chomensbot_mabe.chatParsers.data.ChatParser; +import me.chayapak1.chomensbot_mabe.chatParsers.data.PlayerMessage; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TranslatableComponent; + +import java.util.HashMap; +import java.util.List; +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 { + + public ChomeNSCustomChatParser () { + } + + @Override + public PlayerMessage parse (Component message) { + if (message instanceof TranslatableComponent) return parse((TranslatableComponent) message); + return null; + } + + // very similar to MinecraftChatParser + public PlayerMessage parse (TranslatableComponent message) { + final List args = message.args(); + if (args.size() < 3 || !message.key().equals("[%s] %s › %s")) return null; + + final Map parameters = new HashMap<>(); + + final Component username = args.get(1); + final Component contents = args.get(2); + + parameters.put("sender", username); + parameters.put("contents", contents); + + return new PlayerMessage(parameters); + } +} diff --git a/src/main/java/me/chayapak1/chomensbot_mabe/plugins/ChatPlugin.java b/src/main/java/me/chayapak1/chomensbot_mabe/plugins/ChatPlugin.java index 736d364..495b32b 100644 --- a/src/main/java/me/chayapak1/chomensbot_mabe/plugins/ChatPlugin.java +++ b/src/main/java/me/chayapak1/chomensbot_mabe/plugins/ChatPlugin.java @@ -7,6 +7,7 @@ import com.github.steveice10.packetlib.Session; import com.github.steveice10.packetlib.event.session.SessionAdapter; import com.github.steveice10.packetlib.packet.Packet; import me.chayapak1.chomensbot_mabe.Bot; +import me.chayapak1.chomensbot_mabe.chatParsers.ChomeNSCustomChatParser; import me.chayapak1.chomensbot_mabe.chatParsers.KaboomChatParser; import me.chayapak1.chomensbot_mabe.chatParsers.MinecraftChatParser; import me.chayapak1.chomensbot_mabe.chatParsers.data.ChatParser; @@ -36,6 +37,7 @@ public class ChatPlugin extends SessionAdapter { chatParsers = new ArrayList<>(); chatParsers.add(new MinecraftChatParser()); chatParsers.add(new KaboomChatParser()); + chatParsers.add(new ChomeNSCustomChatParser()); } @Override