Jump to content

Hooking into vanilla keybindings.


Sin

Recommended Posts

Hey guys it's been a while, modding has been going pretty smooth until now. But to better explain my problem here is a small class which handles a few key bindings... note some lines including class name have been changed here.

 

package <package>;

import <all the right imports>

public class KeyHandlerThingy extends KeyHandler
{

public static KeyBinding accendBinding = new KeyBinding("Accend", Keyboard.KEY_Z);
public static KeyBinding decendBinding = new KeyBinding("Decend", Keyboard.KEY_X);
public static KeyBinding forwardBinding = new KeyBinding("Thrust", Keyboard.KEY_W);
public static KeyBinding brakeBinding = new KeyBinding("Air Brake", Keyboard.KEY_S);
public static KeyBinding leftRotationBinding = new KeyBinding("Turn Left", Keyboard.KEY_A);
public static KeyBinding rightRotationBinding = new KeyBinding("Turn Right", Keyboard.KEY_D);
public static int keyLift = 0;
public static int keyThrust = 0;
public static int keyTurn = 0;


public static KeyBinding[] arrayOfKeys = new KeyBinding[] {accendBinding, decendBinding, forwardBinding, brakeBinding, leftRotationBinding, rightRotationBinding};
public static boolean[] areRepeating = new boolean[] {true, true, true, true, true, true};

public KeyHandlerThingy() 
{
	super(arrayOfKeys, areRepeating);
}

@Override
public String getLabel() 
{
	return "Mod KeyBindings";
}
@Override
public void keyDown(EnumSet<TickType> types, KeyBinding keyb, boolean tickEnd, boolean isRepeat) 
{
	if (FMLClientHandler.instance().getClient().currentScreen == null) 
	{
	    if(keyb.keyCode == accendBinding.keyCode) 
	    {
	    	keyLift = 1;
	    }
	    else if(keyb.keyCode == decendBinding.keyCode)
	    {
	    	keyLift = 2;
	    }
	    else
	    {
	    	keyLift = 0;
	    }
	    
	    if(keyb.keyCode == forwardBinding.keyCode)
	    {
	    	keyThrust = 1;
	    }
	    else if(keyb.keyCode == brakeBinding.keyCode)
	    {
	    	keyThrust = 2;
	    }
	    else
	    {
	    	keyThrust = 0;
	    }
	    
	    if(keyb.keyCode == leftRotationBinding.keyCode)
	    {
	    	keyTurn = 1;
	    }
	    else if(keyb.keyCode == rightRotationBinding.keyCode)
	    {
	    	keyTurn = 2;
	    }
	    else
	    {
	    	keyTurn = 0;
	    }   
	}
}

@Override
public void keyUp(EnumSet<TickType> types, KeyBinding kb, boolean tickEnd) 
{

}
@Override
public EnumSet<TickType> ticks() 
{
	return EnumSet.of(TickType.CLIENT);
}

}

 

So once the vehicle is mounted by the player - everything works fine. The problem is, the key bindings override the "forward, backward, left and right" keys when in game (conflicts). So my intention was to simply find if the vanilla key has been pressed and work with that.

 

Although crude my first attempt was to test for the following:

 

if(keyb.keyCode == Minecraft.getMinecraft().gameSettings.forwardKeyBinding.keyCode)
{
//do stuff
}

 

But this was damned pretty quickly (the reverse happened, player could move, vehicle couldn't). Now here's (what I believe), the odd part. Printing the keyCode out reveals the correct value (in this case '17'), but I'm still unable to manipulate the aircraft which must mean one of two things, '17' is not being registered as being pressed when KeyDown is being called, or the key is being overwritten earlier on in the program each tick.

 

TLDR; I need to access the keybinding for player movement so I may override it.

 

Cheers everyone!

Link to comment
Share on other sites

I actually did this once, while starting to update/revamp Pchan3's Airship mod.

You simply get Minecraft.getMinecraft().gameSettings.forwardKeyBinding.pressed to know if whatever key is assigned to the command (in this case forward) is pressed. You can do this in a TickHandler, after all KeyHandlers are a sort of TickHandlers.

Mind though that key bindings are not part of the server side (simulation) of the game, so you need to relay the input in some way. I did that with a packet handler that then set some kind of command flag (bit flags) in whatever entity ((EntityPlayer)player).ridingEntity was, as long as it was an EntityAirship. The variable player was supplied by the onPacketReceived method of the packet handler.

Finally, the entity processed the command and moved. It worked fine, even though more efficient ways exist for sure. The advantage was that the key used by the player was configurable in the options menu, instead of the original which had it in the config file. The config file allowed also a way to separate each command from the default command, for example separating the Airship Forward from the default Move Forward.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.