22
33import gregtech .api .GTValues ;
44import gregtech .api .GregTechAPI ;
5- import gregtech .api .util .GTLog ;
65import gregtech .core .network .packets .PacketKeysPressed ;
76
87import net .minecraft .client .Minecraft ;
1110import net .minecraft .entity .player .EntityPlayerMP ;
1211import net .minecraftforge .client .settings .IKeyConflictContext ;
1312import net .minecraftforge .client .settings .KeyConflictContext ;
14- import net .minecraftforge .common .MinecraftForge ;
1513import net .minecraftforge .fml .client .registry .ClientRegistry ;
1614import net .minecraftforge .fml .common .FMLCommonHandler ;
1715import net .minecraftforge .fml .common .eventhandler .SubscribeEvent ;
1816import net .minecraftforge .fml .common .gameevent .InputEvent ;
1917import net .minecraftforge .fml .relauncher .Side ;
2018import net .minecraftforge .fml .relauncher .SideOnly ;
2119
22- import org .apache .commons .lang3 .tuple .MutablePair ;
20+ import org .jetbrains .annotations .ApiStatus ;
21+ import org .jetbrains .annotations .NotNull ;
2322import org .lwjgl .input .Keyboard ;
2423import org .lwjgl .input .Mouse ;
2524
2625import java .util .ArrayList ;
2726import java .util .List ;
27+ import java .util .Map ;
2828import java .util .WeakHashMap ;
2929import java .util .function .Supplier ;
3030
@@ -43,13 +43,6 @@ public enum KeyBind {
4343
4444 public static final KeyBind [] VALUES = values ();
4545
46- public static void init () {
47- GTLog .logger .info ("Registering KeyBinds" );
48- if (FMLCommonHandler .instance ().getSide ().isClient ()) {
49- MinecraftForge .EVENT_BUS .register (KeyBind .class );
50- }
51- }
52-
5346 @ SubscribeEvent
5447 @ SideOnly (Side .CLIENT )
5548 public static void onInputEvent (InputEvent .KeyInputEvent event ) {
@@ -83,75 +76,89 @@ public static boolean scrollingDown() {
8376 return Mouse .getEventDWheel () < 0 ;
8477 }
8578
79+ private final Map <EntityPlayerMP , Boolean > keysPressed = new WeakHashMap <>();
80+ private final Map <EntityPlayerMP , Boolean > keysDown = new WeakHashMap <>();
81+
8682 @ SideOnly (Side .CLIENT )
87- private KeyBinding keybinding ;
83+ private KeyBinding mcKeyBinding ;
8884 @ SideOnly (Side .CLIENT )
89- private boolean isPressed , isKeyDown ;
90-
91- private final WeakHashMap <EntityPlayerMP , MutablePair <Boolean , Boolean >> mapping = new WeakHashMap <>();
92-
93- // For Vanilla/Other Mod keybinds
94- // Double Supplier to keep client classes from loading
95- KeyBind (Supplier <Supplier <KeyBinding >> keybindingGetter ) {
85+ private boolean isPressed ;
86+ @ SideOnly (Side .CLIENT )
87+ private boolean isKeyDown ;
88+
89+ /**
90+ * For Vanilla/Other Mod keybinds
91+ * <p>
92+ * Double Supplier keeps client classes from loading
93+ *
94+ * @param keybindingSupplier supplier to the client side keybinding
95+ */
96+ KeyBind (@ NotNull Supplier <Supplier <KeyBinding >> keybindingSupplier ) {
9697 if (FMLCommonHandler .instance ().getSide ().isClient ()) {
97- this .keybinding = keybindingGetter .get ().get ();
98+ this .mcKeyBinding = keybindingSupplier .get ().get ();
9899 }
99100 }
100101
101- KeyBind (String langKey , int button ) {
102+ KeyBind (@ NotNull String langKey , int button ) {
102103 if (FMLCommonHandler .instance ().getSide ().isClient ()) {
103- this .keybinding = new KeyBinding (langKey , button , GTValues .MOD_NAME );
104- ClientRegistry .registerKeyBinding (this .keybinding );
104+ this .mcKeyBinding = new KeyBinding (langKey , button , GTValues .MOD_NAME );
105+ ClientRegistry .registerKeyBinding (this .mcKeyBinding );
105106 }
106107 }
107108
108- KeyBind (String langKey , IKeyConflictContext ctx , int button ) {
109+ KeyBind (@ NotNull String langKey , @ NotNull IKeyConflictContext ctx , int button ) {
109110 if (FMLCommonHandler .instance ().getSide ().isClient ()) {
110- this .keybinding = new KeyBinding (langKey , ctx , button , GTValues .MOD_NAME );
111- ClientRegistry .registerKeyBinding (this .keybinding );
111+ this .mcKeyBinding = new KeyBinding (langKey , ctx , button , GTValues .MOD_NAME );
112+ ClientRegistry .registerKeyBinding (this .mcKeyBinding );
112113 }
113114 }
114115
115116 @ SideOnly (Side .CLIENT )
116117 public KeyBinding toMinecraft () {
117- return this .keybinding ;
118+ return this .mcKeyBinding ;
118119 }
119120
120121 @ SideOnly (Side .CLIENT )
121122 public boolean isPressed () {
122- return this .keybinding .isPressed ();
123+ return this .mcKeyBinding .isPressed ();
123124 }
124125
125126 @ SideOnly (Side .CLIENT )
126127 public boolean isKeyDown () {
127- return this .keybinding .isKeyDown ();
128+ return this .mcKeyBinding .isKeyDown ();
128129 }
129130
130- public void update (boolean pressed , boolean keyDown , EntityPlayerMP player ) {
131- MutablePair <Boolean , Boolean > pair = this .mapping .get (player );
132- if (pair == null ) {
133- this .mapping .put (player , MutablePair .of (pressed , keyDown ));
134- } else {
135- pair .left = pressed ;
136- pair .right = keyDown ;
137- }
131+ @ ApiStatus .Internal
132+ public void updateServerState (@ NotNull EntityPlayerMP player , boolean pressed , boolean keyDown ) {
133+ this .keysPressed .put (player , pressed );
134+ this .keysDown .put (player , keyDown );
138135 }
139136
140- public boolean isPressed (EntityPlayer player ) {
137+ /**
138+ * Can call on either the {@code Server} or {@code Client} side.
139+ *
140+ * @param player the player to test
141+ * @return if the player pressed the key
142+ */
143+ public boolean isPressed (@ NotNull EntityPlayer player ) {
141144 if (player .world .isRemote ) {
142145 return isPressed ();
143146 } else {
144- MutablePair <Boolean , Boolean > pair = this .mapping .get ((EntityPlayerMP ) player );
145- return pair != null && pair .left ;
147+ return keysPressed .getOrDefault ((EntityPlayerMP ) player , false );
146148 }
147149 }
148150
149- public boolean isKeyDown (EntityPlayer player ) {
151+ /**
152+ * Can call on either the {@code Server} or {@code Client} side.
153+ *
154+ * @param player the player to test
155+ * @return if the player is holding the key down
156+ */
157+ public boolean isKeyDown (@ NotNull EntityPlayer player ) {
150158 if (player .world .isRemote ) {
151159 return isKeyDown ();
152160 } else {
153- MutablePair <Boolean , Boolean > pair = this .mapping .get ((EntityPlayerMP ) player );
154- return pair != null && pair .right ;
161+ return keysDown .getOrDefault ((EntityPlayerMP ) player , false );
155162 }
156163 }
157164}
0 commit comments