Skip to content

Commit

Permalink
Keybindings API: New Years cleanup (#2799)
Browse files Browse the repository at this point in the history
* Keybindings API: New Years cleanup

* Remove throws
  • Loading branch information
apple502j authored Jan 2, 2023
1 parent e498f5f commit bc01e09
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,24 @@

package net.fabricmc.fabric.api.client.keybinding.v1;

import java.util.Objects;

import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;

import net.fabricmc.fabric.impl.client.keybinding.KeyBindingRegistryImpl;
import net.fabricmc.fabric.mixin.client.keybinding.KeyBindingAccessor;

/**
* Helper for registering key bindings.
*
* <p>Helper class for {@link KeyBinding} for use by Fabric mods.</p>
* Helper for registering {@link KeyBinding}s.
*
* <pre>{@code
* KeyBinding left = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.example.left", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_P, "key.category.example"));
* KeyBinding right = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.example.right", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_U, "key.category.example"));
* }</pre>
*
* @see KeyBinding
* @see net.minecraft.client.option.StickyKeyBinding
*/
public final class KeyBindingHelper {
private KeyBindingHelper() {
Expand All @@ -41,8 +44,10 @@ private KeyBindingHelper() {
*
* @param keyBinding the keybinding
* @return the keybinding itself
* @throws IllegalArgumentException when a key binding with the same ID is already registered
*/
public static KeyBinding registerKeyBinding(KeyBinding keyBinding) {
Objects.requireNonNull(keyBinding, "key binding cannot be null");
return KeyBindingRegistryImpl.registerKeyBinding(keyBinding);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import net.fabricmc.fabric.mixin.client.keybinding.KeyBindingAccessor;

public final class KeyBindingRegistryImpl {
private static final List<KeyBinding> moddedKeyBindings = new ReferenceArrayList<>(); // ArrayList with identity based comparisons for contains/remove/indexOf etc., required for correctly handling duplicate keybinds
private static final List<KeyBinding> MODDED_KEY_BINDINGS = new ReferenceArrayList<>(); // ArrayList with identity based comparisons for contains/remove/indexOf etc., required for correctly handling duplicate keybinds

private KeyBindingRegistryImpl() {
}
Expand All @@ -37,10 +37,6 @@ private static Map<String, Integer> getCategoryMap() {
return KeyBindingAccessor.fabric_getCategoryMap();
}

private static boolean hasCategory(String categoryTranslationKey) {
return getCategoryMap().containsKey(categoryTranslationKey);
}

public static boolean addCategory(String categoryTranslationKey) {
Map<String, Integer> map = getCategoryMap();

Expand All @@ -55,19 +51,18 @@ public static boolean addCategory(String categoryTranslationKey) {
}

public static KeyBinding registerKeyBinding(KeyBinding binding) {
for (KeyBinding existingKeyBindings : moddedKeyBindings) {
for (KeyBinding existingKeyBindings : MODDED_KEY_BINDINGS) {
if (existingKeyBindings == binding) {
throw null;
throw new IllegalArgumentException("Attempted to register a key binding twice: " + binding.getTranslationKey());
} else if (existingKeyBindings.getTranslationKey().equals(binding.getTranslationKey())) {
throw new RuntimeException("Attempted to register two key bindings with equal ID: " + binding.getTranslationKey() + "!");
throw new IllegalArgumentException("Attempted to register two key bindings with equal ID: " + binding.getTranslationKey() + "!");
}
}

if (!hasCategory(binding.getCategory())) {
addCategory(binding.getCategory());
}

return moddedKeyBindings.add(binding) ? binding : null;
// This will do nothing if the category already exists.
addCategory(binding.getCategory());
MODDED_KEY_BINDINGS.add(binding);
return binding;
}

/**
Expand All @@ -76,8 +71,8 @@ public static KeyBinding registerKeyBinding(KeyBinding binding) {
*/
public static KeyBinding[] process(KeyBinding[] keysAll) {
List<KeyBinding> newKeysAll = Lists.newArrayList(keysAll);
newKeysAll.removeAll(moddedKeyBindings);
newKeysAll.addAll(moddedKeyBindings);
newKeysAll.removeAll(MODDED_KEY_BINDINGS);
newKeysAll.addAll(MODDED_KEY_BINDINGS);
return newKeysAll.toArray(new KeyBinding[0]);
}
}

0 comments on commit bc01e09

Please sign in to comment.