11package com .minecrafttas .mctcommon ;
22
3- import java .util .ArrayList ;
4- import java .util .List ;
3+ import java .util .HashMap ;
4+ import java .util .Map ;
55
66import org .apache .commons .lang3 .ArrayUtils ;
77
@@ -21,47 +21,7 @@ public class KeybindManager implements EventClientGameLoop {
2121
2222 private final IsKeyDownFunc defaultFunction ;
2323
24- public static class Keybind {
25-
26- public final KeyBinding vanillaKeyBinding ;
27- private final String category ;
28- private final Runnable onKeyDown ;
29- private final IsKeyDownFunc isKeyDownFunc ;
30-
31- /**
32- * Initialize keybind
33- *
34- * @param name Name of keybind
35- * @param category Category of keybind
36- * @param defaultKey Default key of keybind
37- * @param onKeyDown Will be run when the keybind is pressed
38- */
39- public Keybind (String name , String category , int defaultKey , Runnable onKeyDown ) {
40- this (name , category , defaultKey , onKeyDown , null );
41- }
42-
43- /**
44- * Initialize keybind with a different "isKeyDown" method
45- *
46- * @param name Name of keybind
47- * @param category Category of keybind
48- * @param defaultKey Default key of keybind
49- * @param onKeyDown Will be run when the keybind is pressed
50- */
51- public Keybind (String name , String category , int defaultKey , Runnable onKeyDown , IsKeyDownFunc func ) {
52- this .vanillaKeyBinding = new KeyBinding (name , defaultKey , category );
53- this .category = category ;
54- this .onKeyDown = onKeyDown ;
55- this .isKeyDownFunc = func ;
56- }
57-
58- @ Override
59- public String toString () {
60- return this .vanillaKeyBinding .getKeyDescription ();
61- }
62- }
63-
64- private List <Keybind > keybindings ;
24+ private Map <KeybindID , Keybind > keybindings ;
6525
6626 /**
6727 * Initialize keybind manage
@@ -71,15 +31,15 @@ public String toString() {
7131 */
7232 public KeybindManager (IsKeyDownFunc defaultFunction ) {
7333 this .defaultFunction = defaultFunction ;
74- this .keybindings = new ArrayList <>();
34+ this .keybindings = new HashMap <>();
7535 }
7636
7737 /**
7838 * Handle registered keybindings on game loop
7939 */
8040 @ Override
8141 public void onRunClientGameLoop (Minecraft mc ) {
82- for (Keybind keybind : this .keybindings ) {
42+ for (Keybind keybind : this .keybindings . values () ) {
8343 IsKeyDownFunc keyDown = keybind .isKeyDownFunc != null ? keybind .isKeyDownFunc : defaultFunction ;
8444 if (keyDown .isKeyDown (keybind .vanillaKeyBinding )) {
8545 keybind .onKeyDown .run ();
@@ -88,26 +48,87 @@ public void onRunClientGameLoop(Minecraft mc) {
8848
8949 }
9050
51+ public void registerKeybinds (GameSettings options , Class <? extends KeybindID > keybindIDclass ) {
52+ if (keybindIDclass .isEnum ())
53+ registerKeybinds (options , keybindIDclass .getEnumConstants ());
54+ }
55+
56+ public void registerKeybinds (GameSettings options , KeybindID ... keybind ) {
57+ for (KeybindID keybindEnum : keybind ) {
58+ registerKeybind (options , keybindEnum , keybindEnum .getKeybind ());
59+ }
60+ }
61+
9162 /**
92- * Register new keybind
63+ * Register a new keybind
9364 *
94- * @param keybind Keybind to register
95- * @param options
65+ * @param keybindID The {@link KeybindID} to register this underI
66+ * @param keybind The {@link Keybind} to register
9667 */
97- public void registerKeybind (Keybind keybind , GameSettings options ) {
98- this .keybindings .add ( keybind );
68+ public void registerKeybind (GameSettings options , KeybindID keybindID , Keybind keybind ) {
69+ this .keybindings .put ( keybindID , keybind );
9970 KeyBinding keyBinding = keybind .vanillaKeyBinding ;
10071
101- if (!AccessorKeyBinding .getCategoryOrder ().containsKey (keybind .category ))
102- AccessorKeyBinding .getCategoryOrder ().put (keybind .category , AccessorKeyBinding .getCategoryOrder ().size () + 1 );
72+ Map <String , Integer > categoryOrder = AccessorKeyBinding .getCategoryOrder ();
73+
74+ if (!categoryOrder .containsKey (keybind .category ))
75+ categoryOrder .put (keybind .category , categoryOrder .size () + 1 );
10376
10477 // add keybinding
10578 options .keyBindings = ArrayUtils .add (options .keyBindings , keyBinding );
10679 }
10780
81+ public Keybind getKeybind (KeybindID id ) {
82+ return keybindings .get (id );
83+ }
84+
10885 @ FunctionalInterface
10986 public static interface IsKeyDownFunc {
11087
11188 public boolean isKeyDown (KeyBinding keybind );
11289 }
90+
91+ public static interface KeybindID {
92+ public Keybind getKeybind ();
93+ }
94+
95+ public static class Keybind {
96+
97+ public final KeyBinding vanillaKeyBinding ;
98+ private final String category ;
99+ private final Runnable onKeyDown ;
100+ private final IsKeyDownFunc isKeyDownFunc ;
101+
102+ /**
103+ * Initialize keybind
104+ *
105+ * @param name Name of keybind
106+ * @param category Category of keybind
107+ * @param defaultKey Default key of keybind
108+ * @param onKeyDown Will be run when the keybind is pressed
109+ */
110+ public Keybind (String name , String category , int defaultKey , Runnable onKeyDown ) {
111+ this (name , category , defaultKey , onKeyDown , null );
112+ }
113+
114+ /**
115+ * Initialize keybind with a different "isKeyDown" method
116+ *
117+ * @param name Name of keybind
118+ * @param category Category of keybind
119+ * @param defaultKey Default key of keybind
120+ * @param onKeyDown Will be run when the keybind is pressed
121+ */
122+ public Keybind (String name , String category , int defaultKey , Runnable onKeyDown , IsKeyDownFunc func ) {
123+ this .vanillaKeyBinding = new KeyBinding (name , defaultKey , category );
124+ this .category = category ;
125+ this .onKeyDown = onKeyDown ;
126+ this .isKeyDownFunc = func ;
127+ }
128+
129+ @ Override
130+ public String toString () {
131+ return this .vanillaKeyBinding .getKeyDescription ();
132+ }
133+ }
113134}
0 commit comments