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