aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/bscv/modules/motion/GuiMove.java
blob: 3732e37fceffd7cc0e988ee36d77e48441cd2e57 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package ftbsc.bscv.modules.motion;

import com.google.auto.service.AutoService;

import ftbsc.bscv.Boscovicino;
import ftbsc.bscv.api.ILoadable;
import ftbsc.bscv.modules.AbstractModule;
import ftbsc.bscv.patches.BackgroundPatch.RenderBackgroundEvent;
import ftbsc.bscv.tools.Setting;
import net.minecraft.client.gui.advancements.AdvancementsScreen;
import net.minecraft.client.gui.screen.CustomizeSkinScreen;
import net.minecraft.client.gui.screen.IngameMenuScreen;
import net.minecraft.client.gui.screen.OptionsScreen;
import net.minecraft.client.gui.screen.OptionsSoundsScreen;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.client.util.InputMappings;
import net.minecraft.entity.Entity;
import net.minecraft.util.MovementInput;
import net.minecraftforge.client.event.InputUpdateEvent;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.client.gui.screen.ModListScreen;

@AutoService(ILoadable.class)
public class GuiMove extends AbstractModule {

   // TODO allow pausing GuiMove when a textfield is selected

   public final ForgeConfigSpec.ConfigValue<Boolean> background;

   private AutoWalk autoWalk_mod;

   public GuiMove() {
      super();

      this.background = Setting.Bool.builder()
         .fallback(true)
         .name("background")
         .comment("show background on inventories when allowing to move")
         .build(this);
   }

   @Override
   public void enable() {
      this.autoWalk_mod = (AutoWalk) Boscovicino.getInstance().modules.get(AutoWalk.class);
      super.enable();
   }

   private boolean isKeyDown(KeyBinding key) {
      return InputMappings.isKeyDown(MC.getWindow().getWindow(), key.getKey().getValue());
   }

   private KeyBinding[] keys = {
      MC.options.keyUp,
      MC.options.keyDown,
      MC.options.keyLeft,
      MC.options.keyRight,
      MC.options.keyJump,
   };

   private Class<?>[] screens = {
      ContainerScreen.class,
      OptionsScreen.class,
      OptionsSoundsScreen.class,
      IngameMenuScreen.class,
      AdvancementsScreen.class,
      ModListScreen.class,
      CustomizeSkinScreen.class,
   };

   private void forceMovementTick(MovementInput input) {
      // TODO can we patch to make this always happen instead of duplicating code?
      input.up = this.autoWalk_mod.isEnabled() || this.isKeyDown(MC.options.keyUp);
      input.down = this.isKeyDown(MC.options.keyDown);
      input.left = this.isKeyDown(MC.options.keyLeft);
      input.right = this.isKeyDown(MC.options.keyRight);
      input.jumping = this.isKeyDown(MC.options.keyJump);
      input.shiftKeyDown = this.isKeyDown(MC.options.keyShift);

      input.forwardImpulse = input.up == input.down ? 0.0F : (input.up ? 1.0F : -1.0F);
      input.leftImpulse = input.left == input.right ? 0.0F : (input.left ? 1.0F : -1.0F);

      if (input.shiftKeyDown && !MC.player.isSprinting()) {
         MC.player.setSprinting(true);
      }
   }

   private boolean allowMovementOnThisScreen(Screen screen) {
      for (Class<?> clazz : this.screens) {
         if (clazz.isInstance(screen)) {
            return true;
         }
      }
      return false;
   }

   @SubscribeEvent
   public void onRenderBackground(RenderBackgroundEvent event) {
      if (!this.background.get() && event.screen instanceof ContainerScreen) {
         event.setCanceled(true);
      }
   }

   private boolean overrideEntityInput(Entity in) {
      if (MC.player == null) return false;
      if (MC.player.getVehicle() != null) {
         if (in.equals(MC.player.getVehicle())) return true;
      }
      if (in.equals(MC.player)) return true;
      return false;
   }

   @SubscribeEvent
   public void onInputUpdate(InputUpdateEvent event) {
      if (!this.overrideEntityInput(event.getEntityLiving())) return;

      if (this.allowMovementOnThisScreen(MC.screen)) {
         for (KeyBinding key : this.keys) {
            boolean state = InputMappings.isKeyDown(MC.getWindow().getWindow(), key.getKey().getValue());
            if (state ^ key.isDown()) {
               KeyBinding.set(key.getKey(), state);
               key.setDown(state);
            }
         }
         this.forceMovementTick(MC.player.input);
      }
   }
}