aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-11-18 16:21:50 +0100
committerLinnea Gräf <nea@nea.moe>2024-11-18 17:31:39 +0100
commitb6ef3d00916a836eaca6882add39781283e1f9d7 (patch)
tree8ecc927e6de7fc717bb6a8c581314ef0c3926f0a /src/main/java
parent68948baff37e139a2a3f88ce3b233effb55955c4 (diff)
downloadFirmament-b6ef3d00916a836eaca6882add39781283e1f9d7.tar.gz
Firmament-b6ef3d00916a836eaca6882add39781283e1f9d7.tar.bz2
Firmament-b6ef3d00916a836eaca6882add39781283e1f9d7.zip
feat: Allow opening config with just /firm
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/moe/nea/firmament/mixins/RedirectWithoutSubCommands.java42
1 files changed, 42 insertions, 0 deletions
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<S> {
+ @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<S> node, StringReader originalReader, CommandContextBuilder<S> contextSoFar, CallbackInfoReturnable<ParseResults<S>> cir,
+ @Local(index = 10) CommandContextBuilder<S> context,
+ @Local(index = 9) CommandNode<S> 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);
+ }
+ }
+ }
+}