Package dev.munebase.dynamickeybinds
Class DynamicKeyRegistryImpl
java.lang.Object
dev.munebase.dynamickeybinds.DynamicKeyRegistryImpl
- All Implemented Interfaces:
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionCollection<net.minecraft.client.KeyMapping>Retrieve all currently registered dynamic keybinds.getDisplaySpec(String id) Get the display metadata for a registered keybind.getKeyBindAction(net.minecraft.client.KeyMapping keyBinding) Retrieve the action associated with a keybind.net.minecraft.client.KeyMappinggetKeyBindById(String id) Retrieve a specific keybind by its ID.getKeyBindId(net.minecraft.client.KeyMapping keyBinding) Resolve the stable keybind ID for a registered mapping.net.minecraft.client.KeyMappingregisterDynamicKey(String id, int keyCode, String category, Optional<DynamicKeybindAction> action) Register a new dynamic keybind at runtime.net.minecraft.client.KeyMappingregisterDynamicKey(String id, int keyCode, String category, Optional<DynamicKeybindAction> action, DisplaySpec displaySpec) Register a new dynamic keybind at runtime with custom display metadata.voidunregisterDynamicKey(net.minecraft.client.KeyMapping keyBinding) Unregister an existing dynamic keybind.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface dev.munebase.dynamickeybinds.DynamicKeyRegistry
unregisterDynamicKey
-
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:
registerDynamicKeyin interfaceDynamicKeyRegistry- 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
-
registerDynamicKey
public net.minecraft.client.KeyMapping registerDynamicKey(String id, int keyCode, String category, Optional<DynamicKeybindAction> action, DisplaySpec displaySpec) Register a new dynamic keybind at runtime with custom display metadata.- Specified by:
registerDynamicKeyin interfaceDynamicKeyRegistry- 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 presseddisplaySpec- custom display metadata for the keybind label- 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:
unregisterDynamicKeyin interfaceDynamicKeyRegistry- Parameters:
keyBinding- the KeyMapping to unregister
-
getAllDynamicKeys
Retrieve all currently registered dynamic keybinds.- Specified by:
getAllDynamicKeysin interfaceDynamicKeyRegistry- Returns:
- immutable collection of all dynamic KeyMappings
-
getKeyBindById
Retrieve a specific keybind by its ID.- Specified by:
getKeyBindByIdin interfaceDynamicKeyRegistry- Parameters:
id- the unique identifier of the keybind- Returns:
- the KeyMapping if found, or null if not registered
-
getKeyBindAction
Retrieve the action associated with a keybind.- Specified by:
getKeyBindActionin interfaceDynamicKeyRegistry- Parameters:
keyBinding- the KeyMapping to query- Returns:
- an Optional containing the action, or empty if no action is registered
-
getKeyBindId
Description copied from interface:DynamicKeyRegistryResolve the stable keybind ID for a registered mapping.- Specified by:
getKeyBindIdin interfaceDynamicKeyRegistry- Parameters:
keyBinding- the key mapping instance- Returns:
- keybind ID, or empty if mapping is unknown to this registry
-
getDisplaySpec
Get the display metadata for a registered keybind.- Parameters:
id- the keybind ID- Returns:
- the DisplaySpec if registered, or an empty one if not found
-