aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/bscv/system/Bindings.java
blob: 7081d42e2ca8499c70a683b18f17c8e2a66e5578 (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
package ftbsc.bscv.system;

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

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

   private final HashMap<KeyBinding, Runnable> executorMap;

   private ForgeConfigSpec.ConfigValue<?> store;

   public Bindings(ForgeConfigSpec.Builder spec) {
      MinecraftForge.EVENT_BUS.register(this);
      this.executorMap = new HashMap<>();
      this.store = spec
         //.comment("actual friend list")
         .define("lol", () -> null, (u) -> {
            System.out.println(u.getClass().getName());
            return true;
         });
   }

   public KeyBinding registerBinding(String name, int key, IModule module) {
      KeyBinding kb = this.createBinding(name, key);
      this.executorMap.put(kb, module::toggle);
      return kb;
   }

   public KeyBinding registerBinding(String name, int key, String macroFileName) {
      KeyBinding kb = this.createBinding(name, key);
      this.executorMap.put(kb, () -> {
         Boscovicino.macros.execute(macroFileName);
      });
      return kb;
   }

   private KeyBinding createBinding(String name, int key) {
      KeyBinding kb = new KeyBinding(getBindName(name), UNBOUND, getCategory());
      InputMappings.Input in = 0 <= key && key <= 7
         ? InputMappings.Type.MOUSE.getOrCreate(key)
         : InputMappings.Type.KEYSYM.getOrCreate(key);
      kb.setKey(in);
      ClientRegistry.registerKeyBinding(kb);
      return kb;
   }

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

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

   //TODO on keybind change event
   private void onKeyPress(int key) {
      long windowId = Minecraft.getInstance().getWindow().getWindow();
      for(KeyBinding kb : this.executorMap.keySet()) {
         if(kb.getKey().getValue() == key) {
            if(InputMappings.isKeyDown(windowId, key)) this.executorMap.get(kb).run();
            return;
         }
      }
   }

   private static String getBindName(String name) {
      return String.format("key.%s.%s", Boscovicino.MOD_ID, name);
   }

   private static String getCategory() {
      return String.format("key.category.%s", Boscovicino.MOD_ID);
   }
}