Skip to content

Commit

Permalink
Finish the v1 feature set
Browse files Browse the repository at this point in the history
  • Loading branch information
EnnuiL committed Feb 2, 2022
1 parent 9cdb892 commit f01a4b1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.quiltmc.qsl.key.bindings.api;

import java.util.Map;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;

Expand Down Expand Up @@ -115,13 +117,19 @@ public static boolean isEnabled(KeyBinding key) {
*
* @param key the key binding
* @param enabled the new state
* @return {@code true} if the state change was successful, else {@code false}
* @throws IllegalArgumentException if {@code key} is either unregistered or a Vanilla key bind
*/
public static boolean setEnabled(KeyBinding key, boolean enabled) {
return KeyBindingRegistryImpl.setEnabled(key, enabled);
public static void setEnabled(KeyBinding key, boolean enabled) {
KeyBindingRegistryImpl.setEnabled(key, enabled);
}

public static void getAllKeyBindings(boolean includeVanilla) {
// TODO - Make me into a real method
/**
* Returns a map containing all modded key bindings (and vanilla ones if specified).
*
* @param includeVanilla {@code true} if vanilla entries should be included, else {@code false}
* @return a map containing all modded (and optionally vanilla) key bindings
*/
public static Map<KeyBinding, Boolean> getAllKeyBindings(boolean includeVanilla) {
return KeyBindingRegistryImpl.getAllKeyBindings(includeVanilla);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,26 @@ public static KeyBinding getKeyBinding(String translationKey) {
return null;
}

public static boolean throwUnregisteredKeyException(KeyBinding key) {
if (keyBindingManager != null && Arrays.asList(keyBindingManager.getAllKeys()).contains(key)) {
throw new IllegalArgumentException(String.format("%s is a vanilla key and therefore doesn't have an active state!", key.getTranslationKey()));
}

throw new IllegalArgumentException(String.format("%s isn't a registered key!", key.getTranslationKey()));
}

public static boolean isEnabled(KeyBinding key) {
if (quiltKeys.containsKey(key)) {
return quiltKeys.get(key);
}

if (keyBindingManager != null && Arrays.asList(keyBindingManager.getAllKeys()).contains(key)) {
throw new IllegalArgumentException(String.format("%s is a vanilla key and therefore doesn't have an active state!", key.getTranslationKey()));
}

throw new IllegalArgumentException(String.format("%s hasn't been registered!", key.getTranslationKey()));
return throwUnregisteredKeyException(key);
}

public static boolean setEnabled(KeyBinding key, boolean enabled) {
public static void setEnabled(KeyBinding key, boolean enabled) {
if (quiltKeys.containsKey(key)) {
quiltKeys.replace(key, enabled);

applyChanges();
if (enabled) {
KeyBindingAccessor.getKeysById().put(key.getTranslationKey(), key);
Expand All @@ -96,14 +101,26 @@ public static boolean setEnabled(KeyBinding key, boolean enabled) {

((KeyBindingAccessor) key).callReset();
KeyBinding.updateKeysByCode();
}

throwUnregisteredKeyException(key);
}

return true;
public static Map<KeyBinding, Boolean> getAllKeyBindings(boolean includeVanilla) {
Map<KeyBinding, Boolean> allKeys = new HashMap<>();

if (includeVanilla) {
for (int i = 0; i < keyBindingManager.getAllKeys().length; i++) {
allKeys.put(keyBindingManager.getAllKeys()[i], false);
}
}

return false;
allKeys.putAll(quiltKeys);

return allKeys;
}

public static void updateKeysArray() {
public static void applyChanges() {
List<KeyBinding> enabledQuiltKeys = new ArrayList<>();
disabledQuiltKeys.clear();
for (var entry : quiltKeys.entrySet()) {
Expand All @@ -115,6 +132,10 @@ public static void updateKeysArray() {
}

enabledQuiltKeysArray = enabledQuiltKeys.toArray(new KeyBinding[enabledQuiltKeys.size()]);

if (keyBindingManager != null) {
keyBindingManager.addModdedKeyBinds();
}
}

public static KeyBinding[] getKeyBindings() {
Expand All @@ -128,11 +149,4 @@ public static List<KeyBinding> getDisabledKeyBindings() {
public static void setKeyBindingManager(KeyBindingManager manager) {
keyBindingManager = manager;
}

public static void applyChanges() {
updateKeysArray();
if (keyBindingManager != null) {
keyBindingManager.addModdedKeyBinds();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

@Environment(EnvType.CLIENT)
public class KeyBindingRegistryTestMod implements ClientModInitializer {
public static final Logger LOGGER = LogManager.getLogger("KeyBindingRegistryTest");
public static final Logger LOGGER = LogManager.getFormatterLogger("KeyBindingRegistryTest");

@Override
public void onInitializeClient() {
Expand All @@ -30,6 +30,11 @@ public void onInitializeClient() {
}
});
}

LOGGER.info("The registry has the following keys registered:");
KeyBindingRegistry.getAllKeyBindings(true).forEach((key, value) -> {
LOGGER.info("%s: %s", key.getTranslationKey(), value);
});
});
}
}

0 comments on commit f01a4b1

Please sign in to comment.