Class DynamicKeyRegistryImpl

java.lang.Object
dev.munebase.dynamickeybinds.DynamicKeyRegistryImpl
All Implemented Interfaces:
DynamicKeyRegistry

public class DynamicKeyRegistryImpl extends Object implements DynamicKeyRegistry
Thread-safe implementation of DynamicKeyRegistry using concurrent data structures. Manages the registration and retrieval of dynamic keybinds at runtime. This implementation uses ConcurrentHashMap and ConcurrentHashMap.newKeySet() to safely handle keybind management from multiple threads without external synchronization.

Features:

  • Thread-safe keybind registration and unregistration
  • GLFW keycode validation
  • Duplicate ID prevention
  • Association of custom actions with keybinds

Example Usage:

 
 DynamicKeyRegistry registry = new DynamicKeyRegistryImpl();
 Optional<DynamicKeybindAction> action = Optional.of(
   new DynamicKeybindAction("mymod:cast_spell", new CompoundTag())
 );
 KeyMapping key = registry.registerDynamicKey(
   "mymod:ability_cast",
   GLFW.GLFW_KEY_Q,
   "mymod",
   action
 );
 
 
See Also:
  • Constructor Details

    • DynamicKeyRegistryImpl

      public DynamicKeyRegistryImpl()
  • Method Details

    • registerDynamicKey

      public net.minecraft.client.KeyMapping registerDynamicKey(String id, int keyCode, String category, Optional<DynamicKeybindAction> action)
      Register a new dynamic keybind at runtime.
      Specified by:
      registerDynamicKey in interface DynamicKeyRegistry
      Parameters:
      id - unique identifier for the keybind (e.g., "mymod:ability_cast")
      keyCode - the GLFW key code (must be in valid GLFW range)
      category - the keybind category for menu organization (e.g., "mymod")
      action - optional action to trigger when the key is pressed
      Returns:
      the registered KeyMapping
      Throws:
      IllegalArgumentException - if ID already exists or keyCode is invalid
    • unregisterDynamicKey

      public void unregisterDynamicKey(net.minecraft.client.KeyMapping keyBinding)
      Unregister an existing dynamic keybind. Removes the keybind from all internal maps, preventing further key presses from triggering its action.
      Specified by:
      unregisterDynamicKey in interface DynamicKeyRegistry
      Parameters:
      keyBinding - the KeyMapping to unregister
    • getAllDynamicKeys

      public Collection<net.minecraft.client.KeyMapping> getAllDynamicKeys()
      Retrieve all currently registered dynamic keybinds.
      Specified by:
      getAllDynamicKeys in interface DynamicKeyRegistry
      Returns:
      immutable collection of all dynamic KeyMappings
    • getKeyBindById

      public net.minecraft.client.KeyMapping getKeyBindById(String id)
      Retrieve a specific keybind by its ID.
      Specified by:
      getKeyBindById in interface DynamicKeyRegistry
      Parameters:
      id - the unique identifier of the keybind
      Returns:
      the KeyMapping if found, or null if not registered
    • getKeyBindAction

      public Optional<DynamicKeybindAction> getKeyBindAction(net.minecraft.client.KeyMapping keyBinding)
      Retrieve the action associated with a keybind.
      Specified by:
      getKeyBindAction in interface DynamicKeyRegistry
      Parameters:
      keyBinding - the KeyMapping to query
      Returns:
      an Optional containing the action, or empty if no action is registered