Jump to content

Ununoctium118

Members
  • Posts

    2
  • Joined

  • Last visited

Everything posted by Ununoctium118

  1. 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)?
  2. I'm also having this issue. So far, this is what I know: It initially looks like an array indexing error, with GuiListExtended::getListEntry being called with an entryID which is out of bounds. In my case, the entryID is 39. However, my listEntries is 40 elements long, with the last one (index 39) having value "null". (Stupid java and it's "everything is a pointer and can be null hur-dur-dur"). listEntries is created with a fixed length equal to the number of key bindings in the game (stored in the field Minecraft::gameSettings.keyBindings) plus the number of keybind categories (stored in the static field net.minecraft.client.settings.KeyBinding.KEYBIND_SET, which is very misleading - it's actually a set of strings, each being the unlocalized name of a keybind category). Whenever you initialize a KeyBinding, its category is added to the set of categories. Note that it's a set, not a list, so it can't have duplicates. Once the length is known, the values are initialized by iterating over the key bindings. (The "current category" string is initialized to an empty string outside the loop.) If the key binding's category is different than the current category, the game adds a "category header" UI element for it. Then, it adds the actual UI element for the key binding. The issue is arising (I think) because we're adding a keybinding category to the set, but a key isn't known to the game with that category, so the category header UI element is never added to listEntries. This leaves the last value of listEntries null, since the game is expecting there to be an additional category header which was never actually created. This is very bad defensive coding on the part of the original coders, and is incredibly fragile, as we both discovered. At the very least there should be checks that the list is fully populated, so that the game can fail fast and make issues like this a lot easier to debug. As far as how to fix it, for me, I had just forgotten to call ClientRegistry.registerKeyBinding for all of my bindings. registerKeyBinding extends the array in Minecraft::gameSettings.keyBindings so that the UI elements will be created for your custom bindings, and so the game won't crash because a category isn't used. I'm initializing all the KeyBinding instances during the init phase (as opposed to pre-init, post-init, or at static initialization time), and calling ClientRegistry.registerKeyBinding immediately after that. Maybe you're initializing them at the wrong time?
×
×
  • Create New...

Important Information

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