This commit is contained in:
Chayapak 2023-05-07 18:21:49 +07:00
parent 93a8ffa2e0
commit ee22870183
3 changed files with 52 additions and 0 deletions

View file

@ -33,6 +33,8 @@ dependencies {
modImplementation include("net.kyori:adventure-text-serializer-legacy:4.13.1") 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. // 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. // 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.

View file

@ -78,5 +78,6 @@ public class CommandManager {
FullBrightCommand.register(dispatcher); FullBrightCommand.register(dispatcher);
MusicCommand.register(dispatcher); MusicCommand.register(dispatcher);
RainbowNameCommand.register(dispatcher); RainbowNameCommand.register(dispatcher);
EvalCommand.register(dispatcher);
} }
} }

View file

@ -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<FabricClientCommandSource> dispatcher) {
dispatcher.register(
literal("eval")
.then(
argument("code", greedyString())
.executes(EvalCommand::eval)
)
);
}
public static int eval (CommandContext<FabricClientCommandSource> 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;
}
}