diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java index 71bda59..68e4d74 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java @@ -65,7 +65,6 @@ public class Bot { public BossbarManagerPlugin bossbar; public MusicPlayerPlugin music; public TPSPlugin tps; - public EvalPlugin eval; public TrustedPlugin trusted; public BruhifyPlugin bruhify; public CloopPlugin cloop; @@ -115,7 +114,6 @@ public class Bot { this.bossbar = new BossbarManagerPlugin(this); this.music = new MusicPlayerPlugin(this); this.tps = new TPSPlugin(this); - this.eval = new EvalPlugin(this); this.trusted = new TrustedPlugin(this); this.bruhify = new BruhifyPlugin(this); this.cloop = new CloopPlugin(this); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/EvalCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/EvalCommand.java deleted file mode 100644 index 70f8dc9..0000000 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/EvalCommand.java +++ /dev/null @@ -1,60 +0,0 @@ -package land.chipmunk.chayapak.chomens_bot.commands; - -import land.chipmunk.chayapak.chomens_bot.Bot; -import land.chipmunk.chayapak.chomens_bot.command.Command; -import land.chipmunk.chayapak.chomens_bot.command.CommandContext; -import land.chipmunk.chayapak.chomens_bot.command.CommandException; -import land.chipmunk.chayapak.chomens_bot.command.TrustLevel; -import land.chipmunk.chayapak.chomens_bot.data.EvalOutput; -import land.chipmunk.chayapak.chomens_bot.util.ColorUtilities; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.format.NamedTextColor; - -import java.util.concurrent.CompletableFuture; - -public class EvalCommand extends Command { - public EvalCommand () { - super( - "eval", - "Evaluate JavaScript codes", - new String[] { "run ", "reset" }, - new String[] {}, - TrustLevel.PUBLIC, - false - ); - } - - @Override - public Component execute(CommandContext context) throws CommandException { - final Bot bot = context.bot; - - if (!bot.eval.connected) throw new CommandException(Component.text("Eval server is not online")); - - final String action = context.getString(false, true); - - switch (action) { - case "run" -> { - final String command = context.getString(true, true); - - final CompletableFuture future = bot.eval.run(command); - - future.thenApply((output) -> { - if (output.isError()) context.sendOutput(Component.text(output.output()).color(NamedTextColor.RED)); - else context.sendOutput(Component.text(output.output())); - - return output; - }); - } - case "reset" -> { - bot.eval.reset(); - - return Component.text("Reset the eval worker").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); - } - default -> { - throw new CommandException(Component.text("Invalid action")); - } - } - - return null; - } -} diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java index 90bc0df..068c7ee 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java @@ -49,7 +49,6 @@ public class CommandHandlerPlugin { registerCommand(new ClearChatQueueCommand()); registerCommand(new FilterCommand()); registerCommand(new MailCommand()); - registerCommand(new EvalCommand()); registerCommand(new InfoCommand()); registerCommand(new ConsoleCommand()); registerCommand(new PCrashCommand()); 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 deleted file mode 100644 index 0b75601..0000000 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/EvalPlugin.java +++ /dev/null @@ -1,101 +0,0 @@ -package land.chipmunk.chayapak.chomens_bot.plugins; - -import com.google.gson.Gson; -import com.google.gson.JsonArray; -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.*; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.CompletableFuture; - -public class EvalPlugin { - public static final String BRIDGE_PREFIX = "function:"; - - public boolean connected = false; - - private Socket socket = null; - - private int transactionId = 0; - - private final Map> futures = new HashMap<>(); - - public final List functions = new ArrayList<>(); - - private final Gson gson = new Gson(); - - public EvalPlugin (Bot bot) { - functions.add(new CoreFunction(bot)); - functions.add(new CorePlaceBlockFunction(bot)); - functions.add(new ChatFunction(bot)); - functions.add(new GetPlayerListFunction(bot)); - - try { - socket = IO.socket(bot.config.eval.address); - } catch (Exception e) { - e.printStackTrace(); - } - - socket.on(Socket.EVENT_CONNECT, (args) -> { - connected = true; - - final JsonArray array = new JsonArray(); - - for (EvalFunction function : functions) array.add(function.name); - - socket.emit( - "setFunctions", - gson.toJson(array) - ); - }); - 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, args -> { - final EvalFunction.Output output = function.execute(args); - - if (output == null) return; - - socket.emit("functionOutput:" + function.name, output.message, output.parseJSON); - }); - } - - socket.on("codeOutput", (args) -> { - final int id = (int) args[0]; - final boolean isError = (boolean) args[1]; - final String output = (String) args[2]; - - final CompletableFuture future = futures.get(id); - - future.complete(new EvalOutput(isError, output)); - }); - - socket.connect(); - } - - public CompletableFuture run (String code) { - final CompletableFuture future = new CompletableFuture<>(); - - if (!connected) return null; - - socket.emit("runCode", transactionId, code); - - futures.put(transactionId, future); - - transactionId++; - - return future; - } - - public void reset () { - if (!connected) return; - - socket.emit("reset"); - } -}