diff --git a/src/main/java/me/chayapak1/chomens_bot/data/eval/EvalFunction.java b/src/main/java/me/chayapak1/chomens_bot/data/eval/EvalFunction.java index 6ebadab..dca3b5d 100644 --- a/src/main/java/me/chayapak1/chomens_bot/data/eval/EvalFunction.java +++ b/src/main/java/me/chayapak1/chomens_bot/data/eval/EvalFunction.java @@ -15,7 +15,7 @@ public class EvalFunction { this.bot = bot; } - public Output execute (Object ...args) { return null; } + public Output execute (Object ...args) throws Exception { return null; } public static class Output { public final String message; diff --git a/src/main/java/me/chayapak1/chomens_bot/evalFunctions/CoreFunction.java b/src/main/java/me/chayapak1/chomens_bot/evalFunctions/CoreFunction.java index e0733ec..cfb71bb 100644 --- a/src/main/java/me/chayapak1/chomens_bot/evalFunctions/CoreFunction.java +++ b/src/main/java/me/chayapak1/chomens_bot/evalFunctions/CoreFunction.java @@ -2,6 +2,11 @@ package me.chayapak1.chomens_bot.evalFunctions; import me.chayapak1.chomens_bot.Bot; import me.chayapak1.chomens_bot.data.eval.EvalFunction; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; public class CoreFunction extends EvalFunction { public CoreFunction (Bot bot) { @@ -9,13 +14,13 @@ public class CoreFunction extends EvalFunction { } @Override - public Output execute(Object... args) { + public Output execute(Object... args) throws Exception { if (args.length == 0) return null; final String command = (String) args[0]; - bot.core.run(command); + final CompletableFuture future = bot.core.runTracked(command); - return null; + return new Output(GsonComponentSerializer.gson().serialize(future.get(1, TimeUnit.SECONDS)), true); } } diff --git a/src/main/java/me/chayapak1/chomens_bot/evalFunctions/GetBotInfoFunction.java b/src/main/java/me/chayapak1/chomens_bot/evalFunctions/GetBotInfoFunction.java new file mode 100644 index 0000000..a187188 --- /dev/null +++ b/src/main/java/me/chayapak1/chomens_bot/evalFunctions/GetBotInfoFunction.java @@ -0,0 +1,23 @@ +package me.chayapak1.chomens_bot.evalFunctions; + +import com.google.gson.JsonObject; +import me.chayapak1.chomens_bot.Bot; +import me.chayapak1.chomens_bot.data.eval.EvalFunction; + +public class GetBotInfoFunction extends EvalFunction { + public GetBotInfoFunction(Bot bot) { + super("getBotInfo", bot); + } + + @Override + public Output execute(Object... args) { + final JsonObject object = new JsonObject(); + + object.addProperty("usernane", bot.username); + object.addProperty("host", bot.host); + object.addProperty("port", bot.port); + object.addProperty("loggedIn", bot.loggedIn); + + return new Output(object.toString(), true); + } +} diff --git a/src/main/java/me/chayapak1/chomens_bot/evalFunctions/GetBotUsernameFunction.java b/src/main/java/me/chayapak1/chomens_bot/evalFunctions/GetBotUsernameFunction.java deleted file mode 100644 index 8a37a9c..0000000 --- a/src/main/java/me/chayapak1/chomens_bot/evalFunctions/GetBotUsernameFunction.java +++ /dev/null @@ -1,15 +0,0 @@ -package me.chayapak1.chomens_bot.evalFunctions; - -import me.chayapak1.chomens_bot.Bot; -import me.chayapak1.chomens_bot.data.eval.EvalFunction; - -public class GetBotUsernameFunction extends EvalFunction { - public GetBotUsernameFunction (Bot bot) { - super("getBotUsername", bot); - } - - @Override - public Output execute(Object... args) { - return new Output(bot.username, false); - } -} 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 43b8d42..4e51a65 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/EvalPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/EvalPlugin.java @@ -35,7 +35,7 @@ public class EvalPlugin { functions.add(new CorePlaceBlockFunction(bot)); functions.add(new ChatFunction(bot)); functions.add(new GetPlayerListFunction(bot)); - functions.add(new GetBotUsernameFunction(bot)); + functions.add(new GetBotInfoFunction(bot)); functions.add(new GetLatestChatMessageFunction(bot)); try { @@ -60,13 +60,15 @@ public class EvalPlugin { socket.on(Socket.EVENT_CONNECT_ERROR, (args) -> connected = false); for (EvalFunction function : functions) { - socket.on(BRIDGE_PREFIX + function.name, args -> { - final EvalFunction.Output output = function.execute(args); + socket.on(BRIDGE_PREFIX + function.name, args -> new Thread(() -> { + try { + final EvalFunction.Output output = function.execute(args); - if (output == null) return; + if (output == null) return; - socket.emit("functionOutput:" + function.name, output.message, output.parseJSON); - }); + socket.emit("functionOutput:" + function.name, output.message, output.parseJSON); + } catch (Exception ignored) {} + }).start()); } socket.on("codeOutput", (args) -> {