Hi all,
Is it possible to have key bindings that "pass through" key events to other key bindings if some condition is met? For example, I'd like to have an item, which, when equipped in your main hand, changes the number keys to something other than hotbar switching.
Here's what I have so far. This is a class with my key binding setup (init is invoked during regular initialization by my ClientProxy):
@SideOnly(Side.CLIENT)
public class KeyBindings {
static KeyBinding WEAPON_SELECT_TASEN_1;
static KeyBinding WEAPON_SELECT_TASEN_2;
static KeyBinding WEAPON_SELECT_TASEN_3;
static KeyBinding WEAPON_SELECT_TASEN_4;
static KeyBinding WEAPON_SELECT_KOMATO_1;
static KeyBinding WEAPON_SELECT_KOMATO_2;
static KeyBinding WEAPON_SELECT_KOMATO_3;
static KeyBinding WEAPON_SELECT_KOMATO_4;
static KeyBinding WEAPON_SELECT_BANANA;
static KeyBinding WEAPON_SELECT_MASSACRE;
static KeyBinding WEAPON_SELECT_NULL_DRIVER;
public static void init() {
final String category = "key.categories." + IjiMod.MOD_ID;
WEAPON_SELECT_TASEN_1 = new KeyBinding("key.tasen_1", Keyboard.KEY_1, category);
WEAPON_SELECT_TASEN_2 = new KeyBinding("key.tasen_2", Keyboard.KEY_2, category);
WEAPON_SELECT_TASEN_3 = new KeyBinding("key.tasen_3", Keyboard.KEY_3, category);
WEAPON_SELECT_TASEN_4 = new KeyBinding("key.tasen_4", Keyboard.KEY_4, category);
WEAPON_SELECT_KOMATO_1 = new KeyBinding("key.komato_1", Keyboard.KEY_5, category);
WEAPON_SELECT_KOMATO_2 = new KeyBinding("key.komato_2", Keyboard.KEY_6, category);
WEAPON_SELECT_KOMATO_3 = new KeyBinding("key.komato_3", Keyboard.KEY_7, category);
WEAPON_SELECT_KOMATO_4 = new KeyBinding("key.komato_4", Keyboard.KEY_8, category);
WEAPON_SELECT_BANANA = new KeyBinding("key.banana", Keyboard.KEY_9, category);
WEAPON_SELECT_MASSACRE = new KeyBinding("key.massacre", Keyboard.KEY_0, category);
WEAPON_SELECT_NULL_DRIVER = new KeyBinding("key.null_driver", Keyboard.KEY_MINUS, category);
ClientRegistry.registerKeyBinding(WEAPON_SELECT_TASEN_1);
ClientRegistry.registerKeyBinding(WEAPON_SELECT_TASEN_2);
ClientRegistry.registerKeyBinding(WEAPON_SELECT_TASEN_3);
ClientRegistry.registerKeyBinding(WEAPON_SELECT_TASEN_4);
ClientRegistry.registerKeyBinding(WEAPON_SELECT_KOMATO_1);
ClientRegistry.registerKeyBinding(WEAPON_SELECT_KOMATO_2);
ClientRegistry.registerKeyBinding(WEAPON_SELECT_KOMATO_3);
ClientRegistry.registerKeyBinding(WEAPON_SELECT_KOMATO_4);
ClientRegistry.registerKeyBinding(WEAPON_SELECT_BANANA);
ClientRegistry.registerKeyBinding(WEAPON_SELECT_MASSACRE);
ClientRegistry.registerKeyBinding(WEAPON_SELECT_NULL_DRIVER);
}
}
I have another class which includes a subscriber to ClientTickEvents and checks if the correct item is equipped and then looks at which keybinds are pressed to determine if it should fire a network event to update the item's metadata:
@SideOnly(Side.CLIENT)
@SubscribeEvent
public void onClientTick(TickEvent.ClientTickEvent event) {
final EntityPlayer player = Minecraft.getMinecraft().player;
if (player == null || !(player.getHeldItemMainhand().getItem() instanceof Nanogun)) {
return;
}
final WeaponType currentType = WeaponType.values()[player.getHeldItemMainhand().getMetadata()];
WeaponType target = null;
if (KeyBindings.WEAPON_SELECT_TASEN_1.isPressed()) {
target = currentType == WeaponType.Shotgun ? WeaponType.BusterGun : WeaponType.Shotgun;
} else if (KeyBindings.WEAPON_SELECT_TASEN_2.isPressed()) {
target = currentType == WeaponType.MachineGun ? WeaponType.SplinterGun : WeaponType.MachineGun;
} else if (KeyBindings.WEAPON_SELECT_TASEN_3.isPressed()) {
target = currentType == WeaponType.RocketLauncher ? WeaponType.SpreadRockets : WeaponType.RocketLauncher;
} else if (KeyBindings.WEAPON_SELECT_TASEN_4.isPressed()) {
target = currentType == WeaponType.MpfbDevastator ? WeaponType.Nuke : WeaponType.MpfbDevastator;
} else if (KeyBindings.WEAPON_SELECT_KOMATO_1.isPressed()) {
target = currentType == WeaponType.ResonanceDetonator ? WeaponType.ResonanceReflector : WeaponType.ResonanceDetonator;
} else if (KeyBindings.WEAPON_SELECT_KOMATO_2.isPressed()) {
target = currentType == WeaponType.PulseCannon ? WeaponType.HyperPulse : WeaponType.PulseCannon;
} else if (KeyBindings.WEAPON_SELECT_KOMATO_3.isPressed()) {
target = currentType == WeaponType.Shocksplinter ? WeaponType.PlasmaCannon : WeaponType.Shocksplinter;
} else if (KeyBindings.WEAPON_SELECT_KOMATO_4.isPressed()) {
target = currentType == WeaponType.Cfis ? WeaponType.Velocithor : WeaponType.Cfis;
} else if (KeyBindings.WEAPON_SELECT_BANANA.isPressed()) {
target = WeaponType.BananaGun;
} else if (KeyBindings.WEAPON_SELECT_MASSACRE.isPressed()) {
target = WeaponType.Massacre;
} else if (KeyBindings.WEAPON_SELECT_NULL_DRIVER.isPressed()) {
target = WeaponType.NullDriver;
}
if (target != null) {
PacketHandler.INSTANCE.sendToServer(new WeaponChanged(target));
}
}
Right now, this partially works, and the metadata is updated. I know because I overrode the item's getUnlocalizedName like this:
@Override
public String getUnlocalizedName(ItemStack stack) {
final WeaponType activeWeapon = WeaponType.values()[stack.getMetadata()];
return MyMod.MOD_ID + "." + ID + "." + activeWeapon.name();
}
and the name changes when I press keys 1-4, 9, 0, and "-", but not keys 5-8. Additionally, when I have some other item or an empty hand equipped, the keys 1-4 and 9 don't work to select hotbar items.
What's the best way to achieve the effect I'm looking for (1-9, 0, and "-" switching weapons with my item or equipped, and behaving as usual without it equipped)?