diff options
author | alemi <me@alemi.dev> | 2023-03-04 04:02:38 +0100 |
---|---|---|
committer | alemi <me@alemi.dev> | 2023-03-04 04:02:38 +0100 |
commit | 1991ea5fbae0df188992286b52a25b613fcb21d6 (patch) | |
tree | ddef12fa0cf5916e97f259e41fef8b5a900bdb77 | |
parent | 00cf2570e37c3101798224ea6e6f918fa7db60e2 (diff) |
feat: add patch and event to add command hints
finally /hints is no longer necessary since we fire an event every time
the server makes us rebuild command hints. Still need to figure out how
to namespace stuff
-rw-r--r-- | src/main/java/ftbsc/bscv/Boscovicino.java | 8 | ||||
-rw-r--r-- | src/main/java/ftbsc/bscv/events/CommandsBuiltEvent.java | 19 | ||||
-rw-r--r-- | src/main/java/ftbsc/bscv/patches/CommandsPatch.java | 55 |
3 files changed, 82 insertions, 0 deletions
diff --git a/src/main/java/ftbsc/bscv/Boscovicino.java b/src/main/java/ftbsc/bscv/Boscovicino.java index d54a3d4..d0860bf 100644 --- a/src/main/java/ftbsc/bscv/Boscovicino.java +++ b/src/main/java/ftbsc/bscv/Boscovicino.java @@ -2,7 +2,10 @@ package ftbsc.bscv; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.mojang.brigadier.tree.CommandNode; + import ftbsc.bscv.api.IModule; +import ftbsc.bscv.events.CommandsBuiltEvent; import ftbsc.bscv.system.Friends; import ftbsc.bscv.system.ModManager; import net.minecraft.client.entity.player.ClientPlayerEntity; @@ -123,6 +126,11 @@ public class Boscovicino implements ICommons { return 1; }) ); + @SubscribeEvent + public void onCommandSuggestionsBuilt(CommandsBuiltEvent event) { + for (CommandNode<CommandSource> child : this.dispatcher.getRoot().getChildren()) { + event.dispatcher().getRoot().addChild(child); + } } @SubscribeEvent diff --git a/src/main/java/ftbsc/bscv/events/CommandsBuiltEvent.java b/src/main/java/ftbsc/bscv/events/CommandsBuiltEvent.java new file mode 100644 index 0000000..99a2dde --- /dev/null +++ b/src/main/java/ftbsc/bscv/events/CommandsBuiltEvent.java @@ -0,0 +1,19 @@ +package ftbsc.bscv.events; + +import com.mojang.brigadier.CommandDispatcher; + +import net.minecraft.command.CommandSource; +import net.minecraft.command.ISuggestionProvider; +import net.minecraftforge.eventbus.api.Event; + +public class CommandsBuiltEvent extends Event { + private CommandDispatcher<CommandSource> dispatcher; + + public CommandDispatcher<CommandSource> dispatcher() { + return this.dispatcher; + } + + public CommandsBuiltEvent(CommandDispatcher<CommandSource> dispatcher) { + this.dispatcher = dispatcher; + } +} diff --git a/src/main/java/ftbsc/bscv/patches/CommandsPatch.java b/src/main/java/ftbsc/bscv/patches/CommandsPatch.java new file mode 100644 index 0000000..410b660 --- /dev/null +++ b/src/main/java/ftbsc/bscv/patches/CommandsPatch.java @@ -0,0 +1,55 @@ +package ftbsc.bscv.patches; + +import ftbsc.bscv.events.CommandsBuiltEvent; +import ftbsc.lll.processor.annotations.Injector; +import ftbsc.lll.processor.annotations.Patch; +import ftbsc.lll.processor.annotations.Target; +import ftbsc.lll.tools.InsnSequence; +import ftbsc.lll.tools.PatternMatcher; +import net.minecraft.client.network.play.ClientPlayNetHandler; +import net.minecraft.command.CommandSource; +import net.minecraft.network.play.server.SCommandListPacket; +import net.minecraftforge.common.MinecraftForge; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.tree.*; + +import com.mojang.brigadier.CommandDispatcher; + +public class CommandsPatch { + + public static class CommandsHook { + public static void cmdBuilt(CommandDispatcher<CommandSource> dispatcher) { + MinecraftForge.EVENT_BUS.post(new CommandsBuiltEvent(dispatcher)); + } + } + + @Patch(value = ClientPlayNetHandler.class, reason = "add hook to insert our command suggestions") + public abstract static class CommandsDispatcherCatcher implements Opcodes { + @Target + abstract void handleCommands(SCommandListPacket pkt); + + @Injector + public void inject(ClassNode clazz, MethodNode main) { + AbstractInsnNode found = PatternMatcher.builder() + .opcodes(ALOAD, INVOKEVIRTUAL, INVOKESPECIAL) + .ignoreFrames() + .ignoreLabels() + .ignoreLineNumbers() + .build() + .find(main) + .getLast() + .getNext(); // TODO temp fix!!! + + InsnSequence is = new InsnSequence(); + is.add(new InsnNode(DUP)); + is.add(new MethodInsnNode( + INVOKESTATIC, + "ftbsc/bscv/patches/CommandsPatch$CommandsHook", + "cmdBuilt", + "(Lcom/mojang/brigadier/CommandDispatcher;)V" + )); + + main.instructions.insert(found, is); + } + } +} |