diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/evalFunctions/ChatFunction.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/evalFunctions/ChatFunction.java new file mode 100644 index 0000000..1d96ddd --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/evalFunctions/ChatFunction.java @@ -0,0 +1,16 @@ +package land.chipmunk.chayapak.chomens_bot.evalFunctions; + +import land.chipmunk.chayapak.chomens_bot.Bot; + +public class ChatFunction extends EvalFunction { + public ChatFunction (Bot bot) { + super("chat", bot); + } + + @Override + public void execute(Object... args) { + final String message = (String) args[0]; + + bot.chat.send(message); + } +} diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/evalFunctions/CoreFunction.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/evalFunctions/CoreFunction.java new file mode 100644 index 0000000..72ec82e --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/evalFunctions/CoreFunction.java @@ -0,0 +1,16 @@ +package land.chipmunk.chayapak.chomens_bot.evalFunctions; + +import land.chipmunk.chayapak.chomens_bot.Bot; + +public class CoreFunction extends EvalFunction { + public CoreFunction (Bot bot) { + super("core", bot); + } + + @Override + public void execute(Object... args) { + final String command = (String) args[0]; + + bot.core.run(command); + } +} diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/evalFunctions/EvalFunction.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/evalFunctions/EvalFunction.java new file mode 100644 index 0000000..cdfdfec --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/evalFunctions/EvalFunction.java @@ -0,0 +1,20 @@ +package land.chipmunk.chayapak.chomens_bot.evalFunctions; + +import land.chipmunk.chayapak.chomens_bot.Bot; +import land.chipmunk.chayapak.chomens_bot.data.EvalOutput; + +public class EvalFunction { + public final String name; + + protected final Bot bot; + + public EvalFunction ( + String name, + Bot bot + ) { + this.name = name; + this.bot = bot; + } + + public void execute (Object ...args) {} +} diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/EvalPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/EvalPlugin.java index a8c173e..2d34617 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/EvalPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/EvalPlugin.java @@ -4,9 +4,11 @@ import io.socket.client.IO; import io.socket.client.Socket; import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.data.EvalOutput; +import land.chipmunk.chayapak.chomens_bot.evalFunctions.ChatFunction; +import land.chipmunk.chayapak.chomens_bot.evalFunctions.CoreFunction; +import land.chipmunk.chayapak.chomens_bot.evalFunctions.EvalFunction; -import java.util.HashMap; -import java.util.Map; +import java.util.*; import java.util.concurrent.CompletableFuture; public class EvalPlugin { @@ -20,17 +22,28 @@ public class EvalPlugin { private final Map> futures = new HashMap<>(); + public final List functions = new ArrayList<>(); + public EvalPlugin (Bot bot) { + functions.add(new CoreFunction(bot)); + functions.add(new ChatFunction(bot)); + try { socket = IO.socket(bot.config.eval.address); } catch (Exception e) { e.printStackTrace(); } - socket.on(Socket.EVENT_CONNECT, (args) -> connected = true); + socket.on(Socket.EVENT_CONNECT, (args) -> { + connected = true; + + socket.emit("setFunctions", "chat", "core"); + }); socket.on(Socket.EVENT_DISCONNECT, (args) -> connected = false); socket.on(Socket.EVENT_CONNECT_ERROR, (args) -> connected = false); + for (EvalFunction function : functions) socket.on(BRIDGE_PREFIX + function.name, function::execute); + socket.on("codeOutput", (args) -> { final int id = (int) args[0]; final boolean isError = (boolean) args[1]; @@ -41,18 +54,6 @@ public class EvalPlugin { future.complete(new EvalOutput(isError, output)); }); - socket.on(BRIDGE_PREFIX + "chat", (args) -> { - final String message = (String) args[0]; - - bot.chat.send(message); - }); - - socket.on(BRIDGE_PREFIX + "core", (args) -> { - final String command = (String) args[0]; - - bot.core.run(command); - }); - socket.connect(); }