aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/bscv/patches/CommandsPatch.java
blob: a7474a0aedf31e5c7cd7e68c024eedeee7d42168 (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
56
57
58
59
60
61
62
63
64
65
66
67
package ftbsc.bscv.patches;

import ftbsc.lll.processor.annotations.Find;
import ftbsc.lll.processor.annotations.Injector;
import ftbsc.lll.processor.annotations.Patch;
import ftbsc.lll.processor.annotations.Target;
import ftbsc.lll.proxies.impl.MethodProxy;
import ftbsc.lll.tools.InsnSequence;
import ftbsc.lll.tools.PatternMatcher;
import ftbsc.lll.tools.debug.BytecodePrinter;
import ftbsc.lll.tools.nodes.MethodProxyInsnNode;
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 net.minecraftforge.eventbus.api.Event;

import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.*;

import com.mojang.brigadier.CommandDispatcher;

public class CommandsPatch {

   public static class CommandsBuiltEvent extends Event {
      public final CommandDispatcher<CommandSource> dispatcher;
   
      public CommandsBuiltEvent(CommandDispatcher<CommandSource> dispatcher) {
         this.dispatcher = dispatcher;
      }
   }

   public static void cmdBuilt(CommandDispatcher<CommandSource> dispatcher) {
      MinecraftForge.EVENT_BUS.post(new CommandsBuiltEvent(dispatcher));
   }

   @Patch(ClientPlayNetHandler.class)
   public abstract static class CommandsDispatcherCatcher implements Opcodes {
      @Find(CommandsPatch.class)
      MethodProxy cmdBuilt;


      @Target(of = "cmdBuilt")
      abstract void cmdBuilt(CommandDispatcher<CommandSource> dispatcher);

      @Target(of = "injectCommandHandler")
      abstract void handleCommands(SCommandListPacket pkt);

      @Injector(reason = "add hook to insert our command suggestions")
      public void injectCommandHandler(ClassNode clazz, MethodNode main) {
         AbstractInsnNode found = PatternMatcher.builder()
            .opcodes(ALOAD, INVOKEVIRTUAL, INVOKESPECIAL)
            .ignoreFrames()
            .ignoreLabels()
            .ignoreLineNumbers()
            .build()
            .find(main)
            .getLast();

         InsnSequence is = new InsnSequence();
         is.add(new InsnNode(DUP));
         is.add(new MethodProxyInsnNode(INVOKESTATIC, cmdBuilt));

         main.instructions.insert(found, is);
      }
   }
}