Keybindings API: New Years cleanup (#2799)

* Keybindings API: New Years cleanup

* Remove throws

(cherry picked from commit bc01e09759)
This commit is contained in:
apple502j 2023-01-02 22:04:48 +09:00 committed by modmuss50
parent 9785ec356d
commit 5c4fce2850
2 changed files with 18 additions and 18 deletions

View file

@ -16,6 +16,8 @@
package net.fabricmc.fabric.api.client.keybinding.v1;
import java.util.Objects;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
@ -23,14 +25,15 @@ 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() {
@ -41,8 +44,10 @@ public final class 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);
}

View file

@ -28,7 +28,7 @@ import net.minecraft.client.option.KeyBinding;
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() {
}
@ -37,10 +37,6 @@ public final class KeyBindingRegistryImpl {
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();
@ -55,19 +51,18 @@ public final class KeyBindingRegistryImpl {
}
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;
}
/**
@ -76,8 +71,8 @@ public final class KeyBindingRegistryImpl {
*/
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]);
}
}