aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/bscv/patches/NoSlowPatch.java
blob: 98400ef5cdf5d41fbe9a05777f2b7cc1ba1dfbc1 (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
package ftbsc.bscv.patches;

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.entity.player.ClientPlayerEntity;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.Cancelable;
import net.minecraftforge.eventbus.api.Event;

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

public class NoSlowPatch {

   @Cancelable
   public static class PlayerSlowDownEvent extends Event {  }

   public static boolean shouldSlowPlayer() {
      return MinecraftForge.EVENT_BUS.post(new PlayerSlowDownEvent());
   }

   @Patch(value = ClientPlayerEntity.class, reason = "add hook to cancel slowing down effect of using items")
   public abstract static class SlowDownOverride implements Opcodes {
      @Target
      abstract void aiStep();

      @Injector
      public void inject(ClassNode clazz, MethodNode main) {
         AbstractInsnNode found = PatternMatcher.builder()
            .opcodes(ALOAD, INVOKEVIRTUAL, IFEQ, ALOAD, INVOKEVIRTUAL, IFNE)
            .ignoreFrames()
            .ignoreLabels()
            .ignoreLineNumbers()
            .build()
            .find(main)
            .getLast();

         AbstractInsnNode after = PatternMatcher.builder()
            .opcodes(ALOAD, ICONST_0, PUTFIELD)
            .ignoreFrames()
            .ignoreLabels()
            .ignoreLineNumbers()
            .build()
            .find(main)
            .getLast();

         LabelNode skip = new LabelNode(); // TODO can we get the label that the original IF is jumping to without adding one ourselves?
         InsnSequence is = new InsnSequence();
         is.add(new MethodInsnNode(
            INVOKESTATIC,
            "ftbsc/bscv/patches/NoSlowPatch",
            "shouldSlowPlayer",
            "()Z"
         ));
         is.add(new JumpInsnNode(IFNE, skip));

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