From b6ef3d00916a836eaca6882add39781283e1f9d7 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Mon, 18 Nov 2024 16:21:50 +0100 Subject: feat: Allow opening config with just /firm --- .../mixins/RedirectWithoutSubCommands.java | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/main/java/moe/nea/firmament/mixins/RedirectWithoutSubCommands.java (limited to 'src/main/java/moe/nea/firmament') diff --git a/src/main/java/moe/nea/firmament/mixins/RedirectWithoutSubCommands.java b/src/main/java/moe/nea/firmament/mixins/RedirectWithoutSubCommands.java new file mode 100644 index 0000000..2186aee --- /dev/null +++ b/src/main/java/moe/nea/firmament/mixins/RedirectWithoutSubCommands.java @@ -0,0 +1,42 @@ +package moe.nea.firmament.mixins; + +import com.llamalad7.mixinextras.sugar.Local; +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.ParseResults; +import com.mojang.brigadier.StringReader; +import com.mojang.brigadier.context.CommandContextBuilder; +import com.mojang.brigadier.tree.CommandNode; +import moe.nea.firmament.util.ErrorUtil; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import java.util.HashSet; + +@Mixin(CommandDispatcher.class) +public class RedirectWithoutSubCommands { + @Inject( + method = "parseNodes", + at = @At( + value = "INVOKE", + target = "Lcom/mojang/brigadier/context/CommandContextBuilder;withCommand(Lcom/mojang/brigadier/Command;)Lcom/mojang/brigadier/context/CommandContextBuilder;", + shift = At.Shift.AFTER + ) + ) + private void injectCommandForRedirects( + CommandNode node, StringReader originalReader, CommandContextBuilder contextSoFar, CallbackInfoReturnable> cir, + @Local(index = 10) CommandContextBuilder context, + @Local(index = 9) CommandNode child + ) { + var p = child; + var set = new HashSet<>(); + if (context.getCommand() == null && p.getRedirect() != null) { + p = p.getRedirect(); + context.withCommand(p.getCommand()); + if (!set.add(p)) { + ErrorUtil.INSTANCE.softError("Redirect circle detected in " + p); + } + } + } +} -- cgit