diff --git a/build-number.txt b/build-number.txt index 7dd08159..b5334936 100644 --- a/build-number.txt +++ b/build-number.txt @@ -1 +1 @@ -1753 \ No newline at end of file +1757 \ No newline at end of file diff --git a/build.gradle b/build.gradle index 1e4875d5..5cb38bcb 100644 --- a/build.gradle +++ b/build.gradle @@ -52,7 +52,9 @@ dependencies { implementation 'com.github.pircbotx:pircbotx:2.3.1' implementation 'com.github.ricksbrown:cowsay:1.1.0' implementation 'org.yaml:snakeyaml:2.0' - implementation 'org.luaj:luaj-jse:3.0.1' + implementation 'party.iroiro.luajava:luajava:4.0.2' + implementation 'party.iroiro.luajava:lua54:4.0.2' + runtimeOnly 'party.iroiro.luajava:lua54-platform:4.0.2:natives-desktop' // what is this? is this necessary? implementation 'net.dv8tion:JDA:5.2.2' implementation 'net.kyori:adventure-text-serializer-legacy:4.15.0' implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.20.0' 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 0d6ea75c..3bb67f70 100644 --- a/src/main/java/me/chayapak1/chomens_bot/commands/ServerEvalCommand.java +++ b/src/main/java/me/chayapak1/chomens_bot/commands/ServerEvalCommand.java @@ -5,14 +5,18 @@ import me.chayapak1.chomens_bot.command.Command; import me.chayapak1.chomens_bot.command.CommandContext; import me.chayapak1.chomens_bot.command.CommandException; import me.chayapak1.chomens_bot.command.TrustLevel; +import me.chayapak1.chomens_bot.util.ColorUtilities; 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 party.iroiro.luajava.Lua; +import party.iroiro.luajava.lua54.Lua54; +import party.iroiro.luajava.value.LuaValue; + +import java.util.Arrays; public class ServerEvalCommand extends Command { + public Lua lua; + public ServerEvalCommand () { super( "servereval", @@ -28,21 +32,29 @@ public class ServerEvalCommand extends Command { public Component execute(CommandContext context) throws CommandException { final Bot bot = context.bot; + final String code = context.getString(true, true); + + if (code.equalsIgnoreCase("reset")) { + if (lua != null) lua.close(); + lua = null; + + return Component.text("Reset the Lua instance").color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor)); + } + bot.executorService.submit(() -> { try { - final Globals globals = JsePlatform.standardGlobals(); + if (lua == null) lua = new Lua54(); - globals.set("bot", CoerceJavaToLua.coerce(bot)); - globals.set("class", CoerceJavaToLua.coerce(Class.class)); - globals.set("context", CoerceJavaToLua.coerce(context)); + lua.openLibraries(); - LuaValue chunk = globals.load(context.getString(true, true)); + lua.set("bot", bot); + lua.set("context", context); - final LuaValue output = chunk.call(); + final LuaValue[] values = lua.eval(code); - context.sendOutput(Component.text(output.toString()).color(NamedTextColor.GREEN)); - } catch (CommandException e) { - context.sendOutput(e.message.color(NamedTextColor.RED)); + final String output = values.length < 1 ? Arrays.toString(values) : values[0].toString(); + + context.sendOutput(Component.text(output).color(NamedTextColor.GREEN)); } catch (Exception e) { context.sendOutput(Component.text(e.toString()).color(NamedTextColor.RED)); }