From d06ca203aecfdd612327f3416f3c1879578ad6db Mon Sep 17 00:00:00 2001 From: ChomeNS Date: Sat, 25 Mar 2023 17:11:35 +0700 Subject: [PATCH] revert yaml and move servereval to plugins --- pom.xml | 2 +- .../java/me/chayapak1/chomens_bot/Bot.java | 2 ++ .../java/me/chayapak1/chomens_bot/Main.java | 1 + .../commands/ServerEvalCommand.java | 11 +--------- .../chomens_bot/plugins/EvalRunnerPlugin.java | 22 +++++++++++++++++++ 5 files changed, 27 insertions(+), 11 deletions(-) create mode 100644 src/main/java/me/chayapak1/chomens_bot/plugins/EvalRunnerPlugin.java diff --git a/pom.xml b/pom.xml index a3ba439..b503d68 100644 --- a/pom.xml +++ b/pom.xml @@ -88,7 +88,7 @@ org.yaml snakeyaml - 2.0 + 1.29 diff --git a/src/main/java/me/chayapak1/chomens_bot/Bot.java b/src/main/java/me/chayapak1/chomens_bot/Bot.java index 75e481f..9c1fd24 100644 --- a/src/main/java/me/chayapak1/chomens_bot/Bot.java +++ b/src/main/java/me/chayapak1/chomens_bot/Bot.java @@ -43,6 +43,7 @@ public class Bot { @Getter private final HashingPlugin hashing; @Getter private final MusicPlayerPlugin music; @Getter private final TPSPlugin tps; + @Getter private final EvalRunnerPlugin eval; public Bot (String host, int port, String _username, boolean kaboom, List allBots, Configuration config) { this.host = host; @@ -63,6 +64,7 @@ public class Bot { this.hashing = new HashingPlugin(this); this.music = new MusicPlayerPlugin(this); this.tps = new TPSPlugin(this); + this.eval = new EvalRunnerPlugin(this); reconnect(); } diff --git a/src/main/java/me/chayapak1/chomens_bot/Main.java b/src/main/java/me/chayapak1/chomens_bot/Main.java index 7b68621..f0bd504 100644 --- a/src/main/java/me/chayapak1/chomens_bot/Main.java +++ b/src/main/java/me/chayapak1/chomens_bot/Main.java @@ -1,6 +1,7 @@ package me.chayapak1.chomens_bot; import me.chayapak1.chomens_bot.plugins.ConsolePlugin; +import org.yaml.snakeyaml.LoaderOptions; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.Constructor; diff --git a/src/main/java/me/chayapak1/chomens_bot/commands/ServerEvalCommand.java b/src/main/java/me/chayapak1/chomens_bot/commands/ServerEvalCommand.java index 7da273d..3d04896 100644 --- a/src/main/java/me/chayapak1/chomens_bot/commands/ServerEvalCommand.java +++ b/src/main/java/me/chayapak1/chomens_bot/commands/ServerEvalCommand.java @@ -4,10 +4,7 @@ import me.chayapak1.chomens_bot.command.Command; import me.chayapak1.chomens_bot.command.CommandContext; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; -import org.luaj.vm2.Globals; import org.luaj.vm2.LuaValue; -import org.luaj.vm2.lib.jse.CoerceJavaToLua; -import org.luaj.vm2.lib.jse.JsePlatform; import java.util.ArrayList; import java.util.List; @@ -39,13 +36,7 @@ public class ServerEvalCommand implements Command { public Component execute(CommandContext context, String[] args, String[] fullArgs) { try { - Globals globals = JsePlatform.standardGlobals(); - - globals.set("bot", CoerceJavaToLua.coerce(context.bot())); - - LuaValue chunk = globals.load(String.join(" ", args)); - - LuaValue output = chunk.call(); + final LuaValue output = context.bot().eval().run(String.join(" ", args)); context.sendOutput(Component.text(output.toString()).color(NamedTextColor.GREEN)); } catch (Exception e) { diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/EvalRunnerPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/EvalRunnerPlugin.java new file mode 100644 index 0000000..4e420ad --- /dev/null +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/EvalRunnerPlugin.java @@ -0,0 +1,22 @@ +package me.chayapak1.chomens_bot.plugins; + +import lombok.Getter; +import me.chayapak1.chomens_bot.Bot; +import org.luaj.vm2.Globals; +import org.luaj.vm2.LuaValue; +import org.luaj.vm2.lib.jse.CoerceJavaToLua; +import org.luaj.vm2.lib.jse.JsePlatform; + +public class EvalRunnerPlugin { + @Getter private final Globals globals = JsePlatform.standardGlobals(); + + public EvalRunnerPlugin (Bot bot) { + globals.set("bot", CoerceJavaToLua.coerce(bot)); + } + + public LuaValue run (String code) { + LuaValue chunk = globals.load(code); + + return chunk.call(); + } +}