aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/bscv/modules/QuickModule.java
blob: 648b02062415dc32d2fed4575200a166d8d934ce (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
package ftbsc.bscv.modules;

import ftbsc.bscv.Boscovicino;
import ftbsc.bscv.tools.Keybind;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.client.util.InputMappings;
import net.minecraftforge.client.event.InputEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.client.registry.ClientRegistry;

// TODO rename
public abstract class QuickModule extends AbstractModule {

   public static final int UNBOUND = InputMappings.UNKNOWN.getValue();

   private class ToggleHook {
      private final KeyBinding key;
      private final QuickModule mod;
      private       boolean    debounce;
      // TODO all examples show isPressed() to get a debounced value
      //  but it seems to be missing? making my own debounce for now
      protected ToggleHook(KeyBinding key, QuickModule mod) {
         this.key = key;
         this.mod = mod;
         this.debounce = false;
      }

      private void onInput() {
         if (this.debounce) {
            if (!this.key.isDown()) {
               this.debounce = false;
            }
         } else {
            if (this.key.isDown()) {
               this.mod.toggle();
               this.debounce = true;
            }
         }
      }

      @SubscribeEvent
      public void onKeyPress(InputEvent.KeyInputEvent event) { this.onInput(); }
      @SubscribeEvent
      public void onKeyPress(InputEvent.MouseInputEvent event) { this.onInput(); }
   }

   public final KeyBinding keybind;

   protected int getDefaultKey() { return UNBOUND; }
   
   public QuickModule() {
      super();

      this.keybind = new KeyBinding(Keybind.name(this.getName()), this.getDefaultKey(), Keybind.category());
      ClientRegistry.registerKeyBinding(this.keybind);

      // register a separate subclass on the hook, so that it's always listening
      MinecraftForge.EVENT_BUS.register(new ToggleHook(this.keybind, this));

      // dispatcher.register(
      //    Commands.literal(this.name.toLowerCase())
      //       .then(
      //          Commands.literal("bind")
      //             .then(
      //                Commands.argument("key", StringArgumentType.word())
      //                   .executes( ctx -> {
      //                      this.keybind.setKey(
      //                         InputMappings.getKey( // TODO it's not this easy!
      //                            StringArgumentType.getString(ctx, "key")
      //                         )
      //                      );
      //                      return 1;
      //                   })
      //             )
      //       )
      // );
   }

}