From ee22870183ad61ba11d01f24608165626da76c30 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Sun, 7 May 2023 18:21:49 +0700 Subject: [PATCH] eval --- build.gradle | 2 + .../chipmunkmod/command/CommandManager.java | 1 + .../chipmunkmod/commands/EvalCommand.java | 49 +++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 src/main/java/land/chipmunk/chipmunkmod/commands/EvalCommand.java diff --git a/build.gradle b/build.gradle index fb1a455..631f623 100644 --- a/build.gradle +++ b/build.gradle @@ -33,6 +33,8 @@ dependencies { modImplementation include("net.kyori:adventure-text-serializer-legacy:4.13.1") + modImplementation include("org.luaj:luaj-jse:3.0.1") + // Uncomment the following line to enable the deprecated Fabric API modules. // These are included in the Fabric API production distribution and allow you to update your mod to the latest modules at a later more convenient time. diff --git a/src/main/java/land/chipmunk/chipmunkmod/command/CommandManager.java b/src/main/java/land/chipmunk/chipmunkmod/command/CommandManager.java index 0c317ae..79e155c 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/command/CommandManager.java +++ b/src/main/java/land/chipmunk/chipmunkmod/command/CommandManager.java @@ -78,5 +78,6 @@ public class CommandManager { FullBrightCommand.register(dispatcher); MusicCommand.register(dispatcher); RainbowNameCommand.register(dispatcher); + EvalCommand.register(dispatcher); } } \ No newline at end of file diff --git a/src/main/java/land/chipmunk/chipmunkmod/commands/EvalCommand.java b/src/main/java/land/chipmunk/chipmunkmod/commands/EvalCommand.java new file mode 100644 index 0000000..95f43e9 --- /dev/null +++ b/src/main/java/land/chipmunk/chipmunkmod/commands/EvalCommand.java @@ -0,0 +1,49 @@ +package land.chipmunk.chipmunkmod.commands; + +import com.mojang.brigadier.Command; +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.context.CommandContext; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.minecraft.client.MinecraftClient; +import net.minecraft.text.Text; +import net.minecraft.util.Formatting; +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 static com.mojang.brigadier.arguments.StringArgumentType.getString; +import static com.mojang.brigadier.arguments.StringArgumentType.greedyString; +import static land.chipmunk.chipmunkmod.command.CommandManager.argument; +import static land.chipmunk.chipmunkmod.command.CommandManager.literal; + +public class EvalCommand { + public static void register (CommandDispatcher dispatcher) { + dispatcher.register( + literal("eval") + .then( + argument("code", greedyString()) + .executes(EvalCommand::eval) + ) + ); + } + + public static int eval (CommandContext context) { + final String code = getString(context, "code"); + + try { + final Globals globals = JsePlatform.standardGlobals(); + + globals.set("client", CoerceJavaToLua.coerce(MinecraftClient.getInstance())); + globals.set("context", CoerceJavaToLua.coerce(context)); + + LuaValue chunk = globals.load(code); + + context.getSource().sendFeedback(Text.literal(chunk.call().toString()).formatted(Formatting.GREEN)); + } catch (Exception e) { + context.getSource().sendError(Text.literal(e.toString())); + } + + return Command.SINGLE_SUCCESS; + } +}