Hello! I'm trying to figure an optimal way of being able to "intercept" key presses. I made a capability that gets attached to the player, and when active, it should prevent player input until it's inactive. I have it written down so there is function called disableMovement() in my Event Handler class that sets whether the player is allowed to move like so:
public class MyEventHandler {
...
//This method is called from a LivingUpdateEvent method
private void doAerial(EntityLivingBase entity, IEntityEffect cap) {
switch(cap.getPhase()) {
case 0:
...
cap.changeStatus();
disableMovement(true);
break;
...
case 3:
if (entity.onGround || entity.isInWater()) {
cap.reset();
disableMovement(false);
}
break;
...
}
}
}
The capability works fine and so does reaching the disableMovement() function, Despite the name, however, I want to control all player input depending on whether or not "disableMovement()" is enabled. I've tried to accomplish this several different methods, including the ones listed below:
Cancelling the Mouse Event and setting playerPos in the EntityLivingUpdate event
Using the onClientTick event and reflection to try to intercept keys when the client updates
Setting motionZ and motionX on LivingUpdateEvent - For this option I directly tried to control the player's motionZ and motionX in the same event, but I was still able to move a tiny bit in all directions - and this isn't the same as preventing player input.
Creating another KeyBinding class that extends KeyBinding. Honestly if done right, I feel like this might be a good way to achieve what I'm trying to do. I was thinking I could create a new KeyBinding class that extends it:
@SideOnly(Side.CLIENT)
public class MOKeyBinding extends KeyBinding {
boolean enableInput = true;
public MOKeyBinding(KeyBinding key) {
super(key.getKeyDescription(), key.getKeyConflictContext(),
key.getKeyModifier(), key.getKeyCode(), key.getKeyCategory());
}
public void changeInputStatus(boolean disabled) {
enableInput = !(disabled);
}
@Override
public boolean isPressed() {
boolean isPressed = false;
if (enableInput) isPressed = super.isPressed();
System.out.println(isPressed);
return isPressed;
}
}
And call this function in postInit():
public void changeKeybindings() {
KeyBinding[] keys = Minecraft.getMinecraft().gameSettings.keyBindings;
for (int i = 0; i < keys.length; i++)
keys[i] = new MOKeyBinding(keys[i]);
}
Then add this code to my event handler (which gets called by the function doAerial()):
public void disableMovement(boolean disable) {
for (int i = 0; i < Minecraft.getMinecraft().gameSettings.keyBindings.length; i++)
((MOKeyBinding) Minecraft.getMinecraft().gameSettings.keyBindings[i]).changeInputStatus(disable)
}
This would allow me to use my custom keyBindings class and also be able to store them in gameSettings. However, when loading up the game, I am unable to press any keys or interact with anything whatsoever. The only button that works is escape and moving the mouse to move the camera. However, when I go to the control options, all of the controls are their listed defaults (WASD for movement, E for inventory, etc). If I change these to other values, pressing the respective key would still not work. I'm assuming this has to do with the private static final variables listed in the KeyBinding class like KEYBIND_ARRAY, KEYBIND_SET, and HASH? It seems that by initializing a new KeyBinding object they are being added to these maps again which I think could cause problems, I'm not sure. Regardless, I was wondering if the final method that I used to try and solve my problem is an optimal choice, and if so, what can I do to fix the problems I've encountered? Thanks!