From b118d502494911ff4d7cf960081f1faf3d52a9d8 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Sat, 2 Nov 2024 17:12:23 +0700 Subject: [PATCH] feat: `getLatestChatMessage` eval function --- .../GetLatestChatMessageFunction.java | 33 +++++++++++++++++++ .../chomens_bot/plugins/EvalPlugin.java | 1 + 2 files changed, 34 insertions(+) create mode 100644 src/main/java/me/chayapak1/chomens_bot/evalFunctions/GetLatestChatMessageFunction.java diff --git a/src/main/java/me/chayapak1/chomens_bot/evalFunctions/GetLatestChatMessageFunction.java b/src/main/java/me/chayapak1/chomens_bot/evalFunctions/GetLatestChatMessageFunction.java new file mode 100644 index 0000000..37feeca --- /dev/null +++ b/src/main/java/me/chayapak1/chomens_bot/evalFunctions/GetLatestChatMessageFunction.java @@ -0,0 +1,33 @@ +package me.chayapak1.chomens_bot.evalFunctions; + +import me.chayapak1.chomens_bot.Bot; +import me.chayapak1.chomens_bot.data.eval.EvalFunction; +import me.chayapak1.chomens_bot.plugins.ChatPlugin; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; + +public class GetLatestChatMessageFunction extends EvalFunction { + private String latestMessage = ""; + + public GetLatestChatMessageFunction (Bot bot) { + super("getLatestChatMessage", bot); + + bot.chat.addListener(new ChatPlugin.Listener() { + @Override + public boolean systemMessageReceived(Component component, String string, String ansi) { + messageReceived(component); + + return true; + } + }); + } + + private void messageReceived (Component component) { + latestMessage = GsonComponentSerializer.gson().serialize(component); + } + + @Override + public Output execute(Object... args) { + return new Output(latestMessage, true); + } +} diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/EvalPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/EvalPlugin.java index b011ca5..6e5ab51 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/EvalPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/EvalPlugin.java @@ -36,6 +36,7 @@ public class EvalPlugin { functions.add(new ChatFunction(bot)); functions.add(new GetPlayerListFunction(bot)); functions.add(new GetBotUsernameFunction(bot)); + functions.add(new GetLatestChatMessageFunction(bot)); try { socket = IO.socket(bot.config.eval.address);