summaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/bscv/patches/CommandsPatch.java
blob: 410b660903a5615ba257b45d197285ebd6df09bd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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);
      }
   }
}