From fb288875d589329ce6ccf74affd07bfe45587dde Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Sun, 28 Nov 2021 11:26:57 -0300 Subject: [PATCH 01/56] Begin Key Bindings API Oh, this is definitely not ready yet, I just needed an easy way to sync progress between computers --- build-logic/src/main/groovy/qsl.common.gradle | 3 ++ library/gui/key_bindings/build.gradle | 9 +++++ .../bindings/impl/KeyBindingRegistryImpl.java | 21 ++++++++++ .../mixin/client/GameOptionsMixin.java | 36 ++++++++++++++++++ .../assets/quilt_key_bindings/icon.png | Bin 0 -> 261 bytes .../src/main/resources/fabric.mod.json | 24 ++++++++++++ .../resources/quilt_key_bindings.mixins.json | 11 ++++++ .../test/client/KeyBindingsTestMod.java | 26 +++++++++++++ .../src/testmod/resources/fabric.mod.json | 16 ++++++++ 9 files changed, 146 insertions(+) create mode 100644 library/gui/key_bindings/build.gradle create mode 100644 library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java create mode 100644 library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java create mode 100644 library/gui/key_bindings/src/main/resources/assets/quilt_key_bindings/icon.png create mode 100644 library/gui/key_bindings/src/main/resources/fabric.mod.json create mode 100644 library/gui/key_bindings/src/main/resources/quilt_key_bindings.mixins.json create mode 100644 library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java create mode 100644 library/gui/key_bindings/src/testmod/resources/fabric.mod.json diff --git a/build-logic/src/main/groovy/qsl.common.gradle b/build-logic/src/main/groovy/qsl.common.gradle index b5df571b20..99b30e5122 100644 --- a/build-logic/src/main/groovy/qsl.common.gradle +++ b/build-logic/src/main/groovy/qsl.common.gradle @@ -48,6 +48,9 @@ dependencies { modImplementation "org.quiltmc:quilt-loader:${Versions.LOADER_VERSION}" } +sourceCompatibility = JavaVersion.VERSION_17 +targetCompatibility = JavaVersion.VERSION_17 + tasks.withType(JavaCompile).configureEach { it.options.encoding = "UTF-8" it.options.release.set(Versions.JAVA_VERSION) diff --git a/library/gui/key_bindings/build.gradle b/library/gui/key_bindings/build.gradle new file mode 100644 index 0000000000..0cf0dfe6f5 --- /dev/null +++ b/library/gui/key_bindings/build.gradle @@ -0,0 +1,9 @@ +plugins { + id("qsl.module") +} + +qslModule { + moduleName = "key_bindings" + version = "1.0.0" + library = "gui" +} diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java new file mode 100644 index 0000000000..0c4be1aa3a --- /dev/null +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java @@ -0,0 +1,21 @@ +package org.quiltmc.qsl.key.bindings.impl; + +import org.apache.commons.lang3.ArrayUtils; +import org.lwjgl.glfw.GLFW; + +import net.minecraft.client.option.KeyBinding; + +public class KeyBindingRegistryImpl { + private static KeyBinding[] allVanillaKeys = new KeyBinding[] {}; + private static KeyBinding[] keysArray = new KeyBinding[] { + new KeyBinding("key.hello_world", GLFW.GLFW_KEY_H, "key.categories.misc") + }; + + public static void setVanillaKeys(KeyBinding[] allKeys) { + allVanillaKeys = allKeys; + } + + public static KeyBinding[] getKeysArray() { + return ArrayUtils.addAll(allVanillaKeys, keysArray); + } +} diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java new file mode 100644 index 0000000000..244ae950ca --- /dev/null +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java @@ -0,0 +1,36 @@ +package org.quiltmc.qsl.key.bindings.mixin.client; + +import java.io.File; + +import org.quiltmc.qsl.key.bindings.impl.KeyBindingRegistryImpl; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Mutable; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.option.GameOptions; +import net.minecraft.client.option.KeyBinding; + +@Mixin(GameOptions.class) +public class GameOptionsMixin { + @Mutable + @Shadow + @Final + public KeyBinding[] keysAll; + + @Inject( + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/client/option/GameOptions;load()V" + ), + method = "" + ) + private void modifyAllKeys(MinecraftClient client, File file, CallbackInfo ci) { + KeyBindingRegistryImpl.setVanillaKeys(this.keysAll); + this.keysAll = KeyBindingRegistryImpl.getKeysArray(); + } +} diff --git a/library/gui/key_bindings/src/main/resources/assets/quilt_key_bindings/icon.png b/library/gui/key_bindings/src/main/resources/assets/quilt_key_bindings/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..8d54ad0021bbf0258acfe2ba452ccc0e6f36f566 GIT binary patch literal 261 zcmV+g0s8)lP)O|2F_@TM=AcYyV#WxOBw)fQtW(8;_SF|EJmfx^Vx~qyO1! z+uE1g+$r5#A>8-?004nWL_t(I%jJ{V3V=0.12" + }, + "description": "Key bindings registration.", + "mixins": [ + "quilt_key_bindings.mixins.json" + ] +} diff --git a/library/gui/key_bindings/src/main/resources/quilt_key_bindings.mixins.json b/library/gui/key_bindings/src/main/resources/quilt_key_bindings.mixins.json new file mode 100644 index 0000000000..25fc9d6ce1 --- /dev/null +++ b/library/gui/key_bindings/src/main/resources/quilt_key_bindings.mixins.json @@ -0,0 +1,11 @@ +{ + "required": true, + "package": "org.quiltmc.qsl.key.bindings.mixin", + "compatibilityLevel": "JAVA_17", + "client": [ + "client.GameOptionsMixin" + ], + "injectors": { + "defaultRequire": 1 + } +} diff --git a/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java b/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java new file mode 100644 index 0000000000..b65e1de92c --- /dev/null +++ b/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java @@ -0,0 +1,26 @@ +/* + * Copyright 2021 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.quiltmc.qsl.key.bindings.test.client; + +import net.fabricmc.api.ClientModInitializer; + +public class KeyBindingsTestMod implements ClientModInitializer { + @Override + public void onInitializeClient() { + System.out.println("It's working! I can't believe it!"); + } +} diff --git a/library/gui/key_bindings/src/testmod/resources/fabric.mod.json b/library/gui/key_bindings/src/testmod/resources/fabric.mod.json new file mode 100644 index 0000000000..65d4c6174e --- /dev/null +++ b/library/gui/key_bindings/src/testmod/resources/fabric.mod.json @@ -0,0 +1,16 @@ +{ + "schemaVersion": 1, + "id": "quilt_key_bindings_testmod", + "name": "Quilt Key Bindings API Test Mod", + "version": "1.0.0", + "environment": "*", + "license": "Apache-2.0", + "depends": { + "quilt_key_bindings": "*" + }, + "entrypoints": { + "client": [ + "org.quiltmc.qsl.key.bindings.test.client.KeyBindingsTestMod" + ] + } +} From f3564b5c0297bfeedf1ad5d5740ca09f3a939a88 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Mon, 29 Nov 2021 22:10:22 -0300 Subject: [PATCH 02/56] Begin dynamic key binding system It's alive! --- library/gui/key_bindings/build.gradle | 4 ++ .../bindings/impl/KeyBindingRegistryImpl.java | 43 ++++++++++++++++--- .../mixin/client/GameOptionsAccessor.java | 13 ++++++ .../mixin/client/GameOptionsMixin.java | 6 ++- .../resources/quilt_key_bindings.mixins.json | 1 + .../test/client/KeyBindingsTestMod.java | 30 ++++++++++++- .../lang/en_us.json | 4 ++ 7 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsAccessor.java create mode 100644 library/gui/key_bindings/src/testmod/resources/assets/quilt_key_bindings_testmod/lang/en_us.json diff --git a/library/gui/key_bindings/build.gradle b/library/gui/key_bindings/build.gradle index 0cf0dfe6f5..06f1e92e84 100644 --- a/library/gui/key_bindings/build.gradle +++ b/library/gui/key_bindings/build.gradle @@ -6,4 +6,8 @@ qslModule { moduleName = "key_bindings" version = "1.0.0" library = "gui" + coreTestmodDependencies([ + "qsl_base", + "lifecycle_events" + ]) } diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java index 0c4be1aa3a..5aa7a659a7 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java @@ -1,21 +1,52 @@ package org.quiltmc.qsl.key.bindings.impl; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import org.apache.commons.lang3.ArrayUtils; -import org.lwjgl.glfw.GLFW; +import org.quiltmc.qsl.key.bindings.mixin.client.GameOptionsAccessor; +import net.minecraft.client.MinecraftClient; import net.minecraft.client.option.KeyBinding; public class KeyBindingRegistryImpl { + private static Map quiltKeys = new HashMap<>(); private static KeyBinding[] allVanillaKeys = new KeyBinding[] {}; - private static KeyBinding[] keysArray = new KeyBinding[] { - new KeyBinding("key.hello_world", GLFW.GLFW_KEY_H, "key.categories.misc") - }; + + public static KeyBinding registerKeyBinding(KeyBinding key, boolean enabled) { + quiltKeys.put(key, enabled); + applyChanges(); + return key; + } + + public static void setActive(KeyBinding key, boolean newEnabled) { + quiltKeys.replace(key, newEnabled); + applyChanges(); + } public static void setVanillaKeys(KeyBinding[] allKeys) { allVanillaKeys = allKeys; } - public static KeyBinding[] getKeysArray() { - return ArrayUtils.addAll(allVanillaKeys, keysArray); + public static KeyBinding[] updateKeysArray() { + List enabledQuiltKeys = new ArrayList<>(); + for (var entry : quiltKeys.entrySet()) { + if (entry.getValue()) { + enabledQuiltKeys.add(entry.getKey()); + } + } + + KeyBinding[] quiltKeysArray = enabledQuiltKeys.toArray(new KeyBinding[enabledQuiltKeys.size()]); + + return ArrayUtils.addAll(allVanillaKeys, quiltKeysArray); + } + + public static void applyChanges() { + MinecraftClient client = MinecraftClient.getInstance(); + if (client.options != null) { + ((GameOptionsAccessor)(Object)client.options).setKeysAll(updateKeysArray()); + } } } diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsAccessor.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsAccessor.java new file mode 100644 index 0000000000..ad29e4f304 --- /dev/null +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsAccessor.java @@ -0,0 +1,13 @@ +package org.quiltmc.qsl.key.bindings.mixin.client; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +import net.minecraft.client.option.GameOptions; +import net.minecraft.client.option.KeyBinding; + +@Mixin(GameOptions.class) +public interface GameOptionsAccessor { + @Accessor + void setKeysAll(KeyBinding[] keysAll); +} diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java index 244ae950ca..f8a921f297 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java @@ -7,6 +7,7 @@ import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mutable; import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @@ -17,6 +18,7 @@ @Mixin(GameOptions.class) public class GameOptionsMixin { + // While there is an injection on the constructor, this Mutable is necessary @Mutable @Shadow @Final @@ -30,7 +32,7 @@ public class GameOptionsMixin { method = "" ) private void modifyAllKeys(MinecraftClient client, File file, CallbackInfo ci) { - KeyBindingRegistryImpl.setVanillaKeys(this.keysAll); - this.keysAll = KeyBindingRegistryImpl.getKeysArray(); + KeyBindingRegistryImpl.setVanillaKeys(this.keysAll.clone()); + this.keysAll = KeyBindingRegistryImpl.updateKeysArray(); } } diff --git a/library/gui/key_bindings/src/main/resources/quilt_key_bindings.mixins.json b/library/gui/key_bindings/src/main/resources/quilt_key_bindings.mixins.json index 25fc9d6ce1..fbaa11d3e7 100644 --- a/library/gui/key_bindings/src/main/resources/quilt_key_bindings.mixins.json +++ b/library/gui/key_bindings/src/main/resources/quilt_key_bindings.mixins.json @@ -3,6 +3,7 @@ "package": "org.quiltmc.qsl.key.bindings.mixin", "compatibilityLevel": "JAVA_17", "client": [ + "client.GameOptionsAccessor", "client.GameOptionsMixin" ], "injectors": { diff --git a/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java b/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java index b65e1de92c..b0c496c213 100644 --- a/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java +++ b/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java @@ -16,11 +16,39 @@ package org.quiltmc.qsl.key.bindings.test.client; +import org.lwjgl.glfw.GLFW; +import org.quiltmc.qsl.key.bindings.impl.KeyBindingRegistryImpl; +import org.quiltmc.qsl.lifecycle.api.client.event.ClientTickEvents; + import net.fabricmc.api.ClientModInitializer; +import net.minecraft.client.option.KeyBinding; +import net.minecraft.text.LiteralText; public class KeyBindingsTestMod implements ClientModInitializer { + public static final KeyBinding DISABLE_KEY_BIND = KeyBindingRegistryImpl.registerKeyBinding( + new KeyBinding("key.qsl.disable_key_bind", GLFW.GLFW_KEY_H, "key.categories.misc"), true + ); + + public static final KeyBinding ENABLE_KEY_BIND = KeyBindingRegistryImpl.registerKeyBinding( + new KeyBinding("key.qsl.enable_key_bind", GLFW.GLFW_KEY_I, "key.categories.misc"), true + ); + @Override public void onInitializeClient() { - System.out.println("It's working! I can't believe it!"); + ClientTickEvents.START.register(client -> { + if (DISABLE_KEY_BIND.isPressed()) { + if (client.player != null) { + client.player.sendMessage(new LiteralText("The key has disappeared! Bye bye, key!"), true); + } + KeyBindingRegistryImpl.setActive(DISABLE_KEY_BIND, false); + } + + if (ENABLE_KEY_BIND.isPressed()) { + if (client.player != null) { + client.player.sendMessage(new LiteralText("The key is back!"), true); + } + KeyBindingRegistryImpl.setActive(DISABLE_KEY_BIND, true); + } + }); } } diff --git a/library/gui/key_bindings/src/testmod/resources/assets/quilt_key_bindings_testmod/lang/en_us.json b/library/gui/key_bindings/src/testmod/resources/assets/quilt_key_bindings_testmod/lang/en_us.json new file mode 100644 index 0000000000..b493057288 --- /dev/null +++ b/library/gui/key_bindings/src/testmod/resources/assets/quilt_key_bindings_testmod/lang/en_us.json @@ -0,0 +1,4 @@ +{ + "key.qsl.disable_key_bind": "Disable Key Bind", + "key.qsl.enable_key_bind": "Enable Key Bind" +} \ No newline at end of file From c727b5a8aafe60ffa2285ac1a9faa08375717ce6 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Tue, 30 Nov 2021 20:33:01 -0300 Subject: [PATCH 03/56] Further progress on Key Binding API --- .../key/bindings/api/KeyBindingRegistry.java | 24 ++++++++ .../key/bindings/impl/KeyBindingManager.java | 22 +++++++ .../bindings/impl/KeyBindingRegistryImpl.java | 59 ++++++++++++++----- .../mixin/client/GameOptionsAccessor.java | 2 + .../mixin/client/GameOptionsMixin.java | 14 ++--- .../mixin/client/KeyBindingAccessor.java | 25 ++++++++ .../mixin/client/KeyBindingMixin.java | 30 ++++++++++ .../resources/quilt_key_bindings.mixins.json | 4 +- .../test/client/KeyBindingsTestMod.java | 25 +++++--- .../lang/en_us.json | 4 +- 10 files changed, 178 insertions(+), 31 deletions(-) create mode 100644 library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java create mode 100644 library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java create mode 100644 library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingAccessor.java create mode 100644 library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingMixin.java diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java new file mode 100644 index 0000000000..b9522da8b7 --- /dev/null +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java @@ -0,0 +1,24 @@ +package org.quiltmc.qsl.key.bindings.api; + +import org.quiltmc.qsl.key.bindings.impl.KeyBindingRegistryImpl; + +import net.minecraft.client.option.KeyBinding; + +public class KeyBindingRegistry { + public static KeyBinding registerKeyBinding(KeyBinding key) { + return registerKeyBinding(key, true); + } + + public static KeyBinding registerKeyBinding(KeyBinding key, boolean enabled) { + return KeyBindingRegistryImpl.registerKeyBinding(key, enabled); + } + + // TODO - Add get method for easier intercompat + + public static void setEnabled(KeyBinding key, boolean newEnabled) { + KeyBindingRegistryImpl.setEnabled(key, newEnabled); + } + + // TODO - Add getEnabled method + +} diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java new file mode 100644 index 0000000000..2d77e8260b --- /dev/null +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java @@ -0,0 +1,22 @@ +package org.quiltmc.qsl.key.bindings.impl; + +import org.quiltmc.qsl.key.bindings.mixin.client.GameOptionsAccessor; + +import net.minecraft.client.option.GameOptions; +import net.minecraft.client.option.KeyBinding; + +public class KeyBindingManager { + private final GameOptions options; + private final KeyBinding[] allKeys; + + public KeyBindingManager(GameOptions options, KeyBinding[] allKeys) { + this.options = options; + this.allKeys = allKeys; + this.addModdedKeyBinds(); + KeyBindingRegistryImpl.registerKeyBindingManager(this); + } + + public void addModdedKeyBinds() { + ((GameOptionsAccessor)(Object)this.options).setKeysAll(KeyBindingRegistryImpl.getKeyBindings(this.allKeys)); + } +} diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java index 5aa7a659a7..d7ad6e0047 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java @@ -6,47 +6,76 @@ import java.util.Map; import org.apache.commons.lang3.ArrayUtils; -import org.quiltmc.qsl.key.bindings.mixin.client.GameOptionsAccessor; +import org.quiltmc.qsl.key.bindings.mixin.client.KeyBindingAccessor; -import net.minecraft.client.MinecraftClient; import net.minecraft.client.option.KeyBinding; public class KeyBindingRegistryImpl { private static Map quiltKeys = new HashMap<>(); - private static KeyBinding[] allVanillaKeys = new KeyBinding[] {}; + private static List keyBindingManagers = new ArrayList<>(); + private static KeyBinding[] enabledQuiltKeysArray = new KeyBinding[] {}; + private static KeyBinding[] totalQuiltKeysArray = new KeyBinding[] {}; public static KeyBinding registerKeyBinding(KeyBinding key, boolean enabled) { quiltKeys.put(key, enabled); - applyChanges(); + applyChanges(true); + if (!enabled) { + KeyBindingAccessor.getKeysById().remove(key.getTranslationKey()); + } return key; } - public static void setActive(KeyBinding key, boolean newEnabled) { - quiltKeys.replace(key, newEnabled); - applyChanges(); + public static void setEnabled(KeyBinding key, boolean newEnabled) { + if (quiltKeys.containsKey(key)) { + quiltKeys.replace(key, newEnabled); + applyChanges(false); + if (newEnabled) { + KeyBindingAccessor.getKeysById().put(key.getTranslationKey(), key); + } else { + KeyBindingAccessor.getKeysById().remove(key.getTranslationKey(), key); + } + ((KeyBindingAccessor)key).callReset(); + KeyBinding.updateKeysByCode(); + } } - public static void setVanillaKeys(KeyBinding[] allKeys) { - allVanillaKeys = allKeys; + protected static void registerKeyBindingManager(KeyBindingManager manager) { + keyBindingManagers.add(manager); } - public static KeyBinding[] updateKeysArray() { + public static void updateKeysArray(boolean updateTotal) { List enabledQuiltKeys = new ArrayList<>(); + List totalQuiltKeys = new ArrayList<>(); for (var entry : quiltKeys.entrySet()) { if (entry.getValue()) { enabledQuiltKeys.add(entry.getKey()); } + + if (updateTotal) { + totalQuiltKeys.add(entry.getKey()); + } } KeyBinding[] quiltKeysArray = enabledQuiltKeys.toArray(new KeyBinding[enabledQuiltKeys.size()]); - return ArrayUtils.addAll(allVanillaKeys, quiltKeysArray); + enabledQuiltKeysArray = quiltKeysArray; + if (updateTotal) { + totalQuiltKeysArray = totalQuiltKeys.toArray(new KeyBinding[totalQuiltKeys.size()]); + } + } + + public static KeyBinding[] getKeyBindings(KeyBinding[] allVanillaKeys) { + return ArrayUtils.addAll(allVanillaKeys, enabledQuiltKeysArray); + } + + public static KeyBinding[] getAllKeyBindings(KeyBinding[] allVanillaKeys) { + return ArrayUtils.addAll(allVanillaKeys, totalQuiltKeysArray); } - public static void applyChanges() { - MinecraftClient client = MinecraftClient.getInstance(); - if (client.options != null) { - ((GameOptionsAccessor)(Object)client.options).setKeysAll(updateKeysArray()); + public static void applyChanges(boolean updateTotal) { + updateKeysArray(updateTotal); + for (KeyBindingManager manager : keyBindingManagers) { + manager.addModdedKeyBinds(); } } } diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsAccessor.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsAccessor.java index ad29e4f304..77e9336819 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsAccessor.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsAccessor.java @@ -1,6 +1,7 @@ package org.quiltmc.qsl.key.bindings.mixin.client; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Mutable; import org.spongepowered.asm.mixin.gen.Accessor; import net.minecraft.client.option.GameOptions; @@ -8,6 +9,7 @@ @Mixin(GameOptions.class) public interface GameOptionsAccessor { + @Mutable @Accessor void setKeysAll(KeyBinding[] keysAll); } diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java index f8a921f297..0b8c256ff2 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java @@ -2,10 +2,9 @@ import java.io.File; -import org.quiltmc.qsl.key.bindings.impl.KeyBindingRegistryImpl; +import org.quiltmc.qsl.key.bindings.impl.KeyBindingManager; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Mutable; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; @@ -17,13 +16,15 @@ import net.minecraft.client.option.KeyBinding; @Mixin(GameOptions.class) -public class GameOptionsMixin { - // While there is an injection on the constructor, this Mutable is necessary - @Mutable +public abstract class GameOptionsMixin { @Shadow @Final public KeyBinding[] keysAll; + // TODO - This variable might be unnecessary. Remove it + @Unique + public KeyBindingManager quilt$keyBindingManager; + @Inject( at = @At( value = "INVOKE", @@ -32,7 +33,6 @@ public class GameOptionsMixin { method = "" ) private void modifyAllKeys(MinecraftClient client, File file, CallbackInfo ci) { - KeyBindingRegistryImpl.setVanillaKeys(this.keysAll.clone()); - this.keysAll = KeyBindingRegistryImpl.updateKeysArray(); + this.quilt$keyBindingManager = new KeyBindingManager((GameOptions)(Object)this, keysAll); } } diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingAccessor.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingAccessor.java new file mode 100644 index 0000000000..8791a31909 --- /dev/null +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingAccessor.java @@ -0,0 +1,25 @@ +package org.quiltmc.qsl.key.bindings.mixin.client; + +import java.util.Map; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; +import org.spongepowered.asm.mixin.gen.Invoker; + +import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.util.InputUtil; + +@Mixin(KeyBinding.class) +public interface KeyBindingAccessor { + @Accessor(value = "KEYS_BY_ID") + static Map getKeysById() { return null; } + + @Accessor(value = "KEY_TO_BINDINGS") + static Map getKeyToBindings() { return null; } + + @Accessor + InputUtil.Key getBoundKey(); + + @Invoker + void callReset(); +} diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingMixin.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingMixin.java new file mode 100644 index 0000000000..9d903d91b2 --- /dev/null +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingMixin.java @@ -0,0 +1,30 @@ +package org.quiltmc.qsl.key.bindings.mixin.client; + +import java.util.Map; + +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Mutable; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.util.InputUtil; + +@Mixin(KeyBinding.class) +public abstract class KeyBindingMixin { + @Mutable + @Final + private static Map CATEGORY_ORDER_MAP; + + @Inject( + at = @At("TAIL"), + method = "(Ljava/lang/String;Lnet/minecraft/client/util/InputUtil$Type;ILjava/lang/String;)V" + ) + private void addModdedCategory(String string, InputUtil.Type type, int i, String string2, CallbackInfo ci) { + if (!CATEGORY_ORDER_MAP.containsKey(string2)) { + CATEGORY_ORDER_MAP.put(string2, CATEGORY_ORDER_MAP.size() + 1); + } + } +} diff --git a/library/gui/key_bindings/src/main/resources/quilt_key_bindings.mixins.json b/library/gui/key_bindings/src/main/resources/quilt_key_bindings.mixins.json index fbaa11d3e7..9298500cf7 100644 --- a/library/gui/key_bindings/src/main/resources/quilt_key_bindings.mixins.json +++ b/library/gui/key_bindings/src/main/resources/quilt_key_bindings.mixins.json @@ -4,7 +4,9 @@ "compatibilityLevel": "JAVA_17", "client": [ "client.GameOptionsAccessor", - "client.GameOptionsMixin" + "client.GameOptionsMixin", + "client.KeyBindingAccessor", + "client.KeyBindingMixin" ], "injectors": { "defaultRequire": 1 diff --git a/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java b/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java index b0c496c213..357c8f11c6 100644 --- a/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java +++ b/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java @@ -17,7 +17,7 @@ package org.quiltmc.qsl.key.bindings.test.client; import org.lwjgl.glfw.GLFW; -import org.quiltmc.qsl.key.bindings.impl.KeyBindingRegistryImpl; +import org.quiltmc.qsl.key.bindings.api.KeyBindingRegistry; import org.quiltmc.qsl.lifecycle.api.client.event.ClientTickEvents; import net.fabricmc.api.ClientModInitializer; @@ -25,12 +25,17 @@ import net.minecraft.text.LiteralText; public class KeyBindingsTestMod implements ClientModInitializer { - public static final KeyBinding DISABLE_KEY_BIND = KeyBindingRegistryImpl.registerKeyBinding( - new KeyBinding("key.qsl.disable_key_bind", GLFW.GLFW_KEY_H, "key.categories.misc"), true + public static final KeyBinding DISABLE_KEY_BIND = KeyBindingRegistry.registerKeyBinding( + new KeyBinding("key.qsl.disable_key_bind", GLFW.GLFW_KEY_H, "key.qsl.category"), true ); - public static final KeyBinding ENABLE_KEY_BIND = KeyBindingRegistryImpl.registerKeyBinding( - new KeyBinding("key.qsl.enable_key_bind", GLFW.GLFW_KEY_I, "key.categories.misc"), true + public static final KeyBinding ENABLE_KEY_BIND = KeyBindingRegistry.registerKeyBinding( + new KeyBinding("key.qsl.enable_key_bind", GLFW.GLFW_KEY_I, "key.qsl.category"), true + ); + + // A conflicting key test + public static final KeyBinding CONFLICT_TEST_KEY_BIND = KeyBindingRegistry.registerKeyBinding( + new KeyBinding("key.qsl.conflict_test", GLFW.GLFW_KEY_H, "key.qsl.category"), true ); @Override @@ -40,14 +45,20 @@ public void onInitializeClient() { if (client.player != null) { client.player.sendMessage(new LiteralText("The key has disappeared! Bye bye, key!"), true); } - KeyBindingRegistryImpl.setActive(DISABLE_KEY_BIND, false); + KeyBindingRegistry.setEnabled(DISABLE_KEY_BIND, false); } if (ENABLE_KEY_BIND.isPressed()) { if (client.player != null) { client.player.sendMessage(new LiteralText("The key is back!"), true); } - KeyBindingRegistryImpl.setActive(DISABLE_KEY_BIND, true); + KeyBindingRegistry.setEnabled(DISABLE_KEY_BIND, true); + } + + if (TEST_KEY_BIND.isPressed()) { + if (client.player != null) { + client.player.sendMessage(new LiteralText("Conflicts!"), false); + } } }); } diff --git a/library/gui/key_bindings/src/testmod/resources/assets/quilt_key_bindings_testmod/lang/en_us.json b/library/gui/key_bindings/src/testmod/resources/assets/quilt_key_bindings_testmod/lang/en_us.json index b493057288..0ad07dfc56 100644 --- a/library/gui/key_bindings/src/testmod/resources/assets/quilt_key_bindings_testmod/lang/en_us.json +++ b/library/gui/key_bindings/src/testmod/resources/assets/quilt_key_bindings_testmod/lang/en_us.json @@ -1,4 +1,6 @@ { + "key.qsl.category": "QSL Key Binding API Test", "key.qsl.disable_key_bind": "Disable Key Bind", - "key.qsl.enable_key_bind": "Enable Key Bind" + "key.qsl.enable_key_bind": "Enable Key Bind", + "key.qsl.conflict_test": "Conflict Test" } \ No newline at end of file From bd1f189987c30ed02c46ea52878692599845d520 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Thu, 2 Dec 2021 01:00:15 -0300 Subject: [PATCH 04/56] More API progress If everything goes right, tomorrow we'll have a PR! --- library/gui/key_bindings/build.gradle | 4 ++ .../key/bindings/api/KeyBindingRegistry.java | 33 +++++++--- .../key/bindings/impl/KeyBindingManager.java | 39 ++++++++---- .../bindings/impl/KeyBindingRegistryImpl.java | 62 +++++++++++++------ .../mixin/client/GameOptionsAccessor.java | 22 ++++++- .../mixin/client/GameOptionsMixin.java | 44 ++++++++++--- .../mixin/client/KeyBindingAccessor.java | 36 ++++++++--- .../mixin/client/KeyBindingMixin.java | 37 +++++++---- .../quilt_key_bindings.accesswidener | 3 + .../test/client/KeyBindingsTestMod.java | 12 ++-- 10 files changed, 217 insertions(+), 75 deletions(-) create mode 100644 library/gui/key_bindings/src/main/resources/quilt_key_bindings.accesswidener diff --git a/library/gui/key_bindings/build.gradle b/library/gui/key_bindings/build.gradle index 06f1e92e84..39fec62295 100644 --- a/library/gui/key_bindings/build.gradle +++ b/library/gui/key_bindings/build.gradle @@ -11,3 +11,7 @@ qslModule { "lifecycle_events" ]) } + +loom { + accessWidenerPath = file("src/main/resources/quilt_key_bindings.accesswidener") +} diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java index b9522da8b7..6e37894cba 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java @@ -1,24 +1,41 @@ -package org.quiltmc.qsl.key.bindings.api; +/* + * Copyright 2021 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ -import org.quiltmc.qsl.key.bindings.impl.KeyBindingRegistryImpl; +package org.quiltmc.qsl.key.bindings.api; import net.minecraft.client.option.KeyBinding; +import org.quiltmc.qsl.key.bindings.impl.KeyBindingRegistryImpl; + public class KeyBindingRegistry { - public static KeyBinding registerKeyBinding(KeyBinding key) { + public static KeyBinding registerKeyBinding(KeyBinding key) { return registerKeyBinding(key, true); } - public static KeyBinding registerKeyBinding(KeyBinding key, boolean enabled) { + public static KeyBinding registerKeyBinding(KeyBinding key, boolean enabled) { return KeyBindingRegistryImpl.registerKeyBinding(key, enabled); } // TODO - Add get method for easier intercompat - public static void setEnabled(KeyBinding key, boolean newEnabled) { - KeyBindingRegistryImpl.setEnabled(key, newEnabled); + public static boolean getEnabled(KeyBinding key) { + return KeyBindingRegistryImpl.getEnabled(key); } - // TODO - Add getEnabled method - + public static void setEnabled(KeyBinding key, boolean newEnabled) { + KeyBindingRegistryImpl.setEnabled(key, newEnabled); + } } diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java index 2d77e8260b..a964a7ca2e 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java @@ -1,22 +1,37 @@ -package org.quiltmc.qsl.key.bindings.impl; +/* + * Copyright 2021 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ -import org.quiltmc.qsl.key.bindings.mixin.client.GameOptionsAccessor; +package org.quiltmc.qsl.key.bindings.impl; import net.minecraft.client.option.GameOptions; import net.minecraft.client.option.KeyBinding; +import org.quiltmc.qsl.key.bindings.mixin.client.GameOptionsAccessor; + public class KeyBindingManager { - private final GameOptions options; - private final KeyBinding[] allKeys; + private final GameOptions options; + private final KeyBinding[] allKeys; - public KeyBindingManager(GameOptions options, KeyBinding[] allKeys) { - this.options = options; - this.allKeys = allKeys; - this.addModdedKeyBinds(); - KeyBindingRegistryImpl.registerKeyBindingManager(this); - } + public KeyBindingManager(GameOptions options, KeyBinding[] allKeys) { + this.options = options; + this.allKeys = allKeys; + this.addModdedKeyBinds(); + } - public void addModdedKeyBinds() { - ((GameOptionsAccessor)(Object)this.options).setKeysAll(KeyBindingRegistryImpl.getKeyBindings(this.allKeys)); + public void addModdedKeyBinds() { + ((GameOptionsAccessor) (Object) this.options).setKeysAll(KeyBindingRegistryImpl.getKeyBindings(this.allKeys)); } } diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java index d7ad6e0047..36c19e0a75 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java @@ -1,3 +1,19 @@ +/* + * Copyright 2021 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.quiltmc.qsl.key.bindings.impl; import java.util.ArrayList; @@ -6,25 +22,37 @@ import java.util.Map; import org.apache.commons.lang3.ArrayUtils; -import org.quiltmc.qsl.key.bindings.mixin.client.KeyBindingAccessor; +import net.minecraft.client.option.GameOptions; import net.minecraft.client.option.KeyBinding; +import org.quiltmc.qsl.key.bindings.mixin.client.KeyBindingAccessor; + public class KeyBindingRegistryImpl { private static Map quiltKeys = new HashMap<>(); - private static List keyBindingManagers = new ArrayList<>(); + private static List keyBindingManagers = new ArrayList<>(1); private static KeyBinding[] enabledQuiltKeysArray = new KeyBinding[] {}; - private static KeyBinding[] totalQuiltKeysArray = new KeyBinding[] {}; + private static List disabledQuiltKeys = new ArrayList<>(); + // TODO - Currently, two different key binds with the same key can be registered. This is not good public static KeyBinding registerKeyBinding(KeyBinding key, boolean enabled) { quiltKeys.put(key, enabled); applyChanges(true); if (!enabled) { KeyBindingAccessor.getKeysById().remove(key.getTranslationKey()); } + return key; } + public static boolean getEnabled(KeyBinding key) { + if (quiltKeys.containsKey(key)) { + return quiltKeys.get(key); + } + + return false; + } + public static void setEnabled(KeyBinding key, boolean newEnabled) { if (quiltKeys.containsKey(key)) { quiltKeys.replace(key, newEnabled); @@ -34,46 +62,42 @@ public static void setEnabled(KeyBinding key, boolean newEnabled) { } else { KeyBindingAccessor.getKeysById().remove(key.getTranslationKey(), key); } - ((KeyBindingAccessor)key).callReset(); + + ((KeyBindingAccessor) key).callReset(); KeyBinding.updateKeysByCode(); } } - protected static void registerKeyBindingManager(KeyBindingManager manager) { - keyBindingManagers.add(manager); + public static void registerKeyBindingManager(GameOptions options, KeyBinding[] allKeys) { + keyBindingManagers.add(new KeyBindingManager(options, allKeys)); } - public static void updateKeysArray(boolean updateTotal) { + public static void updateKeysArray() { List enabledQuiltKeys = new ArrayList<>(); - List totalQuiltKeys = new ArrayList<>(); + disabledQuiltKeys.clear(); for (var entry : quiltKeys.entrySet()) { if (entry.getValue()) { enabledQuiltKeys.add(entry.getKey()); - } - - if (updateTotal) { - totalQuiltKeys.add(entry.getKey()); + } else { + disabledQuiltKeys.add(entry.getKey()); } } KeyBinding[] quiltKeysArray = enabledQuiltKeys.toArray(new KeyBinding[enabledQuiltKeys.size()]); - + enabledQuiltKeysArray = quiltKeysArray; - if (updateTotal) { - totalQuiltKeysArray = totalQuiltKeys.toArray(new KeyBinding[totalQuiltKeys.size()]); - } } public static KeyBinding[] getKeyBindings(KeyBinding[] allVanillaKeys) { return ArrayUtils.addAll(allVanillaKeys, enabledQuiltKeysArray); } - public static KeyBinding[] getAllKeyBindings(KeyBinding[] allVanillaKeys) { - return ArrayUtils.addAll(allVanillaKeys, totalQuiltKeysArray); + public static List getDisabledKeyBindings() { + return disabledQuiltKeys; } public static void applyChanges(boolean updateTotal) { - updateKeysArray(updateTotal); + updateKeysArray(); for (KeyBindingManager manager : keyBindingManagers) { manager.addModdedKeyBinds(); } diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsAccessor.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsAccessor.java index 77e9336819..80720b8f0d 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsAccessor.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsAccessor.java @@ -1,3 +1,19 @@ +/* + * Copyright 2021 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.quiltmc.qsl.key.bindings.mixin.client; import org.spongepowered.asm.mixin.Mixin; @@ -9,7 +25,7 @@ @Mixin(GameOptions.class) public interface GameOptionsAccessor { - @Mutable - @Accessor - void setKeysAll(KeyBinding[] keysAll); + @Mutable + @Accessor + void setKeysAll(KeyBinding[] keysAll); } diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java index 0b8c256ff2..226b3e28b4 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java @@ -1,12 +1,26 @@ +/* + * Copyright 2021 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.quiltmc.qsl.key.bindings.mixin.client; import java.io.File; -import org.quiltmc.qsl.key.bindings.impl.KeyBindingManager; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @@ -14,6 +28,9 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.client.option.GameOptions; import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.util.InputUtil; + +import org.quiltmc.qsl.key.bindings.impl.KeyBindingRegistryImpl; @Mixin(GameOptions.class) public abstract class GameOptionsMixin { @@ -21,10 +38,6 @@ public abstract class GameOptionsMixin { @Final public KeyBinding[] keysAll; - // TODO - This variable might be unnecessary. Remove it - @Unique - public KeyBindingManager quilt$keyBindingManager; - @Inject( at = @At( value = "INVOKE", @@ -33,6 +46,23 @@ public abstract class GameOptionsMixin { method = "" ) private void modifyAllKeys(MinecraftClient client, File file, CallbackInfo ci) { - this.quilt$keyBindingManager = new KeyBindingManager((GameOptions)(Object)this, keysAll); + KeyBindingRegistryImpl.registerKeyBindingManager((GameOptions) (Object) this, keysAll); + } + + @Inject( + at = @At( + value = "FIELD", + target = "Lnet/minecraft/client/option/GameOptions;keysAll:[Lnet/minecraft/client/option/KeyBinding;" + ), + method = "accept(Lnet/minecraft/client/option/GameOptions$Visitor;)V" + ) + private void addDisabledEntries(GameOptions.Visitor visitor, CallbackInfo ci) { + for (KeyBinding keyBinding : KeyBindingRegistryImpl.getDisabledKeyBindings()) { + String keyKey = keyBinding.getBoundKeyTranslationKey(); + String keyBindKey = visitor.visitString("key_" + keyBinding.getTranslationKey(), keyKey); + if (!keyKey.equals(keyBindKey)) { + keyBinding.setBoundKey(InputUtil.fromTranslationKey(keyKey)); + } + }; } } diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingAccessor.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingAccessor.java index 8791a31909..482102e0d6 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingAccessor.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingAccessor.java @@ -1,3 +1,19 @@ +/* + * Copyright 2021 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.quiltmc.qsl.key.bindings.mixin.client; import java.util.Map; @@ -11,15 +27,19 @@ @Mixin(KeyBinding.class) public interface KeyBindingAccessor { - @Accessor(value = "KEYS_BY_ID") - static Map getKeysById() { return null; } + @Accessor(value = "KEYS_BY_ID") + static Map getKeysById() { + return null; + } - @Accessor(value = "KEY_TO_BINDINGS") - static Map getKeyToBindings() { return null; } + @Accessor(value = "KEY_TO_BINDINGS") + static Map getKeyToBindings() { + return null; + } - @Accessor - InputUtil.Key getBoundKey(); + @Accessor + InputUtil.Key getBoundKey(); - @Invoker - void callReset(); + @Invoker + void callReset(); } diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingMixin.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingMixin.java index 9d903d91b2..98e7538344 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingMixin.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingMixin.java @@ -1,3 +1,19 @@ +/* + * Copyright 2021 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.quiltmc.qsl.key.bindings.mixin.client; import java.util.Map; @@ -14,17 +30,14 @@ @Mixin(KeyBinding.class) public abstract class KeyBindingMixin { - @Mutable - @Final - private static Map CATEGORY_ORDER_MAP; + @Mutable + @Final + private static Map CATEGORY_ORDER_MAP; - @Inject( - at = @At("TAIL"), - method = "(Ljava/lang/String;Lnet/minecraft/client/util/InputUtil$Type;ILjava/lang/String;)V" - ) - private void addModdedCategory(String string, InputUtil.Type type, int i, String string2, CallbackInfo ci) { - if (!CATEGORY_ORDER_MAP.containsKey(string2)) { - CATEGORY_ORDER_MAP.put(string2, CATEGORY_ORDER_MAP.size() + 1); - } - } + @Inject(method = "(Ljava/lang/String;Lnet/minecraft/client/util/InputUtil$Type;ILjava/lang/String;)V", at = @At("TAIL")) + private void addModdedCategory(String string, InputUtil.Type type, int i, String string2, CallbackInfo ci) { + if (!CATEGORY_ORDER_MAP.containsKey(string2)) { + CATEGORY_ORDER_MAP.put(string2, CATEGORY_ORDER_MAP.size() + 1); + } + } } diff --git a/library/gui/key_bindings/src/main/resources/quilt_key_bindings.accesswidener b/library/gui/key_bindings/src/main/resources/quilt_key_bindings.accesswidener new file mode 100644 index 0000000000..ffdba3474c --- /dev/null +++ b/library/gui/key_bindings/src/main/resources/quilt_key_bindings.accesswidener @@ -0,0 +1,3 @@ +accessWidener v1 named + +accessible class net/minecraft/client/option/GameOptions$Visitor diff --git a/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java b/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java index 357c8f11c6..39388a2e6b 100644 --- a/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java +++ b/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java @@ -25,6 +25,11 @@ import net.minecraft.text.LiteralText; public class KeyBindingsTestMod implements ClientModInitializer { + // A conflicting key test + public static final KeyBinding CONFLICT_TEST_KEY_BIND = KeyBindingRegistry.registerKeyBinding( + new KeyBinding("key.qsl.conflict_test", GLFW.GLFW_KEY_H, "key.qsl.category"), true + ); + public static final KeyBinding DISABLE_KEY_BIND = KeyBindingRegistry.registerKeyBinding( new KeyBinding("key.qsl.disable_key_bind", GLFW.GLFW_KEY_H, "key.qsl.category"), true ); @@ -32,11 +37,6 @@ public class KeyBindingsTestMod implements ClientModInitializer { public static final KeyBinding ENABLE_KEY_BIND = KeyBindingRegistry.registerKeyBinding( new KeyBinding("key.qsl.enable_key_bind", GLFW.GLFW_KEY_I, "key.qsl.category"), true ); - - // A conflicting key test - public static final KeyBinding CONFLICT_TEST_KEY_BIND = KeyBindingRegistry.registerKeyBinding( - new KeyBinding("key.qsl.conflict_test", GLFW.GLFW_KEY_H, "key.qsl.category"), true - ); @Override public void onInitializeClient() { @@ -55,7 +55,7 @@ public void onInitializeClient() { KeyBindingRegistry.setEnabled(DISABLE_KEY_BIND, true); } - if (TEST_KEY_BIND.isPressed()) { + if (CONFLICT_TEST_KEY_BIND.isPressed()) { if (client.player != null) { client.player.sendMessage(new LiteralText("Conflicts!"), false); } From 310c5f08117434404364ce475a4beaeec5c2bc6f Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Thu, 2 Dec 2021 18:19:55 -0300 Subject: [PATCH 05/56] All ready for a draft PR! --- .../key/bindings/api/KeyBindingRegistry.java | 47 +++++++- .../bindings/impl/ConflictTooltipOwner.java | 29 +++++ .../key/bindings/impl/KeyBindingManager.java | 4 + .../bindings/impl/KeyBindingRegistryImpl.java | 41 +++++-- .../mixin/client/EntryListWidgetAccessor.java | 32 ++++++ .../mixin/client/GameOptionsAccessor.java | 3 + .../mixin/client/GameOptionsMixin.java | 5 +- .../mixin/client/KeyBindingAccessor.java | 3 + .../mixin/client/KeyBindingEntryMixin.java | 100 ++++++++++++++++++ .../mixin/client/KeyBindingMixin.java | 3 + .../mixin/client/KeyBindingsScreenMixin.java | 61 +++++++++++ .../assets/quilt_key_bindings/lang/en_us.json | 4 + .../assets/quilt_key_bindings/lang/pt_br.json | 4 + .../src/main/resources/fabric.mod.json | 7 +- .../resources/quilt_key_bindings.mixins.json | 5 +- .../test/client/KeyBindingsTestMod.java | 15 +-- 16 files changed, 341 insertions(+), 22 deletions(-) create mode 100644 library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/ConflictTooltipOwner.java create mode 100644 library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/EntryListWidgetAccessor.java create mode 100644 library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java create mode 100644 library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingsScreenMixin.java create mode 100644 library/gui/key_bindings/src/main/resources/assets/quilt_key_bindings/lang/en_us.json create mode 100644 library/gui/key_bindings/src/main/resources/assets/quilt_key_bindings/lang/pt_br.json diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java index 6e37894cba..8652bc3af1 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java @@ -16,26 +16,63 @@ package org.quiltmc.qsl.key.bindings.api; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; + import net.minecraft.client.option.KeyBinding; import org.quiltmc.qsl.key.bindings.impl.KeyBindingRegistryImpl; +@Environment(EnvType.CLIENT) public class KeyBindingRegistry { + /** + * Registers a key binding with it being initially enabled. + * + * @param key the key binding to be registered + * @return the key binding if successfully registered, else null + */ public static KeyBinding registerKeyBinding(KeyBinding key) { return registerKeyBinding(key, true); } + /** + * Registers a key binding with the specified initial state. + * + * @param key the key binding to be registered + * @param enabled the key binding's initial state + * @return the key binding if successfully registered, {@code null} otherwise + */ public static KeyBinding registerKeyBinding(KeyBinding key, boolean enabled) { return KeyBindingRegistryImpl.registerKeyBinding(key, enabled); } - // TODO - Add get method for easier intercompat + /** + * Searches for a key binding with the specified translation key in the registry. + * + * @param translationKey the translation key of the key binding + * @return the key binding if it was found, {@code null} otherwise + */ + public static KeyBinding getKeyBinding(String translationKey) { + return KeyBindingRegistryImpl.getKeyBinding(translationKey); + } - public static boolean getEnabled(KeyBinding key) { - return KeyBindingRegistryImpl.getEnabled(key); + /** + * Gets the state of the key binding. + * + * @param key the key binding + * @return {@code true} if the key binding is enabled, {@code false} otherwise + */ + public static boolean isEnabled(KeyBinding key) { + return KeyBindingRegistryImpl.isEnabled(key); } - public static void setEnabled(KeyBinding key, boolean newEnabled) { - KeyBindingRegistryImpl.setEnabled(key, newEnabled); + /** + * Sets the state of the key binding. + * + * @param key the key binding + * @param enabled the new state + */ + public static void setEnabled(KeyBinding key, boolean enabled) { + KeyBindingRegistryImpl.setEnabled(key, enabled); } } diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/ConflictTooltipOwner.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/ConflictTooltipOwner.java new file mode 100644 index 0000000000..aeb35c4df1 --- /dev/null +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/ConflictTooltipOwner.java @@ -0,0 +1,29 @@ +/* + * Copyright 2021 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.quiltmc.qsl.key.bindings.impl; + +import java.util.List; + +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; + +import net.minecraft.text.Text; + +@Environment(EnvType.CLIENT) +public interface ConflictTooltipOwner { + List getConflictTooltips(); +} diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java index a964a7ca2e..731b0af1ed 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java @@ -16,11 +16,15 @@ package org.quiltmc.qsl.key.bindings.impl; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; + import net.minecraft.client.option.GameOptions; import net.minecraft.client.option.KeyBinding; import org.quiltmc.qsl.key.bindings.mixin.client.GameOptionsAccessor; +@Environment(EnvType.CLIENT) public class KeyBindingManager { private final GameOptions options; private final KeyBinding[] allKeys; diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java index 36c19e0a75..b431e9f195 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java @@ -22,20 +22,39 @@ import java.util.Map; import org.apache.commons.lang3.ArrayUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; import net.minecraft.client.option.GameOptions; import net.minecraft.client.option.KeyBinding; import org.quiltmc.qsl.key.bindings.mixin.client.KeyBindingAccessor; +@Environment(EnvType.CLIENT) public class KeyBindingRegistryImpl { + public static final Logger LOGGER = LogManager.getLogger(); + private static Map quiltKeys = new HashMap<>(); private static List keyBindingManagers = new ArrayList<>(1); private static KeyBinding[] enabledQuiltKeysArray = new KeyBinding[] {}; - private static List disabledQuiltKeys = new ArrayList<>(); + private static List disabledQuiltKeys = new ArrayList<>(0); - // TODO - Currently, two different key binds with the same key can be registered. This is not good public static KeyBinding registerKeyBinding(KeyBinding key, boolean enabled) { + if (key == null) { + LOGGER.error("Attempted to register a null key bind!"); + return null; + } + + for (KeyBinding otherKey : quiltKeys.keySet()) { + if (key == otherKey || key.getTranslationKey() == otherKey.getTranslationKey()) { + // FIXME - This message is not great + LOGGER.error("Attempted to register %s, but a conflicting key has already been registered!", key.getTranslationKey()); + return null; + } + } + quiltKeys.put(key, enabled); applyChanges(true); if (!enabled) { @@ -45,7 +64,17 @@ public static KeyBinding registerKeyBinding(KeyBinding key, boolean enabled) { return key; } - public static boolean getEnabled(KeyBinding key) { + public static KeyBinding getKeyBinding(String translationKey) { + for (KeyBinding key : quiltKeys.keySet()) { + if (key.getTranslationKey() == translationKey) { + return key; + } + } + + return null; + } + + public static boolean isEnabled(KeyBinding key) { if (quiltKeys.containsKey(key)) { return quiltKeys.get(key); } @@ -53,11 +82,11 @@ public static boolean getEnabled(KeyBinding key) { return false; } - public static void setEnabled(KeyBinding key, boolean newEnabled) { + public static void setEnabled(KeyBinding key, boolean enabled) { if (quiltKeys.containsKey(key)) { - quiltKeys.replace(key, newEnabled); + quiltKeys.replace(key, enabled); applyChanges(false); - if (newEnabled) { + if (enabled) { KeyBindingAccessor.getKeysById().put(key.getTranslationKey(), key); } else { KeyBindingAccessor.getKeysById().remove(key.getTranslationKey(), key); diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/EntryListWidgetAccessor.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/EntryListWidgetAccessor.java new file mode 100644 index 0000000000..5f82adee65 --- /dev/null +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/EntryListWidgetAccessor.java @@ -0,0 +1,32 @@ +/* + * Copyright 2021 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.quiltmc.qsl.key.bindings.mixin.client; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.widget.EntryListWidget; + +@Environment(EnvType.CLIENT) +@Mixin(EntryListWidget.class) +public interface EntryListWidgetAccessor { + @Accessor + MinecraftClient getClient(); +} diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsAccessor.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsAccessor.java index 80720b8f0d..078c9de596 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsAccessor.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsAccessor.java @@ -19,10 +19,13 @@ import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mutable; import org.spongepowered.asm.mixin.gen.Accessor; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; import net.minecraft.client.option.GameOptions; import net.minecraft.client.option.KeyBinding; +@Environment(EnvType.CLIENT) @Mixin(GameOptions.class) public interface GameOptionsAccessor { @Mutable diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java index 226b3e28b4..33b69beed7 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java @@ -24,6 +24,8 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; import net.minecraft.client.option.GameOptions; @@ -32,6 +34,7 @@ import org.quiltmc.qsl.key.bindings.impl.KeyBindingRegistryImpl; +@Environment(EnvType.CLIENT) @Mixin(GameOptions.class) public abstract class GameOptionsMixin { @Shadow @@ -56,7 +59,7 @@ private void modifyAllKeys(MinecraftClient client, File file, CallbackInfo ci) { ), method = "accept(Lnet/minecraft/client/option/GameOptions$Visitor;)V" ) - private void addDisabledEntries(GameOptions.Visitor visitor, CallbackInfo ci) { + private void includeDisabledEntries(GameOptions.Visitor visitor, CallbackInfo ci) { for (KeyBinding keyBinding : KeyBindingRegistryImpl.getDisabledKeyBindings()) { String keyKey = keyBinding.getBoundKeyTranslationKey(); String keyBindKey = visitor.visitString("key_" + keyBinding.getTranslationKey(), keyKey); diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingAccessor.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingAccessor.java index 482102e0d6..e766891ec4 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingAccessor.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingAccessor.java @@ -21,10 +21,13 @@ import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.gen.Accessor; import org.spongepowered.asm.mixin.gen.Invoker; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; import net.minecraft.client.option.KeyBinding; import net.minecraft.client.util.InputUtil; +@Environment(EnvType.CLIENT) @Mixin(KeyBinding.class) public interface KeyBindingAccessor { @Accessor(value = "KEYS_BY_ID") diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java new file mode 100644 index 0000000000..996bca288d --- /dev/null +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java @@ -0,0 +1,100 @@ +/* + * Copyright 2021 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.quiltmc.qsl.key.bindings.mixin.client; + +import java.util.ArrayList; +import java.util.List; + +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.ModifyArg; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.LocalCapture; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.client.gui.widget.option.KeyBindingListWidget; +import net.minecraft.client.gui.widget.option.KeyBindingListWidget.KeyBindingEntry; +import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.text.Text; +import net.minecraft.text.TranslatableText; +import net.minecraft.util.Formatting; + +import org.quiltmc.qsl.key.bindings.impl.ConflictTooltipOwner; + +@Environment(EnvType.CLIENT) +@Mixin(KeyBindingEntry.class) +public abstract class KeyBindingEntryMixin extends KeyBindingListWidget.Entry implements ConflictTooltipOwner { + @Shadow + @Final + private KeyBinding binding; + + @Shadow + @Final + private ButtonWidget bindButton; + + @Unique + private List quilt$conflictTooltips = new ArrayList<>(1); + + @Shadow(aliases = "field_2742") + @Final + KeyBindingListWidget field_2742; + + // FIXME - Find a way to get all conflicting keys that reuses the iteration done by Vanilla + @Inject( + method = "render", + at = @At( + value = "INVOKE_ASSIGN", + target = "Lnet/minecraft/client/option/KeyBinding;isUnbound()Z", + shift = At.Shift.AFTER + ), + locals = LocalCapture.CAPTURE_FAILHARD + ) + private void collectConflictTooltips(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta, CallbackInfo ci, boolean bl, boolean bl2) { + quilt$conflictTooltips.clear(); + MinecraftClient client = ((EntryListWidgetAccessor) (Object) field_2742).getClient(); + for (KeyBinding keyBinding : client.options.keysAll) { + if (keyBinding != this.binding && this.binding.equals(keyBinding)) { + quilt$conflictTooltips.add(new TranslatableText("key.qsl.key_conflict.tooltip", new TranslatableText(keyBinding.getTranslationKey())).formatted(Formatting.RED)); + } + } + } + + @ModifyArg( + method = "render", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/client/gui/widget/ButtonWidget;setMessage(Lnet/minecraft/text/Text;)V", + ordinal = 2 + ) + ) + private Text addConflictIndicator(Text originalText) { + return new TranslatableText("key.qsl.key_conflict.indicator", originalText).formatted(Formatting.RED); + } + + @Override + public List getConflictTooltips() { + return this.quilt$conflictTooltips; + } +} diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingMixin.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingMixin.java index 98e7538344..e2e06989c1 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingMixin.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingMixin.java @@ -24,10 +24,13 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; import net.minecraft.client.option.KeyBinding; import net.minecraft.client.util.InputUtil; +@Environment(EnvType.CLIENT) @Mixin(KeyBinding.class) public abstract class KeyBindingMixin { @Mutable diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingsScreenMixin.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingsScreenMixin.java new file mode 100644 index 0000000000..8c22d63bc3 --- /dev/null +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingsScreenMixin.java @@ -0,0 +1,61 @@ +/* + * Copyright 2021 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.quiltmc.qsl.key.bindings.mixin.client; + +import java.util.List; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; + +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.gui.screen.option.GameOptionsScreen; +import net.minecraft.client.gui.screen.option.KeyBindingsScreen; +import net.minecraft.client.gui.widget.option.KeyBindingListWidget; +import net.minecraft.client.gui.widget.option.KeyBindingListWidget.KeyBindingEntry; +import net.minecraft.client.option.GameOptions; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.text.Text; + +import org.quiltmc.qsl.key.bindings.impl.ConflictTooltipOwner; + +@Environment(EnvType.CLIENT) +@Mixin(KeyBindingsScreen.class) +public abstract class KeyBindingsScreenMixin extends GameOptionsScreen { + @Shadow + private KeyBindingListWidget keybindsList; + + private KeyBindingsScreenMixin(Screen screen, GameOptions gameOptions, Text text) { + super(screen, gameOptions, text); + } + + @Inject(method = "render", at = @At("TAIL")) + private void renderConflictTooltips(MatrixStack matrices, int mouseX, int mouseY, float delta, CallbackInfo ci) { + for (KeyBindingListWidget.Entry entry : this.keybindsList.children()) { + if (entry instanceof KeyBindingEntry keyBindingEntry) { + if (keyBindingEntry.isMouseOver(mouseX, mouseY)) { + List tooltipLines = ((ConflictTooltipOwner) (Object) keyBindingEntry).getConflictTooltips(); + this.renderTooltip(matrices, tooltipLines, mouseX, mouseY); + } + } + } + } +} diff --git a/library/gui/key_bindings/src/main/resources/assets/quilt_key_bindings/lang/en_us.json b/library/gui/key_bindings/src/main/resources/assets/quilt_key_bindings/lang/en_us.json new file mode 100644 index 0000000000..e47076969e --- /dev/null +++ b/library/gui/key_bindings/src/main/resources/assets/quilt_key_bindings/lang/en_us.json @@ -0,0 +1,4 @@ +{ + "key.qsl.key_conflict.indicator": "%s (!)", + "key.qsl.key_conflict.tooltip": "Conflicts with: %s" +} \ No newline at end of file diff --git a/library/gui/key_bindings/src/main/resources/assets/quilt_key_bindings/lang/pt_br.json b/library/gui/key_bindings/src/main/resources/assets/quilt_key_bindings/lang/pt_br.json new file mode 100644 index 0000000000..467c88046d --- /dev/null +++ b/library/gui/key_bindings/src/main/resources/assets/quilt_key_bindings/lang/pt_br.json @@ -0,0 +1,4 @@ +{ + "key.qsl.key_conflict.indicator": "%s (!)", + "key.qsl.conflict_tooltip": "Há conflito com: %s" +} \ No newline at end of file diff --git a/library/gui/key_bindings/src/main/resources/fabric.mod.json b/library/gui/key_bindings/src/main/resources/fabric.mod.json index 626989d4eb..98eca19b72 100644 --- a/library/gui/key_bindings/src/main/resources/fabric.mod.json +++ b/library/gui/key_bindings/src/main/resources/fabric.mod.json @@ -3,7 +3,7 @@ "id": "quilt_key_bindings", "name": "Quilt Key Bindings API", "version": "${version}", - "environment": "*", + "environment": "client", "license": "Apache-2.0", "icon": "assets/quilt_key_bindings/icon.png", "contact": { @@ -15,9 +15,10 @@ "QuiltMC" ], "depends": { - "fabricloader": ">=0.12" + "fabricloader": ">=0.12", + "minecraft": ">=1.18" }, - "description": "Key bindings registration.", + "description": "Key bindings registration and utilities.", "mixins": [ "quilt_key_bindings.mixins.json" ] diff --git a/library/gui/key_bindings/src/main/resources/quilt_key_bindings.mixins.json b/library/gui/key_bindings/src/main/resources/quilt_key_bindings.mixins.json index 9298500cf7..4949cb8c13 100644 --- a/library/gui/key_bindings/src/main/resources/quilt_key_bindings.mixins.json +++ b/library/gui/key_bindings/src/main/resources/quilt_key_bindings.mixins.json @@ -3,10 +3,13 @@ "package": "org.quiltmc.qsl.key.bindings.mixin", "compatibilityLevel": "JAVA_17", "client": [ + "client.EntryListWidgetAccessor", "client.GameOptionsAccessor", "client.GameOptionsMixin", "client.KeyBindingAccessor", - "client.KeyBindingMixin" + "client.KeyBindingEntryMixin", + "client.KeyBindingMixin", + "client.KeyBindingsScreenMixin" ], "injectors": { "defaultRequire": 1 diff --git a/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java b/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java index 39388a2e6b..21ae757a6a 100644 --- a/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java +++ b/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java @@ -17,19 +17,20 @@ package org.quiltmc.qsl.key.bindings.test.client; import org.lwjgl.glfw.GLFW; -import org.quiltmc.qsl.key.bindings.api.KeyBindingRegistry; -import org.quiltmc.qsl.lifecycle.api.client.event.ClientTickEvents; - import net.fabricmc.api.ClientModInitializer; + import net.minecraft.client.option.KeyBinding; import net.minecraft.text.LiteralText; +import org.quiltmc.qsl.key.bindings.api.KeyBindingRegistry; +import org.quiltmc.qsl.lifecycle.api.client.event.ClientTickEvents; + public class KeyBindingsTestMod implements ClientModInitializer { // A conflicting key test public static final KeyBinding CONFLICT_TEST_KEY_BIND = KeyBindingRegistry.registerKeyBinding( new KeyBinding("key.qsl.conflict_test", GLFW.GLFW_KEY_H, "key.qsl.category"), true ); - + public static final KeyBinding DISABLE_KEY_BIND = KeyBindingRegistry.registerKeyBinding( new KeyBinding("key.qsl.disable_key_bind", GLFW.GLFW_KEY_H, "key.qsl.category"), true ); @@ -37,7 +38,7 @@ public class KeyBindingsTestMod implements ClientModInitializer { public static final KeyBinding ENABLE_KEY_BIND = KeyBindingRegistry.registerKeyBinding( new KeyBinding("key.qsl.enable_key_bind", GLFW.GLFW_KEY_I, "key.qsl.category"), true ); - + @Override public void onInitializeClient() { ClientTickEvents.START.register(client -> { @@ -45,6 +46,7 @@ public void onInitializeClient() { if (client.player != null) { client.player.sendMessage(new LiteralText("The key has disappeared! Bye bye, key!"), true); } + KeyBindingRegistry.setEnabled(DISABLE_KEY_BIND, false); } @@ -52,12 +54,13 @@ public void onInitializeClient() { if (client.player != null) { client.player.sendMessage(new LiteralText("The key is back!"), true); } + KeyBindingRegistry.setEnabled(DISABLE_KEY_BIND, true); } if (CONFLICT_TEST_KEY_BIND.isPressed()) { if (client.player != null) { - client.player.sendMessage(new LiteralText("Conflicts!"), false); + client.player.sendMessage(new LiteralText("This is the conflict key being pressed"), false); } } }); From c277d51330bb5c0aabada05c03c72e2d99df569e Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Thu, 2 Dec 2021 18:44:02 -0300 Subject: [PATCH 06/56] Hm, maybe it's not bad after all --- .../quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java | 1 - 1 file changed, 1 deletion(-) diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java index b431e9f195..ead9f3aedb 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java @@ -49,7 +49,6 @@ public static KeyBinding registerKeyBinding(KeyBinding key, boolean enabled) { for (KeyBinding otherKey : quiltKeys.keySet()) { if (key == otherKey || key.getTranslationKey() == otherKey.getTranslationKey()) { - // FIXME - This message is not great LOGGER.error("Attempted to register %s, but a conflicting key has already been registered!", key.getTranslationKey()); return null; } From 86aba913fe7c294d8395e1cd551ac47f7dfa8cb6 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Thu, 2 Dec 2021 20:31:43 -0300 Subject: [PATCH 07/56] Address suggestions --- .../key/bindings/api/KeyBindingRegistry.java | 18 +++++++++++++++++- .../bindings/impl/KeyBindingRegistryImpl.java | 9 +++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java index 8652bc3af1..003cf7d701 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java @@ -23,6 +23,12 @@ import org.quiltmc.qsl.key.bindings.impl.KeyBindingRegistryImpl; +/** + * Handles the registration of modded key binds and allows for changing their properties, + * such as their state. + * + *

This class also allows for getting key binds registered by other mods. + */ @Environment(EnvType.CLIENT) public class KeyBindingRegistry { /** @@ -47,7 +53,7 @@ public static KeyBinding registerKeyBinding(KeyBinding key, boolean enabled) { } /** - * Searches for a key binding with the specified translation key in the registry. + * Searches for a modded key binding with the specified translation key in the registry. * * @param translationKey the translation key of the key binding * @return the key binding if it was found, {@code null} otherwise @@ -59,6 +65,12 @@ public static KeyBinding getKeyBinding(String translationKey) { /** * Gets the state of the key binding. * + *

The state of a key binding determines whenever it's disabled or not. + * + *

When a keybind is disabled, it is effectively hidden from the game, + * being non-existent to it. options.txt is the only expection, who still + * stores the disabled key binds. + * * @param key the key binding * @return {@code true} if the key binding is enabled, {@code false} otherwise */ @@ -69,6 +81,10 @@ public static boolean isEnabled(KeyBinding key) { /** * Sets the state of the key binding. * + *

This method allows for disabling the key binding, making it invisible + * to the game. options.txt is the only expection, who still stores the disabled + * key binds. + * * @param key the key binding * @param enabled the new state */ diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java index ead9f3aedb..bde1329579 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java @@ -48,8 +48,13 @@ public static KeyBinding registerKeyBinding(KeyBinding key, boolean enabled) { } for (KeyBinding otherKey : quiltKeys.keySet()) { - if (key == otherKey || key.getTranslationKey() == otherKey.getTranslationKey()) { - LOGGER.error("Attempted to register %s, but a conflicting key has already been registered!", key.getTranslationKey()); + if (key == otherKey) { + LOGGER.error("%s has already been registered!", key.getTranslationKey()); + return null; + } + + if (key.getTranslationKey() == otherKey.getTranslationKey()) { + LOGGER.error("Attempted to register %s, but a key bind with the same translation key has already been registered!", key.getTranslationKey()); return null; } } From bffed776dd97c3a3226d50dac519fa0081493d28 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Thu, 2 Dec 2021 21:55:52 -0300 Subject: [PATCH 08/56] Minor tweaks and enhancements --- .../key/bindings/api/KeyBindingRegistry.java | 23 +++++++++++++++++++ .../mixin/client/KeyBindingAccessor.java | 5 ---- .../test/client/KeyBindingsTestMod.java | 9 ++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java index 003cf7d701..6c298daf87 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java @@ -20,8 +20,10 @@ import net.fabricmc.api.Environment; import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.util.InputUtil; import org.quiltmc.qsl.key.bindings.impl.KeyBindingRegistryImpl; +import org.quiltmc.qsl.key.bindings.mixin.client.KeyBindingAccessor; /** * Handles the registration of modded key binds and allows for changing their properties, @@ -33,6 +35,14 @@ public class KeyBindingRegistry { /** * Registers a key binding with it being initially enabled. + * + *

+	 * {@code
+	 * public static final KeyBinding EXAMPLE_KEY_BIND = KeyBinding.registerKeyBinding(
+	 * 	new KeyBinding("key.modid.example", GLFW.GLFW_KEY_M, "key.modid.category")
+	 * )
+	 * }
+	 * 
* * @param key the key binding to be registered * @return the key binding if successfully registered, else null @@ -62,6 +72,19 @@ public static KeyBinding getKeyBinding(String translationKey) { return KeyBindingRegistryImpl.getKeyBinding(translationKey); } + /** + * Gets the bound key of the key binding. + * + *

The bound key is only directly used by the key bind system's internal logic. + * If possible, use the method provided by the KeyBinding class. + * + * @param key + * @return + */ + public static InputUtil.Key getBoundKey(KeyBinding key) { + return ((KeyBindingAccessor) key).getBoundKey(); + } + /** * Gets the state of the key binding. * diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingAccessor.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingAccessor.java index e766891ec4..0d21123350 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingAccessor.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingAccessor.java @@ -35,11 +35,6 @@ static Map getKeysById() { return null; } - @Accessor(value = "KEY_TO_BINDINGS") - static Map getKeyToBindings() { - return null; - } - @Accessor InputUtil.Key getBoundKey(); diff --git a/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java b/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java index 21ae757a6a..603592e584 100644 --- a/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java +++ b/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java @@ -39,6 +39,15 @@ public class KeyBindingsTestMod implements ClientModInitializer { new KeyBinding("key.qsl.enable_key_bind", GLFW.GLFW_KEY_I, "key.qsl.category"), true ); + // These key binds will give an error + public static final KeyBinding DUPLICATE_KEY_BIND = KeyBindingRegistry.registerKeyBinding( + new KeyBinding("key.qsl.enable_key_bind", GLFW.GLFW_KEY_J, "key.qsl.category"), true + ); + + public static final KeyBinding EXACT_SAME_KEY_BIND = KeyBindingRegistry.registerKeyBinding(ENABLE_KEY_BIND); + + public static final KeyBinding NULL_KEY_BIND = KeyBindingRegistry.registerKeyBinding(null); + @Override public void onInitializeClient() { ClientTickEvents.START.register(client -> { From abf605851cd935a1ff8921b143c5cdd025debb13 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Thu, 2 Dec 2021 22:34:10 -0300 Subject: [PATCH 09/56] Progress...? --- .../bindings/impl/KeyBindingRegistryImpl.java | 18 +++++++----------- .../test/client/KeyBindingsTestMod.java | 6 +++++- .../quilt_key_bindings_testmod/lang/en_us.json | 3 ++- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java index bde1329579..0d92e1133b 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java @@ -49,18 +49,16 @@ public static KeyBinding registerKeyBinding(KeyBinding key, boolean enabled) { for (KeyBinding otherKey : quiltKeys.keySet()) { if (key == otherKey) { - LOGGER.error("%s has already been registered!", key.getTranslationKey()); + LOGGER.error("{} has already been registered!", key.getTranslationKey()); return null; - } - - if (key.getTranslationKey() == otherKey.getTranslationKey()) { - LOGGER.error("Attempted to register %s, but a key bind with the same translation key has already been registered!", key.getTranslationKey()); + } else if (key.getTranslationKey() == otherKey.getTranslationKey()) { + LOGGER.error("Attempted to register {}, but a key bind with the same translation key has already been registered!", key.getTranslationKey()); return null; } } quiltKeys.put(key, enabled); - applyChanges(true); + applyChanges(); if (!enabled) { KeyBindingAccessor.getKeysById().remove(key.getTranslationKey()); } @@ -89,7 +87,7 @@ public static boolean isEnabled(KeyBinding key) { public static void setEnabled(KeyBinding key, boolean enabled) { if (quiltKeys.containsKey(key)) { quiltKeys.replace(key, enabled); - applyChanges(false); + applyChanges(); if (enabled) { KeyBindingAccessor.getKeysById().put(key.getTranslationKey(), key); } else { @@ -116,9 +114,7 @@ public static void updateKeysArray() { } } - KeyBinding[] quiltKeysArray = enabledQuiltKeys.toArray(new KeyBinding[enabledQuiltKeys.size()]); - - enabledQuiltKeysArray = quiltKeysArray; + enabledQuiltKeysArray = enabledQuiltKeys.toArray(new KeyBinding[enabledQuiltKeys.size()]); } public static KeyBinding[] getKeyBindings(KeyBinding[] allVanillaKeys) { @@ -129,7 +125,7 @@ public static List getDisabledKeyBindings() { return disabledQuiltKeys; } - public static void applyChanges(boolean updateTotal) { + public static void applyChanges() { updateKeysArray(); for (KeyBindingManager manager : keyBindingManagers) { manager.addModdedKeyBinds(); diff --git a/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java b/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java index 603592e584..568d653147 100644 --- a/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java +++ b/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java @@ -28,7 +28,7 @@ public class KeyBindingsTestMod implements ClientModInitializer { // A conflicting key test public static final KeyBinding CONFLICT_TEST_KEY_BIND = KeyBindingRegistry.registerKeyBinding( - new KeyBinding("key.qsl.conflict_test", GLFW.GLFW_KEY_H, "key.qsl.category"), true + new KeyBinding("key.qsl.conflict_test", GLFW.GLFW_KEY_H, "key.qsl.category") ); public static final KeyBinding DISABLE_KEY_BIND = KeyBindingRegistry.registerKeyBinding( @@ -44,6 +44,10 @@ public class KeyBindingsTestMod implements ClientModInitializer { new KeyBinding("key.qsl.enable_key_bind", GLFW.GLFW_KEY_J, "key.qsl.category"), true ); + public static final KeyBinding DISABLED_CONFLICT_TEST_KEY_BIND = KeyBindingRegistry.registerKeyBinding( + new KeyBinding("key.qsl.disabled_conflict_test", GLFW.GLFW_KEY_H, "key.qsl.category"), false + ); + public static final KeyBinding EXACT_SAME_KEY_BIND = KeyBindingRegistry.registerKeyBinding(ENABLE_KEY_BIND); public static final KeyBinding NULL_KEY_BIND = KeyBindingRegistry.registerKeyBinding(null); diff --git a/library/gui/key_bindings/src/testmod/resources/assets/quilt_key_bindings_testmod/lang/en_us.json b/library/gui/key_bindings/src/testmod/resources/assets/quilt_key_bindings_testmod/lang/en_us.json index 0ad07dfc56..0a00f36596 100644 --- a/library/gui/key_bindings/src/testmod/resources/assets/quilt_key_bindings_testmod/lang/en_us.json +++ b/library/gui/key_bindings/src/testmod/resources/assets/quilt_key_bindings_testmod/lang/en_us.json @@ -2,5 +2,6 @@ "key.qsl.category": "QSL Key Binding API Test", "key.qsl.disable_key_bind": "Disable Key Bind", "key.qsl.enable_key_bind": "Enable Key Bind", - "key.qsl.conflict_test": "Conflict Test" + "key.qsl.conflict_test": "Conflict Test", + "key.qsl.disabled_conflict_test": "Disabled Conflict Test" } \ No newline at end of file From ac3ce1210f93343a3d92c1a39dcf237bd7d95cbc Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Sun, 5 Dec 2021 11:24:57 -0300 Subject: [PATCH 10/56] Fix typos and checkstyle issues --- .../qsl/key/bindings/api/KeyBindingRegistry.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java index 6c298daf87..8c5467c533 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java @@ -35,7 +35,7 @@ public class KeyBindingRegistry { /** * Registers a key binding with it being initially enabled. - * + * *

 	 * {@code
 	 * public static final KeyBinding EXAMPLE_KEY_BIND = KeyBinding.registerKeyBinding(
@@ -74,10 +74,10 @@ public static KeyBinding getKeyBinding(String translationKey) {
 
 	/**
 	 * Gets the bound key of the key binding.
-	 * 
+	 *
 	 * 

The bound key is only directly used by the key bind system's internal logic. * If possible, use the method provided by the KeyBinding class. - * + * * @param key * @return */ @@ -91,7 +91,7 @@ public static InputUtil.Key getBoundKey(KeyBinding key) { *

The state of a key binding determines whenever it's disabled or not. * *

When a keybind is disabled, it is effectively hidden from the game, - * being non-existent to it. options.txt is the only expection, who still + * being non-existent to it. options.txt is the only exception, who still * stores the disabled key binds. * * @param key the key binding @@ -105,7 +105,7 @@ public static boolean isEnabled(KeyBinding key) { * Sets the state of the key binding. * *

This method allows for disabling the key binding, making it invisible - * to the game. options.txt is the only expection, who still stores the disabled + * to the game. options.txt is the only exception, who still stores the disabled * key binds. * * @param key the key binding From a88ce916e7a861c46ab61354c69f2124fd40b2cc Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Sun, 5 Dec 2021 12:26:00 -0300 Subject: [PATCH 11/56] Apply suggestions, fix AW --- .../key/bindings/impl/KeyBindingRegistryImpl.java | 15 ++++++--------- .../mixin/client/KeyBindingEntryMixin.java | 6 +++++- .../src/main/resources/fabric.mod.json | 1 + 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java index 0d92e1133b..e4266485d5 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java @@ -20,6 +20,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import org.apache.commons.lang3.ArrayUtils; import org.apache.logging.log4j.LogManager; @@ -42,18 +43,13 @@ public class KeyBindingRegistryImpl { private static List disabledQuiltKeys = new ArrayList<>(0); public static KeyBinding registerKeyBinding(KeyBinding key, boolean enabled) { - if (key == null) { - LOGGER.error("Attempted to register a null key bind!"); - return null; - } + Objects.requireNonNull(key, "Attempted to register a null key bind!"); for (KeyBinding otherKey : quiltKeys.keySet()) { if (key == otherKey) { - LOGGER.error("{} has already been registered!", key.getTranslationKey()); - return null; + throw new IllegalArgumentException(String.format("%s has already been registered!", key.getTranslationKey())); } else if (key.getTranslationKey() == otherKey.getTranslationKey()) { - LOGGER.error("Attempted to register {}, but a key bind with the same translation key has already been registered!", key.getTranslationKey()); - return null; + throw new IllegalArgumentException(String.format("Attempted to register {}, but a key bind with the same translation key has already been registered!", key.getTranslationKey())); } } @@ -81,7 +77,8 @@ public static boolean isEnabled(KeyBinding key) { return quiltKeys.get(key); } - return false; + // TODO - Split those two + throw new IllegalArgumentException(String.format("%s has either not been registered or it is a vanilla key!", key.getTranslationKey())); } public static void setEnabled(KeyBinding key, boolean enabled) { diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java index 996bca288d..f4d4f8b98e 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java @@ -76,7 +76,11 @@ private void collectConflictTooltips(MatrixStack matrices, int index, int y, int MinecraftClient client = ((EntryListWidgetAccessor) (Object) field_2742).getClient(); for (KeyBinding keyBinding : client.options.keysAll) { if (keyBinding != this.binding && this.binding.equals(keyBinding)) { - quilt$conflictTooltips.add(new TranslatableText("key.qsl.key_conflict.tooltip", new TranslatableText(keyBinding.getTranslationKey())).formatted(Formatting.RED)); + if (quilt$conflictTooltips.isEmpty()) { + quilt$conflictTooltips.add(new TranslatableText("key.qsl.key_conflict.tooltip").formatted(Formatting.RED)); + } + + quilt$conflictTooltips.add(new TranslatableText(keyBinding.getTranslationKey()).formatted(Formatting.RED)); } } } diff --git a/library/gui/key_bindings/src/main/resources/fabric.mod.json b/library/gui/key_bindings/src/main/resources/fabric.mod.json index 98eca19b72..32d7cf980f 100644 --- a/library/gui/key_bindings/src/main/resources/fabric.mod.json +++ b/library/gui/key_bindings/src/main/resources/fabric.mod.json @@ -19,6 +19,7 @@ "minecraft": ">=1.18" }, "description": "Key bindings registration and utilities.", + "accessWidener": "quilt_key_bindings.accesswidener", "mixins": [ "quilt_key_bindings.mixins.json" ] From ae115f8e825864ef7fde9d7b7824240e1265c7a5 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Sun, 5 Dec 2021 13:35:42 -0300 Subject: [PATCH 12/56] Small progress! --- .../qsl/key/bindings/api/KeyBindingRegistry.java | 11 ++++++++--- .../key/bindings/impl/KeyBindingRegistryImpl.java | 2 +- .../assets/quilt_key_bindings/lang/pt_br.json | 2 +- .../bindings/test/client/KeyBindingsTestMod.java | 15 ++++++--------- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java index 8c5467c533..c7d898b800 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java @@ -45,7 +45,9 @@ public class KeyBindingRegistry { *

* * @param key the key binding to be registered - * @return the key binding if successfully registered, else null + * @return the successfully-registered key binding + * @throws NullPointerException if {@code key} is null + * @throws IllegalArgumentException if either {@code key} or it's translation key is already registered */ public static KeyBinding registerKeyBinding(KeyBinding key) { return registerKeyBinding(key, true); @@ -57,6 +59,8 @@ public static KeyBinding registerKeyBinding(KeyBinding key) { * @param key the key binding to be registered * @param enabled the key binding's initial state * @return the key binding if successfully registered, {@code null} otherwise + * @throws NullPointerException if {@code key} is null + * @throws IllegalArgumentException if either {@code key} or it's translation key is already registered */ public static KeyBinding registerKeyBinding(KeyBinding key, boolean enabled) { return KeyBindingRegistryImpl.registerKeyBinding(key, enabled); @@ -78,8 +82,8 @@ public static KeyBinding getKeyBinding(String translationKey) { *

The bound key is only directly used by the key bind system's internal logic. * If possible, use the method provided by the KeyBinding class. * - * @param key - * @return + * @param key the key binding + * @return the key binding's bound key */ public static InputUtil.Key getBoundKey(KeyBinding key) { return ((KeyBindingAccessor) key).getBoundKey(); @@ -96,6 +100,7 @@ public static InputUtil.Key getBoundKey(KeyBinding key) { * * @param key the key binding * @return {@code true} if the key binding is enabled, {@code false} otherwise + * @throws IllegalArgumentException if {@code key} is either unregistered or a Vanilla key bind */ public static boolean isEnabled(KeyBinding key) { return KeyBindingRegistryImpl.isEnabled(key); diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java index e4266485d5..20ce878f83 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java @@ -48,7 +48,7 @@ public static KeyBinding registerKeyBinding(KeyBinding key, boolean enabled) { for (KeyBinding otherKey : quiltKeys.keySet()) { if (key == otherKey) { throw new IllegalArgumentException(String.format("%s has already been registered!", key.getTranslationKey())); - } else if (key.getTranslationKey() == otherKey.getTranslationKey()) { + } else if (key.getTranslationKey().equals(otherKey.getTranslationKey())) { throw new IllegalArgumentException(String.format("Attempted to register {}, but a key bind with the same translation key has already been registered!", key.getTranslationKey())); } } diff --git a/library/gui/key_bindings/src/main/resources/assets/quilt_key_bindings/lang/pt_br.json b/library/gui/key_bindings/src/main/resources/assets/quilt_key_bindings/lang/pt_br.json index 467c88046d..92948b285b 100644 --- a/library/gui/key_bindings/src/main/resources/assets/quilt_key_bindings/lang/pt_br.json +++ b/library/gui/key_bindings/src/main/resources/assets/quilt_key_bindings/lang/pt_br.json @@ -1,4 +1,4 @@ { "key.qsl.key_conflict.indicator": "%s (!)", - "key.qsl.conflict_tooltip": "Há conflito com: %s" + "key.qsl.conflict_tooltip": "Há conflitos com: %s" } \ No newline at end of file diff --git a/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java b/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java index 568d653147..712fbe6bc2 100644 --- a/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java +++ b/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java @@ -39,19 +39,10 @@ public class KeyBindingsTestMod implements ClientModInitializer { new KeyBinding("key.qsl.enable_key_bind", GLFW.GLFW_KEY_I, "key.qsl.category"), true ); - // These key binds will give an error - public static final KeyBinding DUPLICATE_KEY_BIND = KeyBindingRegistry.registerKeyBinding( - new KeyBinding("key.qsl.enable_key_bind", GLFW.GLFW_KEY_J, "key.qsl.category"), true - ); - public static final KeyBinding DISABLED_CONFLICT_TEST_KEY_BIND = KeyBindingRegistry.registerKeyBinding( new KeyBinding("key.qsl.disabled_conflict_test", GLFW.GLFW_KEY_H, "key.qsl.category"), false ); - public static final KeyBinding EXACT_SAME_KEY_BIND = KeyBindingRegistry.registerKeyBinding(ENABLE_KEY_BIND); - - public static final KeyBinding NULL_KEY_BIND = KeyBindingRegistry.registerKeyBinding(null); - @Override public void onInitializeClient() { ClientTickEvents.START.register(client -> { @@ -76,6 +67,12 @@ public void onInitializeClient() { client.player.sendMessage(new LiteralText("This is the conflict key being pressed"), false); } } + + if (DISABLED_CONFLICT_TEST_KEY_BIND.isPressed()) { + if (client.player != null) { + client.player.sendMessage(new LiteralText("I'm not supposed to do things! Why am I enabled?"), false); + } + } }); } } From 42e58397c012e9680b724525dfa1f855dd7f6312 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Sun, 5 Dec 2021 13:47:47 -0300 Subject: [PATCH 13/56] But wait, there's more! --- .../quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java index 20ce878f83..f4196976a7 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java @@ -64,7 +64,7 @@ public static KeyBinding registerKeyBinding(KeyBinding key, boolean enabled) { public static KeyBinding getKeyBinding(String translationKey) { for (KeyBinding key : quiltKeys.keySet()) { - if (key.getTranslationKey() == translationKey) { + if (key.getTranslationKey().equals(translationKey)) { return key; } } From 00043a5c98d4bed6649913bc8052e3020da07b77 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Sun, 5 Dec 2021 20:29:39 -0300 Subject: [PATCH 14/56] Improve tooltip formatting --- .../bindings/mixin/client/KeyBindingEntryMixin.java | 10 ++++------ .../assets/quilt_key_bindings/lang/en_us.json | 3 ++- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java index f4d4f8b98e..693c1fd2b3 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java @@ -76,11 +76,7 @@ private void collectConflictTooltips(MatrixStack matrices, int index, int y, int MinecraftClient client = ((EntryListWidgetAccessor) (Object) field_2742).getClient(); for (KeyBinding keyBinding : client.options.keysAll) { if (keyBinding != this.binding && this.binding.equals(keyBinding)) { - if (quilt$conflictTooltips.isEmpty()) { - quilt$conflictTooltips.add(new TranslatableText("key.qsl.key_conflict.tooltip").formatted(Formatting.RED)); - } - - quilt$conflictTooltips.add(new TranslatableText(keyBinding.getTranslationKey()).formatted(Formatting.RED)); + quilt$conflictTooltips.add(new TranslatableText("key.qsl.key_conflict.tooltip.entry", new TranslatableText(keyBinding.getTranslationKey())).formatted(Formatting.RED)); } } } @@ -99,6 +95,8 @@ private Text addConflictIndicator(Text originalText) { @Override public List getConflictTooltips() { - return this.quilt$conflictTooltips; + List returnedText = this.quilt$conflictTooltips; + returnedText.add(0, new TranslatableText("key.qsl.key_conflict.tooltip").formatted(Formatting.RED)); + return returnedText; } } diff --git a/library/gui/key_bindings/src/main/resources/assets/quilt_key_bindings/lang/en_us.json b/library/gui/key_bindings/src/main/resources/assets/quilt_key_bindings/lang/en_us.json index e47076969e..6733e534f0 100644 --- a/library/gui/key_bindings/src/main/resources/assets/quilt_key_bindings/lang/en_us.json +++ b/library/gui/key_bindings/src/main/resources/assets/quilt_key_bindings/lang/en_us.json @@ -1,4 +1,5 @@ { "key.qsl.key_conflict.indicator": "%s (!)", - "key.qsl.key_conflict.tooltip": "Conflicts with: %s" + "key.qsl.key_conflict.tooltip": "Conflicts with:", + "key.qsl.key_conflict.tooltip.entry": " %s" } \ No newline at end of file From 156723ee43947e4c4f3281f128437dbdf1c1e8ea Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Tue, 7 Dec 2021 17:39:55 -0300 Subject: [PATCH 15/56] Progress progress progress --- .../key/bindings/impl/KeyBindingManager.java | 10 ++++- .../bindings/impl/KeyBindingRegistryImpl.java | 15 ++++---- .../mixin/client/GameOptionsAccessor.java | 3 ++ .../mixin/client/GameOptionsMixin.java | 15 +++++--- .../mixin/client/KeyBindingEntryMixin.java | 38 ++++++++++++++----- 5 files changed, 56 insertions(+), 25 deletions(-) diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java index 731b0af1ed..7d082fa4ca 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java @@ -19,6 +19,7 @@ import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; +import net.minecraft.client.MinecraftClient; import net.minecraft.client.option.GameOptions; import net.minecraft.client.option.KeyBinding; @@ -29,12 +30,17 @@ public class KeyBindingManager { private final GameOptions options; private final KeyBinding[] allKeys; - public KeyBindingManager(GameOptions options, KeyBinding[] allKeys) { + public KeyBindingManager(GameOptions options) { this.options = options; - this.allKeys = allKeys; + this.allKeys = ((GameOptionsAccessor) (Object) options).getKeysAll(); this.addModdedKeyBinds(); } + public static KeyBindingManager createFromClientOptions() { + MinecraftClient client = MinecraftClient.getInstance(); + return new KeyBindingManager(client.options); + } + public void addModdedKeyBinds() { ((GameOptionsAccessor) (Object) this.options).setKeysAll(KeyBindingRegistryImpl.getKeyBindings(this.allKeys)); } diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java index f4196976a7..1691526dbc 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java @@ -28,7 +28,6 @@ import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; -import net.minecraft.client.option.GameOptions; import net.minecraft.client.option.KeyBinding; import org.quiltmc.qsl.key.bindings.mixin.client.KeyBindingAccessor; @@ -38,9 +37,9 @@ public class KeyBindingRegistryImpl { public static final Logger LOGGER = LogManager.getLogger(); private static Map quiltKeys = new HashMap<>(); - private static List keyBindingManagers = new ArrayList<>(1); private static KeyBinding[] enabledQuiltKeysArray = new KeyBinding[] {}; private static List disabledQuiltKeys = new ArrayList<>(0); + private static KeyBindingManager keyBindingManager = null; public static KeyBinding registerKeyBinding(KeyBinding key, boolean enabled) { Objects.requireNonNull(key, "Attempted to register a null key bind!"); @@ -96,10 +95,6 @@ public static void setEnabled(KeyBinding key, boolean enabled) { } } - public static void registerKeyBindingManager(GameOptions options, KeyBinding[] allKeys) { - keyBindingManagers.add(new KeyBindingManager(options, allKeys)); - } - public static void updateKeysArray() { List enabledQuiltKeys = new ArrayList<>(); disabledQuiltKeys.clear(); @@ -122,10 +117,14 @@ public static List getDisabledKeyBindings() { return disabledQuiltKeys; } + public static void setupKeyBindingManager() { + keyBindingManager = KeyBindingManager.createFromClientOptions(); + } + public static void applyChanges() { updateKeysArray(); - for (KeyBindingManager manager : keyBindingManagers) { - manager.addModdedKeyBinds(); + if (keyBindingManager != null) { + keyBindingManager.addModdedKeyBinds(); } } } diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsAccessor.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsAccessor.java index 078c9de596..792ebb3976 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsAccessor.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsAccessor.java @@ -28,6 +28,9 @@ @Environment(EnvType.CLIENT) @Mixin(GameOptions.class) public interface GameOptionsAccessor { + @Accessor + KeyBinding[] getKeysAll(); + @Mutable @Accessor void setKeysAll(KeyBinding[] keysAll); diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java index 33b69beed7..6cacde11d1 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java @@ -41,6 +41,9 @@ public abstract class GameOptionsMixin { @Final public KeyBinding[] keysAll; + @Shadow + protected MinecraftClient client; + @Inject( at = @At( value = "INVOKE", @@ -49,7 +52,9 @@ public abstract class GameOptionsMixin { method = "" ) private void modifyAllKeys(MinecraftClient client, File file, CallbackInfo ci) { - KeyBindingRegistryImpl.registerKeyBindingManager((GameOptions) (Object) this, keysAll); + if (client.options == ((GameOptionsAccessor) (Object) this)) { + KeyBindingRegistryImpl.setupKeyBindingManager(); + } } @Inject( @@ -61,10 +66,10 @@ private void modifyAllKeys(MinecraftClient client, File file, CallbackInfo ci) { ) private void includeDisabledEntries(GameOptions.Visitor visitor, CallbackInfo ci) { for (KeyBinding keyBinding : KeyBindingRegistryImpl.getDisabledKeyBindings()) { - String keyKey = keyBinding.getBoundKeyTranslationKey(); - String keyBindKey = visitor.visitString("key_" + keyBinding.getTranslationKey(), keyKey); - if (!keyKey.equals(keyBindKey)) { - keyBinding.setBoundKey(InputUtil.fromTranslationKey(keyKey)); + String keyTranslationKey = keyBinding.getBoundKeyTranslationKey(); + String keyBindTranslationKey = visitor.visitString("key_" + keyBinding.getTranslationKey(), keyTranslationKey); + if (!keyTranslationKey.equals(keyBindTranslationKey)) { + keyBinding.setBoundKey(InputUtil.fromTranslationKey(keyTranslationKey)); } }; } diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java index 693c1fd2b3..0fb2736e34 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java @@ -36,11 +36,13 @@ import net.minecraft.client.gui.widget.option.KeyBindingListWidget; import net.minecraft.client.gui.widget.option.KeyBindingListWidget.KeyBindingEntry; import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.util.InputUtil; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.text.Text; import net.minecraft.text.TranslatableText; import net.minecraft.util.Formatting; +import org.quiltmc.qsl.key.bindings.api.KeyBindingRegistry; import org.quiltmc.qsl.key.bindings.impl.ConflictTooltipOwner; @Environment(EnvType.CLIENT) @@ -55,13 +57,20 @@ public abstract class KeyBindingEntryMixin extends KeyBindingListWidget.Entry im private ButtonWidget bindButton; @Unique - private List quilt$conflictTooltips = new ArrayList<>(1); + private List quilt$conflictTooltips = new ArrayList<>(2); + + @Unique + private InputUtil.Key quilt$previousBoundKey; @Shadow(aliases = "field_2742") @Final KeyBindingListWidget field_2742; - // FIXME - Find a way to get all conflicting keys that reuses the iteration done by Vanilla + @Inject(method = "", at = @At("TAIL")) + private void initPreviousBoundKey(KeyBindingListWidget list, KeyBinding keyBinding, Text text, CallbackInfo ci) { + this.quilt$previousBoundKey = InputUtil.UNKNOWN_KEY; + } + @Inject( method = "render", at = @At( @@ -72,13 +81,24 @@ public abstract class KeyBindingEntryMixin extends KeyBindingListWidget.Entry im locals = LocalCapture.CAPTURE_FAILHARD ) private void collectConflictTooltips(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta, CallbackInfo ci, boolean bl, boolean bl2) { - quilt$conflictTooltips.clear(); - MinecraftClient client = ((EntryListWidgetAccessor) (Object) field_2742).getClient(); - for (KeyBinding keyBinding : client.options.keysAll) { - if (keyBinding != this.binding && this.binding.equals(keyBinding)) { - quilt$conflictTooltips.add(new TranslatableText("key.qsl.key_conflict.tooltip.entry", new TranslatableText(keyBinding.getTranslationKey())).formatted(Formatting.RED)); + InputUtil.Key boundKey = KeyBindingRegistry.getBoundKey(this.binding); + + if (!boundKey.equals(this.quilt$previousBoundKey)) { + this.quilt$conflictTooltips.clear(); + + MinecraftClient client = ((EntryListWidgetAccessor) (Object) field_2742).getClient(); + for (KeyBinding keyBinding : client.options.keysAll) { + if (keyBinding != this.binding && this.binding.equals(keyBinding)) { + if (this.quilt$conflictTooltips.isEmpty()) { + this.quilt$conflictTooltips.add(new TranslatableText("key.qsl.key_conflict.tooltip").formatted(Formatting.RED)); + } + + this.quilt$conflictTooltips.add(new TranslatableText("key.qsl.key_conflict.tooltip.entry", new TranslatableText(keyBinding.getTranslationKey())).formatted(Formatting.RED)); + } } } + + this.quilt$previousBoundKey = boundKey; } @ModifyArg( @@ -95,8 +115,6 @@ private Text addConflictIndicator(Text originalText) { @Override public List getConflictTooltips() { - List returnedText = this.quilt$conflictTooltips; - returnedText.add(0, new TranslatableText("key.qsl.key_conflict.tooltip").formatted(Formatting.RED)); - return returnedText; + return this.quilt$conflictTooltips; } } From 757b3cd3bcaa8b39ac57ec0d994aade30d74b733 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Wed, 8 Dec 2021 18:05:20 -0300 Subject: [PATCH 16/56] Fix key binding manager --- .../qsl/key/bindings/api/KeyBindingRegistry.java | 9 +++++++-- .../qsl/key/bindings/impl/KeyBindingManager.java | 10 ++-------- .../qsl/key/bindings/impl/KeyBindingRegistryImpl.java | 10 +++++++--- .../key/bindings/mixin/client/GameOptionsMixin.java | 8 +++++--- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java index c7d898b800..c98e26609c 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java @@ -115,8 +115,13 @@ 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} */ - public static void setEnabled(KeyBinding key, boolean enabled) { - KeyBindingRegistryImpl.setEnabled(key, enabled); + public static boolean setEnabled(KeyBinding key, boolean enabled) { + return KeyBindingRegistryImpl.setEnabled(key, enabled); + } + + public static void getAllKeyBindings(boolean includeVanilla) { + // TODO - Make me into a real method } } diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java index 7d082fa4ca..731b0af1ed 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java @@ -19,7 +19,6 @@ import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; -import net.minecraft.client.MinecraftClient; import net.minecraft.client.option.GameOptions; import net.minecraft.client.option.KeyBinding; @@ -30,17 +29,12 @@ public class KeyBindingManager { private final GameOptions options; private final KeyBinding[] allKeys; - public KeyBindingManager(GameOptions options) { + public KeyBindingManager(GameOptions options, KeyBinding[] allKeys) { this.options = options; - this.allKeys = ((GameOptionsAccessor) (Object) options).getKeysAll(); + this.allKeys = allKeys; this.addModdedKeyBinds(); } - public static KeyBindingManager createFromClientOptions() { - MinecraftClient client = MinecraftClient.getInstance(); - return new KeyBindingManager(client.options); - } - public void addModdedKeyBinds() { ((GameOptionsAccessor) (Object) this.options).setKeysAll(KeyBindingRegistryImpl.getKeyBindings(this.allKeys)); } diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java index 1691526dbc..f25f250c41 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java @@ -80,7 +80,7 @@ public static boolean isEnabled(KeyBinding key) { throw new IllegalArgumentException(String.format("%s has either not been registered or it is a vanilla key!", key.getTranslationKey())); } - public static void setEnabled(KeyBinding key, boolean enabled) { + public static boolean setEnabled(KeyBinding key, boolean enabled) { if (quiltKeys.containsKey(key)) { quiltKeys.replace(key, enabled); applyChanges(); @@ -92,7 +92,11 @@ public static void setEnabled(KeyBinding key, boolean enabled) { ((KeyBindingAccessor) key).callReset(); KeyBinding.updateKeysByCode(); + + return true; } + + return false; } public static void updateKeysArray() { @@ -117,8 +121,8 @@ public static List getDisabledKeyBindings() { return disabledQuiltKeys; } - public static void setupKeyBindingManager() { - keyBindingManager = KeyBindingManager.createFromClientOptions(); + public static void setKeyBindingManager(KeyBindingManager manager) { + keyBindingManager = manager; } public static void applyChanges() { diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java index 6cacde11d1..16c603d3ec 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java @@ -32,6 +32,7 @@ import net.minecraft.client.option.KeyBinding; import net.minecraft.client.util.InputUtil; +import org.quiltmc.qsl.key.bindings.impl.KeyBindingManager; import org.quiltmc.qsl.key.bindings.impl.KeyBindingRegistryImpl; @Environment(EnvType.CLIENT) @@ -42,7 +43,8 @@ public abstract class GameOptionsMixin { public KeyBinding[] keysAll; @Shadow - protected MinecraftClient client; + @Final + private File optionsFile; @Inject( at = @At( @@ -52,8 +54,8 @@ public abstract class GameOptionsMixin { method = "" ) private void modifyAllKeys(MinecraftClient client, File file, CallbackInfo ci) { - if (client.options == ((GameOptionsAccessor) (Object) this)) { - KeyBindingRegistryImpl.setupKeyBindingManager(); + if (this.optionsFile.equals(new File(file, "options.txt"))) { + KeyBindingRegistryImpl.setKeyBindingManager(new KeyBindingManager((GameOptions) (Object) this, this.keysAll)); } } From b547b721a9aa07a13c80d92db3dbe681123ca34b Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Thu, 9 Dec 2021 00:04:33 -0300 Subject: [PATCH 17/56] Add "Middle Button to Unbind" behavior --- .../mixin/client/ClickableWidgetAccessor.java | 12 +++++++++++ .../mixin/client/KeyBindingEntryMixin.java | 21 ++++++++++++++++++- .../resources/quilt_key_bindings.mixins.json | 1 + 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/ClickableWidgetAccessor.java diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/ClickableWidgetAccessor.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/ClickableWidgetAccessor.java new file mode 100644 index 0000000000..d9c923d606 --- /dev/null +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/ClickableWidgetAccessor.java @@ -0,0 +1,12 @@ +package org.quiltmc.qsl.key.bindings.mixin.client; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Invoker; + +import net.minecraft.client.gui.widget.ClickableWidget; + +@Mixin(ClickableWidget.class) +public interface ClickableWidgetAccessor { + @Invoker + boolean callClicked(double mouseX, double mouseY); +} diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java index 0fb2736e34..21daac89d5 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java @@ -19,6 +19,7 @@ import java.util.ArrayList; import java.util.List; +import org.lwjgl.glfw.GLFW; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -27,6 +28,7 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.ModifyArg; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import org.spongepowered.asm.mixin.injection.callback.LocalCapture; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; @@ -62,7 +64,7 @@ public abstract class KeyBindingEntryMixin extends KeyBindingListWidget.Entry im @Unique private InputUtil.Key quilt$previousBoundKey; - @Shadow(aliases = "field_2742") + @Shadow(remap = false) @Final KeyBindingListWidget field_2742; @@ -117,4 +119,21 @@ private Text addConflictIndicator(Text originalText) { public List getConflictTooltips() { return this.quilt$conflictTooltips; } + + @Inject(method = "mouseClicked", at = @At("HEAD"), cancellable = true) + private void addMiddleButtonBehavior(double mouseX, double mouseY, int button, CallbackInfoReturnable cir) { + if (button == GLFW.GLFW_MOUSE_BUTTON_MIDDLE) { + if (this.bindButton.active && this.bindButton.visible) { + if (((ClickableWidgetAccessor) this.bindButton).callClicked(mouseX, mouseY)) { + this.bindButton.playDownSound(MinecraftClient.getInstance().getSoundManager()); + this.binding.setBoundKey(InputUtil.UNKNOWN_KEY); + KeyBinding.updateKeysByCode(); + + cir.setReturnValue(true); + } + } + } + + System.out.println(String.format("Hmmmm, is %s = %s?", button, GLFW.GLFW_MOUSE_BUTTON_MIDDLE)); + } } diff --git a/library/gui/key_bindings/src/main/resources/quilt_key_bindings.mixins.json b/library/gui/key_bindings/src/main/resources/quilt_key_bindings.mixins.json index 4949cb8c13..0a1299051c 100644 --- a/library/gui/key_bindings/src/main/resources/quilt_key_bindings.mixins.json +++ b/library/gui/key_bindings/src/main/resources/quilt_key_bindings.mixins.json @@ -3,6 +3,7 @@ "package": "org.quiltmc.qsl.key.bindings.mixin", "compatibilityLevel": "JAVA_17", "client": [ + "client.ClickableWidgetAccessor", "client.EntryListWidgetAccessor", "client.GameOptionsAccessor", "client.GameOptionsMixin", From aaa1290b4241b48c11083326792421738f07efb5 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Thu, 9 Dec 2021 00:15:56 -0300 Subject: [PATCH 18/56] oof --- .../mixin/client/ClickableWidgetAccessor.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/ClickableWidgetAccessor.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/ClickableWidgetAccessor.java index d9c923d606..70a85ef472 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/ClickableWidgetAccessor.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/ClickableWidgetAccessor.java @@ -1,3 +1,19 @@ +/* + * Copyright 2021 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.quiltmc.qsl.key.bindings.mixin.client; import org.spongepowered.asm.mixin.Mixin; From 58ba47990251b8569d273aca4fa4d3cbcad52bcd Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Thu, 9 Dec 2021 08:46:15 -0300 Subject: [PATCH 19/56] Fix conflict tooltips --- .../key/bindings/impl/KeyBindingManager.java | 7 +++- .../bindings/impl/KeyBindingRegistryImpl.java | 4 +- .../mixin/client/GameOptionsMixin.java | 3 ++ .../mixin/client/KeyBindingEntryMixin.java | 40 ++++++++++++------- .../mixin/client/KeyBindingMixin.java | 2 + 5 files changed, 37 insertions(+), 19 deletions(-) diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java index 731b0af1ed..ee19be1422 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java @@ -32,10 +32,13 @@ public class KeyBindingManager { public KeyBindingManager(GameOptions options, KeyBinding[] allKeys) { this.options = options; this.allKeys = allKeys; - this.addModdedKeyBinds(); } public void addModdedKeyBinds() { - ((GameOptionsAccessor) (Object) this.options).setKeysAll(KeyBindingRegistryImpl.getKeyBindings(this.allKeys)); + ((GameOptionsAccessor) (Object) this.options).setKeysAll(KeyBindingRegistryImpl.getKeyBindings()); + } + + public KeyBinding[] getAllKeys() { + return this.allKeys; } } diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java index f25f250c41..70c9c5d61d 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java @@ -113,8 +113,8 @@ public static void updateKeysArray() { enabledQuiltKeysArray = enabledQuiltKeys.toArray(new KeyBinding[enabledQuiltKeys.size()]); } - public static KeyBinding[] getKeyBindings(KeyBinding[] allVanillaKeys) { - return ArrayUtils.addAll(allVanillaKeys, enabledQuiltKeysArray); + public static KeyBinding[] getKeyBindings() { + return ArrayUtils.addAll(keyBindingManager.getAllKeys(), enabledQuiltKeysArray); } public static List getDisabledKeyBindings() { diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java index 16c603d3ec..22aa6b01b2 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java @@ -20,6 +20,7 @@ import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Mutable; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -39,6 +40,7 @@ @Mixin(GameOptions.class) public abstract class GameOptionsMixin { @Shadow + @Mutable @Final public KeyBinding[] keysAll; @@ -56,6 +58,7 @@ public abstract class GameOptionsMixin { private void modifyAllKeys(MinecraftClient client, File file, CallbackInfo ci) { if (this.optionsFile.equals(new File(file, "options.txt"))) { KeyBindingRegistryImpl.setKeyBindingManager(new KeyBindingManager((GameOptions) (Object) this, this.keysAll)); + this.keysAll = KeyBindingRegistryImpl.getKeyBindings(); } } diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java index 21daac89d5..f0e96270ba 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java @@ -46,6 +46,7 @@ import org.quiltmc.qsl.key.bindings.api.KeyBindingRegistry; import org.quiltmc.qsl.key.bindings.impl.ConflictTooltipOwner; +import org.quiltmc.qsl.key.bindings.impl.KeyBindingRegistryImpl; @Environment(EnvType.CLIENT) @Mixin(KeyBindingEntry.class) @@ -64,38 +65,48 @@ public abstract class KeyBindingEntryMixin extends KeyBindingListWidget.Entry im @Unique private InputUtil.Key quilt$previousBoundKey; + @Unique + private static InputUtil.Key quilt$changedBoundKey; + @Shadow(remap = false) @Final KeyBindingListWidget field_2742; @Inject(method = "", at = @At("TAIL")) private void initPreviousBoundKey(KeyBindingListWidget list, KeyBinding keyBinding, Text text, CallbackInfo ci) { - this.quilt$previousBoundKey = InputUtil.UNKNOWN_KEY; + this.quilt$previousBoundKey = null; + quilt$changedBoundKey = null; } @Inject( method = "render", at = @At( - value = "INVOKE_ASSIGN", - target = "Lnet/minecraft/client/option/KeyBinding;isUnbound()Z", - shift = At.Shift.AFTER + value = "INVOKE", + target = "Lnet/minecraft/client/option/KeyBinding;isUnbound()Z" ), locals = LocalCapture.CAPTURE_FAILHARD ) private void collectConflictTooltips(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta, CallbackInfo ci, boolean bl, boolean bl2) { InputUtil.Key boundKey = KeyBindingRegistry.getBoundKey(this.binding); - if (!boundKey.equals(this.quilt$previousBoundKey)) { + if (!boundKey.equals(this.quilt$previousBoundKey) || quilt$changedBoundKey != null) { this.quilt$conflictTooltips.clear(); - MinecraftClient client = ((EntryListWidgetAccessor) (Object) field_2742).getClient(); - for (KeyBinding keyBinding : client.options.keysAll) { - if (keyBinding != this.binding && this.binding.equals(keyBinding)) { - if (this.quilt$conflictTooltips.isEmpty()) { - this.quilt$conflictTooltips.add(new TranslatableText("key.qsl.key_conflict.tooltip").formatted(Formatting.RED)); - } + if (quilt$changedBoundKey == null) { + quilt$changedBoundKey = boundKey; + } else if (quilt$changedBoundKey.equals(boundKey)) { + quilt$changedBoundKey = null; + } + + if (!this.binding.isUnbound()) { + for (KeyBinding keyBinding : KeyBindingRegistryImpl.getKeyBindings()) { + if (keyBinding != this.binding && this.binding.equals(keyBinding)) { + if (this.quilt$conflictTooltips.isEmpty()) { + this.quilt$conflictTooltips.add(new TranslatableText("key.qsl.key_conflict.tooltip").formatted(Formatting.RED)); + } - this.quilt$conflictTooltips.add(new TranslatableText("key.qsl.key_conflict.tooltip.entry", new TranslatableText(keyBinding.getTranslationKey())).formatted(Formatting.RED)); + this.quilt$conflictTooltips.add(new TranslatableText("key.qsl.key_conflict.tooltip.entry", new TranslatableText(keyBinding.getTranslationKey())).formatted(Formatting.RED)); + } } } } @@ -125,7 +136,8 @@ private void addMiddleButtonBehavior(double mouseX, double mouseY, int button, C if (button == GLFW.GLFW_MOUSE_BUTTON_MIDDLE) { if (this.bindButton.active && this.bindButton.visible) { if (((ClickableWidgetAccessor) this.bindButton).callClicked(mouseX, mouseY)) { - this.bindButton.playDownSound(MinecraftClient.getInstance().getSoundManager()); + MinecraftClient client = ((EntryListWidgetAccessor) (Object) field_2742).getClient(); + this.bindButton.playDownSound(client.getSoundManager()); this.binding.setBoundKey(InputUtil.UNKNOWN_KEY); KeyBinding.updateKeysByCode(); @@ -133,7 +145,5 @@ private void addMiddleButtonBehavior(double mouseX, double mouseY, int button, C } } } - - System.out.println(String.format("Hmmmm, is %s = %s?", button, GLFW.GLFW_MOUSE_BUTTON_MIDDLE)); } } diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingMixin.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingMixin.java index e2e06989c1..5e2207d54b 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingMixin.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingMixin.java @@ -21,6 +21,7 @@ import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mutable; +import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @@ -33,6 +34,7 @@ @Environment(EnvType.CLIENT) @Mixin(KeyBinding.class) public abstract class KeyBindingMixin { + @Shadow @Mutable @Final private static Map CATEGORY_ORDER_MAP; From fd213286e30d8e9ecbb15f2cd4b002008fca9440 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Thu, 9 Dec 2021 17:56:32 -0300 Subject: [PATCH 20/56] Add registry test, progress --- .../bindings/impl/KeyBindingRegistryImpl.java | 8 +++-- .../mixin/client/ClickableWidgetAccessor.java | 3 ++ .../client/KeyBindingRegistryTestMod.java | 35 +++++++++++++++++++ .../test/client/KeyBindingsTestMod.java | 3 ++ .../src/testmod/resources/fabric.mod.json | 1 + 5 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingRegistryTestMod.java diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java index 70c9c5d61d..74c244beb8 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java @@ -17,6 +17,7 @@ package org.quiltmc.qsl.key.bindings.impl; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -76,8 +77,11 @@ public static boolean isEnabled(KeyBinding key) { return quiltKeys.get(key); } - // TODO - Split those two - throw new IllegalArgumentException(String.format("%s has either not been registered or it is a vanilla key!", key.getTranslationKey())); + 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())); } public static boolean setEnabled(KeyBinding key, boolean enabled) { diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/ClickableWidgetAccessor.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/ClickableWidgetAccessor.java index 70a85ef472..dc050aa10e 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/ClickableWidgetAccessor.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/ClickableWidgetAccessor.java @@ -18,9 +18,12 @@ import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.gen.Invoker; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; import net.minecraft.client.gui.widget.ClickableWidget; +@Environment(EnvType.CLIENT) @Mixin(ClickableWidget.class) public interface ClickableWidgetAccessor { @Invoker diff --git a/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingRegistryTestMod.java b/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingRegistryTestMod.java new file mode 100644 index 0000000000..8c6d335650 --- /dev/null +++ b/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingRegistryTestMod.java @@ -0,0 +1,35 @@ +package org.quiltmc.qsl.key.bindings.test.client; + +import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import net.minecraft.client.option.KeyBinding; + +import org.quiltmc.qsl.key.bindings.api.KeyBindingRegistry; +import org.quiltmc.qsl.lifecycle.api.client.event.ClientLifecycleEvents; +import org.quiltmc.qsl.lifecycle.api.client.event.ClientTickEvents; + +@Environment(EnvType.CLIENT) +public class KeyBindingRegistryTestMod implements ClientModInitializer { + public static final Logger LOGGER = LogManager.getLogger("KeyBindingRegistryTest"); + + @Override + public void onInitializeClient() { + ClientLifecycleEvents.READY.register(lifecycleClient -> { + KeyBinding enableKeyBindKey = KeyBindingRegistry.getKeyBinding("key.qsl.enable_key_bind"); + + if (enableKeyBindKey != null) { + LOGGER.info("Successfully got the \"Enable Key Bind\" key!"); + + ClientTickEvents.END.register(tickClient -> { + if (enableKeyBindKey.wasPressed()) { + LOGGER.info("I can add behavior to other keys!"); + } + }); + } + }); + } +} diff --git a/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java b/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java index 712fbe6bc2..09a2843528 100644 --- a/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java +++ b/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java @@ -18,6 +18,8 @@ import org.lwjgl.glfw.GLFW; import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; import net.minecraft.client.option.KeyBinding; import net.minecraft.text.LiteralText; @@ -25,6 +27,7 @@ import org.quiltmc.qsl.key.bindings.api.KeyBindingRegistry; import org.quiltmc.qsl.lifecycle.api.client.event.ClientTickEvents; +@Environment(EnvType.CLIENT) public class KeyBindingsTestMod implements ClientModInitializer { // A conflicting key test public static final KeyBinding CONFLICT_TEST_KEY_BIND = KeyBindingRegistry.registerKeyBinding( diff --git a/library/gui/key_bindings/src/testmod/resources/fabric.mod.json b/library/gui/key_bindings/src/testmod/resources/fabric.mod.json index 65d4c6174e..0f98e74b3f 100644 --- a/library/gui/key_bindings/src/testmod/resources/fabric.mod.json +++ b/library/gui/key_bindings/src/testmod/resources/fabric.mod.json @@ -10,6 +10,7 @@ }, "entrypoints": { "client": [ + "org.quiltmc.qsl.key.bindings.test.client.KeyBindingRegistryTestMod", "org.quiltmc.qsl.key.bindings.test.client.KeyBindingsTestMod" ] } From dd28d6da8f38da4a10ec12f0e46ffb6243fddf4a Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Fri, 10 Dec 2021 16:36:14 -0300 Subject: [PATCH 21/56] Mappings update --- .../quiltmc/qsl/key/bindings/impl/KeyBindingManager.java | 2 +- .../key/bindings/mixin/client/GameOptionsAccessor.java | 4 ++-- .../qsl/key/bindings/mixin/client/GameOptionsMixin.java | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java index ee19be1422..0866925e79 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java @@ -35,7 +35,7 @@ public KeyBindingManager(GameOptions options, KeyBinding[] allKeys) { } public void addModdedKeyBinds() { - ((GameOptionsAccessor) (Object) this.options).setKeysAll(KeyBindingRegistryImpl.getKeyBindings()); + ((GameOptionsAccessor) (Object) this.options).setAllKeys(KeyBindingRegistryImpl.getKeyBindings()); } public KeyBinding[] getAllKeys() { diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsAccessor.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsAccessor.java index 792ebb3976..6720cd6e10 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsAccessor.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsAccessor.java @@ -29,9 +29,9 @@ @Mixin(GameOptions.class) public interface GameOptionsAccessor { @Accessor - KeyBinding[] getKeysAll(); + KeyBinding[] getAllKeys(); @Mutable @Accessor - void setKeysAll(KeyBinding[] keysAll); + void setAllKeys(KeyBinding[] allKeys); } diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java index 22aa6b01b2..1097cafc3e 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java @@ -42,7 +42,7 @@ public abstract class GameOptionsMixin { @Shadow @Mutable @Final - public KeyBinding[] keysAll; + public KeyBinding[] allKeys; @Shadow @Final @@ -57,15 +57,15 @@ public abstract class GameOptionsMixin { ) private void modifyAllKeys(MinecraftClient client, File file, CallbackInfo ci) { if (this.optionsFile.equals(new File(file, "options.txt"))) { - KeyBindingRegistryImpl.setKeyBindingManager(new KeyBindingManager((GameOptions) (Object) this, this.keysAll)); - this.keysAll = KeyBindingRegistryImpl.getKeyBindings(); + KeyBindingRegistryImpl.setKeyBindingManager(new KeyBindingManager((GameOptions) (Object) this, this.allKeys)); + this.allKeys = KeyBindingRegistryImpl.getKeyBindings(); } } @Inject( at = @At( value = "FIELD", - target = "Lnet/minecraft/client/option/GameOptions;keysAll:[Lnet/minecraft/client/option/KeyBinding;" + target = "Lnet/minecraft/client/option/GameOptions;allKeys:[Lnet/minecraft/client/option/KeyBinding;" ), method = "accept(Lnet/minecraft/client/option/GameOptions$Visitor;)V" ) From 80c249040424934f87e4acaf7245583ce68bd6ba Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Fri, 10 Dec 2021 20:23:24 -0300 Subject: [PATCH 22/56] Finish the v1 feature set --- .../key/bindings/api/KeyBindingRegistry.java | 18 ++++++-- .../bindings/impl/KeyBindingRegistryImpl.java | 46 ++++++++++++------- .../client/KeyBindingRegistryTestMod.java | 7 ++- 3 files changed, 49 insertions(+), 22 deletions(-) diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java index c98e26609c..6e00237d62 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java @@ -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; @@ -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 getAllKeyBindings(boolean includeVanilla) { + return KeyBindingRegistryImpl.getAllKeyBindings(includeVanilla); } } diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java index 74c244beb8..bd251e704e 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java @@ -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); @@ -96,14 +101,26 @@ public static boolean setEnabled(KeyBinding key, boolean enabled) { ((KeyBindingAccessor) key).callReset(); KeyBinding.updateKeysByCode(); + } + + throwUnregisteredKeyException(key); + } - return true; + public static Map getAllKeyBindings(boolean includeVanilla) { + Map 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 enabledQuiltKeys = new ArrayList<>(); disabledQuiltKeys.clear(); for (var entry : quiltKeys.entrySet()) { @@ -115,6 +132,10 @@ public static void updateKeysArray() { } enabledQuiltKeysArray = enabledQuiltKeys.toArray(new KeyBinding[enabledQuiltKeys.size()]); + + if (keyBindingManager != null) { + keyBindingManager.addModdedKeyBinds(); + } } public static KeyBinding[] getKeyBindings() { @@ -128,11 +149,4 @@ public static List getDisabledKeyBindings() { public static void setKeyBindingManager(KeyBindingManager manager) { keyBindingManager = manager; } - - public static void applyChanges() { - updateKeysArray(); - if (keyBindingManager != null) { - keyBindingManager.addModdedKeyBinds(); - } - } } diff --git a/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingRegistryTestMod.java b/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingRegistryTestMod.java index 8c6d335650..8da3e29895 100644 --- a/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingRegistryTestMod.java +++ b/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingRegistryTestMod.java @@ -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() { @@ -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); + }); }); } } From 1da6af0c08bf637df96e234c99b492338f77cf18 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Fri, 10 Dec 2021 20:51:17 -0300 Subject: [PATCH 23/56] Fix issues --- .../qsl/key/bindings/impl/KeyBindingRegistryImpl.java | 8 ++++---- .../key/bindings/mixin/client/KeyBindingEntryMixin.java | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java index bd251e704e..ffbdbdced0 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java @@ -83,9 +83,9 @@ public static boolean throwUnregisteredKeyException(KeyBinding key) { public static boolean isEnabled(KeyBinding key) { if (quiltKeys.containsKey(key)) { return quiltKeys.get(key); + } else { + return throwUnregisteredKeyException(key); } - - return throwUnregisteredKeyException(key); } public static void setEnabled(KeyBinding key, boolean enabled) { @@ -101,9 +101,9 @@ public static void setEnabled(KeyBinding key, boolean enabled) { ((KeyBindingAccessor) key).callReset(); KeyBinding.updateKeysByCode(); + } else { + throwUnregisteredKeyException(key); } - - throwUnregisteredKeyException(key); } public static Map getAllKeyBindings(boolean includeVanilla) { diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java index f0e96270ba..68b796afe7 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java @@ -68,7 +68,7 @@ public abstract class KeyBindingEntryMixin extends KeyBindingListWidget.Entry im @Unique private static InputUtil.Key quilt$changedBoundKey; - @Shadow(remap = false) + @Shadow(aliases = "field_2742", remap = false) @Final KeyBindingListWidget field_2742; @@ -92,10 +92,10 @@ private void collectConflictTooltips(MatrixStack matrices, int index, int y, int if (!boundKey.equals(this.quilt$previousBoundKey) || quilt$changedBoundKey != null) { this.quilt$conflictTooltips.clear(); - if (quilt$changedBoundKey == null) { - quilt$changedBoundKey = boundKey; - } else if (quilt$changedBoundKey.equals(boundKey)) { + if (quilt$changedBoundKey != null && quilt$changedBoundKey.equals(boundKey)) { quilt$changedBoundKey = null; + } else { + quilt$changedBoundKey = boundKey; } if (!this.binding.isUnbound()) { From f38908bef3e48755e245afa91636338504f704ec Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Fri, 10 Dec 2021 23:26:00 -0300 Subject: [PATCH 24/56] whyyyyyyyyy --- .../test/client/KeyBindingRegistryTestMod.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingRegistryTestMod.java b/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingRegistryTestMod.java index 8da3e29895..902c3e83eb 100644 --- a/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingRegistryTestMod.java +++ b/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingRegistryTestMod.java @@ -1,3 +1,19 @@ +/* + * Copyright 2021 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.quiltmc.qsl.key.bindings.test.client; import net.fabricmc.api.ClientModInitializer; From ae29f50c2893e69a829a2cbd8c319bc81b7dc2a7 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Sat, 11 Dec 2021 14:02:27 -0300 Subject: [PATCH 25/56] Remove "Middle Click to Unbind" Because Vanilla actually has a way to unbind keys: "Select and Press Esc to Unbind" --- .../mixin/client/ClickableWidgetAccessor.java | 31 ------------------- .../mixin/client/KeyBindingEntryMixin.java | 19 ------------ .../resources/quilt_key_bindings.mixins.json | 1 - 3 files changed, 51 deletions(-) delete mode 100644 library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/ClickableWidgetAccessor.java diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/ClickableWidgetAccessor.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/ClickableWidgetAccessor.java deleted file mode 100644 index dc050aa10e..0000000000 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/ClickableWidgetAccessor.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2021 QuiltMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.quiltmc.qsl.key.bindings.mixin.client; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.gen.Invoker; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; - -import net.minecraft.client.gui.widget.ClickableWidget; - -@Environment(EnvType.CLIENT) -@Mixin(ClickableWidget.class) -public interface ClickableWidgetAccessor { - @Invoker - boolean callClicked(double mouseX, double mouseY); -} diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java index 68b796afe7..33af521154 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java +++ b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java @@ -19,7 +19,6 @@ import java.util.ArrayList; import java.util.List; -import org.lwjgl.glfw.GLFW; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -28,12 +27,10 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.ModifyArg; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import org.spongepowered.asm.mixin.injection.callback.LocalCapture; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; -import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.widget.ButtonWidget; import net.minecraft.client.gui.widget.option.KeyBindingListWidget; import net.minecraft.client.gui.widget.option.KeyBindingListWidget.KeyBindingEntry; @@ -130,20 +127,4 @@ private Text addConflictIndicator(Text originalText) { public List getConflictTooltips() { return this.quilt$conflictTooltips; } - - @Inject(method = "mouseClicked", at = @At("HEAD"), cancellable = true) - private void addMiddleButtonBehavior(double mouseX, double mouseY, int button, CallbackInfoReturnable cir) { - if (button == GLFW.GLFW_MOUSE_BUTTON_MIDDLE) { - if (this.bindButton.active && this.bindButton.visible) { - if (((ClickableWidgetAccessor) this.bindButton).callClicked(mouseX, mouseY)) { - MinecraftClient client = ((EntryListWidgetAccessor) (Object) field_2742).getClient(); - this.bindButton.playDownSound(client.getSoundManager()); - this.binding.setBoundKey(InputUtil.UNKNOWN_KEY); - KeyBinding.updateKeysByCode(); - - cir.setReturnValue(true); - } - } - } - } } diff --git a/library/gui/key_bindings/src/main/resources/quilt_key_bindings.mixins.json b/library/gui/key_bindings/src/main/resources/quilt_key_bindings.mixins.json index 0a1299051c..4949cb8c13 100644 --- a/library/gui/key_bindings/src/main/resources/quilt_key_bindings.mixins.json +++ b/library/gui/key_bindings/src/main/resources/quilt_key_bindings.mixins.json @@ -3,7 +3,6 @@ "package": "org.quiltmc.qsl.key.bindings.mixin", "compatibilityLevel": "JAVA_17", "client": [ - "client.ClickableWidgetAccessor", "client.EntryListWidgetAccessor", "client.GameOptionsAccessor", "client.GameOptionsMixin", From 709341beeb5075d7601e1fc5de92304757b8f99c Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Fri, 11 Feb 2022 13:12:32 -0300 Subject: [PATCH 26/56] Begin refactor to Key Binds, cleanup tooltip mixin More will come after the mappings update --- .../key/bindings/api/KeyBindingRegistry.java | 135 ------------------ .../src/testmod/resources/fabric.mod.json | 17 --- .../{key_bindings => key_binds}/build.gradle | 4 +- .../qsl/key/binds/api/KeyBindRegistry.java | 135 ++++++++++++++++++ .../key/binds}/impl/ConflictTooltipOwner.java | 4 +- .../qsl/key/binds/impl/KeyBindManager.java} | 14 +- .../key/binds/impl/KeyBindRegistryImpl.java} | 44 +++--- .../mixin/client/EntryListWidgetAccessor.java | 12 +- .../mixin/client/GameOptionsAccessor.java | 2 +- .../binds}/mixin/client/GameOptionsMixin.java | 19 ++- .../mixin/client/KeyBindingAccessor.java | 2 +- .../mixin/client/KeyBindingEntryMixin.java | 17 ++- .../binds}/mixin/client/KeyBindingMixin.java | 2 +- .../mixin/client/KeyBindingsScreenMixin.java | 20 ++- .../assets/quilt_key_binds}/icon.png | Bin .../assets/quilt_key_binds}/lang/en_us.json | 0 .../src/main/resources/fabric.mod.json | 14 +- .../resources/quilt_key_binds.accesswidener} | 0 .../resources/quilt_key_binds.mixins.json} | 2 +- .../test/client/KeyBindRegistryTestMod.java} | 16 +-- .../binds/test/client/KeyBindsTestMod.java} | 28 ++-- .../quilt_key_binds_testmod}/lang/en_us.json | 2 +- .../src/testmod/resources/fabric.mod.json | 17 +++ 23 files changed, 255 insertions(+), 251 deletions(-) delete mode 100644 library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java delete mode 100644 library/gui/key_bindings/src/testmod/resources/fabric.mod.json rename library/gui/{key_bindings => key_binds}/build.gradle (59%) create mode 100644 library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java rename library/gui/{key_bindings/src/main/java/org/quiltmc/qsl/key/bindings => key_binds/src/main/java/org/quiltmc/qsl/key/binds}/impl/ConflictTooltipOwner.java (88%) rename library/gui/{key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java => key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindManager.java} (78%) rename library/gui/{key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java => key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java} (75%) rename library/gui/{key_bindings/src/main/java/org/quiltmc/qsl/key/bindings => key_binds/src/main/java/org/quiltmc/qsl/key/binds}/mixin/client/EntryListWidgetAccessor.java (78%) rename library/gui/{key_bindings/src/main/java/org/quiltmc/qsl/key/bindings => key_binds/src/main/java/org/quiltmc/qsl/key/binds}/mixin/client/GameOptionsAccessor.java (95%) rename library/gui/{key_bindings/src/main/java/org/quiltmc/qsl/key/bindings => key_binds/src/main/java/org/quiltmc/qsl/key/binds}/mixin/client/GameOptionsMixin.java (77%) rename library/gui/{key_bindings/src/main/java/org/quiltmc/qsl/key/bindings => key_binds/src/main/java/org/quiltmc/qsl/key/binds}/mixin/client/KeyBindingAccessor.java (95%) rename library/gui/{key_bindings/src/main/java/org/quiltmc/qsl/key/bindings => key_binds/src/main/java/org/quiltmc/qsl/key/binds}/mixin/client/KeyBindingEntryMixin.java (87%) rename library/gui/{key_bindings/src/main/java/org/quiltmc/qsl/key/bindings => key_binds/src/main/java/org/quiltmc/qsl/key/binds}/mixin/client/KeyBindingMixin.java (97%) rename library/gui/{key_bindings/src/main/java/org/quiltmc/qsl/key/bindings => key_binds/src/main/java/org/quiltmc/qsl/key/binds}/mixin/client/KeyBindingsScreenMixin.java (75%) rename library/gui/{key_bindings/src/main/resources/assets/quilt_key_bindings => key_binds/src/main/resources/assets/quilt_key_binds}/icon.png (100%) rename library/gui/{key_bindings/src/main/resources/assets/quilt_key_bindings => key_binds/src/main/resources/assets/quilt_key_binds}/lang/en_us.json (100%) rename library/gui/{key_bindings => key_binds}/src/main/resources/fabric.mod.json (58%) rename library/gui/{key_bindings/src/main/resources/quilt_key_bindings.accesswidener => key_binds/src/main/resources/quilt_key_binds.accesswidener} (100%) rename library/gui/{key_bindings/src/main/resources/quilt_key_bindings.mixins.json => key_binds/src/main/resources/quilt_key_binds.mixins.json} (87%) rename library/gui/{key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingRegistryTestMod.java => key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java} (73%) rename library/gui/{key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java => key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java} (70%) rename library/gui/{key_bindings/src/testmod/resources/assets/quilt_key_bindings_testmod => key_binds/src/testmod/resources/assets/quilt_key_binds_testmod}/lang/en_us.json (80%) create mode 100644 library/gui/key_binds/src/testmod/resources/fabric.mod.json diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java b/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java deleted file mode 100644 index 6e00237d62..0000000000 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/api/KeyBindingRegistry.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * Copyright 2021 QuiltMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.quiltmc.qsl.key.bindings.api; - -import java.util.Map; - -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; - -import net.minecraft.client.option.KeyBinding; -import net.minecraft.client.util.InputUtil; - -import org.quiltmc.qsl.key.bindings.impl.KeyBindingRegistryImpl; -import org.quiltmc.qsl.key.bindings.mixin.client.KeyBindingAccessor; - -/** - * Handles the registration of modded key binds and allows for changing their properties, - * such as their state. - * - *

This class also allows for getting key binds registered by other mods. - */ -@Environment(EnvType.CLIENT) -public class KeyBindingRegistry { - /** - * Registers a key binding with it being initially enabled. - * - *

-	 * {@code
-	 * public static final KeyBinding EXAMPLE_KEY_BIND = KeyBinding.registerKeyBinding(
-	 * 	new KeyBinding("key.modid.example", GLFW.GLFW_KEY_M, "key.modid.category")
-	 * )
-	 * }
-	 * 
- * - * @param key the key binding to be registered - * @return the successfully-registered key binding - * @throws NullPointerException if {@code key} is null - * @throws IllegalArgumentException if either {@code key} or it's translation key is already registered - */ - public static KeyBinding registerKeyBinding(KeyBinding key) { - return registerKeyBinding(key, true); - } - - /** - * Registers a key binding with the specified initial state. - * - * @param key the key binding to be registered - * @param enabled the key binding's initial state - * @return the key binding if successfully registered, {@code null} otherwise - * @throws NullPointerException if {@code key} is null - * @throws IllegalArgumentException if either {@code key} or it's translation key is already registered - */ - public static KeyBinding registerKeyBinding(KeyBinding key, boolean enabled) { - return KeyBindingRegistryImpl.registerKeyBinding(key, enabled); - } - - /** - * Searches for a modded key binding with the specified translation key in the registry. - * - * @param translationKey the translation key of the key binding - * @return the key binding if it was found, {@code null} otherwise - */ - public static KeyBinding getKeyBinding(String translationKey) { - return KeyBindingRegistryImpl.getKeyBinding(translationKey); - } - - /** - * Gets the bound key of the key binding. - * - *

The bound key is only directly used by the key bind system's internal logic. - * If possible, use the method provided by the KeyBinding class. - * - * @param key the key binding - * @return the key binding's bound key - */ - public static InputUtil.Key getBoundKey(KeyBinding key) { - return ((KeyBindingAccessor) key).getBoundKey(); - } - - /** - * Gets the state of the key binding. - * - *

The state of a key binding determines whenever it's disabled or not. - * - *

When a keybind is disabled, it is effectively hidden from the game, - * being non-existent to it. options.txt is the only exception, who still - * stores the disabled key binds. - * - * @param key the key binding - * @return {@code true} if the key binding is enabled, {@code false} otherwise - * @throws IllegalArgumentException if {@code key} is either unregistered or a Vanilla key bind - */ - public static boolean isEnabled(KeyBinding key) { - return KeyBindingRegistryImpl.isEnabled(key); - } - - /** - * Sets the state of the key binding. - * - *

This method allows for disabling the key binding, making it invisible - * to the game. options.txt is the only exception, who still stores the disabled - * key binds. - * - * @param key the key binding - * @param enabled the new state - * @throws IllegalArgumentException if {@code key} is either unregistered or a Vanilla key bind - */ - public static void setEnabled(KeyBinding key, boolean enabled) { - KeyBindingRegistryImpl.setEnabled(key, enabled); - } - - /** - * 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 getAllKeyBindings(boolean includeVanilla) { - return KeyBindingRegistryImpl.getAllKeyBindings(includeVanilla); - } -} diff --git a/library/gui/key_bindings/src/testmod/resources/fabric.mod.json b/library/gui/key_bindings/src/testmod/resources/fabric.mod.json deleted file mode 100644 index 0f98e74b3f..0000000000 --- a/library/gui/key_bindings/src/testmod/resources/fabric.mod.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "schemaVersion": 1, - "id": "quilt_key_bindings_testmod", - "name": "Quilt Key Bindings API Test Mod", - "version": "1.0.0", - "environment": "*", - "license": "Apache-2.0", - "depends": { - "quilt_key_bindings": "*" - }, - "entrypoints": { - "client": [ - "org.quiltmc.qsl.key.bindings.test.client.KeyBindingRegistryTestMod", - "org.quiltmc.qsl.key.bindings.test.client.KeyBindingsTestMod" - ] - } -} diff --git a/library/gui/key_bindings/build.gradle b/library/gui/key_binds/build.gradle similarity index 59% rename from library/gui/key_bindings/build.gradle rename to library/gui/key_binds/build.gradle index 39fec62295..5ee28c50d0 100644 --- a/library/gui/key_bindings/build.gradle +++ b/library/gui/key_binds/build.gradle @@ -3,7 +3,7 @@ plugins { } qslModule { - moduleName = "key_bindings" + moduleName = "key_binds" version = "1.0.0" library = "gui" coreTestmodDependencies([ @@ -13,5 +13,5 @@ qslModule { } loom { - accessWidenerPath = file("src/main/resources/quilt_key_bindings.accesswidener") + accessWidenerPath = file("src/main/resources/quilt_key_binds.accesswidener") } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java new file mode 100644 index 0000000000..e374521672 --- /dev/null +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java @@ -0,0 +1,135 @@ +/* + * Copyright 2021 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.quiltmc.qsl.key.binds.api; + +import java.util.Map; + +import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; +import org.quiltmc.qsl.key.binds.mixin.client.KeyBindingAccessor; + +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; + +import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.util.InputUtil; + +/** + * Handles the registration of modded key binds and allows for changing their properties, + * such as their state. + * + *

This class also allows for getting key binds registered by other mods. + */ +@Environment(EnvType.CLIENT) +public class KeyBindRegistry { + /** + * Registers a key bind, with it being initially enabled. + * + *

+	 * {@code
+	 * public static final KeyBind EXAMPLE_KEY_BIND = KeyBindRegistry.registerKeyBind(
+	 * 	new KeyBind("key.modid.example", GLFW.GLFW_KEY_M, "key.modid.category")
+	 * )
+	 * }
+	 * 
+ * + * @param key the key bind to be registered + * @return the successfully-registered key bind + * @throws NullPointerException if {@code key} is null + * @throws IllegalArgumentException if either {@code key} or it's translation key is already registered + */ + public static KeyBinding registerKeyBind(KeyBinding key) { + return registerKeyBind(key, true); + } + + /** + * Registers a key bind with a specified initial state. + * + * @param key the key bind to be registered + * @param enabled the key bind's initial state + * @return the key bind if successfully registered, {@code null} otherwise + * @throws NullPointerException if {@code key} is null + * @throws IllegalArgumentException if either {@code key} or it's translation key is already registered + */ + public static KeyBinding registerKeyBind(KeyBinding key, boolean enabled) { + return KeyBindRegistryImpl.registerKeyBind(key, enabled); + } + + /** + * Searches for a modded key bind with the specified translation key in the registry. + * + * @param translationKey the key bind's translation key + * @return the key bind if found, {@code null} otherwise + */ + public static KeyBinding getKeyBind(String translationKey) { + return KeyBindRegistryImpl.getKeyBind(translationKey); + } + + /** + * Gets the bound key of the key bind. + * + *

The bound key is only directly used by the key bind system's internal logic. + * If possible, use the methods provided by the KeyBind class instead. + * + * @param key the key bind + * @return the key bind's bound key + */ + public static InputUtil.Key getBoundKey(KeyBinding key) { + return ((KeyBindingAccessor) key).getBoundKey(); + } + + /** + * Gets the state of the key bind. + * + *

The state of a key bind determines whenever it's disabled or not. + * + *

When a key bind is disabled, it is effectively hidden from the game, + * being non-existent to it. options.txt is the only exception, who still + * stores the disabled key binds. + * + * @param key the key bind + * @return {@code true} if the key bind is enabled, {@code false} otherwise + * @throws IllegalArgumentException if {@code key} is either unregistered or a Vanilla key bind + */ + public static boolean isEnabled(KeyBinding key) { + return KeyBindRegistryImpl.isEnabled(key); + } + + /** + * Sets the state of the key bind. + * + *

This method allows for disabling the key bind, making it invisible to + * the game. options.txt is the only exception, who still stores the disabled + * key binds. + * + * @param key the key bind + * @param enabled the new state + * @throws IllegalArgumentException if {@code key} is either unregistered or a Vanilla key bind + */ + public static void setEnabled(KeyBinding key, boolean enabled) { + KeyBindRegistryImpl.setEnabled(key, enabled); + } + + /** + * Returns a map containing all modded key binds (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 binds + */ + public static Map getAllKeyBinds(boolean includeVanilla) { + return KeyBindRegistryImpl.getAllKeyBinds(includeVanilla); + } +} diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/ConflictTooltipOwner.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/ConflictTooltipOwner.java similarity index 88% rename from library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/ConflictTooltipOwner.java rename to library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/ConflictTooltipOwner.java index aeb35c4df1..cbc2ff9a74 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/ConflictTooltipOwner.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/ConflictTooltipOwner.java @@ -14,16 +14,18 @@ * limitations under the License. */ -package org.quiltmc.qsl.key.bindings.impl; +package org.quiltmc.qsl.key.binds.impl; import java.util.List; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; +import org.jetbrains.annotations.ApiStatus; import net.minecraft.text.Text; @Environment(EnvType.CLIENT) +@ApiStatus.Internal public interface ConflictTooltipOwner { List getConflictTooltips(); } diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindManager.java similarity index 78% rename from library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java rename to library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindManager.java index 0866925e79..82981223e6 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingManager.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindManager.java @@ -14,7 +14,10 @@ * limitations under the License. */ -package org.quiltmc.qsl.key.bindings.impl; +package org.quiltmc.qsl.key.binds.impl; + +import org.jetbrains.annotations.ApiStatus; +import org.quiltmc.qsl.key.binds.mixin.client.GameOptionsAccessor; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; @@ -22,20 +25,19 @@ import net.minecraft.client.option.GameOptions; import net.minecraft.client.option.KeyBinding; -import org.quiltmc.qsl.key.bindings.mixin.client.GameOptionsAccessor; - @Environment(EnvType.CLIENT) -public class KeyBindingManager { +@ApiStatus.Internal +public class KeyBindManager { private final GameOptions options; private final KeyBinding[] allKeys; - public KeyBindingManager(GameOptions options, KeyBinding[] allKeys) { + public KeyBindManager(GameOptions options, KeyBinding[] allKeys) { this.options = options; this.allKeys = allKeys; } public void addModdedKeyBinds() { - ((GameOptionsAccessor) (Object) this.options).setAllKeys(KeyBindingRegistryImpl.getKeyBindings()); + ((GameOptionsAccessor) (Object) this.options).setAllKeys(KeyBindRegistryImpl.getKeyBinds()); } public KeyBinding[] getAllKeys() { diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java similarity index 75% rename from library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java rename to library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java index ffbdbdced0..6b2a2816ac 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/impl/KeyBindingRegistryImpl.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.quiltmc.qsl.key.bindings.impl; +package org.quiltmc.qsl.key.binds.impl; import java.util.ArrayList; import java.util.Arrays; @@ -24,25 +24,27 @@ import java.util.Objects; import org.apache.commons.lang3.ArrayUtils; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; +import org.jetbrains.annotations.ApiStatus; +import org.quiltmc.qsl.key.binds.mixin.client.KeyBindingAccessor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.option.KeyBinding; -import org.quiltmc.qsl.key.bindings.mixin.client.KeyBindingAccessor; - @Environment(EnvType.CLIENT) -public class KeyBindingRegistryImpl { - public static final Logger LOGGER = LogManager.getLogger(); +@ApiStatus.Internal +public class KeyBindRegistryImpl { + public static final Logger LOGGER = LoggerFactory.getLogger("KeyBindRegistry"); private static Map quiltKeys = new HashMap<>(); private static KeyBinding[] enabledQuiltKeysArray = new KeyBinding[] {}; private static List disabledQuiltKeys = new ArrayList<>(0); - private static KeyBindingManager keyBindingManager = null; + private static KeyBindManager keyBindManager = null; - public static KeyBinding registerKeyBinding(KeyBinding key, boolean enabled) { + public static KeyBinding registerKeyBind(KeyBinding key, boolean enabled) { Objects.requireNonNull(key, "Attempted to register a null key bind!"); for (KeyBinding otherKey : quiltKeys.keySet()) { @@ -62,7 +64,7 @@ public static KeyBinding registerKeyBinding(KeyBinding key, boolean enabled) { return key; } - public static KeyBinding getKeyBinding(String translationKey) { + public static KeyBinding getKeyBind(String translationKey) { for (KeyBinding key : quiltKeys.keySet()) { if (key.getTranslationKey().equals(translationKey)) { return key; @@ -73,7 +75,7 @@ public static KeyBinding getKeyBinding(String translationKey) { } public static boolean throwUnregisteredKeyException(KeyBinding key) { - if (keyBindingManager != null && Arrays.asList(keyBindingManager.getAllKeys()).contains(key)) { + if (keyBindManager != null && Arrays.asList(keyBindManager.getAllKeys()).contains(key)) { throw new IllegalArgumentException(String.format("%s is a vanilla key and therefore doesn't have an active state!", key.getTranslationKey())); } @@ -106,12 +108,12 @@ public static void setEnabled(KeyBinding key, boolean enabled) { } } - public static Map getAllKeyBindings(boolean includeVanilla) { + public static Map getAllKeyBinds(boolean includeVanilla) { Map allKeys = new HashMap<>(); if (includeVanilla) { - for (int i = 0; i < keyBindingManager.getAllKeys().length; i++) { - allKeys.put(keyBindingManager.getAllKeys()[i], false); + for (int i = 0; i < keyBindManager.getAllKeys().length; i++) { + allKeys.put(keyBindManager.getAllKeys()[i], false); } } @@ -133,20 +135,20 @@ public static void applyChanges() { enabledQuiltKeysArray = enabledQuiltKeys.toArray(new KeyBinding[enabledQuiltKeys.size()]); - if (keyBindingManager != null) { - keyBindingManager.addModdedKeyBinds(); + if (keyBindManager != null) { + keyBindManager.addModdedKeyBinds(); } } - public static KeyBinding[] getKeyBindings() { - return ArrayUtils.addAll(keyBindingManager.getAllKeys(), enabledQuiltKeysArray); + public static KeyBinding[] getKeyBinds() { + return ArrayUtils.addAll(keyBindManager.getAllKeys(), enabledQuiltKeysArray); } - public static List getDisabledKeyBindings() { + public static List getDisabledKeyBinds() { return disabledQuiltKeys; } - public static void setKeyBindingManager(KeyBindingManager manager) { - keyBindingManager = manager; + public static void setKeyBindManager(KeyBindManager manager) { + keyBindManager = manager; } } diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/EntryListWidgetAccessor.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/EntryListWidgetAccessor.java similarity index 78% rename from library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/EntryListWidgetAccessor.java rename to library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/EntryListWidgetAccessor.java index 5f82adee65..28a69cbaa6 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/EntryListWidgetAccessor.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/EntryListWidgetAccessor.java @@ -14,19 +14,19 @@ * limitations under the License. */ -package org.quiltmc.qsl.key.bindings.mixin.client; +package org.quiltmc.qsl.key.binds.mixin.client; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.gen.Accessor; +import org.spongepowered.asm.mixin.gen.Invoker; + import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; -import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.widget.EntryListWidget; @Environment(EnvType.CLIENT) @Mixin(EntryListWidget.class) -public interface EntryListWidgetAccessor { - @Accessor - MinecraftClient getClient(); +public interface EntryListWidgetAccessor> { + @Invoker + E invokeGetHoveredEntry(); } diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsAccessor.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsAccessor.java similarity index 95% rename from library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsAccessor.java rename to library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsAccessor.java index 6720cd6e10..fe3d99cd68 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsAccessor.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsAccessor.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.quiltmc.qsl.key.bindings.mixin.client; +package org.quiltmc.qsl.key.binds.mixin.client; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mutable; diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsMixin.java similarity index 77% rename from library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java rename to library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsMixin.java index 1097cafc3e..f35af15527 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/GameOptionsMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsMixin.java @@ -14,10 +14,12 @@ * limitations under the License. */ -package org.quiltmc.qsl.key.bindings.mixin.client; +package org.quiltmc.qsl.key.binds.mixin.client; import java.io.File; +import org.quiltmc.qsl.key.binds.impl.KeyBindManager; +import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mutable; @@ -33,9 +35,6 @@ import net.minecraft.client.option.KeyBinding; import net.minecraft.client.util.InputUtil; -import org.quiltmc.qsl.key.bindings.impl.KeyBindingManager; -import org.quiltmc.qsl.key.bindings.impl.KeyBindingRegistryImpl; - @Environment(EnvType.CLIENT) @Mixin(GameOptions.class) public abstract class GameOptionsMixin { @@ -57,8 +56,8 @@ public abstract class GameOptionsMixin { ) private void modifyAllKeys(MinecraftClient client, File file, CallbackInfo ci) { if (this.optionsFile.equals(new File(file, "options.txt"))) { - KeyBindingRegistryImpl.setKeyBindingManager(new KeyBindingManager((GameOptions) (Object) this, this.allKeys)); - this.allKeys = KeyBindingRegistryImpl.getKeyBindings(); + KeyBindRegistryImpl.setKeyBindManager(new KeyBindManager((GameOptions) (Object) this, this.allKeys)); + this.allKeys = KeyBindRegistryImpl.getKeyBinds(); } } @@ -70,11 +69,11 @@ private void modifyAllKeys(MinecraftClient client, File file, CallbackInfo ci) { method = "accept(Lnet/minecraft/client/option/GameOptions$Visitor;)V" ) private void includeDisabledEntries(GameOptions.Visitor visitor, CallbackInfo ci) { - for (KeyBinding keyBinding : KeyBindingRegistryImpl.getDisabledKeyBindings()) { - String keyTranslationKey = keyBinding.getBoundKeyTranslationKey(); - String keyBindTranslationKey = visitor.visitString("key_" + keyBinding.getTranslationKey(), keyTranslationKey); + for (KeyBinding keyBind : KeyBindRegistryImpl.getDisabledKeyBinds()) { + String keyTranslationKey = keyBind.getBoundKeyTranslationKey(); + String keyBindTranslationKey = visitor.visitString("key_" + keyBind.getTranslationKey(), keyTranslationKey); if (!keyTranslationKey.equals(keyBindTranslationKey)) { - keyBinding.setBoundKey(InputUtil.fromTranslationKey(keyTranslationKey)); + keyBind.setBoundKey(InputUtil.fromTranslationKey(keyTranslationKey)); } }; } diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingAccessor.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingAccessor.java similarity index 95% rename from library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingAccessor.java rename to library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingAccessor.java index 0d21123350..c7cd8a4c7d 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingAccessor.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingAccessor.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.quiltmc.qsl.key.bindings.mixin.client; +package org.quiltmc.qsl.key.binds.mixin.client; import java.util.Map; diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingEntryMixin.java similarity index 87% rename from library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java rename to library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingEntryMixin.java index 33af521154..c75abe4969 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingEntryMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingEntryMixin.java @@ -14,11 +14,14 @@ * limitations under the License. */ -package org.quiltmc.qsl.key.bindings.mixin.client; +package org.quiltmc.qsl.key.binds.mixin.client; import java.util.ArrayList; import java.util.List; +import org.quiltmc.qsl.key.binds.api.KeyBindRegistry; +import org.quiltmc.qsl.key.binds.impl.ConflictTooltipOwner; +import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -41,10 +44,6 @@ import net.minecraft.text.TranslatableText; import net.minecraft.util.Formatting; -import org.quiltmc.qsl.key.bindings.api.KeyBindingRegistry; -import org.quiltmc.qsl.key.bindings.impl.ConflictTooltipOwner; -import org.quiltmc.qsl.key.bindings.impl.KeyBindingRegistryImpl; - @Environment(EnvType.CLIENT) @Mixin(KeyBindingEntry.class) public abstract class KeyBindingEntryMixin extends KeyBindingListWidget.Entry implements ConflictTooltipOwner { @@ -84,7 +83,7 @@ private void initPreviousBoundKey(KeyBindingListWidget list, KeyBinding keyBindi locals = LocalCapture.CAPTURE_FAILHARD ) private void collectConflictTooltips(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta, CallbackInfo ci, boolean bl, boolean bl2) { - InputUtil.Key boundKey = KeyBindingRegistry.getBoundKey(this.binding); + InputUtil.Key boundKey = KeyBindRegistry.getBoundKey(this.binding); if (!boundKey.equals(this.quilt$previousBoundKey) || quilt$changedBoundKey != null) { this.quilt$conflictTooltips.clear(); @@ -96,13 +95,13 @@ private void collectConflictTooltips(MatrixStack matrices, int index, int y, int } if (!this.binding.isUnbound()) { - for (KeyBinding keyBinding : KeyBindingRegistryImpl.getKeyBindings()) { - if (keyBinding != this.binding && this.binding.equals(keyBinding)) { + for (KeyBinding keyBind : KeyBindRegistryImpl.getKeyBinds()) { + if (keyBind != this.binding && this.binding.equals(keyBind)) { if (this.quilt$conflictTooltips.isEmpty()) { this.quilt$conflictTooltips.add(new TranslatableText("key.qsl.key_conflict.tooltip").formatted(Formatting.RED)); } - this.quilt$conflictTooltips.add(new TranslatableText("key.qsl.key_conflict.tooltip.entry", new TranslatableText(keyBinding.getTranslationKey())).formatted(Formatting.RED)); + this.quilt$conflictTooltips.add(new TranslatableText("key.qsl.key_conflict.tooltip.entry", new TranslatableText(keyBind.getTranslationKey())).formatted(Formatting.RED)); } } } diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingMixin.java similarity index 97% rename from library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingMixin.java rename to library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingMixin.java index 5e2207d54b..e232d855eb 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingMixin.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.quiltmc.qsl.key.bindings.mixin.client; +package org.quiltmc.qsl.key.binds.mixin.client; import java.util.Map; diff --git a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingsScreenMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingsScreenMixin.java similarity index 75% rename from library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingsScreenMixin.java rename to library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingsScreenMixin.java index 8c22d63bc3..2cef3c1b34 100644 --- a/library/gui/key_bindings/src/main/java/org/quiltmc/qsl/key/bindings/mixin/client/KeyBindingsScreenMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingsScreenMixin.java @@ -14,10 +14,11 @@ * limitations under the License. */ -package org.quiltmc.qsl.key.bindings.mixin.client; +package org.quiltmc.qsl.key.binds.mixin.client; import java.util.List; +import org.quiltmc.qsl.key.binds.impl.ConflictTooltipOwner; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; @@ -35,27 +36,24 @@ import net.minecraft.client.util.math.MatrixStack; import net.minecraft.text.Text; -import org.quiltmc.qsl.key.bindings.impl.ConflictTooltipOwner; - @Environment(EnvType.CLIENT) @Mixin(KeyBindingsScreen.class) public abstract class KeyBindingsScreenMixin extends GameOptionsScreen { @Shadow - private KeyBindingListWidget keybindsList; + private KeyBindingListWidget keyBindsList; private KeyBindingsScreenMixin(Screen screen, GameOptions gameOptions, Text text) { super(screen, gameOptions, text); } + @SuppressWarnings("unchecked") @Inject(method = "render", at = @At("TAIL")) private void renderConflictTooltips(MatrixStack matrices, int mouseX, int mouseY, float delta, CallbackInfo ci) { - for (KeyBindingListWidget.Entry entry : this.keybindsList.children()) { - if (entry instanceof KeyBindingEntry keyBindingEntry) { - if (keyBindingEntry.isMouseOver(mouseX, mouseY)) { - List tooltipLines = ((ConflictTooltipOwner) (Object) keyBindingEntry).getConflictTooltips(); - this.renderTooltip(matrices, tooltipLines, mouseX, mouseY); - } - } + // TODO - Somehow extend the hover area to include the label too + KeyBindingListWidget.Entry entry = ((EntryListWidgetAccessor) this.keybindsList).invokeGetHoveredEntry(); + if (entry != null && entry instanceof KeyBindingEntry keyBindEntry) { + List tooltipLines = ((ConflictTooltipOwner) keyBindEntry).getConflictTooltips(); + this.renderTooltip(matrices, tooltipLines, mouseX, mouseY); } } } diff --git a/library/gui/key_bindings/src/main/resources/assets/quilt_key_bindings/icon.png b/library/gui/key_binds/src/main/resources/assets/quilt_key_binds/icon.png similarity index 100% rename from library/gui/key_bindings/src/main/resources/assets/quilt_key_bindings/icon.png rename to library/gui/key_binds/src/main/resources/assets/quilt_key_binds/icon.png diff --git a/library/gui/key_bindings/src/main/resources/assets/quilt_key_bindings/lang/en_us.json b/library/gui/key_binds/src/main/resources/assets/quilt_key_binds/lang/en_us.json similarity index 100% rename from library/gui/key_bindings/src/main/resources/assets/quilt_key_bindings/lang/en_us.json rename to library/gui/key_binds/src/main/resources/assets/quilt_key_binds/lang/en_us.json diff --git a/library/gui/key_bindings/src/main/resources/fabric.mod.json b/library/gui/key_binds/src/main/resources/fabric.mod.json similarity index 58% rename from library/gui/key_bindings/src/main/resources/fabric.mod.json rename to library/gui/key_binds/src/main/resources/fabric.mod.json index 32d7cf980f..935a8c1c07 100644 --- a/library/gui/key_bindings/src/main/resources/fabric.mod.json +++ b/library/gui/key_binds/src/main/resources/fabric.mod.json @@ -1,11 +1,11 @@ { "schemaVersion": 1, - "id": "quilt_key_bindings", - "name": "Quilt Key Bindings API", + "id": "quilt_key_binds", + "name": "Quilt Key Binds API", "version": "${version}", "environment": "client", "license": "Apache-2.0", - "icon": "assets/quilt_key_bindings/icon.png", + "icon": "assets/quilt_key_binds/icon.png", "contact": { "homepage": "https://quiltmc.org", "issues": "https://github.com/QuiltMC/quilt-standard-libraries/issues", @@ -16,11 +16,11 @@ ], "depends": { "fabricloader": ">=0.12", - "minecraft": ">=1.18" + "minecraft": ">=1.18.2-alpha.22.3.a" }, - "description": "Key bindings registration and utilities.", - "accessWidener": "quilt_key_bindings.accesswidener", + "description": "Key binds registration and utilities.", + "accessWidener": "quilt_key_binds.accesswidener", "mixins": [ - "quilt_key_bindings.mixins.json" + "quilt_key_binds.mixins.json" ] } diff --git a/library/gui/key_bindings/src/main/resources/quilt_key_bindings.accesswidener b/library/gui/key_binds/src/main/resources/quilt_key_binds.accesswidener similarity index 100% rename from library/gui/key_bindings/src/main/resources/quilt_key_bindings.accesswidener rename to library/gui/key_binds/src/main/resources/quilt_key_binds.accesswidener diff --git a/library/gui/key_bindings/src/main/resources/quilt_key_bindings.mixins.json b/library/gui/key_binds/src/main/resources/quilt_key_binds.mixins.json similarity index 87% rename from library/gui/key_bindings/src/main/resources/quilt_key_bindings.mixins.json rename to library/gui/key_binds/src/main/resources/quilt_key_binds.mixins.json index 4949cb8c13..ef5e2d17fe 100644 --- a/library/gui/key_bindings/src/main/resources/quilt_key_bindings.mixins.json +++ b/library/gui/key_binds/src/main/resources/quilt_key_binds.mixins.json @@ -1,6 +1,6 @@ { "required": true, - "package": "org.quiltmc.qsl.key.bindings.mixin", + "package": "org.quiltmc.qsl.key.binds.mixin", "compatibilityLevel": "JAVA_17", "client": [ "client.EntryListWidgetAccessor", diff --git a/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingRegistryTestMod.java b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java similarity index 73% rename from library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingRegistryTestMod.java rename to library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java index 902c3e83eb..4f382ea042 100644 --- a/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingRegistryTestMod.java +++ b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java @@ -14,28 +14,28 @@ * limitations under the License. */ -package org.quiltmc.qsl.key.bindings.test.client; +package org.quiltmc.qsl.key.binds.test.client; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; import net.minecraft.client.option.KeyBinding; -import org.quiltmc.qsl.key.bindings.api.KeyBindingRegistry; +import org.quiltmc.qsl.key.binds.api.KeyBindRegistry; import org.quiltmc.qsl.lifecycle.api.client.event.ClientLifecycleEvents; import org.quiltmc.qsl.lifecycle.api.client.event.ClientTickEvents; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; @Environment(EnvType.CLIENT) -public class KeyBindingRegistryTestMod implements ClientModInitializer { - public static final Logger LOGGER = LogManager.getFormatterLogger("KeyBindingRegistryTest"); +public class KeyBindRegistryTestMod implements ClientModInitializer { + public static final Logger LOGGER = LoggerFactory.getLogger("KeyBindRegistryTest"); @Override public void onInitializeClient() { ClientLifecycleEvents.READY.register(lifecycleClient -> { - KeyBinding enableKeyBindKey = KeyBindingRegistry.getKeyBinding("key.qsl.enable_key_bind"); + KeyBinding enableKeyBindKey = KeyBindRegistry.getKeyBind("key.qsl.enable_key_bind"); if (enableKeyBindKey != null) { LOGGER.info("Successfully got the \"Enable Key Bind\" key!"); @@ -48,7 +48,7 @@ public void onInitializeClient() { } LOGGER.info("The registry has the following keys registered:"); - KeyBindingRegistry.getAllKeyBindings(true).forEach((key, value) -> { + KeyBindRegistry.getAllKeyBinds(true).forEach((key, value) -> { LOGGER.info("%s: %s", key.getTranslationKey(), value); }); }); diff --git a/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java similarity index 70% rename from library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java rename to library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java index 09a2843528..5a0c2e7796 100644 --- a/library/gui/key_bindings/src/testmod/java/org/quiltmc/qsl/key/bindings/test/client/KeyBindingsTestMod.java +++ b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.quiltmc.qsl.key.bindings.test.client; +package org.quiltmc.qsl.key.binds.test.client; import org.lwjgl.glfw.GLFW; import net.fabricmc.api.ClientModInitializer; @@ -24,26 +24,28 @@ import net.minecraft.client.option.KeyBinding; import net.minecraft.text.LiteralText; -import org.quiltmc.qsl.key.bindings.api.KeyBindingRegistry; +import org.quiltmc.qsl.key.binds.api.KeyBindRegistry; import org.quiltmc.qsl.lifecycle.api.client.event.ClientTickEvents; @Environment(EnvType.CLIENT) -public class KeyBindingsTestMod implements ClientModInitializer { +public class KeyBindsTestMod implements ClientModInitializer { + public static final String KEY_CATEGORY = "key.qsl.category"; + // A conflicting key test - public static final KeyBinding CONFLICT_TEST_KEY_BIND = KeyBindingRegistry.registerKeyBinding( - new KeyBinding("key.qsl.conflict_test", GLFW.GLFW_KEY_H, "key.qsl.category") + public static final KeyBinding CONFLICT_TEST_KEY_BIND = KeyBindRegistry.registerKeyBind( + new KeyBinding("key.qsl.conflict_test", GLFW.GLFW_KEY_H, KEY_CATEGORY) ); - public static final KeyBinding DISABLE_KEY_BIND = KeyBindingRegistry.registerKeyBinding( - new KeyBinding("key.qsl.disable_key_bind", GLFW.GLFW_KEY_H, "key.qsl.category"), true + public static final KeyBinding DISABLE_KEY_BIND = KeyBindRegistry.registerKeyBind( + new KeyBinding("key.qsl.disable_key_bind", GLFW.GLFW_KEY_H, KEY_CATEGORY), true ); - public static final KeyBinding ENABLE_KEY_BIND = KeyBindingRegistry.registerKeyBinding( - new KeyBinding("key.qsl.enable_key_bind", GLFW.GLFW_KEY_I, "key.qsl.category"), true + public static final KeyBinding ENABLE_KEY_BIND = KeyBindRegistry.registerKeyBind( + new KeyBinding("key.qsl.enable_key_bind", GLFW.GLFW_KEY_I, KEY_CATEGORY), true ); - public static final KeyBinding DISABLED_CONFLICT_TEST_KEY_BIND = KeyBindingRegistry.registerKeyBinding( - new KeyBinding("key.qsl.disabled_conflict_test", GLFW.GLFW_KEY_H, "key.qsl.category"), false + public static final KeyBinding DISABLED_CONFLICT_TEST_KEY_BIND = KeyBindRegistry.registerKeyBind( + new KeyBinding("key.qsl.disabled_conflict_test", GLFW.GLFW_KEY_H, KEY_CATEGORY), false ); @Override @@ -54,7 +56,7 @@ public void onInitializeClient() { client.player.sendMessage(new LiteralText("The key has disappeared! Bye bye, key!"), true); } - KeyBindingRegistry.setEnabled(DISABLE_KEY_BIND, false); + KeyBindRegistry.setEnabled(DISABLE_KEY_BIND, false); } if (ENABLE_KEY_BIND.isPressed()) { @@ -62,7 +64,7 @@ public void onInitializeClient() { client.player.sendMessage(new LiteralText("The key is back!"), true); } - KeyBindingRegistry.setEnabled(DISABLE_KEY_BIND, true); + KeyBindRegistry.setEnabled(DISABLE_KEY_BIND, true); } if (CONFLICT_TEST_KEY_BIND.isPressed()) { diff --git a/library/gui/key_bindings/src/testmod/resources/assets/quilt_key_bindings_testmod/lang/en_us.json b/library/gui/key_binds/src/testmod/resources/assets/quilt_key_binds_testmod/lang/en_us.json similarity index 80% rename from library/gui/key_bindings/src/testmod/resources/assets/quilt_key_bindings_testmod/lang/en_us.json rename to library/gui/key_binds/src/testmod/resources/assets/quilt_key_binds_testmod/lang/en_us.json index 0a00f36596..c9ea46b28b 100644 --- a/library/gui/key_bindings/src/testmod/resources/assets/quilt_key_bindings_testmod/lang/en_us.json +++ b/library/gui/key_binds/src/testmod/resources/assets/quilt_key_binds_testmod/lang/en_us.json @@ -1,5 +1,5 @@ { - "key.qsl.category": "QSL Key Binding API Test", + "key.qsl.category": "QSL Key Binds API Test", "key.qsl.disable_key_bind": "Disable Key Bind", "key.qsl.enable_key_bind": "Enable Key Bind", "key.qsl.conflict_test": "Conflict Test", diff --git a/library/gui/key_binds/src/testmod/resources/fabric.mod.json b/library/gui/key_binds/src/testmod/resources/fabric.mod.json new file mode 100644 index 0000000000..6258bcfc0a --- /dev/null +++ b/library/gui/key_binds/src/testmod/resources/fabric.mod.json @@ -0,0 +1,17 @@ +{ + "schemaVersion": 1, + "id": "quilt_key_binds_testmod", + "name": "Quilt Key Binds API Test Mod", + "version": "1.0.0", + "environment": "client", + "license": "Apache-2.0", + "depends": { + "quilt_key_binds": "*" + }, + "entrypoints": { + "client": [ + "org.quiltmc.qsl.key.binds.test.client.KeyBindRegistryTestMod", + "org.quiltmc.qsl.key.binds.test.client.KeyBindsTestMod" + ] + } +} From e585386a6355cbc0de67d327a443b761e2d7129f Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Fri, 11 Feb 2022 13:25:15 -0300 Subject: [PATCH 27/56] Attempt to remove an unchangable file how did this happen --- .../main/resources/assets/quilt_key_bindings/lang/pt_br.json | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 library/gui/key_bindings/src/main/resources/assets/quilt_key_bindings/lang/pt_br.json diff --git a/library/gui/key_bindings/src/main/resources/assets/quilt_key_bindings/lang/pt_br.json b/library/gui/key_bindings/src/main/resources/assets/quilt_key_bindings/lang/pt_br.json deleted file mode 100644 index 92948b285b..0000000000 --- a/library/gui/key_bindings/src/main/resources/assets/quilt_key_bindings/lang/pt_br.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "key.qsl.key_conflict.indicator": "%s (!)", - "key.qsl.conflict_tooltip": "Há conflitos com: %s" -} \ No newline at end of file From f8db03740984112ed819b169f905bffc9d9cc91e Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Fri, 11 Feb 2022 13:33:25 -0300 Subject: [PATCH 28/56] Add back Brazilian Portuguese translation (1) and despite the previous commit, git still hates this file why --- .../main/resources/assets/quilt_key_binds/lang/pt_br2.json | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 library/gui/key_binds/src/main/resources/assets/quilt_key_binds/lang/pt_br2.json diff --git a/library/gui/key_binds/src/main/resources/assets/quilt_key_binds/lang/pt_br2.json b/library/gui/key_binds/src/main/resources/assets/quilt_key_binds/lang/pt_br2.json new file mode 100644 index 0000000000..c020f56301 --- /dev/null +++ b/library/gui/key_binds/src/main/resources/assets/quilt_key_binds/lang/pt_br2.json @@ -0,0 +1,5 @@ +{ + "key.qsl.key_conflict.indicator": "%s (!)", + "key.qsl.key_conflict.tooltip": "Há conflitos com:", + "key.qsl.key_conflict.tooltip.entry": " %s" +} \ No newline at end of file From ffe07589c5b095362cf25f8755b583aca32e02fd Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Fri, 11 Feb 2022 16:50:49 -0300 Subject: [PATCH 29/56] Update to 22w06a, fix license and testmod --- library/gui/key_binds/build.gradle | 11 +++++++---- .../quiltmc/qsl/key/binds/api/KeyBindRegistry.java | 2 +- .../qsl/key/binds/impl/ConflictTooltipOwner.java | 2 +- .../quiltmc/qsl/key/binds/impl/KeyBindManager.java | 2 +- .../qsl/key/binds/impl/KeyBindRegistryImpl.java | 6 +++--- .../binds/mixin/client/EntryListWidgetAccessor.java | 3 +-- .../key/binds/mixin/client/GameOptionsAccessor.java | 2 +- .../qsl/key/binds/mixin/client/GameOptionsMixin.java | 7 ++++--- .../key/binds/mixin/client/KeyBindingAccessor.java | 2 +- .../key/binds/mixin/client/KeyBindingEntryMixin.java | 9 +++++---- .../qsl/key/binds/mixin/client/KeyBindingMixin.java | 2 +- .../binds/mixin/client/KeyBindingsScreenMixin.java | 7 ++++--- .../key/binds/test/client/KeyBindRegistryTestMod.java | 8 ++++---- .../qsl/key/binds/test/client/KeyBindsTestMod.java | 2 +- 14 files changed, 35 insertions(+), 30 deletions(-) diff --git a/library/gui/key_binds/build.gradle b/library/gui/key_binds/build.gradle index 5ee28c50d0..71cea66d2a 100644 --- a/library/gui/key_binds/build.gradle +++ b/library/gui/key_binds/build.gradle @@ -6,10 +6,13 @@ qslModule { moduleName = "key_binds" version = "1.0.0" library = "gui" - coreTestmodDependencies([ - "qsl_base", - "lifecycle_events" - ]) + moduleDependencies { + core { + testmodOnly("qsl_base") + testmodOnly("lifecycle_events") + testmodOnly("resource_loader") + } + } } loom { diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java index e374521672..a23ebfe3c3 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 QuiltMC + * Copyright 2021-2022 QuiltMC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/ConflictTooltipOwner.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/ConflictTooltipOwner.java index cbc2ff9a74..9cd7cf5841 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/ConflictTooltipOwner.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/ConflictTooltipOwner.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 QuiltMC + * Copyright 2021-2022 QuiltMC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindManager.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindManager.java index 82981223e6..bd68917c8c 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindManager.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 QuiltMC + * Copyright 2021-2022 QuiltMC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java index 6b2a2816ac..ba7765d924 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 QuiltMC + * Copyright 2021-2022 QuiltMC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,15 +25,15 @@ import org.apache.commons.lang3.ArrayUtils; import org.jetbrains.annotations.ApiStatus; -import org.quiltmc.qsl.key.binds.mixin.client.KeyBindingAccessor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.option.KeyBinding; +import org.quiltmc.qsl.key.binds.mixin.client.KeyBindingAccessor; + @Environment(EnvType.CLIENT) @ApiStatus.Internal public class KeyBindRegistryImpl { diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/EntryListWidgetAccessor.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/EntryListWidgetAccessor.java index 28a69cbaa6..266398360a 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/EntryListWidgetAccessor.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/EntryListWidgetAccessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 QuiltMC + * Copyright 2021-2022 QuiltMC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,6 @@ import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.gen.Invoker; - import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsAccessor.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsAccessor.java index fe3d99cd68..e42f4c4992 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsAccessor.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsAccessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 QuiltMC + * Copyright 2021-2022 QuiltMC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsMixin.java index f35af15527..5cd12837e5 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsMixin.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 QuiltMC + * Copyright 2021-2022 QuiltMC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,8 +18,6 @@ import java.io.File; -import org.quiltmc.qsl.key.binds.impl.KeyBindManager; -import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mutable; @@ -35,6 +33,9 @@ import net.minecraft.client.option.KeyBinding; import net.minecraft.client.util.InputUtil; +import org.quiltmc.qsl.key.binds.impl.KeyBindManager; +import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; + @Environment(EnvType.CLIENT) @Mixin(GameOptions.class) public abstract class GameOptionsMixin { diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingAccessor.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingAccessor.java index c7cd8a4c7d..39cd87cd37 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingAccessor.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingAccessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 QuiltMC + * Copyright 2021-2022 QuiltMC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingEntryMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingEntryMixin.java index c75abe4969..e4f12e01fc 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingEntryMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingEntryMixin.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 QuiltMC + * Copyright 2021-2022 QuiltMC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,9 +19,6 @@ import java.util.ArrayList; import java.util.List; -import org.quiltmc.qsl.key.binds.api.KeyBindRegistry; -import org.quiltmc.qsl.key.binds.impl.ConflictTooltipOwner; -import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -44,6 +41,10 @@ import net.minecraft.text.TranslatableText; import net.minecraft.util.Formatting; +import org.quiltmc.qsl.key.binds.api.KeyBindRegistry; +import org.quiltmc.qsl.key.binds.impl.ConflictTooltipOwner; +import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; + @Environment(EnvType.CLIENT) @Mixin(KeyBindingEntry.class) public abstract class KeyBindingEntryMixin extends KeyBindingListWidget.Entry implements ConflictTooltipOwner { diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingMixin.java index e232d855eb..0661766731 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingMixin.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 QuiltMC + * Copyright 2021-2022 QuiltMC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingsScreenMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingsScreenMixin.java index 2cef3c1b34..29282753cb 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingsScreenMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingsScreenMixin.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 QuiltMC + * Copyright 2021-2022 QuiltMC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,6 @@ import java.util.List; -import org.quiltmc.qsl.key.binds.impl.ConflictTooltipOwner; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; @@ -36,11 +35,13 @@ import net.minecraft.client.util.math.MatrixStack; import net.minecraft.text.Text; +import org.quiltmc.qsl.key.binds.impl.ConflictTooltipOwner; + @Environment(EnvType.CLIENT) @Mixin(KeyBindingsScreen.class) public abstract class KeyBindingsScreenMixin extends GameOptionsScreen { @Shadow - private KeyBindingListWidget keyBindsList; + private KeyBindingListWidget keybindsList; private KeyBindingsScreenMixin(Screen screen, GameOptions gameOptions, Text text) { super(screen, gameOptions, text); diff --git a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java index 4f382ea042..102ec3da7a 100644 --- a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java +++ b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 QuiltMC + * Copyright 2021-2022 QuiltMC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,14 +19,14 @@ import net.fabricmc.api.ClientModInitializer; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import net.minecraft.client.option.KeyBinding; import org.quiltmc.qsl.key.binds.api.KeyBindRegistry; import org.quiltmc.qsl.lifecycle.api.client.event.ClientLifecycleEvents; import org.quiltmc.qsl.lifecycle.api.client.event.ClientTickEvents; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; @Environment(EnvType.CLIENT) public class KeyBindRegistryTestMod implements ClientModInitializer { @@ -49,7 +49,7 @@ public void onInitializeClient() { LOGGER.info("The registry has the following keys registered:"); KeyBindRegistry.getAllKeyBinds(true).forEach((key, value) -> { - LOGGER.info("%s: %s", key.getTranslationKey(), value); + LOGGER.info(String.format("%s: %s", key.getTranslationKey(), value)); }); }); } diff --git a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java index 5a0c2e7796..fcfb946f08 100644 --- a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java +++ b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 QuiltMC + * Copyright 2021-2022 QuiltMC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From 3c20a93405157f7ae3571573ff78d98b4fa7b33e Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Fri, 11 Feb 2022 19:37:39 -0300 Subject: [PATCH 30/56] Depend on resource loader Required for translatable texts to work on the tooltips --- library/gui/key_binds/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/gui/key_binds/build.gradle b/library/gui/key_binds/build.gradle index 71cea66d2a..3c27ba1afe 100644 --- a/library/gui/key_binds/build.gradle +++ b/library/gui/key_binds/build.gradle @@ -8,9 +8,9 @@ qslModule { library = "gui" moduleDependencies { core { - testmodOnly("qsl_base") + api("qsl_base") + api("resource_loader") testmodOnly("lifecycle_events") - testmodOnly("resource_loader") } } } From 0dbac3c7fd3caff317986ce8218abcb69d236f41 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Sat, 19 Feb 2022 19:02:48 -0300 Subject: [PATCH 31/56] Use the mappings refactor (I) I'll finish it at my other PC --- .../qsl/key/binds/api/KeyBindRegistry.java | 24 ++++++------- .../qsl/key/binds/impl/KeyBindManager.java | 12 +++---- .../key/binds/impl/KeyBindRegistryImpl.java | 36 +++++++++---------- .../mixin/client/GameOptionsAccessor.java | 6 ++-- .../binds/mixin/client/GameOptionsMixin.java | 10 +++--- .../mixin/client/KeyBindingAccessor.java | 8 ++--- .../mixin/client/KeyBindingEntryMixin.java | 20 +++++------ .../binds/mixin/client/KeyBindingMixin.java | 6 ++-- .../mixin/client/KeyBindingsScreenMixin.java | 14 ++++---- .../test/client/KeyBindRegistryTestMod.java | 4 +-- .../binds/test/client/KeyBindsTestMod.java | 18 +++++----- 11 files changed, 79 insertions(+), 79 deletions(-) diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java index a23ebfe3c3..85d24d468b 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java @@ -18,14 +18,14 @@ import java.util.Map; -import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; -import org.quiltmc.qsl.key.binds.mixin.client.KeyBindingAccessor; - +import com.mojang.blaze3d.platform.InputUtil; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; -import net.minecraft.client.option.KeyBinding; -import net.minecraft.client.util.InputUtil; +import net.minecraft.client.option.KeyBind; + +import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; +import org.quiltmc.qsl.key.binds.mixin.client.KeyBindingAccessor; /** * Handles the registration of modded key binds and allows for changing their properties, @@ -51,7 +51,7 @@ public class KeyBindRegistry { * @throws NullPointerException if {@code key} is null * @throws IllegalArgumentException if either {@code key} or it's translation key is already registered */ - public static KeyBinding registerKeyBind(KeyBinding key) { + public static KeyBind registerKeyBind(KeyBind key) { return registerKeyBind(key, true); } @@ -64,7 +64,7 @@ public static KeyBinding registerKeyBind(KeyBinding key) { * @throws NullPointerException if {@code key} is null * @throws IllegalArgumentException if either {@code key} or it's translation key is already registered */ - public static KeyBinding registerKeyBind(KeyBinding key, boolean enabled) { + public static KeyBind registerKeyBind(KeyBind key, boolean enabled) { return KeyBindRegistryImpl.registerKeyBind(key, enabled); } @@ -74,7 +74,7 @@ public static KeyBinding registerKeyBind(KeyBinding key, boolean enabled) { * @param translationKey the key bind's translation key * @return the key bind if found, {@code null} otherwise */ - public static KeyBinding getKeyBind(String translationKey) { + public static KeyBind getKeyBind(String translationKey) { return KeyBindRegistryImpl.getKeyBind(translationKey); } @@ -87,7 +87,7 @@ public static KeyBinding getKeyBind(String translationKey) { * @param key the key bind * @return the key bind's bound key */ - public static InputUtil.Key getBoundKey(KeyBinding key) { + public static InputUtil.Key getBoundKey(KeyBind key) { return ((KeyBindingAccessor) key).getBoundKey(); } @@ -104,7 +104,7 @@ public static InputUtil.Key getBoundKey(KeyBinding key) { * @return {@code true} if the key bind is enabled, {@code false} otherwise * @throws IllegalArgumentException if {@code key} is either unregistered or a Vanilla key bind */ - public static boolean isEnabled(KeyBinding key) { + public static boolean isEnabled(KeyBind key) { return KeyBindRegistryImpl.isEnabled(key); } @@ -119,7 +119,7 @@ public static boolean isEnabled(KeyBinding key) { * @param enabled the new state * @throws IllegalArgumentException if {@code key} is either unregistered or a Vanilla key bind */ - public static void setEnabled(KeyBinding key, boolean enabled) { + public static void setEnabled(KeyBind key, boolean enabled) { KeyBindRegistryImpl.setEnabled(key, enabled); } @@ -129,7 +129,7 @@ public static void setEnabled(KeyBinding key, boolean enabled) { * @param includeVanilla {@code true} if vanilla entries should be included, else {@code false} * @return a map containing all modded (and optionally vanilla) key binds */ - public static Map getAllKeyBinds(boolean includeVanilla) { + public static Map getAllKeyBinds(boolean includeVanilla) { return KeyBindRegistryImpl.getAllKeyBinds(includeVanilla); } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindManager.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindManager.java index bd68917c8c..03e16d9aa5 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindManager.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindManager.java @@ -17,21 +17,21 @@ package org.quiltmc.qsl.key.binds.impl; import org.jetbrains.annotations.ApiStatus; -import org.quiltmc.qsl.key.binds.mixin.client.GameOptionsAccessor; - import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.option.GameOptions; -import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.option.KeyBind; + +import org.quiltmc.qsl.key.binds.mixin.client.GameOptionsAccessor; @Environment(EnvType.CLIENT) @ApiStatus.Internal public class KeyBindManager { private final GameOptions options; - private final KeyBinding[] allKeys; + private final KeyBind[] allKeys; - public KeyBindManager(GameOptions options, KeyBinding[] allKeys) { + public KeyBindManager(GameOptions options, KeyBind[] allKeys) { this.options = options; this.allKeys = allKeys; } @@ -40,7 +40,7 @@ public void addModdedKeyBinds() { ((GameOptionsAccessor) (Object) this.options).setAllKeys(KeyBindRegistryImpl.getKeyBinds()); } - public KeyBinding[] getAllKeys() { + public KeyBind[] getAllKeys() { return this.allKeys; } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java index ba7765d924..7da0d90bec 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java @@ -30,7 +30,7 @@ import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; -import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.option.KeyBind; import org.quiltmc.qsl.key.binds.mixin.client.KeyBindingAccessor; @@ -39,15 +39,15 @@ public class KeyBindRegistryImpl { public static final Logger LOGGER = LoggerFactory.getLogger("KeyBindRegistry"); - private static Map quiltKeys = new HashMap<>(); - private static KeyBinding[] enabledQuiltKeysArray = new KeyBinding[] {}; - private static List disabledQuiltKeys = new ArrayList<>(0); + private static Map quiltKeys = new HashMap<>(); + private static KeyBind[] enabledQuiltKeysArray = new KeyBind[] {}; + private static List disabledQuiltKeys = new ArrayList<>(0); private static KeyBindManager keyBindManager = null; - public static KeyBinding registerKeyBind(KeyBinding key, boolean enabled) { + public static KeyBind registerKeyBind(KeyBind key, boolean enabled) { Objects.requireNonNull(key, "Attempted to register a null key bind!"); - for (KeyBinding otherKey : quiltKeys.keySet()) { + for (KeyBind otherKey : quiltKeys.keySet()) { if (key == otherKey) { throw new IllegalArgumentException(String.format("%s has already been registered!", key.getTranslationKey())); } else if (key.getTranslationKey().equals(otherKey.getTranslationKey())) { @@ -64,8 +64,8 @@ public static KeyBinding registerKeyBind(KeyBinding key, boolean enabled) { return key; } - public static KeyBinding getKeyBind(String translationKey) { - for (KeyBinding key : quiltKeys.keySet()) { + public static KeyBind getKeyBind(String translationKey) { + for (KeyBind key : quiltKeys.keySet()) { if (key.getTranslationKey().equals(translationKey)) { return key; } @@ -74,7 +74,7 @@ public static KeyBinding getKeyBind(String translationKey) { return null; } - public static boolean throwUnregisteredKeyException(KeyBinding key) { + public static boolean throwUnregisteredKeyException(KeyBind key) { if (keyBindManager != null && Arrays.asList(keyBindManager.getAllKeys()).contains(key)) { throw new IllegalArgumentException(String.format("%s is a vanilla key and therefore doesn't have an active state!", key.getTranslationKey())); } @@ -82,7 +82,7 @@ public static boolean throwUnregisteredKeyException(KeyBinding key) { throw new IllegalArgumentException(String.format("%s isn't a registered key!", key.getTranslationKey())); } - public static boolean isEnabled(KeyBinding key) { + public static boolean isEnabled(KeyBind key) { if (quiltKeys.containsKey(key)) { return quiltKeys.get(key); } else { @@ -90,7 +90,7 @@ public static boolean isEnabled(KeyBinding key) { } } - public static void setEnabled(KeyBinding key, boolean enabled) { + public static void setEnabled(KeyBind key, boolean enabled) { if (quiltKeys.containsKey(key)) { quiltKeys.replace(key, enabled); @@ -102,14 +102,14 @@ public static void setEnabled(KeyBinding key, boolean enabled) { } ((KeyBindingAccessor) key).callReset(); - KeyBinding.updateKeysByCode(); + KeyBind.updateBoundKeys(); } else { throwUnregisteredKeyException(key); } } - public static Map getAllKeyBinds(boolean includeVanilla) { - Map allKeys = new HashMap<>(); + public static Map getAllKeyBinds(boolean includeVanilla) { + Map allKeys = new HashMap<>(); if (includeVanilla) { for (int i = 0; i < keyBindManager.getAllKeys().length; i++) { @@ -123,7 +123,7 @@ public static Map getAllKeyBinds(boolean includeVanilla) { } public static void applyChanges() { - List enabledQuiltKeys = new ArrayList<>(); + List enabledQuiltKeys = new ArrayList<>(); disabledQuiltKeys.clear(); for (var entry : quiltKeys.entrySet()) { if (entry.getValue()) { @@ -133,18 +133,18 @@ public static void applyChanges() { } } - enabledQuiltKeysArray = enabledQuiltKeys.toArray(new KeyBinding[enabledQuiltKeys.size()]); + enabledQuiltKeysArray = enabledQuiltKeys.toArray(new KeyBind[enabledQuiltKeys.size()]); if (keyBindManager != null) { keyBindManager.addModdedKeyBinds(); } } - public static KeyBinding[] getKeyBinds() { + public static KeyBind[] getKeyBinds() { return ArrayUtils.addAll(keyBindManager.getAllKeys(), enabledQuiltKeysArray); } - public static List getDisabledKeyBinds() { + public static List getDisabledKeyBinds() { return disabledQuiltKeys; } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsAccessor.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsAccessor.java index e42f4c4992..5173d90a6c 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsAccessor.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsAccessor.java @@ -23,15 +23,15 @@ import net.fabricmc.api.Environment; import net.minecraft.client.option.GameOptions; -import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.option.KeyBind; @Environment(EnvType.CLIENT) @Mixin(GameOptions.class) public interface GameOptionsAccessor { @Accessor - KeyBinding[] getAllKeys(); + KeyBind[] getAllKeys(); @Mutable @Accessor - void setAllKeys(KeyBinding[] allKeys); + void setAllKeys(KeyBind[] allKeys); } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsMixin.java index 5cd12837e5..1663567ecf 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsMixin.java @@ -18,6 +18,7 @@ import java.io.File; +import com.mojang.blaze3d.platform.InputUtil; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mutable; @@ -30,8 +31,7 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.client.option.GameOptions; -import net.minecraft.client.option.KeyBinding; -import net.minecraft.client.util.InputUtil; +import net.minecraft.client.option.KeyBind; import org.quiltmc.qsl.key.binds.impl.KeyBindManager; import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; @@ -42,7 +42,7 @@ public abstract class GameOptionsMixin { @Shadow @Mutable @Final - public KeyBinding[] allKeys; + public KeyBind[] allKeys; @Shadow @Final @@ -70,8 +70,8 @@ private void modifyAllKeys(MinecraftClient client, File file, CallbackInfo ci) { method = "accept(Lnet/minecraft/client/option/GameOptions$Visitor;)V" ) private void includeDisabledEntries(GameOptions.Visitor visitor, CallbackInfo ci) { - for (KeyBinding keyBind : KeyBindRegistryImpl.getDisabledKeyBinds()) { - String keyTranslationKey = keyBind.getBoundKeyTranslationKey(); + for (KeyBind keyBind : KeyBindRegistryImpl.getDisabledKeyBinds()) { + String keyTranslationKey = keyBind.getKeyTranslationKey(); String keyBindTranslationKey = visitor.visitString("key_" + keyBind.getTranslationKey(), keyTranslationKey); if (!keyTranslationKey.equals(keyBindTranslationKey)) { keyBind.setBoundKey(InputUtil.fromTranslationKey(keyTranslationKey)); diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingAccessor.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingAccessor.java index 39cd87cd37..9cfb3c4b16 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingAccessor.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingAccessor.java @@ -18,20 +18,20 @@ import java.util.Map; +import com.mojang.blaze3d.platform.InputUtil; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.gen.Accessor; import org.spongepowered.asm.mixin.gen.Invoker; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; -import net.minecraft.client.option.KeyBinding; -import net.minecraft.client.util.InputUtil; +import net.minecraft.client.option.KeyBind; @Environment(EnvType.CLIENT) -@Mixin(KeyBinding.class) +@Mixin(KeyBind.class) public interface KeyBindingAccessor { @Accessor(value = "KEYS_BY_ID") - static Map getKeysById() { + static Map getKeysById() { return null; } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingEntryMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingEntryMixin.java index e4f12e01fc..37288de718 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingEntryMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingEntryMixin.java @@ -19,6 +19,7 @@ import java.util.ArrayList; import java.util.List; +import com.mojang.blaze3d.platform.InputUtil; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -32,10 +33,9 @@ import net.fabricmc.api.Environment; import net.minecraft.client.gui.widget.ButtonWidget; -import net.minecraft.client.gui.widget.option.KeyBindingListWidget; -import net.minecraft.client.gui.widget.option.KeyBindingListWidget.KeyBindingEntry; -import net.minecraft.client.option.KeyBinding; -import net.minecraft.client.util.InputUtil; +import net.minecraft.client.gui.widget.option.KeyBindListWidget; +import net.minecraft.client.gui.widget.option.KeyBindListWidget.KeyBindEntry; +import net.minecraft.client.option.KeyBind; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.text.Text; import net.minecraft.text.TranslatableText; @@ -46,11 +46,11 @@ import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; @Environment(EnvType.CLIENT) -@Mixin(KeyBindingEntry.class) -public abstract class KeyBindingEntryMixin extends KeyBindingListWidget.Entry implements ConflictTooltipOwner { +@Mixin(KeyBindEntry.class) +public abstract class KeyBindingEntryMixin extends KeyBindListWidget.Entry implements ConflictTooltipOwner { @Shadow @Final - private KeyBinding binding; + private KeyBind binding; @Shadow @Final @@ -67,10 +67,10 @@ public abstract class KeyBindingEntryMixin extends KeyBindingListWidget.Entry im @Shadow(aliases = "field_2742", remap = false) @Final - KeyBindingListWidget field_2742; + KeyBindListWidget field_2742; @Inject(method = "", at = @At("TAIL")) - private void initPreviousBoundKey(KeyBindingListWidget list, KeyBinding keyBinding, Text text, CallbackInfo ci) { + private void initPreviousBoundKey(KeyBindListWidget list, KeyBind keyBinding, Text text, CallbackInfo ci) { this.quilt$previousBoundKey = null; quilt$changedBoundKey = null; } @@ -96,7 +96,7 @@ private void collectConflictTooltips(MatrixStack matrices, int index, int y, int } if (!this.binding.isUnbound()) { - for (KeyBinding keyBind : KeyBindRegistryImpl.getKeyBinds()) { + for (KeyBind keyBind : KeyBindRegistryImpl.getKeyBinds()) { if (keyBind != this.binding && this.binding.equals(keyBind)) { if (this.quilt$conflictTooltips.isEmpty()) { this.quilt$conflictTooltips.add(new TranslatableText("key.qsl.key_conflict.tooltip").formatted(Formatting.RED)); diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingMixin.java index 0661766731..f23aa842e6 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingMixin.java @@ -18,6 +18,7 @@ import java.util.Map; +import com.mojang.blaze3d.platform.InputUtil; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mutable; @@ -28,11 +29,10 @@ import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; -import net.minecraft.client.option.KeyBinding; -import net.minecraft.client.util.InputUtil; +import net.minecraft.client.option.KeyBind; @Environment(EnvType.CLIENT) -@Mixin(KeyBinding.class) +@Mixin(KeyBind.class) public abstract class KeyBindingMixin { @Shadow @Mutable diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingsScreenMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingsScreenMixin.java index 29282753cb..24a92d36ae 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingsScreenMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingsScreenMixin.java @@ -28,9 +28,9 @@ import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.option.GameOptionsScreen; -import net.minecraft.client.gui.screen.option.KeyBindingsScreen; -import net.minecraft.client.gui.widget.option.KeyBindingListWidget; -import net.minecraft.client.gui.widget.option.KeyBindingListWidget.KeyBindingEntry; +import net.minecraft.client.gui.screen.option.KeyBindsScreen; +import net.minecraft.client.gui.widget.option.KeyBindListWidget; +import net.minecraft.client.gui.widget.option.KeyBindListWidget.KeyBindEntry; import net.minecraft.client.option.GameOptions; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.text.Text; @@ -38,10 +38,10 @@ import org.quiltmc.qsl.key.binds.impl.ConflictTooltipOwner; @Environment(EnvType.CLIENT) -@Mixin(KeyBindingsScreen.class) +@Mixin(KeyBindsScreen.class) public abstract class KeyBindingsScreenMixin extends GameOptionsScreen { @Shadow - private KeyBindingListWidget keybindsList; + private KeyBindListWidget keybindsList; private KeyBindingsScreenMixin(Screen screen, GameOptions gameOptions, Text text) { super(screen, gameOptions, text); @@ -51,8 +51,8 @@ private KeyBindingsScreenMixin(Screen screen, GameOptions gameOptions, Text text @Inject(method = "render", at = @At("TAIL")) private void renderConflictTooltips(MatrixStack matrices, int mouseX, int mouseY, float delta, CallbackInfo ci) { // TODO - Somehow extend the hover area to include the label too - KeyBindingListWidget.Entry entry = ((EntryListWidgetAccessor) this.keybindsList).invokeGetHoveredEntry(); - if (entry != null && entry instanceof KeyBindingEntry keyBindEntry) { + KeyBindListWidget.Entry entry = ((EntryListWidgetAccessor) this.keybindsList).invokeGetHoveredEntry(); + if (entry != null && entry instanceof KeyBindEntry keyBindEntry) { List tooltipLines = ((ConflictTooltipOwner) keyBindEntry).getConflictTooltips(); this.renderTooltip(matrices, tooltipLines, mouseX, mouseY); } diff --git a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java index 102ec3da7a..b98103ac07 100644 --- a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java +++ b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java @@ -22,7 +22,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.option.KeyBind; import org.quiltmc.qsl.key.binds.api.KeyBindRegistry; import org.quiltmc.qsl.lifecycle.api.client.event.ClientLifecycleEvents; @@ -35,7 +35,7 @@ public class KeyBindRegistryTestMod implements ClientModInitializer { @Override public void onInitializeClient() { ClientLifecycleEvents.READY.register(lifecycleClient -> { - KeyBinding enableKeyBindKey = KeyBindRegistry.getKeyBind("key.qsl.enable_key_bind"); + KeyBind enableKeyBindKey = KeyBindRegistry.getKeyBind("key.qsl.enable_key_bind"); if (enableKeyBindKey != null) { LOGGER.info("Successfully got the \"Enable Key Bind\" key!"); diff --git a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java index fcfb946f08..cdc0edc910 100644 --- a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java +++ b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java @@ -21,7 +21,7 @@ import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; -import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.option.KeyBind; import net.minecraft.text.LiteralText; import org.quiltmc.qsl.key.binds.api.KeyBindRegistry; @@ -32,20 +32,20 @@ public class KeyBindsTestMod implements ClientModInitializer { public static final String KEY_CATEGORY = "key.qsl.category"; // A conflicting key test - public static final KeyBinding CONFLICT_TEST_KEY_BIND = KeyBindRegistry.registerKeyBind( - new KeyBinding("key.qsl.conflict_test", GLFW.GLFW_KEY_H, KEY_CATEGORY) + public static final KeyBind CONFLICT_TEST_KEY_BIND = KeyBindRegistry.registerKeyBind( + new KeyBind("key.qsl.conflict_test", GLFW.GLFW_KEY_H, KEY_CATEGORY) ); - public static final KeyBinding DISABLE_KEY_BIND = KeyBindRegistry.registerKeyBind( - new KeyBinding("key.qsl.disable_key_bind", GLFW.GLFW_KEY_H, KEY_CATEGORY), true + public static final KeyBind DISABLE_KEY_BIND = KeyBindRegistry.registerKeyBind( + new KeyBind("key.qsl.disable_key_bind", GLFW.GLFW_KEY_H, KEY_CATEGORY), true ); - public static final KeyBinding ENABLE_KEY_BIND = KeyBindRegistry.registerKeyBind( - new KeyBinding("key.qsl.enable_key_bind", GLFW.GLFW_KEY_I, KEY_CATEGORY), true + public static final KeyBind ENABLE_KEY_BIND = KeyBindRegistry.registerKeyBind( + new KeyBind("key.qsl.enable_key_bind", GLFW.GLFW_KEY_I, KEY_CATEGORY), true ); - public static final KeyBinding DISABLED_CONFLICT_TEST_KEY_BIND = KeyBindRegistry.registerKeyBind( - new KeyBinding("key.qsl.disabled_conflict_test", GLFW.GLFW_KEY_H, KEY_CATEGORY), false + public static final KeyBind DISABLED_CONFLICT_TEST_KEY_BIND = KeyBindRegistry.registerKeyBind( + new KeyBind("key.qsl.disabled_conflict_test", GLFW.GLFW_KEY_H, KEY_CATEGORY), false ); @Override From 04b1b4dbcc5408b9eced729f545fd94f1ecdb2ff Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Sat, 19 Feb 2022 23:08:02 -0300 Subject: [PATCH 32/56] Use the mappings refactor (II) --- .../quiltmc/qsl/key/binds/api/KeyBindRegistry.java | 4 ++-- .../qsl/key/binds/impl/KeyBindRegistryImpl.java | 10 +++++----- .../key/binds/mixin/client/GameOptionsMixin.java | 2 +- ...eyBindingAccessor.java => KeyBindAccessor.java} | 6 +++--- ...ndingEntryMixin.java => KeyBindEntryMixin.java} | 14 +++++++------- .../{KeyBindingMixin.java => KeyBindMixin.java} | 10 +++++----- ...gsScreenMixin.java => KeyBindsScreenMixin.java} | 8 ++++---- .../src/main/resources/quilt_key_binds.mixins.json | 8 ++++---- 8 files changed, 31 insertions(+), 31 deletions(-) rename library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/{KeyBindingAccessor.java => KeyBindAccessor.java} (90%) rename library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/{KeyBindingEntryMixin.java => KeyBindEntryMixin.java} (90%) rename library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/{KeyBindingMixin.java => KeyBindMixin.java} (79%) rename library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/{KeyBindingsScreenMixin.java => KeyBindsScreenMixin.java} (89%) diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java index 85d24d468b..5904ee4f5e 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java @@ -25,7 +25,7 @@ import net.minecraft.client.option.KeyBind; import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; -import org.quiltmc.qsl.key.binds.mixin.client.KeyBindingAccessor; +import org.quiltmc.qsl.key.binds.mixin.client.KeyBindAccessor; /** * Handles the registration of modded key binds and allows for changing their properties, @@ -88,7 +88,7 @@ public static KeyBind getKeyBind(String translationKey) { * @return the key bind's bound key */ public static InputUtil.Key getBoundKey(KeyBind key) { - return ((KeyBindingAccessor) key).getBoundKey(); + return ((KeyBindAccessor) key).getBoundKey(); } /** diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java index 7da0d90bec..1fbef48766 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java @@ -32,7 +32,7 @@ import net.minecraft.client.option.KeyBind; -import org.quiltmc.qsl.key.binds.mixin.client.KeyBindingAccessor; +import org.quiltmc.qsl.key.binds.mixin.client.KeyBindAccessor; @Environment(EnvType.CLIENT) @ApiStatus.Internal @@ -58,7 +58,7 @@ public static KeyBind registerKeyBind(KeyBind key, boolean enabled) { quiltKeys.put(key, enabled); applyChanges(); if (!enabled) { - KeyBindingAccessor.getKeysById().remove(key.getTranslationKey()); + KeyBindAccessor.getKeyBinds().remove(key.getTranslationKey()); } return key; @@ -96,12 +96,12 @@ public static void setEnabled(KeyBind key, boolean enabled) { applyChanges(); if (enabled) { - KeyBindingAccessor.getKeysById().put(key.getTranslationKey(), key); + KeyBindAccessor.getKeyBinds().put(key.getTranslationKey(), key); } else { - KeyBindingAccessor.getKeysById().remove(key.getTranslationKey(), key); + KeyBindAccessor.getKeyBinds().remove(key.getTranslationKey(), key); } - ((KeyBindingAccessor) key).callReset(); + ((KeyBindAccessor) key).callReset(); KeyBind.updateBoundKeys(); } else { throwUnregisteredKeyException(key); diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsMixin.java index 1663567ecf..a31fdb78c4 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsMixin.java @@ -65,7 +65,7 @@ private void modifyAllKeys(MinecraftClient client, File file, CallbackInfo ci) { @Inject( at = @At( value = "FIELD", - target = "Lnet/minecraft/client/option/GameOptions;allKeys:[Lnet/minecraft/client/option/KeyBinding;" + target = "Lnet/minecraft/client/option/GameOptions;allKeys:[Lnet/minecraft/client/option/KeyBind;" ), method = "accept(Lnet/minecraft/client/option/GameOptions$Visitor;)V" ) diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingAccessor.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindAccessor.java similarity index 90% rename from library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingAccessor.java rename to library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindAccessor.java index 9cfb3c4b16..1fb3ce2017 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingAccessor.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindAccessor.java @@ -29,9 +29,9 @@ @Environment(EnvType.CLIENT) @Mixin(KeyBind.class) -public interface KeyBindingAccessor { - @Accessor(value = "KEYS_BY_ID") - static Map getKeysById() { +public interface KeyBindAccessor { + @Accessor(value = "KEY_BINDS") + static Map getKeyBinds() { return null; } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingEntryMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java similarity index 90% rename from library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingEntryMixin.java rename to library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java index 37288de718..2df8a2dda2 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingEntryMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java @@ -47,10 +47,10 @@ @Environment(EnvType.CLIENT) @Mixin(KeyBindEntry.class) -public abstract class KeyBindingEntryMixin extends KeyBindListWidget.Entry implements ConflictTooltipOwner { +public abstract class KeyBindEntryMixin extends KeyBindListWidget.Entry implements ConflictTooltipOwner { @Shadow @Final - private KeyBind binding; + private KeyBind key; @Shadow @Final @@ -70,7 +70,7 @@ public abstract class KeyBindingEntryMixin extends KeyBindListWidget.Entry imple KeyBindListWidget field_2742; @Inject(method = "", at = @At("TAIL")) - private void initPreviousBoundKey(KeyBindListWidget list, KeyBind keyBinding, Text text, CallbackInfo ci) { + private void initPreviousBoundKey(KeyBindListWidget list, KeyBind key, Text text, CallbackInfo ci) { this.quilt$previousBoundKey = null; quilt$changedBoundKey = null; } @@ -79,12 +79,12 @@ private void initPreviousBoundKey(KeyBindListWidget list, KeyBind keyBinding, Te method = "render", at = @At( value = "INVOKE", - target = "Lnet/minecraft/client/option/KeyBinding;isUnbound()Z" + target = "Lnet/minecraft/client/option/KeyBind;isUnbound()Z" ), locals = LocalCapture.CAPTURE_FAILHARD ) private void collectConflictTooltips(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta, CallbackInfo ci, boolean bl, boolean bl2) { - InputUtil.Key boundKey = KeyBindRegistry.getBoundKey(this.binding); + InputUtil.Key boundKey = KeyBindRegistry.getBoundKey(this.key); if (!boundKey.equals(this.quilt$previousBoundKey) || quilt$changedBoundKey != null) { this.quilt$conflictTooltips.clear(); @@ -95,9 +95,9 @@ private void collectConflictTooltips(MatrixStack matrices, int index, int y, int quilt$changedBoundKey = boundKey; } - if (!this.binding.isUnbound()) { + if (!this.key.isUnbound()) { for (KeyBind keyBind : KeyBindRegistryImpl.getKeyBinds()) { - if (keyBind != this.binding && this.binding.equals(keyBind)) { + if (keyBind != this.key && this.key.equals(keyBind)) { if (this.quilt$conflictTooltips.isEmpty()) { this.quilt$conflictTooltips.add(new TranslatableText("key.qsl.key_conflict.tooltip").formatted(Formatting.RED)); } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindMixin.java similarity index 79% rename from library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingMixin.java rename to library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindMixin.java index f23aa842e6..b2ac6d1ecd 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindMixin.java @@ -33,16 +33,16 @@ @Environment(EnvType.CLIENT) @Mixin(KeyBind.class) -public abstract class KeyBindingMixin { +public abstract class KeyBindMixin { @Shadow @Mutable @Final - private static Map CATEGORY_ORDER_MAP; + private static Map ORDER_BY_CATEGORIES; - @Inject(method = "(Ljava/lang/String;Lnet/minecraft/client/util/InputUtil$Type;ILjava/lang/String;)V", at = @At("TAIL")) + @Inject(method = "(Ljava/lang/String;Lcom/mojang/blaze3d/platform/InputUtil$Type;ILjava/lang/String;)V", at = @At("TAIL")) private void addModdedCategory(String string, InputUtil.Type type, int i, String string2, CallbackInfo ci) { - if (!CATEGORY_ORDER_MAP.containsKey(string2)) { - CATEGORY_ORDER_MAP.put(string2, CATEGORY_ORDER_MAP.size() + 1); + if (!ORDER_BY_CATEGORIES.containsKey(string2)) { + ORDER_BY_CATEGORIES.put(string2, ORDER_BY_CATEGORIES.size() + 1); } } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingsScreenMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindsScreenMixin.java similarity index 89% rename from library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingsScreenMixin.java rename to library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindsScreenMixin.java index 24a92d36ae..35f59bef78 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindingsScreenMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindsScreenMixin.java @@ -39,11 +39,11 @@ @Environment(EnvType.CLIENT) @Mixin(KeyBindsScreen.class) -public abstract class KeyBindingsScreenMixin extends GameOptionsScreen { +public abstract class KeyBindsScreenMixin extends GameOptionsScreen { @Shadow - private KeyBindListWidget keybindsList; + private KeyBindListWidget keyBindList; - private KeyBindingsScreenMixin(Screen screen, GameOptions gameOptions, Text text) { + private KeyBindsScreenMixin(Screen screen, GameOptions gameOptions, Text text) { super(screen, gameOptions, text); } @@ -51,7 +51,7 @@ private KeyBindingsScreenMixin(Screen screen, GameOptions gameOptions, Text text @Inject(method = "render", at = @At("TAIL")) private void renderConflictTooltips(MatrixStack matrices, int mouseX, int mouseY, float delta, CallbackInfo ci) { // TODO - Somehow extend the hover area to include the label too - KeyBindListWidget.Entry entry = ((EntryListWidgetAccessor) this.keybindsList).invokeGetHoveredEntry(); + KeyBindListWidget.Entry entry = ((EntryListWidgetAccessor) this.keyBindList).invokeGetHoveredEntry(); if (entry != null && entry instanceof KeyBindEntry keyBindEntry) { List tooltipLines = ((ConflictTooltipOwner) keyBindEntry).getConflictTooltips(); this.renderTooltip(matrices, tooltipLines, mouseX, mouseY); diff --git a/library/gui/key_binds/src/main/resources/quilt_key_binds.mixins.json b/library/gui/key_binds/src/main/resources/quilt_key_binds.mixins.json index ef5e2d17fe..715978352c 100644 --- a/library/gui/key_binds/src/main/resources/quilt_key_binds.mixins.json +++ b/library/gui/key_binds/src/main/resources/quilt_key_binds.mixins.json @@ -6,10 +6,10 @@ "client.EntryListWidgetAccessor", "client.GameOptionsAccessor", "client.GameOptionsMixin", - "client.KeyBindingAccessor", - "client.KeyBindingEntryMixin", - "client.KeyBindingMixin", - "client.KeyBindingsScreenMixin" + "client.KeyBindAccessor", + "client.KeyBindEntryMixin", + "client.KeyBindMixin", + "client.KeyBindsScreenMixin" ], "injectors": { "defaultRequire": 1 From ac51db4b4ac6794231d4e8231bb28003990452f0 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Sat, 19 Feb 2022 23:08:37 -0300 Subject: [PATCH 33/56] Actually fix the Brazilian Portuguese translation Viva! --- .../assets/quilt_key_binds/lang/{pt_br2.json => pt_br.json} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename library/gui/key_binds/src/main/resources/assets/quilt_key_binds/lang/{pt_br2.json => pt_br.json} (100%) diff --git a/library/gui/key_binds/src/main/resources/assets/quilt_key_binds/lang/pt_br2.json b/library/gui/key_binds/src/main/resources/assets/quilt_key_binds/lang/pt_br.json similarity index 100% rename from library/gui/key_binds/src/main/resources/assets/quilt_key_binds/lang/pt_br2.json rename to library/gui/key_binds/src/main/resources/assets/quilt_key_binds/lang/pt_br.json From 9acea91c501eda43947f78b67df4c16727400b8b Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Sun, 20 Feb 2022 13:28:26 -0300 Subject: [PATCH 34/56] Move to QSL event entrypoints --- .../test/client/KeyBindRegistryTestMod.java | 12 ++--- .../binds/test/client/KeyBindsTestMod.java | 47 +++++++++---------- .../src/testmod/resources/fabric.mod.json | 2 +- 3 files changed, 29 insertions(+), 32 deletions(-) diff --git a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java index b98103ac07..bbd217b8e1 100644 --- a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java +++ b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java @@ -16,12 +16,14 @@ package org.quiltmc.qsl.key.binds.test.client; -import net.fabricmc.api.ClientModInitializer; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; +import net.fabricmc.loader.api.ModContainer; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import net.minecraft.client.MinecraftClient; import net.minecraft.client.option.KeyBind; import org.quiltmc.qsl.key.binds.api.KeyBindRegistry; @@ -29,13 +31,12 @@ import org.quiltmc.qsl.lifecycle.api.client.event.ClientTickEvents; @Environment(EnvType.CLIENT) -public class KeyBindRegistryTestMod implements ClientModInitializer { +public class KeyBindRegistryTestMod implements ClientLifecycleEvents.Ready { public static final Logger LOGGER = LoggerFactory.getLogger("KeyBindRegistryTest"); @Override - public void onInitializeClient() { - ClientLifecycleEvents.READY.register(lifecycleClient -> { - KeyBind enableKeyBindKey = KeyBindRegistry.getKeyBind("key.qsl.enable_key_bind"); + public void readyClient(MinecraftClient client) { + KeyBind enableKeyBindKey = KeyBindRegistry.getKeyBind("key.qsl.enable_key_bind"); if (enableKeyBindKey != null) { LOGGER.info("Successfully got the \"Enable Key Bind\" key!"); @@ -51,6 +52,5 @@ public void onInitializeClient() { KeyBindRegistry.getAllKeyBinds(true).forEach((key, value) -> { LOGGER.info(String.format("%s: %s", key.getTranslationKey(), value)); }); - }); } } diff --git a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java index cdc0edc910..41e2fb336c 100644 --- a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java +++ b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java @@ -17,10 +17,9 @@ package org.quiltmc.qsl.key.binds.test.client; import org.lwjgl.glfw.GLFW; -import net.fabricmc.api.ClientModInitializer; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; - +import net.minecraft.client.MinecraftClient; import net.minecraft.client.option.KeyBind; import net.minecraft.text.LiteralText; @@ -28,7 +27,7 @@ import org.quiltmc.qsl.lifecycle.api.client.event.ClientTickEvents; @Environment(EnvType.CLIENT) -public class KeyBindsTestMod implements ClientModInitializer { +public class KeyBindsTestMod implements ClientTickEvents.Start { public static final String KEY_CATEGORY = "key.qsl.category"; // A conflicting key test @@ -49,35 +48,33 @@ public class KeyBindsTestMod implements ClientModInitializer { ); @Override - public void onInitializeClient() { - ClientTickEvents.START.register(client -> { - if (DISABLE_KEY_BIND.isPressed()) { - if (client.player != null) { - client.player.sendMessage(new LiteralText("The key has disappeared! Bye bye, key!"), true); - } - - KeyBindRegistry.setEnabled(DISABLE_KEY_BIND, false); + public void startClientTick(MinecraftClient client) { + if (DISABLE_KEY_BIND.isPressed()) { + if (client.player != null) { + client.player.sendMessage(new LiteralText("The key has disappeared! Bye bye, key!"), true); } - if (ENABLE_KEY_BIND.isPressed()) { - if (client.player != null) { - client.player.sendMessage(new LiteralText("The key is back!"), true); - } + KeyBindRegistry.setEnabled(DISABLE_KEY_BIND, false); + } - KeyBindRegistry.setEnabled(DISABLE_KEY_BIND, true); + if (ENABLE_KEY_BIND.isPressed()) { + if (client.player != null) { + client.player.sendMessage(new LiteralText("The key is back!"), true); } - if (CONFLICT_TEST_KEY_BIND.isPressed()) { - if (client.player != null) { - client.player.sendMessage(new LiteralText("This is the conflict key being pressed"), false); - } + KeyBindRegistry.setEnabled(DISABLE_KEY_BIND, true); + } + + if (CONFLICT_TEST_KEY_BIND.isPressed()) { + if (client.player != null) { + client.player.sendMessage(new LiteralText("This is the conflict key being pressed"), false); } + } - if (DISABLED_CONFLICT_TEST_KEY_BIND.isPressed()) { - if (client.player != null) { - client.player.sendMessage(new LiteralText("I'm not supposed to do things! Why am I enabled?"), false); - } + if (DISABLED_CONFLICT_TEST_KEY_BIND.isPressed()) { + if (client.player != null) { + client.player.sendMessage(new LiteralText("I'm not supposed to do things! Why am I enabled?"), false); } - }); + } } } diff --git a/library/gui/key_binds/src/testmod/resources/fabric.mod.json b/library/gui/key_binds/src/testmod/resources/fabric.mod.json index 6258bcfc0a..ecbfbaa899 100644 --- a/library/gui/key_binds/src/testmod/resources/fabric.mod.json +++ b/library/gui/key_binds/src/testmod/resources/fabric.mod.json @@ -9,7 +9,7 @@ "quilt_key_binds": "*" }, "entrypoints": { - "client": [ + "client_events": [ "org.quiltmc.qsl.key.binds.test.client.KeyBindRegistryTestMod", "org.quiltmc.qsl.key.binds.test.client.KeyBindsTestMod" ] From 16ba2a017bde4273263c7ca77b66d4b08948494a Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Sun, 20 Feb 2022 13:29:48 -0300 Subject: [PATCH 35/56] Fix conflict tooltips --- .../qsl/key/binds/mixin/client/KeyBindEntryMixin.java | 6 +++--- .../quiltmc/qsl/key/binds/mixin/client/KeyBindMixin.java | 6 +++--- .../qsl/key/binds/mixin/client/KeyBindsScreenMixin.java | 4 +++- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java index 2df8a2dda2..a8bccf24a8 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java @@ -96,13 +96,13 @@ private void collectConflictTooltips(MatrixStack matrices, int index, int y, int } if (!this.key.isUnbound()) { - for (KeyBind keyBind : KeyBindRegistryImpl.getKeyBinds()) { - if (keyBind != this.key && this.key.equals(keyBind)) { + for (KeyBind otherKey : KeyBindRegistryImpl.getKeyBinds()) { + if (otherKey != this.key && this.key.keyEquals(otherKey)) { if (this.quilt$conflictTooltips.isEmpty()) { this.quilt$conflictTooltips.add(new TranslatableText("key.qsl.key_conflict.tooltip").formatted(Formatting.RED)); } - this.quilt$conflictTooltips.add(new TranslatableText("key.qsl.key_conflict.tooltip.entry", new TranslatableText(keyBind.getTranslationKey())).formatted(Formatting.RED)); + this.quilt$conflictTooltips.add(new TranslatableText("key.qsl.key_conflict.tooltip.entry", new TranslatableText(this.key.getTranslationKey())).formatted(Formatting.RED)); } } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindMixin.java index b2ac6d1ecd..79fe285bfb 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindMixin.java @@ -40,9 +40,9 @@ public abstract class KeyBindMixin { private static Map ORDER_BY_CATEGORIES; @Inject(method = "(Ljava/lang/String;Lcom/mojang/blaze3d/platform/InputUtil$Type;ILjava/lang/String;)V", at = @At("TAIL")) - private void addModdedCategory(String string, InputUtil.Type type, int i, String string2, CallbackInfo ci) { - if (!ORDER_BY_CATEGORIES.containsKey(string2)) { - ORDER_BY_CATEGORIES.put(string2, ORDER_BY_CATEGORIES.size() + 1); + private void addModdedCategory(String translationKey, InputUtil.Type type, int keyCode, String category, CallbackInfo ci) { + if (!ORDER_BY_CATEGORIES.containsKey(category)) { + ORDER_BY_CATEGORIES.put(category, ORDER_BY_CATEGORIES.size() + 1); } } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindsScreenMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindsScreenMixin.java index 35f59bef78..7c038cd696 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindsScreenMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindsScreenMixin.java @@ -54,7 +54,9 @@ private void renderConflictTooltips(MatrixStack matrices, int mouseX, int mouseY KeyBindListWidget.Entry entry = ((EntryListWidgetAccessor) this.keyBindList).invokeGetHoveredEntry(); if (entry != null && entry instanceof KeyBindEntry keyBindEntry) { List tooltipLines = ((ConflictTooltipOwner) keyBindEntry).getConflictTooltips(); - this.renderTooltip(matrices, tooltipLines, mouseX, mouseY); + if (tooltipLines != null) { + this.renderTooltip(matrices, tooltipLines, mouseX, mouseY); + } } } } From 38c18f1ccbf354d5496dc34d4df5e2b46fa7ab7c Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Wed, 2 Mar 2022 15:14:14 -0300 Subject: [PATCH 36/56] Begin Pink's config system and key chords --- library/gui/key_binds/build.gradle | 4 - .../key/binds/impl/chords/ChordedKeyBind.java | 6 + .../qsl/key/binds/impl/chords/KeyChord.java | 17 ++ .../impl/config/QuiltKeyBindsConfig.java | 52 ++++++ .../config/QuiltKeyBindsConfigManager.java | 127 ++++++++++++++ .../binds/mixin/client/GameOptionsMixin.java | 18 -- .../binds/mixin/client/KeyBindEntryMixin.java | 10 +- .../mixin/client/chords/KeyBindAccessor.java | 15 ++ .../mixin/client/chords/KeyBindMixin.java | 164 ++++++++++++++++++ .../client/chords/KeyBindsScreenMixin.java | 93 ++++++++++ .../mixin/client/config/GameOptionsMixin.java | 37 ++++ .../client/config/MinecraftClientMixin.java | 24 +++ .../src/main/resources/fabric.mod.json | 1 - .../resources/quilt_key_binds.accesswidener | 3 - .../resources/quilt_key_binds.mixins.json | 5 + .../test/client/KeyBindRegistryTestMod.java | 1 - 16 files changed, 545 insertions(+), 32 deletions(-) create mode 100644 library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/ChordedKeyBind.java create mode 100644 library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java create mode 100644 library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java create mode 100644 library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java create mode 100644 library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindAccessor.java create mode 100644 library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java create mode 100644 library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java create mode 100644 library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/GameOptionsMixin.java create mode 100644 library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/MinecraftClientMixin.java delete mode 100644 library/gui/key_binds/src/main/resources/quilt_key_binds.accesswidener diff --git a/library/gui/key_binds/build.gradle b/library/gui/key_binds/build.gradle index 3c27ba1afe..134e500def 100644 --- a/library/gui/key_binds/build.gradle +++ b/library/gui/key_binds/build.gradle @@ -14,7 +14,3 @@ qslModule { } } } - -loom { - accessWidenerPath = file("src/main/resources/quilt_key_binds.accesswidener") -} diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/ChordedKeyBind.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/ChordedKeyBind.java new file mode 100644 index 0000000000..77b492a65e --- /dev/null +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/ChordedKeyBind.java @@ -0,0 +1,6 @@ +package org.quiltmc.qsl.key.binds.impl.chords; + +public interface ChordedKeyBind { + KeyChord getBoundChord(); + void setBoundChord(KeyChord chord); +} diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java new file mode 100644 index 0000000000..62cd98fe66 --- /dev/null +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java @@ -0,0 +1,17 @@ +package org.quiltmc.qsl.key.binds.impl.chords; + +import java.util.HashMap; +import java.util.Map; + +import com.mojang.blaze3d.platform.InputUtil; + +public class KeyChord { + // TODO - Private this, add methods for getting/modifying it + public Map keys = new HashMap<>(); + + public KeyChord(Map keys) { + this.keys = keys; + } + + public KeyChord() {} +} diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java new file mode 100644 index 0000000000..dcb63b4d54 --- /dev/null +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java @@ -0,0 +1,52 @@ +package org.quiltmc.qsl.key.binds.impl.config; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.mojang.serialization.Codec; +import com.mojang.serialization.codecs.RecordCodecBuilder; + +public class QuiltKeyBindsConfig { + public static final Codec CODEC = RecordCodecBuilder.create( + instance -> instance.group( + // TODO - Implement me! + Codec.BOOL.fieldOf("show_tutorial_toast").forGetter(QuiltKeyBindsConfig::getShowTutorialToast), + // TODO - Ideally, it would be a list for chords, a single string for single keys, and an empty list for unbound + Codec.unboundedMap(Codec.STRING, Codec.list(Codec.STRING)).fieldOf("key_binds").forGetter(QuiltKeyBindsConfig::getKeyBinds) + ) + .apply(instance, QuiltKeyBindsConfig::new) + ); + + private boolean showTutorialToast; + private Map> keyBinds; + + public QuiltKeyBindsConfig() { + this.keyBinds = new HashMap<>(); + this.showTutorialToast = false; + } + + public QuiltKeyBindsConfig( + boolean showTutorialToast, + Map> keyBinds + ) { + this.showTutorialToast = showTutorialToast; + this.keyBinds = keyBinds; + } + + public boolean getShowTutorialToast() { + return this.showTutorialToast; + } + + public void setShowTutorialToast(boolean showTutorialToast) { + this.showTutorialToast = showTutorialToast; + } + + public Map> getKeyBinds() { + return keyBinds; + } + + public void setKeyBinds(Map> keyBinds) { + this.keyBinds = keyBinds; + } +} diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java new file mode 100644 index 0000000000..c05334231d --- /dev/null +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java @@ -0,0 +1,127 @@ +package org.quiltmc.qsl.key.binds.impl.config; + +import java.io.IOException; +import java.io.Reader; +import java.io.Writer; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonParseException; +import com.google.gson.JsonParser; +import com.google.gson.stream.JsonWriter; +import com.mojang.blaze3d.platform.InputUtil; +import com.mojang.serialization.JsonOps; + +import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; +import org.quiltmc.qsl.key.binds.impl.chords.ChordedKeyBind; +import org.quiltmc.qsl.key.binds.impl.chords.KeyChord; + +import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.option.KeyBind; + +// TODO - Understand this mess that you have written down +public class QuiltKeyBindsConfigManager { + public static Optional isConfigLoaded = Optional.empty(); + public static final QuiltKeyBindsConfig CONFIG = new QuiltKeyBindsConfig(); + public static final Path QSL_CONFIG_PATH = FabricLoader.getInstance().getConfigDir().resolve("qsl"); + public static final Path KEY_BINDS_CONFIG_PATH = QSL_CONFIG_PATH.resolve("key_binds.json"); + public static final Gson GSON = new GsonBuilder().setPrettyPrinting().serializeNulls().disableHtmlEscaping().create(); + + public static void loadConfig() { + if (Files.exists(KEY_BINDS_CONFIG_PATH)) { + try { + Reader reader = Files.newBufferedReader(KEY_BINDS_CONFIG_PATH, StandardCharsets.UTF_8); + var result = QuiltKeyBindsConfig.CODEC.parse(JsonOps.INSTANCE, JsonParser.parseReader(reader)).result(); + reader.close(); + + if (result.isPresent()) { + registerConfigChanges(result.get()); + populateConfig(); + saveModConfig(); + isConfigLoaded = Optional.of(true); + } else { + isConfigLoaded = Optional.of(false); + } + } catch (IOException | JsonParseException e) { + System.err.println(e); + } + } else { + if (!Files.isDirectory(QSL_CONFIG_PATH)) { + try { + Files.createDirectory(QSL_CONFIG_PATH); + } catch (IOException e) { + e.printStackTrace(); + } + } + populateConfig(); + saveModConfig(); + } + } + + public static void registerConfigChanges(QuiltKeyBindsConfig newConfig) { + MinecraftClient client = MinecraftClient.getInstance(); + for (KeyBind key : client.options.allKeys) { + if (newConfig.getKeyBinds().containsKey(key.getTranslationKey())) { + List keyList = newConfig.getKeyBinds().get(key.getTranslationKey()); + if (keyList.size() == 1) { + key.setBoundKey(InputUtil.fromTranslationKey(keyList.get(0))); + } else if (keyList.size() > 1) { + Map map = new HashMap<>(); + for (String string : keyList) { + map.put(InputUtil.fromTranslationKey(string), false); + } + ((ChordedKeyBind)key).setBoundChord(new KeyChord(map)); + } + } + }; + CONFIG.setKeyBinds(newConfig.getKeyBinds()); + } + + public static void saveModConfig() { + var result = QuiltKeyBindsConfig.CODEC.encodeStart(JsonOps.INSTANCE, CONFIG).result(); + if (result.isPresent()) { + try { + Writer writer = Files.newBufferedWriter(KEY_BINDS_CONFIG_PATH, StandardCharsets.UTF_8); + JsonWriter jsonWriter = GSON.newJsonWriter(writer); + GSON.toJson(result.get(), jsonWriter); + jsonWriter.close(); + } catch (IOException e) { + System.err.println(e); + } + } + } + + public static void populateConfig() { + Map> keyBindMap = new HashMap<>(); + + KeyBindRegistryImpl.getAllKeyBinds(true).forEach((keyBind, disabled) -> { + if (((ChordedKeyBind)keyBind).getBoundChord() != null) { + List list = new ArrayList<>(); + for (InputUtil.Key key : ((ChordedKeyBind)keyBind).getBoundChord().keys.keySet()) { + list.add(key.getTranslationKey()); + } + keyBindMap.put(keyBind.getTranslationKey(), list); + } else { + keyBindMap.put(keyBind.getTranslationKey(), List.of(keyBind.getKeyTranslationKey())); + } + /* + if (!CONFIG.getKeyBinds().containsKey(key.getTranslationKey())) { + keyBindMap.put(key.getTranslationKey(), List.of(key.getKeyTranslationKey())); + } else { + keyBindMap.put(key.getTranslationKey(), List.of(key.getKeyTranslationKey())); + //key.setBoundKey(InputUtil.fromTranslationKey(CONFIG.getKeyBinds().get(key.getTranslationKey()))); + } + */ + }); + CONFIG.setKeyBinds(keyBindMap); + } +} diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsMixin.java index a31fdb78c4..aa8af1b83e 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsMixin.java @@ -18,7 +18,6 @@ import java.io.File; -import com.mojang.blaze3d.platform.InputUtil; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mutable; @@ -61,21 +60,4 @@ private void modifyAllKeys(MinecraftClient client, File file, CallbackInfo ci) { this.allKeys = KeyBindRegistryImpl.getKeyBinds(); } } - - @Inject( - at = @At( - value = "FIELD", - target = "Lnet/minecraft/client/option/GameOptions;allKeys:[Lnet/minecraft/client/option/KeyBind;" - ), - method = "accept(Lnet/minecraft/client/option/GameOptions$Visitor;)V" - ) - private void includeDisabledEntries(GameOptions.Visitor visitor, CallbackInfo ci) { - for (KeyBind keyBind : KeyBindRegistryImpl.getDisabledKeyBinds()) { - String keyTranslationKey = keyBind.getKeyTranslationKey(); - String keyBindTranslationKey = visitor.visitString("key_" + keyBind.getTranslationKey(), keyTranslationKey); - if (!keyTranslationKey.equals(keyBindTranslationKey)) { - keyBind.setBoundKey(InputUtil.fromTranslationKey(keyTranslationKey)); - } - }; - } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java index a8bccf24a8..41eb0f9994 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java @@ -60,7 +60,7 @@ public abstract class KeyBindEntryMixin extends KeyBindListWidget.Entry implemen private List quilt$conflictTooltips = new ArrayList<>(2); @Unique - private InputUtil.Key quilt$previousBoundKey; + private static InputUtil.Key quilt$previousBoundKey; @Unique private static InputUtil.Key quilt$changedBoundKey; @@ -71,7 +71,7 @@ public abstract class KeyBindEntryMixin extends KeyBindListWidget.Entry implemen @Inject(method = "", at = @At("TAIL")) private void initPreviousBoundKey(KeyBindListWidget list, KeyBind key, Text text, CallbackInfo ci) { - this.quilt$previousBoundKey = null; + quilt$previousBoundKey = null; quilt$changedBoundKey = null; } @@ -86,7 +86,7 @@ private void initPreviousBoundKey(KeyBindListWidget list, KeyBind key, Text text private void collectConflictTooltips(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta, CallbackInfo ci, boolean bl, boolean bl2) { InputUtil.Key boundKey = KeyBindRegistry.getBoundKey(this.key); - if (!boundKey.equals(this.quilt$previousBoundKey) || quilt$changedBoundKey != null) { + if (!boundKey.equals(quilt$previousBoundKey) || quilt$changedBoundKey != null) { this.quilt$conflictTooltips.clear(); if (quilt$changedBoundKey != null && quilt$changedBoundKey.equals(boundKey)) { @@ -102,13 +102,13 @@ private void collectConflictTooltips(MatrixStack matrices, int index, int y, int this.quilt$conflictTooltips.add(new TranslatableText("key.qsl.key_conflict.tooltip").formatted(Formatting.RED)); } - this.quilt$conflictTooltips.add(new TranslatableText("key.qsl.key_conflict.tooltip.entry", new TranslatableText(this.key.getTranslationKey())).formatted(Formatting.RED)); + this.quilt$conflictTooltips.add(new TranslatableText("key.qsl.key_conflict.tooltip.entry", new TranslatableText(otherKey.getTranslationKey())).formatted(Formatting.RED)); } } } } - this.quilt$previousBoundKey = boundKey; + quilt$previousBoundKey = boundKey; } @ModifyArg( diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindAccessor.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindAccessor.java new file mode 100644 index 0000000000..e25f2e106f --- /dev/null +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindAccessor.java @@ -0,0 +1,15 @@ +package org.quiltmc.qsl.key.binds.mixin.client.chords; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +import net.minecraft.client.option.KeyBind; + +@Mixin(KeyBind.class) +public interface KeyBindAccessor { + @Accessor + int getTimesPressed(); + + @Accessor + void setTimesPressed(int timesPressed); +} diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java new file mode 100644 index 0000000000..9035408547 --- /dev/null +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java @@ -0,0 +1,164 @@ +package org.quiltmc.qsl.key.binds.mixin.client.chords; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import com.mojang.blaze3d.platform.InputUtil; + +import org.quiltmc.qsl.key.binds.impl.chords.ChordedKeyBind; +import org.quiltmc.qsl.key.binds.impl.chords.KeyChord; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import org.spongepowered.asm.mixin.injection.callback.LocalCapture; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.option.KeyBind; +import net.minecraft.text.LiteralText; +import net.minecraft.text.MutableText; +import net.minecraft.text.Text; + +@Mixin(KeyBind.class) +public class KeyBindMixin implements ChordedKeyBind { + @Shadow + @Final + private static Map KEY_BINDS; + + @Shadow + private InputUtil.Key boundKey; + + @Unique + private static final Map KEY_BINDS_BY_CHORD = new HashMap<>(); + + @Unique + @Final + private KeyChord quilt$defaultChord; + + @Unique + private KeyChord quilt$boundChord; + + @Inject( + at = @At("RETURN"), + method = "(Ljava/lang/String;Lcom/mojang/blaze3d/platform/InputUtil$Type;ILjava/lang/String;)V" + ) + private void expandInit(String string, InputUtil.Type type, int i, String string2, CallbackInfo ci) { + quilt$defaultChord = null; + quilt$boundChord = null; + } + + @Inject(at = @At("HEAD"), method = "onKeyPressed") + private static void detectChordsOnIncrement(InputUtil.Key startingKey, CallbackInfo ci) { + for (KeyChord chord : KEY_BINDS_BY_CHORD.keySet()) { + if (chord.keys.containsKey(startingKey) && !chord.keys.containsValue(false)) { + // This ensures that the chord will only be incremented once instead of N times + if (startingKey.equals(chord.keys.keySet().toArray()[0])) { + KeyBind keyBind = KEY_BINDS_BY_CHORD.get(chord); + ((KeyBindAccessor)keyBind).setTimesPressed(((KeyBindAccessor)keyBind).getTimesPressed() + 1); + } + } + } + } + + @Inject(at = @At("HEAD"), method = "setKeyPressed") + private static void detectChordsOnSet(InputUtil.Key startingKey, boolean pressed, CallbackInfo ci) { + for (KeyChord chord : KEY_BINDS_BY_CHORD.keySet()) { + if (chord.keys.containsKey(startingKey)) { + chord.keys.put(startingKey, pressed); + + if (!chord.keys.containsValue(false)) { + KEY_BINDS_BY_CHORD.get(chord).setPressed(true); + } else { + KEY_BINDS_BY_CHORD.get(chord).setPressed(false); + } + } + } + } + + @Inject(at = @At("HEAD"), method = "getKeyName", cancellable = true) + private void useChordName(CallbackInfoReturnable cir) { + if (this.quilt$boundChord != null) { + MutableText text = LiteralText.EMPTY.shallowCopy(); + for (InputUtil.Key key : this.quilt$boundChord.keys.keySet()) { + if (text.getSiblings().size() != 0) { + text.append(" + "); + } + text.append(key.getDisplayText()); + } + cir.setReturnValue(text); + } + } + + @Inject( + at = @At( + value = "INVOKE", + target = "Lcom/mojang/blaze3d/platform/InputUtil$Key;getType()Lcom/mojang/blaze3d/platform/InputUtil$Type;" + ), + method = "updatePressedStates", + locals = LocalCapture.CAPTURE_FAILHARD, + cancellable = true + ) + private static void updateChordsToo(CallbackInfo ci, Iterator iterator, KeyBind keyBind) { + KeyChord chord = ((KeyBindMixin)(Object)keyBind).quilt$boundChord; + if (chord != null) { + long window = MinecraftClient.getInstance().getWindow().getHandle(); + for (InputUtil.Key key : chord.keys.keySet()) { + if (key.getType() == InputUtil.Type.KEYSYM) { + chord.keys.put(key, InputUtil.isKeyPressed(window, key.getKeyCode())); + } + } + // TODO - Create an "Update Chord" method for this + if (!chord.keys.containsValue(false)) { + KEY_BINDS_BY_CHORD.get(chord).setPressed(true); + } else { + KEY_BINDS_BY_CHORD.get(chord).setPressed(false); + } + + ci.cancel(); + } + } + + @Inject(at = @At("HEAD"), method = "updateBoundKeys") + private static void updateChordBoundKeys(CallbackInfo cir) { + KEY_BINDS_BY_CHORD.clear(); + + for (KeyBind key : KEY_BINDS.values()) { + KeyChord chord = ((KeyBindMixin)(Object)key).quilt$boundChord; + if (chord != null) { + KEY_BINDS_BY_CHORD.put(chord, key); + } + } + } + + // TODO - Detect chords for matchesKey too; They are such a weird case + + // TODO - Detech chords for matchesMouseButton as well + + @Inject(at = @At("HEAD"), method = "isDefault", cancellable = true) + private void detectDefaultChord(CallbackInfoReturnable cir) { + if (this.quilt$boundChord != null) { + cir.setReturnValue(this.quilt$boundChord.equals(this.quilt$defaultChord)); + } + } + + @Inject(at = @At("HEAD"), method = "setBoundKey", cancellable = true) + private void resetChord(CallbackInfo ci) { + this.quilt$boundChord = null; + } + + @Override + public KeyChord getBoundChord() { + return this.quilt$boundChord; + } + + @Override + public void setBoundChord(KeyChord chord) { + this.quilt$boundChord = chord; + this.boundKey = InputUtil.UNKNOWN_KEY; + } +} diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java new file mode 100644 index 0000000000..b6d3c8d020 --- /dev/null +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java @@ -0,0 +1,93 @@ +package org.quiltmc.qsl.key.binds.mixin.client.chords; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.mojang.blaze3d.platform.InputUtil; + +import org.jetbrains.annotations.Nullable; +import org.quiltmc.qsl.key.binds.impl.chords.ChordedKeyBind; +import org.quiltmc.qsl.key.binds.impl.chords.KeyChord; +import org.quiltmc.qsl.key.binds.impl.config.QuiltKeyBindsConfigManager; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.gui.screen.option.GameOptionsScreen; +import net.minecraft.client.gui.screen.option.KeyBindsScreen; +import net.minecraft.client.option.GameOptions; +import net.minecraft.client.option.KeyBind; +import net.minecraft.text.Text; +import net.minecraft.util.Util; + +// TODO - Support mouse buttons on chords +@Mixin(KeyBindsScreen.class) +public abstract class KeyBindsScreenMixin extends GameOptionsScreen { + @Shadow + @Nullable + public KeyBind focusedKey; + + @Shadow + public long time; + + @Unique + private List quilt$protoChord; + + public KeyBindsScreenMixin(Screen screen, GameOptions gameOptions, Text text) { + super(screen, gameOptions, text); + } + + @Inject(at = @At("TAIL"), method = "init") + private void initializeProtoChord(CallbackInfo ci) { + this.quilt$protoChord = new ArrayList<>(); + } + + @Inject( + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/client/option/GameOptions;setKeyCode(Lnet/minecraft/client/option/KeyBind;Lcom/mojang/blaze3d/platform/InputUtil$Key;)V", + ordinal = 1 + ), + method = "keyPressed", + cancellable = true + ) + private void modifyKeyPressed(int keyCode, int scanCode, int modifiers, CallbackInfoReturnable cir) { + InputUtil.Key key = InputUtil.fromKeyCode(keyCode, scanCode); + if (!quilt$protoChord.contains(key)) { + quilt$protoChord.add(key); + } + cir.setReturnValue(true); + } + + @Override + public boolean keyReleased(int keyCode, int scanCode, int modifiers) { + if (this.focusedKey != null) { + if (quilt$protoChord.size() == 1) { + this.gameOptions.setKeyCode(this.focusedKey, quilt$protoChord.get(0)); + } else if (quilt$protoChord.size() > 1) { + Map map = new HashMap<>(); + for (int i = 0; i < quilt$protoChord.size(); i++) { + map.put(quilt$protoChord.get(i), false); + } + ((ChordedKeyBind)this.focusedKey).setBoundChord(new KeyChord(map)); + QuiltKeyBindsConfigManager.populateConfig(); + QuiltKeyBindsConfigManager.saveModConfig(); + } + + quilt$protoChord.clear(); + this.focusedKey = null; + this.time = Util.getMeasuringTimeMs(); + KeyBind.updateBoundKeys(); + return true; + } else { + return super.keyReleased(keyCode, scanCode, modifiers); + } + } +} diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/GameOptionsMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/GameOptionsMixin.java new file mode 100644 index 0000000000..58ed54eca7 --- /dev/null +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/GameOptionsMixin.java @@ -0,0 +1,37 @@ +package org.quiltmc.qsl.key.binds.mixin.client.config; + +import com.mojang.blaze3d.platform.InputUtil; + +import org.quiltmc.qsl.key.binds.impl.config.QuiltKeyBindsConfigManager; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.Redirect; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.client.option.GameOptions; +import net.minecraft.client.option.KeyBind; + +@Mixin(GameOptions.class) +public abstract class GameOptionsMixin { + @Inject( + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/client/option/GameOptions;write()V" + ), + method = "setKeyCode" + ) + private void writeToKeyBindConfig(KeyBind key, InputUtil.Key code, CallbackInfo ci) { + QuiltKeyBindsConfigManager.populateConfig(); + QuiltKeyBindsConfigManager.saveModConfig(); + } + + @Redirect( + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/client/option/KeyBind;setBoundKey(Lcom/mojang/blaze3d/platform/InputUtil$Key;)V" + ), + method = "accept" + ) + private void useOurConfigInstead(KeyBind keyBind, InputUtil.Key key) {} +} diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/MinecraftClientMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/MinecraftClientMixin.java new file mode 100644 index 0000000000..a13dc77cb8 --- /dev/null +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/MinecraftClientMixin.java @@ -0,0 +1,24 @@ +package org.quiltmc.qsl.key.binds.mixin.client.config; + +import org.quiltmc.qsl.key.binds.impl.config.QuiltKeyBindsConfigManager; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.RunArgs; +import net.minecraft.client.option.KeyBind; + +@Mixin(MinecraftClient.class) +public abstract class MinecraftClientMixin { + // You can't INVOKE_ASSIGN at GameOptions for some reason + @Inject( + at = @At(value = "INVOKE", target = "Lnet/minecraft/client/tutorial/TutorialManager;(Lnet/minecraft/client/MinecraftClient;Lnet/minecraft/client/option/GameOptions;)V"), + method = "" + ) + private void handleQuiltKeyBindsConfig(RunArgs runArgs, CallbackInfo ci) { + QuiltKeyBindsConfigManager.loadConfig(); + KeyBind.updateBoundKeys(); + } +} diff --git a/library/gui/key_binds/src/main/resources/fabric.mod.json b/library/gui/key_binds/src/main/resources/fabric.mod.json index 935a8c1c07..cb434c2200 100644 --- a/library/gui/key_binds/src/main/resources/fabric.mod.json +++ b/library/gui/key_binds/src/main/resources/fabric.mod.json @@ -19,7 +19,6 @@ "minecraft": ">=1.18.2-alpha.22.3.a" }, "description": "Key binds registration and utilities.", - "accessWidener": "quilt_key_binds.accesswidener", "mixins": [ "quilt_key_binds.mixins.json" ] diff --git a/library/gui/key_binds/src/main/resources/quilt_key_binds.accesswidener b/library/gui/key_binds/src/main/resources/quilt_key_binds.accesswidener deleted file mode 100644 index ffdba3474c..0000000000 --- a/library/gui/key_binds/src/main/resources/quilt_key_binds.accesswidener +++ /dev/null @@ -1,3 +0,0 @@ -accessWidener v1 named - -accessible class net/minecraft/client/option/GameOptions$Visitor diff --git a/library/gui/key_binds/src/main/resources/quilt_key_binds.mixins.json b/library/gui/key_binds/src/main/resources/quilt_key_binds.mixins.json index 715978352c..d698baeda5 100644 --- a/library/gui/key_binds/src/main/resources/quilt_key_binds.mixins.json +++ b/library/gui/key_binds/src/main/resources/quilt_key_binds.mixins.json @@ -3,6 +3,11 @@ "package": "org.quiltmc.qsl.key.binds.mixin", "compatibilityLevel": "JAVA_17", "client": [ + "client.chords.KeyBindAccessor", + "client.chords.KeyBindMixin", + "client.chords.KeyBindsScreenMixin", + "client.config.GameOptionsMixin", + "client.config.MinecraftClientMixin", "client.EntryListWidgetAccessor", "client.GameOptionsAccessor", "client.GameOptionsMixin", diff --git a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java index bbd217b8e1..3ba8c4b37a 100644 --- a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java +++ b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java @@ -18,7 +18,6 @@ import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; -import net.fabricmc.loader.api.ModContainer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; From 2ad3f63896e86c2c8f920ab95c1acc24bd6a7160 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Sat, 12 Mar 2022 17:59:44 -0300 Subject: [PATCH 37/56] Apply license; Use sorted maps for chords --- .../key/binds/impl/chords/ChordedKeyBind.java | 16 +++++++++++++ .../qsl/key/binds/impl/chords/KeyChord.java | 23 ++++++++++++++++--- .../impl/config/QuiltKeyBindsConfig.java | 16 +++++++++++++ .../config/QuiltKeyBindsConfigManager.java | 20 +++++++++++++++- .../client/chords/InputUtilKeyMixin.java | 16 +++++++++++++ .../mixin/client/chords/KeyBindAccessor.java | 16 +++++++++++++ .../mixin/client/chords/KeyBindMixin.java | 16 +++++++++++++ .../client/chords/KeyBindsScreenMixin.java | 22 +++++++++++++++--- .../mixin/client/config/GameOptionsMixin.java | 16 +++++++++++++ .../client/config/MinecraftClientMixin.java | 16 +++++++++++++ .../resources/quilt_key_binds.mixins.json | 1 + 11 files changed, 171 insertions(+), 7 deletions(-) create mode 100644 library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/InputUtilKeyMixin.java diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/ChordedKeyBind.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/ChordedKeyBind.java index 77b492a65e..0e2c672da2 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/ChordedKeyBind.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/ChordedKeyBind.java @@ -1,3 +1,19 @@ +/* + * Copyright 2022 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.quiltmc.qsl.key.binds.impl.chords; public interface ChordedKeyBind { diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java index 62cd98fe66..d2fdb8fe58 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java @@ -1,15 +1,32 @@ +/* + * Copyright 2022 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.quiltmc.qsl.key.binds.impl.chords; -import java.util.HashMap; import java.util.Map; +import java.util.SortedMap; +import java.util.TreeMap; import com.mojang.blaze3d.platform.InputUtil; public class KeyChord { // TODO - Private this, add methods for getting/modifying it - public Map keys = new HashMap<>(); + public SortedMap keys = new TreeMap<>(); - public KeyChord(Map keys) { + public KeyChord(SortedMap keys) { this.keys = keys; } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java index dcb63b4d54..38151639c4 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java @@ -1,3 +1,19 @@ +/* + * Copyright 2022 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.quiltmc.qsl.key.binds.impl.config; import java.util.HashMap; diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java index c05334231d..a66d370e43 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java @@ -1,3 +1,19 @@ +/* + * Copyright 2022 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.quiltmc.qsl.key.binds.impl.config; import java.io.IOException; @@ -11,6 +27,8 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.SortedMap; +import java.util.TreeMap; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -75,7 +93,7 @@ public static void registerConfigChanges(QuiltKeyBindsConfig newConfig) { if (keyList.size() == 1) { key.setBoundKey(InputUtil.fromTranslationKey(keyList.get(0))); } else if (keyList.size() > 1) { - Map map = new HashMap<>(); + SortedMap map = new TreeMap<>(); for (String string : keyList) { map.put(InputUtil.fromTranslationKey(string), false); } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/InputUtilKeyMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/InputUtilKeyMixin.java new file mode 100644 index 0000000000..348be3acb8 --- /dev/null +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/InputUtilKeyMixin.java @@ -0,0 +1,16 @@ +package org.quiltmc.qsl.key.binds.mixin.client.chords; + +import com.mojang.blaze3d.platform.InputUtil; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +@Mixin(InputUtil.Key.class) +public abstract class InputUtilKeyMixin implements Comparable { + @Override + public int compareTo(InputUtil.Key key) { + return Integer.compare(this.getKeyCode(), key.getKeyCode()); + } + + @Shadow + public abstract int getKeyCode(); +} diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindAccessor.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindAccessor.java index e25f2e106f..018059a949 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindAccessor.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindAccessor.java @@ -1,3 +1,19 @@ +/* + * Copyright 2022 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.quiltmc.qsl.key.binds.mixin.client.chords; import org.spongepowered.asm.mixin.Mixin; diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java index 9035408547..337a11b337 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java @@ -1,3 +1,19 @@ +/* + * Copyright 2022 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.quiltmc.qsl.key.binds.mixin.client.chords; import java.util.HashMap; diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java index b6d3c8d020..aff7022d1e 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java @@ -1,9 +1,25 @@ +/* + * Copyright 2022 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.quiltmc.qsl.key.binds.mixin.client.chords; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; +import java.util.SortedMap; +import java.util.TreeMap; import com.mojang.blaze3d.platform.InputUtil; @@ -72,7 +88,7 @@ public boolean keyReleased(int keyCode, int scanCode, int modifiers) { if (quilt$protoChord.size() == 1) { this.gameOptions.setKeyCode(this.focusedKey, quilt$protoChord.get(0)); } else if (quilt$protoChord.size() > 1) { - Map map = new HashMap<>(); + SortedMap map = new TreeMap<>(); for (int i = 0; i < quilt$protoChord.size(); i++) { map.put(quilt$protoChord.get(i), false); } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/GameOptionsMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/GameOptionsMixin.java index 58ed54eca7..e831ddf572 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/GameOptionsMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/GameOptionsMixin.java @@ -1,3 +1,19 @@ +/* + * Copyright 2022 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.quiltmc.qsl.key.binds.mixin.client.config; import com.mojang.blaze3d.platform.InputUtil; diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/MinecraftClientMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/MinecraftClientMixin.java index a13dc77cb8..19e87f7318 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/MinecraftClientMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/MinecraftClientMixin.java @@ -1,3 +1,19 @@ +/* + * Copyright 2022 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.quiltmc.qsl.key.binds.mixin.client.config; import org.quiltmc.qsl.key.binds.impl.config.QuiltKeyBindsConfigManager; diff --git a/library/gui/key_binds/src/main/resources/quilt_key_binds.mixins.json b/library/gui/key_binds/src/main/resources/quilt_key_binds.mixins.json index d698baeda5..0a3d6bb91e 100644 --- a/library/gui/key_binds/src/main/resources/quilt_key_binds.mixins.json +++ b/library/gui/key_binds/src/main/resources/quilt_key_binds.mixins.json @@ -3,6 +3,7 @@ "package": "org.quiltmc.qsl.key.binds.mixin", "compatibilityLevel": "JAVA_17", "client": [ + "client.chords.InputUtilKeyMixin", "client.chords.KeyBindAccessor", "client.chords.KeyBindMixin", "client.chords.KeyBindsScreenMixin", From 029225d6686efee08161c49849e50e6e4156b661 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Sat, 12 Mar 2022 22:54:15 -0300 Subject: [PATCH 38/56] Add chord support for conflict warnings (I) --- .../binds/mixin/client/KeyBindEntryMixin.java | 32 +++++++---- .../mixin/client/chords/KeyBindMixin.java | 56 ++++++++++++------- 2 files changed, 59 insertions(+), 29 deletions(-) diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java index 41eb0f9994..65be96dd1c 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java @@ -18,6 +18,8 @@ import java.util.ArrayList; import java.util.List; +import java.util.SortedMap; +import java.util.TreeMap; import com.mojang.blaze3d.platform.InputUtil; import org.spongepowered.asm.mixin.Final; @@ -44,6 +46,8 @@ import org.quiltmc.qsl.key.binds.api.KeyBindRegistry; import org.quiltmc.qsl.key.binds.impl.ConflictTooltipOwner; import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; +import org.quiltmc.qsl.key.binds.impl.chords.ChordedKeyBind; +import org.quiltmc.qsl.key.binds.impl.chords.KeyChord; @Environment(EnvType.CLIENT) @Mixin(KeyBindEntry.class) @@ -60,10 +64,10 @@ public abstract class KeyBindEntryMixin extends KeyBindListWidget.Entry implemen private List quilt$conflictTooltips = new ArrayList<>(2); @Unique - private static InputUtil.Key quilt$previousBoundKey; + private static List quilt$previousProtoChord; @Unique - private static InputUtil.Key quilt$changedBoundKey; + private static List quilt$changedProtoChord; @Shadow(aliases = "field_2742", remap = false) @Final @@ -71,10 +75,11 @@ public abstract class KeyBindEntryMixin extends KeyBindListWidget.Entry implemen @Inject(method = "", at = @At("TAIL")) private void initPreviousBoundKey(KeyBindListWidget list, KeyBind key, Text text, CallbackInfo ci) { - quilt$previousBoundKey = null; - quilt$changedBoundKey = null; + quilt$previousProtoChord = null; + quilt$changedProtoChord = null; } + // TODO - Oh god, what is going on here? Investigate @Inject( method = "render", at = @At( @@ -85,14 +90,21 @@ private void initPreviousBoundKey(KeyBindListWidget list, KeyBind key, Text text ) private void collectConflictTooltips(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta, CallbackInfo ci, boolean bl, boolean bl2) { InputUtil.Key boundKey = KeyBindRegistry.getBoundKey(this.key); + KeyChord boundChord = ((ChordedKeyBind)(this.key)).getBoundChord(); + List boundProtoChord = new ArrayList<>(); - if (!boundKey.equals(quilt$previousBoundKey) || quilt$changedBoundKey != null) { - this.quilt$conflictTooltips.clear(); + if (boundChord == null) { + boundProtoChord.add(boundKey); + } else { + boundProtoChord.addAll(boundChord.keys.keySet()); + } - if (quilt$changedBoundKey != null && quilt$changedBoundKey.equals(boundKey)) { - quilt$changedBoundKey = null; + if (!boundProtoChord.equals(quilt$previousProtoChord) || quilt$changedProtoChord != null) { + this.quilt$conflictTooltips.clear(); + if (quilt$changedProtoChord != null && quilt$changedProtoChord.equals(boundProtoChord)) { + quilt$changedProtoChord = null; } else { - quilt$changedBoundKey = boundKey; + quilt$changedProtoChord = boundProtoChord; } if (!this.key.isUnbound()) { @@ -108,7 +120,7 @@ private void collectConflictTooltips(MatrixStack matrices, int index, int y, int } } - quilt$previousBoundKey = boundKey; + quilt$previousProtoChord = boundProtoChord; } @ModifyArg( diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java index 337a11b337..f9273d8e67 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java @@ -96,20 +96,6 @@ private static void detectChordsOnSet(InputUtil.Key startingKey, boolean pressed } } - @Inject(at = @At("HEAD"), method = "getKeyName", cancellable = true) - private void useChordName(CallbackInfoReturnable cir) { - if (this.quilt$boundChord != null) { - MutableText text = LiteralText.EMPTY.shallowCopy(); - for (InputUtil.Key key : this.quilt$boundChord.keys.keySet()) { - if (text.getSiblings().size() != 0) { - text.append(" + "); - } - text.append(key.getDisplayText()); - } - cir.setReturnValue(text); - } - } - @Inject( at = @At( value = "INVOKE", @@ -155,6 +141,43 @@ private static void updateChordBoundKeys(CallbackInfo cir) { // TODO - Detech chords for matchesMouseButton as well + @Inject(at = @At("HEAD"), method = "setBoundKey", cancellable = true) + private void resetChord(CallbackInfo ci) { + this.quilt$boundChord = null; + } + + @Inject(at = @At("HEAD"), method = "keyEquals", cancellable = true) + private void keyOrChordEquals(KeyBind other, CallbackInfoReturnable cir) { + if (this.quilt$boundChord != null) { + if (((ChordedKeyBind)other).getBoundChord() != null) { + cir.setReturnValue(this.quilt$boundChord.keys.keySet().containsAll(((ChordedKeyBind)other).getBoundChord().keys.keySet())); + } else { + cir.setReturnValue(false); + } + } + } + + @Inject(at = @At("RETURN"), method = "isUnbound", cancellable = true) + private void isChordUnbound(CallbackInfoReturnable cir) { + if (cir.getReturnValueZ() && this.quilt$boundChord != null) { + cir.setReturnValue(false); + } + } + + @Inject(at = @At("HEAD"), method = "getKeyName", cancellable = true) + private void useChordName(CallbackInfoReturnable cir) { + if (this.quilt$boundChord != null) { + MutableText text = LiteralText.EMPTY.shallowCopy(); + for (InputUtil.Key key : this.quilt$boundChord.keys.keySet()) { + if (text.getSiblings().size() != 0) { + text.append(" + "); + } + text.append(key.getDisplayText()); + } + cir.setReturnValue(text); + } + } + @Inject(at = @At("HEAD"), method = "isDefault", cancellable = true) private void detectDefaultChord(CallbackInfoReturnable cir) { if (this.quilt$boundChord != null) { @@ -162,11 +185,6 @@ private void detectDefaultChord(CallbackInfoReturnable cir) { } } - @Inject(at = @At("HEAD"), method = "setBoundKey", cancellable = true) - private void resetChord(CallbackInfo ci) { - this.quilt$boundChord = null; - } - @Override public KeyChord getBoundChord() { return this.quilt$boundChord; From 52338541e702b12dffaa847e991a4d50e1e222a8 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Sat, 12 Mar 2022 23:49:36 -0300 Subject: [PATCH 39/56] =?UTF-8?q?Uh,=20licen=C3=A7a?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mixin/client/chords/InputUtilKeyMixin.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/InputUtilKeyMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/InputUtilKeyMixin.java index 348be3acb8..7afdc05e50 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/InputUtilKeyMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/InputUtilKeyMixin.java @@ -1,3 +1,19 @@ +/* + * Copyright 2022 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.quiltmc.qsl.key.binds.mixin.client.chords; import com.mojang.blaze3d.platform.InputUtil; From 4b366c2b52f317aa3f12749938c31017554e1b1f Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Mon, 14 Mar 2022 18:13:24 -0300 Subject: [PATCH 40/56] Fix conflict tooltips --- .../qsl/key/binds/impl/chords/KeyChord.java | 1 - .../binds/mixin/client/KeyBindEntryMixin.java | 20 +++++++++---------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java index d2fdb8fe58..70ff054b9a 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java @@ -16,7 +16,6 @@ package org.quiltmc.qsl.key.binds.impl.chords; -import java.util.Map; import java.util.SortedMap; import java.util.TreeMap; diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java index 65be96dd1c..91360a90b1 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java @@ -16,10 +16,7 @@ package org.quiltmc.qsl.key.binds.mixin.client; -import java.util.ArrayList; import java.util.List; -import java.util.SortedMap; -import java.util.TreeMap; import com.mojang.blaze3d.platform.InputUtil; import org.spongepowered.asm.mixin.Final; @@ -31,6 +28,8 @@ import org.spongepowered.asm.mixin.injection.ModifyArg; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.LocalCapture; + +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; @@ -61,10 +60,10 @@ public abstract class KeyBindEntryMixin extends KeyBindListWidget.Entry implemen private ButtonWidget bindButton; @Unique - private List quilt$conflictTooltips = new ArrayList<>(2); + private List quilt$conflictTooltips = new ObjectArrayList<>(); @Unique - private static List quilt$previousProtoChord; + private List quilt$previousProtoChord; @Unique private static List quilt$changedProtoChord; @@ -79,7 +78,6 @@ private void initPreviousBoundKey(KeyBindListWidget list, KeyBind key, Text text quilt$changedProtoChord = null; } - // TODO - Oh god, what is going on here? Investigate @Inject( method = "render", at = @At( @@ -91,15 +89,15 @@ private void initPreviousBoundKey(KeyBindListWidget list, KeyBind key, Text text private void collectConflictTooltips(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta, CallbackInfo ci, boolean bl, boolean bl2) { InputUtil.Key boundKey = KeyBindRegistry.getBoundKey(this.key); KeyChord boundChord = ((ChordedKeyBind)(this.key)).getBoundChord(); - List boundProtoChord = new ArrayList<>(); + List boundProtoChord; if (boundChord == null) { - boundProtoChord.add(boundKey); + boundProtoChord = List.of(boundKey); } else { - boundProtoChord.addAll(boundChord.keys.keySet()); + boundProtoChord = List.copyOf(boundChord.keys.keySet()); } - if (!boundProtoChord.equals(quilt$previousProtoChord) || quilt$changedProtoChord != null) { + if (!boundProtoChord.equals(this.quilt$previousProtoChord) || quilt$changedProtoChord != null) { this.quilt$conflictTooltips.clear(); if (quilt$changedProtoChord != null && quilt$changedProtoChord.equals(boundProtoChord)) { quilt$changedProtoChord = null; @@ -120,7 +118,7 @@ private void collectConflictTooltips(MatrixStack matrices, int index, int y, int } } - quilt$previousProtoChord = boundProtoChord; + this.quilt$previousProtoChord = boundProtoChord; } @ModifyArg( From 70f4f34e5963a95cc5b393108b1acb6bc15fcea4 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Mon, 14 Mar 2022 22:19:24 -0300 Subject: [PATCH 41/56] Begin to use FastUtils, add overflow hiding, more! --- ...ipOwner.java => KeyBindTooltipHolder.java} | 4 +- .../qsl/key/binds/impl/chords/KeyChord.java | 7 +- .../binds/mixin/client/KeyBindEntryMixin.java | 69 +++++++++++++++- .../mixin/client/KeyBindsScreenMixin.java | 5 +- .../mixin/client/chords/KeyBindMixin.java | 10 ++- .../client/chords/KeyBindsScreenMixin.java | 79 +++++++++++++++---- 6 files changed, 146 insertions(+), 28 deletions(-) rename library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/{ConflictTooltipOwner.java => KeyBindTooltipHolder.java} (91%) diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/ConflictTooltipOwner.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindTooltipHolder.java similarity index 91% rename from library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/ConflictTooltipOwner.java rename to library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindTooltipHolder.java index 9cd7cf5841..6475d89e90 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/ConflictTooltipOwner.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindTooltipHolder.java @@ -26,6 +26,6 @@ @Environment(EnvType.CLIENT) @ApiStatus.Internal -public interface ConflictTooltipOwner { - List getConflictTooltips(); +public interface KeyBindTooltipHolder { + List getKeyBindTooltips(); } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java index 70ff054b9a..e607cf91bd 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java @@ -17,17 +17,20 @@ package org.quiltmc.qsl.key.binds.impl.chords; import java.util.SortedMap; -import java.util.TreeMap; import com.mojang.blaze3d.platform.InputUtil; +import it.unimi.dsi.fastutil.objects.Object2BooleanAVLTreeMap; + public class KeyChord { // TODO - Private this, add methods for getting/modifying it - public SortedMap keys = new TreeMap<>(); + public SortedMap keys = new Object2BooleanAVLTreeMap<>(); public KeyChord(SortedMap keys) { this.keys = keys; } public KeyChord() {} + + // TODO - Override hashCode() so it only counts keys } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java index 91360a90b1..8199a460a0 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java @@ -32,25 +32,27 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; - +import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.widget.ButtonWidget; import net.minecraft.client.gui.widget.option.KeyBindListWidget; import net.minecraft.client.gui.widget.option.KeyBindListWidget.KeyBindEntry; import net.minecraft.client.option.KeyBind; import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.text.LiteralText; import net.minecraft.text.Text; import net.minecraft.text.TranslatableText; import net.minecraft.util.Formatting; +import org.objectweb.asm.Opcodes; import org.quiltmc.qsl.key.binds.api.KeyBindRegistry; -import org.quiltmc.qsl.key.binds.impl.ConflictTooltipOwner; +import org.quiltmc.qsl.key.binds.impl.KeyBindTooltipHolder; import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; import org.quiltmc.qsl.key.binds.impl.chords.ChordedKeyBind; import org.quiltmc.qsl.key.binds.impl.chords.KeyChord; @Environment(EnvType.CLIENT) @Mixin(KeyBindEntry.class) -public abstract class KeyBindEntryMixin extends KeyBindListWidget.Entry implements ConflictTooltipOwner { +public abstract class KeyBindEntryMixin extends KeyBindListWidget.Entry implements KeyBindTooltipHolder { @Shadow @Final private KeyBind key; @@ -68,6 +70,9 @@ public abstract class KeyBindEntryMixin extends KeyBindListWidget.Entry implemen @Unique private static List quilt$changedProtoChord; + @Unique + private boolean quilt$addKeyNameToTooltip; + @Shadow(aliases = "field_2742", remap = false) @Final KeyBindListWidget field_2742; @@ -76,6 +81,7 @@ public abstract class KeyBindEntryMixin extends KeyBindListWidget.Entry implemen private void initPreviousBoundKey(KeyBindListWidget list, KeyBind key, Text text, CallbackInfo ci) { quilt$previousProtoChord = null; quilt$changedProtoChord = null; + quilt$addKeyNameToTooltip = false; } @Inject( @@ -105,6 +111,8 @@ private void collectConflictTooltips(MatrixStack matrices, int index, int y, int quilt$changedProtoChord = boundProtoChord; } + quilt$addKeyNameToTooltip = true; + if (!this.key.isUnbound()) { for (KeyBind otherKey : KeyBindRegistryImpl.getKeyBinds()) { if (otherKey != this.key && this.key.keyEquals(otherKey)) { @@ -121,6 +129,59 @@ private void collectConflictTooltips(MatrixStack matrices, int index, int y, int this.quilt$previousProtoChord = boundProtoChord; } + @Inject( + method = "render", + at = @At( + value = "JUMP", + opcode = Opcodes.IFEQ, + ordinal = 1, + shift = At.Shift.BEFORE + ), + locals = LocalCapture.CAPTURE_FAILHARD + ) + private void shortenText(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta, CallbackInfo ci, boolean bl, boolean bl2) { + // TODO - Get client from the parent screen instead + MinecraftClient client = MinecraftClient.getInstance(); + Text text = this.bindButton.getMessage(); + int targetWidth = bl || bl2 ? 50 - 10 : 75 - 10; + if (client.textRenderer.getWidth(text) > targetWidth) { + String protoText = text.getString(); + if (((ChordedKeyBind)this.key).getBoundChord() != null) { + protoText = ""; + KeyChord chord = ((ChordedKeyBind)this.key).getBoundChord(); + + for (InputUtil.Key key : chord.keys.keySet()) { + if (!protoText.isEmpty()) { + protoText += " + "; + } + + String keyString = key.getDisplayText().getString(); + + if (keyString.length() > 3) { + String[] keySegments = keyString.split(" "); + keyString = ""; + for (String keySegment : keySegments) { + keyString += keySegment.substring(0, 1); + } + } + + protoText += keyString; + } + } + + if (client.textRenderer.getWidth(protoText) > targetWidth) { + if (quilt$addKeyNameToTooltip) { + this.quilt$conflictTooltips.add(0, this.key.getKeyName()); + quilt$addKeyNameToTooltip = false; + } + protoText = client.textRenderer.trimToWidth(protoText, targetWidth); + protoText += "..."; + } + + this.bindButton.setMessage(new LiteralText(protoText)); + } + } + @ModifyArg( method = "render", at = @At( @@ -134,7 +195,7 @@ private Text addConflictIndicator(Text originalText) { } @Override - public List getConflictTooltips() { + public List getKeyBindTooltips() { return this.quilt$conflictTooltips; } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindsScreenMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindsScreenMixin.java index 7c038cd696..8ff6fac1d3 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindsScreenMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindsScreenMixin.java @@ -35,7 +35,7 @@ import net.minecraft.client.util.math.MatrixStack; import net.minecraft.text.Text; -import org.quiltmc.qsl.key.binds.impl.ConflictTooltipOwner; +import org.quiltmc.qsl.key.binds.impl.KeyBindTooltipHolder; @Environment(EnvType.CLIENT) @Mixin(KeyBindsScreen.class) @@ -53,8 +53,9 @@ private void renderConflictTooltips(MatrixStack matrices, int mouseX, int mouseY // TODO - Somehow extend the hover area to include the label too KeyBindListWidget.Entry entry = ((EntryListWidgetAccessor) this.keyBindList).invokeGetHoveredEntry(); if (entry != null && entry instanceof KeyBindEntry keyBindEntry) { - List tooltipLines = ((ConflictTooltipOwner) keyBindEntry).getConflictTooltips(); + List tooltipLines = ((KeyBindTooltipHolder) keyBindEntry).getKeyBindTooltips(); if (tooltipLines != null) { + // TODO - With key names, it's getting too big! Add a maximum width this.renderTooltip(matrices, tooltipLines, mouseX, mouseY); } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java index f9273d8e67..d1dc7e2de7 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java @@ -16,7 +16,6 @@ package org.quiltmc.qsl.key.binds.mixin.client.chords; -import java.util.HashMap; import java.util.Iterator; import java.util.Map; @@ -34,6 +33,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import org.spongepowered.asm.mixin.injection.callback.LocalCapture; +import it.unimi.dsi.fastutil.objects.Reference2ReferenceOpenHashMap; import net.minecraft.client.MinecraftClient; import net.minecraft.client.option.KeyBind; import net.minecraft.text.LiteralText; @@ -48,9 +48,9 @@ public class KeyBindMixin implements ChordedKeyBind { @Shadow private InputUtil.Key boundKey; - + @Unique - private static final Map KEY_BINDS_BY_CHORD = new HashMap<>(); + private static final Map KEY_BINDS_BY_CHORD = new Reference2ReferenceOpenHashMap<>(); @Unique @Final @@ -138,6 +138,8 @@ private static void updateChordBoundKeys(CallbackInfo cir) { } // TODO - Detect chords for matchesKey too; They are such a weird case + @Inject(at = @At("HEAD"), method = "matchesKey", cancellable = true) + private void matchesChordKey(CallbackInfoReturnable ci) {} // TODO - Detech chords for matchesMouseButton as well @@ -150,7 +152,7 @@ private void resetChord(CallbackInfo ci) { private void keyOrChordEquals(KeyBind other, CallbackInfoReturnable cir) { if (this.quilt$boundChord != null) { if (((ChordedKeyBind)other).getBoundChord() != null) { - cir.setReturnValue(this.quilt$boundChord.keys.keySet().containsAll(((ChordedKeyBind)other).getBoundChord().keys.keySet())); + cir.setReturnValue(this.quilt$boundChord.keys.keySet().equals(((ChordedKeyBind)other).getBoundChord().keys.keySet())); } else { cir.setReturnValue(false); } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java index aff7022d1e..9d8682381d 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java @@ -16,10 +16,8 @@ package org.quiltmc.qsl.key.binds.mixin.client.chords; -import java.util.ArrayList; import java.util.List; import java.util.SortedMap; -import java.util.TreeMap; import com.mojang.blaze3d.platform.InputUtil; @@ -35,6 +33,8 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import it.unimi.dsi.fastutil.objects.Object2BooleanAVLTreeMap; +import it.unimi.dsi.fastutil.objects.ObjectArrayList; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.option.GameOptionsScreen; import net.minecraft.client.gui.screen.option.KeyBindsScreen; @@ -43,7 +43,6 @@ import net.minecraft.text.Text; import net.minecraft.util.Util; -// TODO - Support mouse buttons on chords @Mixin(KeyBindsScreen.class) public abstract class KeyBindsScreenMixin extends GameOptionsScreen { @Shadow @@ -54,7 +53,10 @@ public abstract class KeyBindsScreenMixin extends GameOptionsScreen { public long time; @Unique - private List quilt$protoChord; + private List quilt$focusedProtoChord; + + @Unique + private boolean quilt$initialMouseRelease; public KeyBindsScreenMixin(Screen screen, GameOptions gameOptions, Text text) { super(screen, gameOptions, text); @@ -62,7 +64,29 @@ public KeyBindsScreenMixin(Screen screen, GameOptions gameOptions, Text text) { @Inject(at = @At("TAIL"), method = "init") private void initializeProtoChord(CallbackInfo ci) { - this.quilt$protoChord = new ArrayList<>(); + this.quilt$focusedProtoChord = new ObjectArrayList<>(); + this.quilt$initialMouseRelease = true; + } + + @Inject( + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/client/option/GameOptions;setKeyCode(Lnet/minecraft/client/option/KeyBind;Lcom/mojang/blaze3d/platform/InputUtil$Key;)V" + ), + method = "mouseClicked", + cancellable = true + ) + private void modifyMouseClicked(double mouseX, double mouseY, int button, CallbackInfoReturnable cir) { + InputUtil.Key key = InputUtil.Type.MOUSE.createFromKeyCode(button); + if (!quilt$focusedProtoChord.contains(key)) { + quilt$focusedProtoChord.add(key); + } + cir.setReturnValue(true); + } + + @Inject(at = @At(value = "RETURN", ordinal = 1), method = "mouseClicked") + private void excludeFirstMouseClick(double mouseX, double mouseY, int button, CallbackInfoReturnable cir) { + this.quilt$initialMouseRelease = true; } @Inject( @@ -76,8 +100,8 @@ private void initializeProtoChord(CallbackInfo ci) { ) private void modifyKeyPressed(int keyCode, int scanCode, int modifiers, CallbackInfoReturnable cir) { InputUtil.Key key = InputUtil.fromKeyCode(keyCode, scanCode); - if (!quilt$protoChord.contains(key)) { - quilt$protoChord.add(key); + if (!quilt$focusedProtoChord.contains(key)) { + quilt$focusedProtoChord.add(key); } cir.setReturnValue(true); } @@ -85,19 +109,19 @@ private void modifyKeyPressed(int keyCode, int scanCode, int modifiers, Callback @Override public boolean keyReleased(int keyCode, int scanCode, int modifiers) { if (this.focusedKey != null) { - if (quilt$protoChord.size() == 1) { - this.gameOptions.setKeyCode(this.focusedKey, quilt$protoChord.get(0)); - } else if (quilt$protoChord.size() > 1) { - SortedMap map = new TreeMap<>(); - for (int i = 0; i < quilt$protoChord.size(); i++) { - map.put(quilt$protoChord.get(i), false); + if (quilt$focusedProtoChord.size() == 1) { + this.gameOptions.setKeyCode(this.focusedKey, quilt$focusedProtoChord.get(0)); + } else if (quilt$focusedProtoChord.size() > 1) { + SortedMap map = new Object2BooleanAVLTreeMap<>(); + for (int i = 0; i < quilt$focusedProtoChord.size(); i++) { + map.put(quilt$focusedProtoChord.get(i), false); } ((ChordedKeyBind)this.focusedKey).setBoundChord(new KeyChord(map)); QuiltKeyBindsConfigManager.populateConfig(); QuiltKeyBindsConfigManager.saveModConfig(); } - quilt$protoChord.clear(); + quilt$focusedProtoChord.clear(); this.focusedKey = null; this.time = Util.getMeasuringTimeMs(); KeyBind.updateBoundKeys(); @@ -106,4 +130,31 @@ public boolean keyReleased(int keyCode, int scanCode, int modifiers) { return super.keyReleased(keyCode, scanCode, modifiers); } } + + @Override + public boolean mouseReleased(double mouseX, double mouseY, int button) { + // TODO - Don't duplicate code, have a common method + if (this.focusedKey != null && !this.quilt$initialMouseRelease) { + if (quilt$focusedProtoChord.size() == 1) { + this.gameOptions.setKeyCode(this.focusedKey, quilt$focusedProtoChord.get(0)); + } else if (quilt$focusedProtoChord.size() > 1) { + SortedMap map = new Object2BooleanAVLTreeMap<>(); + for (int i = 0; i < quilt$focusedProtoChord.size(); i++) { + map.put(quilt$focusedProtoChord.get(i), false); + } + ((ChordedKeyBind)this.focusedKey).setBoundChord(new KeyChord(map)); + QuiltKeyBindsConfigManager.populateConfig(); + QuiltKeyBindsConfigManager.saveModConfig(); + } + + quilt$focusedProtoChord.clear(); + this.focusedKey = null; + this.time = Util.getMeasuringTimeMs(); + KeyBind.updateBoundKeys(); + return true; + } else { + this.quilt$initialMouseRelease = false; + return super.mouseReleased(mouseX, mouseY, button); + } + } } From a2f180fe855d908183e2a142743d4c942f821d7a Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Mon, 14 Mar 2022 22:41:50 -0300 Subject: [PATCH 42/56] Pacify Checkstyle wait, so, tabs are actually okay? --- .../key/binds/impl/chords/ChordedKeyBind.java | 4 +- .../qsl/key/binds/impl/chords/KeyChord.java | 17 +- .../impl/config/QuiltKeyBindsConfig.java | 68 ++-- .../config/QuiltKeyBindsConfigManager.java | 201 +++++------ .../binds/mixin/client/KeyBindEntryMixin.java | 11 +- .../mixin/client/KeyBindsScreenMixin.java | 3 +- .../client/chords/InputUtilKeyMixin.java | 12 +- .../mixin/client/chords/KeyBindAccessor.java | 10 +- .../mixin/client/chords/KeyBindMixin.java | 321 +++++++++--------- .../client/chords/KeyBindsScreenMixin.java | 226 ++++++------ .../mixin/client/config/GameOptionsMixin.java | 42 +-- .../client/config/MinecraftClientMixin.java | 21 +- 12 files changed, 475 insertions(+), 461 deletions(-) diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/ChordedKeyBind.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/ChordedKeyBind.java index 0e2c672da2..13720659ee 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/ChordedKeyBind.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/ChordedKeyBind.java @@ -17,6 +17,6 @@ package org.quiltmc.qsl.key.binds.impl.chords; public interface ChordedKeyBind { - KeyChord getBoundChord(); - void setBoundChord(KeyChord chord); + KeyChord getBoundChord(); + void setBoundChord(KeyChord chord); } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java index e607cf91bd..f105115a1f 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java @@ -19,18 +19,17 @@ import java.util.SortedMap; import com.mojang.blaze3d.platform.InputUtil; - import it.unimi.dsi.fastutil.objects.Object2BooleanAVLTreeMap; public class KeyChord { - // TODO - Private this, add methods for getting/modifying it - public SortedMap keys = new Object2BooleanAVLTreeMap<>(); + // TODO - Private this, add methods for getting/modifying it + public SortedMap keys = new Object2BooleanAVLTreeMap<>(); + + public KeyChord(SortedMap keys) { + this.keys = keys; + } - public KeyChord(SortedMap keys) { - this.keys = keys; - } - - public KeyChord() {} + public KeyChord() { } - // TODO - Override hashCode() so it only counts keys + // TODO - Override hashCode() so it only counts keys } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java index 38151639c4..8933094f4d 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java @@ -24,45 +24,45 @@ import com.mojang.serialization.codecs.RecordCodecBuilder; public class QuiltKeyBindsConfig { - public static final Codec CODEC = RecordCodecBuilder.create( - instance -> instance.group( - // TODO - Implement me! - Codec.BOOL.fieldOf("show_tutorial_toast").forGetter(QuiltKeyBindsConfig::getShowTutorialToast), - // TODO - Ideally, it would be a list for chords, a single string for single keys, and an empty list for unbound - Codec.unboundedMap(Codec.STRING, Codec.list(Codec.STRING)).fieldOf("key_binds").forGetter(QuiltKeyBindsConfig::getKeyBinds) - ) - .apply(instance, QuiltKeyBindsConfig::new) - ); + public static final Codec CODEC = RecordCodecBuilder.create( + instance -> instance.group( + // TODO - Implement me! + Codec.BOOL.fieldOf("show_tutorial_toast").forGetter(QuiltKeyBindsConfig::getShowTutorialToast), + // TODO - Ideally, it would be a list for chords, a single string for single keys, and an empty list for unbound + Codec.unboundedMap(Codec.STRING, Codec.list(Codec.STRING)).fieldOf("key_binds").forGetter(QuiltKeyBindsConfig::getKeyBinds) + ) + .apply(instance, QuiltKeyBindsConfig::new) + ); - private boolean showTutorialToast; - private Map> keyBinds; + private boolean showTutorialToast; + private Map> keyBinds; - public QuiltKeyBindsConfig() { - this.keyBinds = new HashMap<>(); - this.showTutorialToast = false; - } + public QuiltKeyBindsConfig() { + this.keyBinds = new HashMap<>(); + this.showTutorialToast = false; + } - public QuiltKeyBindsConfig( - boolean showTutorialToast, - Map> keyBinds - ) { - this.showTutorialToast = showTutorialToast; - this.keyBinds = keyBinds; - } + public QuiltKeyBindsConfig( + boolean showTutorialToast, + Map> keyBinds + ) { + this.showTutorialToast = showTutorialToast; + this.keyBinds = keyBinds; + } - public boolean getShowTutorialToast() { - return this.showTutorialToast; - } + public boolean getShowTutorialToast() { + return this.showTutorialToast; + } - public void setShowTutorialToast(boolean showTutorialToast) { - this.showTutorialToast = showTutorialToast; - } + public void setShowTutorialToast(boolean showTutorialToast) { + this.showTutorialToast = showTutorialToast; + } - public Map> getKeyBinds() { - return keyBinds; - } + public Map> getKeyBinds() { + return keyBinds; + } - public void setKeyBinds(Map> keyBinds) { - this.keyBinds = keyBinds; - } + public void setKeyBinds(Map> keyBinds) { + this.keyBinds = keyBinds; + } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java index a66d370e43..50f48a273f 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java @@ -37,109 +37,114 @@ import com.google.gson.stream.JsonWriter; import com.mojang.blaze3d.platform.InputUtil; import com.mojang.serialization.JsonOps; +import net.fabricmc.loader.api.FabricLoader; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.option.KeyBind; import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; import org.quiltmc.qsl.key.binds.impl.chords.ChordedKeyBind; import org.quiltmc.qsl.key.binds.impl.chords.KeyChord; -import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.option.KeyBind; - // TODO - Understand this mess that you have written down public class QuiltKeyBindsConfigManager { - public static Optional isConfigLoaded = Optional.empty(); - public static final QuiltKeyBindsConfig CONFIG = new QuiltKeyBindsConfig(); - public static final Path QSL_CONFIG_PATH = FabricLoader.getInstance().getConfigDir().resolve("qsl"); - public static final Path KEY_BINDS_CONFIG_PATH = QSL_CONFIG_PATH.resolve("key_binds.json"); - public static final Gson GSON = new GsonBuilder().setPrettyPrinting().serializeNulls().disableHtmlEscaping().create(); - - public static void loadConfig() { - if (Files.exists(KEY_BINDS_CONFIG_PATH)) { - try { - Reader reader = Files.newBufferedReader(KEY_BINDS_CONFIG_PATH, StandardCharsets.UTF_8); - var result = QuiltKeyBindsConfig.CODEC.parse(JsonOps.INSTANCE, JsonParser.parseReader(reader)).result(); - reader.close(); - - if (result.isPresent()) { - registerConfigChanges(result.get()); - populateConfig(); - saveModConfig(); - isConfigLoaded = Optional.of(true); - } else { - isConfigLoaded = Optional.of(false); - } - } catch (IOException | JsonParseException e) { - System.err.println(e); - } - } else { - if (!Files.isDirectory(QSL_CONFIG_PATH)) { - try { - Files.createDirectory(QSL_CONFIG_PATH); - } catch (IOException e) { - e.printStackTrace(); - } - } - populateConfig(); - saveModConfig(); - } - } - - public static void registerConfigChanges(QuiltKeyBindsConfig newConfig) { - MinecraftClient client = MinecraftClient.getInstance(); - for (KeyBind key : client.options.allKeys) { - if (newConfig.getKeyBinds().containsKey(key.getTranslationKey())) { - List keyList = newConfig.getKeyBinds().get(key.getTranslationKey()); - if (keyList.size() == 1) { - key.setBoundKey(InputUtil.fromTranslationKey(keyList.get(0))); - } else if (keyList.size() > 1) { - SortedMap map = new TreeMap<>(); - for (String string : keyList) { - map.put(InputUtil.fromTranslationKey(string), false); - } - ((ChordedKeyBind)key).setBoundChord(new KeyChord(map)); - } - } - }; - CONFIG.setKeyBinds(newConfig.getKeyBinds()); - } - - public static void saveModConfig() { - var result = QuiltKeyBindsConfig.CODEC.encodeStart(JsonOps.INSTANCE, CONFIG).result(); - if (result.isPresent()) { - try { - Writer writer = Files.newBufferedWriter(KEY_BINDS_CONFIG_PATH, StandardCharsets.UTF_8); - JsonWriter jsonWriter = GSON.newJsonWriter(writer); - GSON.toJson(result.get(), jsonWriter); - jsonWriter.close(); - } catch (IOException e) { - System.err.println(e); - } - } - } - - public static void populateConfig() { - Map> keyBindMap = new HashMap<>(); - - KeyBindRegistryImpl.getAllKeyBinds(true).forEach((keyBind, disabled) -> { - if (((ChordedKeyBind)keyBind).getBoundChord() != null) { - List list = new ArrayList<>(); - for (InputUtil.Key key : ((ChordedKeyBind)keyBind).getBoundChord().keys.keySet()) { - list.add(key.getTranslationKey()); - } - keyBindMap.put(keyBind.getTranslationKey(), list); - } else { - keyBindMap.put(keyBind.getTranslationKey(), List.of(keyBind.getKeyTranslationKey())); - } - /* - if (!CONFIG.getKeyBinds().containsKey(key.getTranslationKey())) { - keyBindMap.put(key.getTranslationKey(), List.of(key.getKeyTranslationKey())); - } else { - keyBindMap.put(key.getTranslationKey(), List.of(key.getKeyTranslationKey())); - //key.setBoundKey(InputUtil.fromTranslationKey(CONFIG.getKeyBinds().get(key.getTranslationKey()))); - } - */ - }); - CONFIG.setKeyBinds(keyBindMap); - } + public static Optional isConfigLoaded = Optional.empty(); + public static final QuiltKeyBindsConfig CONFIG = new QuiltKeyBindsConfig(); + public static final Path QSL_CONFIG_PATH = FabricLoader.getInstance().getConfigDir().resolve("qsl"); + public static final Path KEY_BINDS_CONFIG_PATH = QSL_CONFIG_PATH.resolve("key_binds.json"); + public static final Gson GSON = new GsonBuilder().setPrettyPrinting().serializeNulls().disableHtmlEscaping().create(); + + public static void loadConfig() { + if (Files.exists(KEY_BINDS_CONFIG_PATH)) { + try { + Reader reader = Files.newBufferedReader(KEY_BINDS_CONFIG_PATH, StandardCharsets.UTF_8); + var result = QuiltKeyBindsConfig.CODEC.parse(JsonOps.INSTANCE, JsonParser.parseReader(reader)).result(); + reader.close(); + + if (result.isPresent()) { + registerConfigChanges(result.get()); + populateConfig(); + saveModConfig(); + isConfigLoaded = Optional.of(true); + } else { + isConfigLoaded = Optional.of(false); + } + } catch (IOException | JsonParseException e) { + System.err.println(e); + } + } else { + if (!Files.isDirectory(QSL_CONFIG_PATH)) { + try { + Files.createDirectory(QSL_CONFIG_PATH); + } catch (IOException e) { + e.printStackTrace(); + } + } + + populateConfig(); + saveModConfig(); + } + } + + public static void registerConfigChanges(QuiltKeyBindsConfig newConfig) { + MinecraftClient client = MinecraftClient.getInstance(); + for (KeyBind key : client.options.allKeys) { + if (newConfig.getKeyBinds().containsKey(key.getTranslationKey())) { + List keyList = newConfig.getKeyBinds().get(key.getTranslationKey()); + if (keyList.size() == 1) { + key.setBoundKey(InputUtil.fromTranslationKey(keyList.get(0))); + } else if (keyList.size() > 1) { + SortedMap map = new TreeMap<>(); + for (String string : keyList) { + map.put(InputUtil.fromTranslationKey(string), false); + } + + ((ChordedKeyBind) key).setBoundChord(new KeyChord(map)); + } + } + }; + CONFIG.setKeyBinds(newConfig.getKeyBinds()); + } + + public static void saveModConfig() { + var result = QuiltKeyBindsConfig.CODEC.encodeStart(JsonOps.INSTANCE, CONFIG).result(); + if (result.isPresent()) { + try { + Writer writer = Files.newBufferedWriter(KEY_BINDS_CONFIG_PATH, StandardCharsets.UTF_8); + JsonWriter jsonWriter = GSON.newJsonWriter(writer); + GSON.toJson(result.get(), jsonWriter); + jsonWriter.close(); + } catch (IOException e) { + System.err.println(e); + } + } + } + + public static void populateConfig() { + Map> keyBindMap = new HashMap<>(); + + KeyBindRegistryImpl.getAllKeyBinds(true).forEach((keyBind, disabled) -> { + if (((ChordedKeyBind) keyBind).getBoundChord() != null) { + List list = new ArrayList<>(); + for (InputUtil.Key key : ((ChordedKeyBind) keyBind).getBoundChord().keys.keySet()) { + list.add(key.getTranslationKey()); + } + + keyBindMap.put(keyBind.getTranslationKey(), list); + } else { + keyBindMap.put(keyBind.getTranslationKey(), List.of(keyBind.getKeyTranslationKey())); + } + + /* + if (!CONFIG.getKeyBinds().containsKey(key.getTranslationKey())) { + keyBindMap.put(key.getTranslationKey(), List.of(key.getKeyTranslationKey())); + } else { + keyBindMap.put(key.getTranslationKey(), List.of(key.getKeyTranslationKey())); + //key.setBoundKey(InputUtil.fromTranslationKey(CONFIG.getKeyBinds().get(key.getTranslationKey()))); + } + + */ + }); + CONFIG.setKeyBinds(keyBindMap); + } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java index 8199a460a0..d640adec15 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java @@ -19,6 +19,7 @@ import java.util.List; import com.mojang.blaze3d.platform.InputUtil; +import org.objectweb.asm.Opcodes; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -28,10 +29,10 @@ import org.spongepowered.asm.mixin.injection.ModifyArg; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.LocalCapture; - import it.unimi.dsi.fastutil.objects.ObjectArrayList; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; + import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.widget.ButtonWidget; import net.minecraft.client.gui.widget.option.KeyBindListWidget; @@ -43,7 +44,6 @@ import net.minecraft.text.TranslatableText; import net.minecraft.util.Formatting; -import org.objectweb.asm.Opcodes; import org.quiltmc.qsl.key.binds.api.KeyBindRegistry; import org.quiltmc.qsl.key.binds.impl.KeyBindTooltipHolder; import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; @@ -94,7 +94,7 @@ private void initPreviousBoundKey(KeyBindListWidget list, KeyBind key, Text text ) private void collectConflictTooltips(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta, CallbackInfo ci, boolean bl, boolean bl2) { InputUtil.Key boundKey = KeyBindRegistry.getBoundKey(this.key); - KeyChord boundChord = ((ChordedKeyBind)(this.key)).getBoundChord(); + KeyChord boundChord = ((ChordedKeyBind) this.key).getBoundChord(); List boundProtoChord; if (boundChord == null) { @@ -146,9 +146,9 @@ private void shortenText(MatrixStack matrices, int index, int y, int x, int entr int targetWidth = bl || bl2 ? 50 - 10 : 75 - 10; if (client.textRenderer.getWidth(text) > targetWidth) { String protoText = text.getString(); - if (((ChordedKeyBind)this.key).getBoundChord() != null) { + if (((ChordedKeyBind) this.key).getBoundChord() != null) { protoText = ""; - KeyChord chord = ((ChordedKeyBind)this.key).getBoundChord(); + KeyChord chord = ((ChordedKeyBind) this.key).getBoundChord(); for (InputUtil.Key key : chord.keys.keySet()) { if (!protoText.isEmpty()) { @@ -174,6 +174,7 @@ private void shortenText(MatrixStack matrices, int index, int y, int x, int entr this.quilt$conflictTooltips.add(0, this.key.getKeyName()); quilt$addKeyNameToTooltip = false; } + protoText = client.textRenderer.trimToWidth(protoText, targetWidth); protoText += "..."; } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindsScreenMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindsScreenMixin.java index 8ff6fac1d3..eb6d07adc0 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindsScreenMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindsScreenMixin.java @@ -54,7 +54,8 @@ private void renderConflictTooltips(MatrixStack matrices, int mouseX, int mouseY KeyBindListWidget.Entry entry = ((EntryListWidgetAccessor) this.keyBindList).invokeGetHoveredEntry(); if (entry != null && entry instanceof KeyBindEntry keyBindEntry) { List tooltipLines = ((KeyBindTooltipHolder) keyBindEntry).getKeyBindTooltips(); - if (tooltipLines != null) { + + if (tooltipLines != null) { // TODO - With key names, it's getting too big! Add a maximum width this.renderTooltip(matrices, tooltipLines, mouseX, mouseY); } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/InputUtilKeyMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/InputUtilKeyMixin.java index 7afdc05e50..f9cc027e1f 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/InputUtilKeyMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/InputUtilKeyMixin.java @@ -22,11 +22,11 @@ @Mixin(InputUtil.Key.class) public abstract class InputUtilKeyMixin implements Comparable { - @Override - public int compareTo(InputUtil.Key key) { - return Integer.compare(this.getKeyCode(), key.getKeyCode()); - } + @Override + public int compareTo(InputUtil.Key key) { + return Integer.compare(this.getKeyCode(), key.getKeyCode()); + } - @Shadow - public abstract int getKeyCode(); + @Shadow + public abstract int getKeyCode(); } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindAccessor.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindAccessor.java index 018059a949..a4119c0caa 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindAccessor.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindAccessor.java @@ -23,9 +23,9 @@ @Mixin(KeyBind.class) public interface KeyBindAccessor { - @Accessor - int getTimesPressed(); - - @Accessor - void setTimesPressed(int timesPressed); + @Accessor + int getTimesPressed(); + + @Accessor + void setTimesPressed(int timesPressed); } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java index d1dc7e2de7..5dd145e4e8 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java @@ -20,9 +20,6 @@ import java.util.Map; import com.mojang.blaze3d.platform.InputUtil; - -import org.quiltmc.qsl.key.binds.impl.chords.ChordedKeyBind; -import org.quiltmc.qsl.key.binds.impl.chords.KeyChord; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -32,169 +29,175 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import org.spongepowered.asm.mixin.injection.callback.LocalCapture; - import it.unimi.dsi.fastutil.objects.Reference2ReferenceOpenHashMap; + import net.minecraft.client.MinecraftClient; import net.minecraft.client.option.KeyBind; import net.minecraft.text.LiteralText; import net.minecraft.text.MutableText; import net.minecraft.text.Text; +import org.quiltmc.qsl.key.binds.impl.chords.ChordedKeyBind; +import org.quiltmc.qsl.key.binds.impl.chords.KeyChord; + @Mixin(KeyBind.class) public class KeyBindMixin implements ChordedKeyBind { - @Shadow - @Final - private static Map KEY_BINDS; - - @Shadow - private InputUtil.Key boundKey; - - @Unique - private static final Map KEY_BINDS_BY_CHORD = new Reference2ReferenceOpenHashMap<>(); - - @Unique - @Final - private KeyChord quilt$defaultChord; - - @Unique - private KeyChord quilt$boundChord; - - @Inject( - at = @At("RETURN"), - method = "(Ljava/lang/String;Lcom/mojang/blaze3d/platform/InputUtil$Type;ILjava/lang/String;)V" - ) - private void expandInit(String string, InputUtil.Type type, int i, String string2, CallbackInfo ci) { - quilt$defaultChord = null; - quilt$boundChord = null; - } - - @Inject(at = @At("HEAD"), method = "onKeyPressed") - private static void detectChordsOnIncrement(InputUtil.Key startingKey, CallbackInfo ci) { - for (KeyChord chord : KEY_BINDS_BY_CHORD.keySet()) { - if (chord.keys.containsKey(startingKey) && !chord.keys.containsValue(false)) { - // This ensures that the chord will only be incremented once instead of N times - if (startingKey.equals(chord.keys.keySet().toArray()[0])) { - KeyBind keyBind = KEY_BINDS_BY_CHORD.get(chord); - ((KeyBindAccessor)keyBind).setTimesPressed(((KeyBindAccessor)keyBind).getTimesPressed() + 1); - } - } - } - } - - @Inject(at = @At("HEAD"), method = "setKeyPressed") - private static void detectChordsOnSet(InputUtil.Key startingKey, boolean pressed, CallbackInfo ci) { - for (KeyChord chord : KEY_BINDS_BY_CHORD.keySet()) { - if (chord.keys.containsKey(startingKey)) { - chord.keys.put(startingKey, pressed); - - if (!chord.keys.containsValue(false)) { - KEY_BINDS_BY_CHORD.get(chord).setPressed(true); - } else { - KEY_BINDS_BY_CHORD.get(chord).setPressed(false); - } - } - } - } - - @Inject( - at = @At( - value = "INVOKE", - target = "Lcom/mojang/blaze3d/platform/InputUtil$Key;getType()Lcom/mojang/blaze3d/platform/InputUtil$Type;" - ), - method = "updatePressedStates", - locals = LocalCapture.CAPTURE_FAILHARD, - cancellable = true - ) - private static void updateChordsToo(CallbackInfo ci, Iterator iterator, KeyBind keyBind) { - KeyChord chord = ((KeyBindMixin)(Object)keyBind).quilt$boundChord; - if (chord != null) { - long window = MinecraftClient.getInstance().getWindow().getHandle(); - for (InputUtil.Key key : chord.keys.keySet()) { - if (key.getType() == InputUtil.Type.KEYSYM) { - chord.keys.put(key, InputUtil.isKeyPressed(window, key.getKeyCode())); - } - } - // TODO - Create an "Update Chord" method for this - if (!chord.keys.containsValue(false)) { - KEY_BINDS_BY_CHORD.get(chord).setPressed(true); - } else { - KEY_BINDS_BY_CHORD.get(chord).setPressed(false); - } - - ci.cancel(); - } - } - - @Inject(at = @At("HEAD"), method = "updateBoundKeys") - private static void updateChordBoundKeys(CallbackInfo cir) { - KEY_BINDS_BY_CHORD.clear(); - - for (KeyBind key : KEY_BINDS.values()) { - KeyChord chord = ((KeyBindMixin)(Object)key).quilt$boundChord; - if (chord != null) { - KEY_BINDS_BY_CHORD.put(chord, key); - } - } - } - - // TODO - Detect chords for matchesKey too; They are such a weird case - @Inject(at = @At("HEAD"), method = "matchesKey", cancellable = true) - private void matchesChordKey(CallbackInfoReturnable ci) {} - - // TODO - Detech chords for matchesMouseButton as well - - @Inject(at = @At("HEAD"), method = "setBoundKey", cancellable = true) - private void resetChord(CallbackInfo ci) { - this.quilt$boundChord = null; - } - - @Inject(at = @At("HEAD"), method = "keyEquals", cancellable = true) - private void keyOrChordEquals(KeyBind other, CallbackInfoReturnable cir) { - if (this.quilt$boundChord != null) { - if (((ChordedKeyBind)other).getBoundChord() != null) { - cir.setReturnValue(this.quilt$boundChord.keys.keySet().equals(((ChordedKeyBind)other).getBoundChord().keys.keySet())); - } else { - cir.setReturnValue(false); - } - } - } - - @Inject(at = @At("RETURN"), method = "isUnbound", cancellable = true) - private void isChordUnbound(CallbackInfoReturnable cir) { - if (cir.getReturnValueZ() && this.quilt$boundChord != null) { - cir.setReturnValue(false); - } - } - - @Inject(at = @At("HEAD"), method = "getKeyName", cancellable = true) - private void useChordName(CallbackInfoReturnable cir) { - if (this.quilt$boundChord != null) { - MutableText text = LiteralText.EMPTY.shallowCopy(); - for (InputUtil.Key key : this.quilt$boundChord.keys.keySet()) { - if (text.getSiblings().size() != 0) { - text.append(" + "); - } - text.append(key.getDisplayText()); - } - cir.setReturnValue(text); - } - } - - @Inject(at = @At("HEAD"), method = "isDefault", cancellable = true) - private void detectDefaultChord(CallbackInfoReturnable cir) { - if (this.quilt$boundChord != null) { - cir.setReturnValue(this.quilt$boundChord.equals(this.quilt$defaultChord)); - } - } - - @Override - public KeyChord getBoundChord() { - return this.quilt$boundChord; - } - - @Override - public void setBoundChord(KeyChord chord) { - this.quilt$boundChord = chord; - this.boundKey = InputUtil.UNKNOWN_KEY; - } + @Shadow + @Final + private static Map KEY_BINDS; + + @Shadow + private InputUtil.Key boundKey; + + @Unique + private static final Map KEY_BINDS_BY_CHORD = new Reference2ReferenceOpenHashMap<>(); + + @Unique + @Final + private KeyChord quilt$defaultChord; + + @Unique + private KeyChord quilt$boundChord; + + @Inject( + at = @At("RETURN"), + method = "(Ljava/lang/String;Lcom/mojang/blaze3d/platform/InputUtil$Type;ILjava/lang/String;)V" + ) + private void expandInit(String string, InputUtil.Type type, int i, String string2, CallbackInfo ci) { + quilt$defaultChord = null; + quilt$boundChord = null; + } + + @Inject(at = @At("HEAD"), method = "onKeyPressed") + private static void detectChordsOnIncrement(InputUtil.Key startingKey, CallbackInfo ci) { + for (KeyChord chord : KEY_BINDS_BY_CHORD.keySet()) { + if (chord.keys.containsKey(startingKey) && !chord.keys.containsValue(false)) { + // This ensures that the chord will only be incremented once instead of N times + if (startingKey.equals(chord.keys.keySet().toArray()[0])) { + KeyBind keyBind = KEY_BINDS_BY_CHORD.get(chord); + ((KeyBindAccessor) keyBind).setTimesPressed(((KeyBindAccessor) keyBind).getTimesPressed() + 1); + } + } + } + } + + @Inject(at = @At("HEAD"), method = "setKeyPressed") + private static void detectChordsOnSet(InputUtil.Key startingKey, boolean pressed, CallbackInfo ci) { + for (KeyChord chord : KEY_BINDS_BY_CHORD.keySet()) { + if (chord.keys.containsKey(startingKey)) { + chord.keys.put(startingKey, pressed); + + if (!chord.keys.containsValue(false)) { + KEY_BINDS_BY_CHORD.get(chord).setPressed(true); + } else { + KEY_BINDS_BY_CHORD.get(chord).setPressed(false); + } + } + } + } + + @Inject( + at = @At( + value = "INVOKE", + target = "Lcom/mojang/blaze3d/platform/InputUtil$Key;getType()Lcom/mojang/blaze3d/platform/InputUtil$Type;" + ), + method = "updatePressedStates", + locals = LocalCapture.CAPTURE_FAILHARD, + cancellable = true + ) + private static void updateChordsToo(CallbackInfo ci, Iterator iterator, KeyBind keyBind) { + KeyChord chord = ((KeyBindMixin) (Object) keyBind).quilt$boundChord; + if (chord != null) { + long window = MinecraftClient.getInstance().getWindow().getHandle(); + for (InputUtil.Key key : chord.keys.keySet()) { + if (key.getType() == InputUtil.Type.KEYSYM) { + chord.keys.put(key, InputUtil.isKeyPressed(window, key.getKeyCode())); + } + } + + // TODO - Create an "Update Chord" method for this + if (!chord.keys.containsValue(false)) { + KEY_BINDS_BY_CHORD.get(chord).setPressed(true); + } else { + KEY_BINDS_BY_CHORD.get(chord).setPressed(false); + } + + ci.cancel(); + } + } + + @Inject(at = @At("HEAD"), method = "updateBoundKeys") + private static void updateChordBoundKeys(CallbackInfo cir) { + KEY_BINDS_BY_CHORD.clear(); + + for (KeyBind key : KEY_BINDS.values()) { + KeyChord chord = ((KeyBindMixin) (Object) key).quilt$boundChord; + if (chord != null) { + KEY_BINDS_BY_CHORD.put(chord, key); + } + } + } + + // TODO - Detect chords for matchesKey too; They are such a weird case + @Inject(at = @At("HEAD"), method = "matchesKey", cancellable = true) + private void matchesChordKey(CallbackInfoReturnable ci) { } + + // TODO - Detech chords for matchesMouseButton as well + + @Inject(at = @At("HEAD"), method = "setBoundKey", cancellable = true) + private void resetChord(CallbackInfo ci) { + this.quilt$boundChord = null; + } + + @Inject(at = @At("HEAD"), method = "keyEquals", cancellable = true) + private void keyOrChordEquals(KeyBind other, CallbackInfoReturnable cir) { + if (this.quilt$boundChord != null) { + if (((ChordedKeyBind) other).getBoundChord() != null) { + cir.setReturnValue(this.quilt$boundChord.keys.keySet().equals(((ChordedKeyBind) other).getBoundChord().keys.keySet())); + } else { + cir.setReturnValue(false); + } + } + } + + @Inject(at = @At("RETURN"), method = "isUnbound", cancellable = true) + private void isChordUnbound(CallbackInfoReturnable cir) { + if (cir.getReturnValueZ() && this.quilt$boundChord != null) { + cir.setReturnValue(false); + } + } + + @Inject(at = @At("HEAD"), method = "getKeyName", cancellable = true) + private void useChordName(CallbackInfoReturnable cir) { + if (this.quilt$boundChord != null) { + MutableText text = LiteralText.EMPTY.shallowCopy(); + for (InputUtil.Key key : this.quilt$boundChord.keys.keySet()) { + if (text.getSiblings().size() != 0) { + text.append(" + "); + } + + text.append(key.getDisplayText()); + } + + cir.setReturnValue(text); + } + } + + @Inject(at = @At("HEAD"), method = "isDefault", cancellable = true) + private void detectDefaultChord(CallbackInfoReturnable cir) { + if (this.quilt$boundChord != null) { + cir.setReturnValue(this.quilt$boundChord.equals(this.quilt$defaultChord)); + } + } + + @Override + public KeyChord getBoundChord() { + return this.quilt$boundChord; + } + + @Override + public void setBoundChord(KeyChord chord) { + this.quilt$boundChord = chord; + this.boundKey = InputUtil.UNKNOWN_KEY; + } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java index 9d8682381d..e2a2712c70 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java @@ -20,11 +20,7 @@ import java.util.SortedMap; import com.mojang.blaze3d.platform.InputUtil; - import org.jetbrains.annotations.Nullable; -import org.quiltmc.qsl.key.binds.impl.chords.ChordedKeyBind; -import org.quiltmc.qsl.key.binds.impl.chords.KeyChord; -import org.quiltmc.qsl.key.binds.impl.config.QuiltKeyBindsConfigManager; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; @@ -32,9 +28,9 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - import it.unimi.dsi.fastutil.objects.Object2BooleanAVLTreeMap; import it.unimi.dsi.fastutil.objects.ObjectArrayList; + import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.option.GameOptionsScreen; import net.minecraft.client.gui.screen.option.KeyBindsScreen; @@ -43,118 +39,126 @@ import net.minecraft.text.Text; import net.minecraft.util.Util; +import org.quiltmc.qsl.key.binds.impl.chords.ChordedKeyBind; +import org.quiltmc.qsl.key.binds.impl.chords.KeyChord; +import org.quiltmc.qsl.key.binds.impl.config.QuiltKeyBindsConfigManager; + @Mixin(KeyBindsScreen.class) public abstract class KeyBindsScreenMixin extends GameOptionsScreen { - @Shadow - @Nullable - public KeyBind focusedKey; - - @Shadow - public long time; - - @Unique - private List quilt$focusedProtoChord; - - @Unique - private boolean quilt$initialMouseRelease; - - public KeyBindsScreenMixin(Screen screen, GameOptions gameOptions, Text text) { - super(screen, gameOptions, text); - } - - @Inject(at = @At("TAIL"), method = "init") - private void initializeProtoChord(CallbackInfo ci) { - this.quilt$focusedProtoChord = new ObjectArrayList<>(); - this.quilt$initialMouseRelease = true; - } - - @Inject( - at = @At( - value = "INVOKE", - target = "Lnet/minecraft/client/option/GameOptions;setKeyCode(Lnet/minecraft/client/option/KeyBind;Lcom/mojang/blaze3d/platform/InputUtil$Key;)V" - ), - method = "mouseClicked", - cancellable = true - ) - private void modifyMouseClicked(double mouseX, double mouseY, int button, CallbackInfoReturnable cir) { - InputUtil.Key key = InputUtil.Type.MOUSE.createFromKeyCode(button); - if (!quilt$focusedProtoChord.contains(key)) { - quilt$focusedProtoChord.add(key); - } - cir.setReturnValue(true); - } - - @Inject(at = @At(value = "RETURN", ordinal = 1), method = "mouseClicked") - private void excludeFirstMouseClick(double mouseX, double mouseY, int button, CallbackInfoReturnable cir) { - this.quilt$initialMouseRelease = true; - } - - @Inject( - at = @At( - value = "INVOKE", - target = "Lnet/minecraft/client/option/GameOptions;setKeyCode(Lnet/minecraft/client/option/KeyBind;Lcom/mojang/blaze3d/platform/InputUtil$Key;)V", - ordinal = 1 - ), - method = "keyPressed", - cancellable = true - ) - private void modifyKeyPressed(int keyCode, int scanCode, int modifiers, CallbackInfoReturnable cir) { - InputUtil.Key key = InputUtil.fromKeyCode(keyCode, scanCode); - if (!quilt$focusedProtoChord.contains(key)) { - quilt$focusedProtoChord.add(key); - } - cir.setReturnValue(true); - } - - @Override - public boolean keyReleased(int keyCode, int scanCode, int modifiers) { - if (this.focusedKey != null) { - if (quilt$focusedProtoChord.size() == 1) { - this.gameOptions.setKeyCode(this.focusedKey, quilt$focusedProtoChord.get(0)); - } else if (quilt$focusedProtoChord.size() > 1) { - SortedMap map = new Object2BooleanAVLTreeMap<>(); - for (int i = 0; i < quilt$focusedProtoChord.size(); i++) { - map.put(quilt$focusedProtoChord.get(i), false); - } - ((ChordedKeyBind)this.focusedKey).setBoundChord(new KeyChord(map)); - QuiltKeyBindsConfigManager.populateConfig(); - QuiltKeyBindsConfigManager.saveModConfig(); - } - - quilt$focusedProtoChord.clear(); - this.focusedKey = null; + @Shadow + @Nullable + public KeyBind focusedKey; + + @Shadow + public long time; + + @Unique + private List quilt$focusedProtoChord; + + @Unique + private boolean quilt$initialMouseRelease; + + public KeyBindsScreenMixin(Screen screen, GameOptions gameOptions, Text text) { + super(screen, gameOptions, text); + } + + @Inject(at = @At("TAIL"), method = "init") + private void initializeProtoChord(CallbackInfo ci) { + this.quilt$focusedProtoChord = new ObjectArrayList<>(); + this.quilt$initialMouseRelease = true; + } + + @Inject( + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/client/option/GameOptions;setKeyCode(Lnet/minecraft/client/option/KeyBind;Lcom/mojang/blaze3d/platform/InputUtil$Key;)V" + ), + method = "mouseClicked", + cancellable = true + ) + private void modifyMouseClicked(double mouseX, double mouseY, int button, CallbackInfoReturnable cir) { + InputUtil.Key key = InputUtil.Type.MOUSE.createFromKeyCode(button); + if (!quilt$focusedProtoChord.contains(key)) { + quilt$focusedProtoChord.add(key); + } + + cir.setReturnValue(true); + } + + @Inject(at = @At(value = "RETURN", ordinal = 1), method = "mouseClicked") + private void excludeFirstMouseClick(double mouseX, double mouseY, int button, CallbackInfoReturnable cir) { + this.quilt$initialMouseRelease = true; + } + + @Inject( + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/client/option/GameOptions;setKeyCode(Lnet/minecraft/client/option/KeyBind;Lcom/mojang/blaze3d/platform/InputUtil$Key;)V", + ordinal = 1 + ), + method = "keyPressed", + cancellable = true + ) + private void modifyKeyPressed(int keyCode, int scanCode, int modifiers, CallbackInfoReturnable cir) { + InputUtil.Key key = InputUtil.fromKeyCode(keyCode, scanCode); + if (!quilt$focusedProtoChord.contains(key)) { + quilt$focusedProtoChord.add(key); + } + + cir.setReturnValue(true); + } + + @Override + public boolean keyReleased(int keyCode, int scanCode, int modifiers) { + if (this.focusedKey != null) { + if (quilt$focusedProtoChord.size() == 1) { + this.gameOptions.setKeyCode(this.focusedKey, quilt$focusedProtoChord.get(0)); + } else if (quilt$focusedProtoChord.size() > 1) { + SortedMap map = new Object2BooleanAVLTreeMap<>(); + for (int i = 0; i < quilt$focusedProtoChord.size(); i++) { + map.put(quilt$focusedProtoChord.get(i), false); + } + + ((ChordedKeyBind) this.focusedKey).setBoundChord(new KeyChord(map)); + QuiltKeyBindsConfigManager.populateConfig(); + QuiltKeyBindsConfigManager.saveModConfig(); + } + + quilt$focusedProtoChord.clear(); + this.focusedKey = null; this.time = Util.getMeasuringTimeMs(); KeyBind.updateBoundKeys(); return true; - } else { - return super.keyReleased(keyCode, scanCode, modifiers); - } - } - - @Override - public boolean mouseReleased(double mouseX, double mouseY, int button) { - // TODO - Don't duplicate code, have a common method - if (this.focusedKey != null && !this.quilt$initialMouseRelease) { - if (quilt$focusedProtoChord.size() == 1) { - this.gameOptions.setKeyCode(this.focusedKey, quilt$focusedProtoChord.get(0)); - } else if (quilt$focusedProtoChord.size() > 1) { - SortedMap map = new Object2BooleanAVLTreeMap<>(); - for (int i = 0; i < quilt$focusedProtoChord.size(); i++) { - map.put(quilt$focusedProtoChord.get(i), false); - } - ((ChordedKeyBind)this.focusedKey).setBoundChord(new KeyChord(map)); - QuiltKeyBindsConfigManager.populateConfig(); - QuiltKeyBindsConfigManager.saveModConfig(); - } - - quilt$focusedProtoChord.clear(); - this.focusedKey = null; + } else { + return super.keyReleased(keyCode, scanCode, modifiers); + } + } + + @Override + public boolean mouseReleased(double mouseX, double mouseY, int button) { + // TODO - Don't duplicate code, have a common method + if (this.focusedKey != null && !this.quilt$initialMouseRelease) { + if (quilt$focusedProtoChord.size() == 1) { + this.gameOptions.setKeyCode(this.focusedKey, quilt$focusedProtoChord.get(0)); + } else if (quilt$focusedProtoChord.size() > 1) { + SortedMap map = new Object2BooleanAVLTreeMap<>(); + for (int i = 0; i < quilt$focusedProtoChord.size(); i++) { + map.put(quilt$focusedProtoChord.get(i), false); + } + + ((ChordedKeyBind) this.focusedKey).setBoundChord(new KeyChord(map)); + QuiltKeyBindsConfigManager.populateConfig(); + QuiltKeyBindsConfigManager.saveModConfig(); + } + + quilt$focusedProtoChord.clear(); + this.focusedKey = null; this.time = Util.getMeasuringTimeMs(); KeyBind.updateBoundKeys(); return true; - } else { - this.quilt$initialMouseRelease = false; - return super.mouseReleased(mouseX, mouseY, button); - } - } + } else { + this.quilt$initialMouseRelease = false; + return super.mouseReleased(mouseX, mouseY, button); + } + } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/GameOptionsMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/GameOptionsMixin.java index e831ddf572..89f9a89920 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/GameOptionsMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/GameOptionsMixin.java @@ -17,8 +17,6 @@ package org.quiltmc.qsl.key.binds.mixin.client.config; import com.mojang.blaze3d.platform.InputUtil; - -import org.quiltmc.qsl.key.binds.impl.config.QuiltKeyBindsConfigManager; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -28,26 +26,28 @@ import net.minecraft.client.option.GameOptions; import net.minecraft.client.option.KeyBind; +import org.quiltmc.qsl.key.binds.impl.config.QuiltKeyBindsConfigManager; + @Mixin(GameOptions.class) public abstract class GameOptionsMixin { - @Inject( - at = @At( - value = "INVOKE", - target = "Lnet/minecraft/client/option/GameOptions;write()V" - ), - method = "setKeyCode" - ) - private void writeToKeyBindConfig(KeyBind key, InputUtil.Key code, CallbackInfo ci) { - QuiltKeyBindsConfigManager.populateConfig(); - QuiltKeyBindsConfigManager.saveModConfig(); - } + @Inject( + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/client/option/GameOptions;write()V" + ), + method = "setKeyCode" + ) + private void writeToKeyBindConfig(KeyBind key, InputUtil.Key code, CallbackInfo ci) { + QuiltKeyBindsConfigManager.populateConfig(); + QuiltKeyBindsConfigManager.saveModConfig(); + } - @Redirect( - at = @At( - value = "INVOKE", - target = "Lnet/minecraft/client/option/KeyBind;setBoundKey(Lcom/mojang/blaze3d/platform/InputUtil$Key;)V" - ), - method = "accept" - ) - private void useOurConfigInstead(KeyBind keyBind, InputUtil.Key key) {} + @Redirect( + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/client/option/KeyBind;setBoundKey(Lcom/mojang/blaze3d/platform/InputUtil$Key;)V" + ), + method = "accept" + ) + private void useOurConfigInstead(KeyBind keyBind, InputUtil.Key key) { } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/MinecraftClientMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/MinecraftClientMixin.java index 19e87f7318..e9ea334236 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/MinecraftClientMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/MinecraftClientMixin.java @@ -16,7 +16,6 @@ package org.quiltmc.qsl.key.binds.mixin.client.config; -import org.quiltmc.qsl.key.binds.impl.config.QuiltKeyBindsConfigManager; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -26,15 +25,17 @@ import net.minecraft.client.RunArgs; import net.minecraft.client.option.KeyBind; +import org.quiltmc.qsl.key.binds.impl.config.QuiltKeyBindsConfigManager; + @Mixin(MinecraftClient.class) public abstract class MinecraftClientMixin { - // You can't INVOKE_ASSIGN at GameOptions for some reason - @Inject( - at = @At(value = "INVOKE", target = "Lnet/minecraft/client/tutorial/TutorialManager;(Lnet/minecraft/client/MinecraftClient;Lnet/minecraft/client/option/GameOptions;)V"), - method = "" - ) - private void handleQuiltKeyBindsConfig(RunArgs runArgs, CallbackInfo ci) { - QuiltKeyBindsConfigManager.loadConfig(); - KeyBind.updateBoundKeys(); - } + // You can't INVOKE_ASSIGN at GameOptions for some reason + @Inject( + at = @At(value = "INVOKE", target = "Lnet/minecraft/client/tutorial/TutorialManager;(Lnet/minecraft/client/MinecraftClient;Lnet/minecraft/client/option/GameOptions;)V"), + method = "" + ) + private void handleQuiltKeyBindsConfig(RunArgs runArgs, CallbackInfo ci) { + QuiltKeyBindsConfigManager.loadConfig(); + KeyBind.updateBoundKeys(); + } } From a6cbfda4b843c758912263021ba5ba9b7c1545d7 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Tue, 15 Mar 2022 16:41:15 -0300 Subject: [PATCH 43/56] Fix lack of conflicts with the chord map --- .../qsl/key/binds/impl/chords/KeyChord.java | 16 +++++++++++++++- .../binds/mixin/client/chords/KeyBindMixin.java | 4 ++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java index f105115a1f..23de75d180 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java @@ -31,5 +31,19 @@ public KeyChord(SortedMap keys) { public KeyChord() { } - // TODO - Override hashCode() so it only counts keys + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } else if (o != null && o instanceof KeyChord keyChord) { + return this.keys.keySet().equals(keyChord.keys.keySet()); + } else { + return false; + } + } + + @Override + public int hashCode() { + return keys.keySet().hashCode(); + } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java index 5dd145e4e8..f4b8ebb75b 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java @@ -142,7 +142,7 @@ private static void updateChordBoundKeys(CallbackInfo cir) { @Inject(at = @At("HEAD"), method = "matchesKey", cancellable = true) private void matchesChordKey(CallbackInfoReturnable ci) { } - // TODO - Detech chords for matchesMouseButton as well + // TODO - Detect chords for matchesMouseButton as well @Inject(at = @At("HEAD"), method = "setBoundKey", cancellable = true) private void resetChord(CallbackInfo ci) { @@ -153,7 +153,7 @@ private void resetChord(CallbackInfo ci) { private void keyOrChordEquals(KeyBind other, CallbackInfoReturnable cir) { if (this.quilt$boundChord != null) { if (((ChordedKeyBind) other).getBoundChord() != null) { - cir.setReturnValue(this.quilt$boundChord.keys.keySet().equals(((ChordedKeyBind) other).getBoundChord().keys.keySet())); + cir.setReturnValue(this.quilt$boundChord.equals(((ChordedKeyBind) other).getBoundChord())); } else { cir.setReturnValue(false); } From f10008d47504b1fc6c53909ad2212c8466707c73 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Mon, 28 Mar 2022 20:04:50 -0300 Subject: [PATCH 44/56] Allow for setting default chords --- .../{impl/chords => api}/ChordedKeyBind.java | 21 ++++++-- .../config/QuiltKeyBindsConfigManager.java | 7 ++- .../binds/mixin/client/KeyBindEntryMixin.java | 7 ++- .../mixin/client/chords/KeyBindMixin.java | 29 +++++++++-- .../client/chords/KeyBindsScreenMixin.java | 5 +- .../src/main/resources/fabric.mod.json | 29 +++++++++-- .../binds/test/client/KeyBindsTestMod.java | 52 +++++++++++-------- .../quilt_key_binds_testmod/lang/en_us.json | 3 +- 8 files changed, 109 insertions(+), 44 deletions(-) rename library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/{impl/chords => api}/ChordedKeyBind.java (54%) diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/ChordedKeyBind.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ChordedKeyBind.java similarity index 54% rename from library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/ChordedKeyBind.java rename to library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ChordedKeyBind.java index 13720659ee..a3981b8ec7 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/ChordedKeyBind.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ChordedKeyBind.java @@ -14,9 +14,24 @@ * limitations under the License. */ -package org.quiltmc.qsl.key.binds.impl.chords; +package org.quiltmc.qsl.key.binds.api; + +import com.mojang.blaze3d.platform.InputUtil; + +import net.minecraft.client.option.KeyBind; + +import org.quiltmc.qsl.key.binds.impl.chords.KeyChord; public interface ChordedKeyBind { - KeyChord getBoundChord(); - void setBoundChord(KeyChord chord); + default KeyChord getBoundChord() { + System.out.println("This should never happen"); + return null; + } + + default void setBoundChord(KeyChord chord) { } + + // TODO - This is a temporary measure until CHASM comes. Replace it with a proper constructor or builder + default KeyBind withChord(InputUtil.Key... keys) { + return null; + } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java index 50f48a273f..8305a047bf 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java @@ -43,7 +43,6 @@ import net.minecraft.client.option.KeyBind; import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; -import org.quiltmc.qsl.key.binds.impl.chords.ChordedKeyBind; import org.quiltmc.qsl.key.binds.impl.chords.KeyChord; // TODO - Understand this mess that you have written down @@ -99,7 +98,7 @@ public static void registerConfigChanges(QuiltKeyBindsConfig newConfig) { map.put(InputUtil.fromTranslationKey(string), false); } - ((ChordedKeyBind) key).setBoundChord(new KeyChord(map)); + key.setBoundChord(new KeyChord(map)); } } }; @@ -124,9 +123,9 @@ public static void populateConfig() { Map> keyBindMap = new HashMap<>(); KeyBindRegistryImpl.getAllKeyBinds(true).forEach((keyBind, disabled) -> { - if (((ChordedKeyBind) keyBind).getBoundChord() != null) { + if (keyBind.getBoundChord() != null) { List list = new ArrayList<>(); - for (InputUtil.Key key : ((ChordedKeyBind) keyBind).getBoundChord().keys.keySet()) { + for (InputUtil.Key key : keyBind.getBoundChord().keys.keySet()) { list.add(key.getTranslationKey()); } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java index d640adec15..75a43de34d 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java @@ -47,7 +47,6 @@ import org.quiltmc.qsl.key.binds.api.KeyBindRegistry; import org.quiltmc.qsl.key.binds.impl.KeyBindTooltipHolder; import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; -import org.quiltmc.qsl.key.binds.impl.chords.ChordedKeyBind; import org.quiltmc.qsl.key.binds.impl.chords.KeyChord; @Environment(EnvType.CLIENT) @@ -94,7 +93,7 @@ private void initPreviousBoundKey(KeyBindListWidget list, KeyBind key, Text text ) private void collectConflictTooltips(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta, CallbackInfo ci, boolean bl, boolean bl2) { InputUtil.Key boundKey = KeyBindRegistry.getBoundKey(this.key); - KeyChord boundChord = ((ChordedKeyBind) this.key).getBoundChord(); + KeyChord boundChord = this.key.getBoundChord(); List boundProtoChord; if (boundChord == null) { @@ -146,9 +145,9 @@ private void shortenText(MatrixStack matrices, int index, int y, int x, int entr int targetWidth = bl || bl2 ? 50 - 10 : 75 - 10; if (client.textRenderer.getWidth(text) > targetWidth) { String protoText = text.getString(); - if (((ChordedKeyBind) this.key).getBoundChord() != null) { + if (this.key.getBoundChord() != null) { protoText = ""; - KeyChord chord = ((ChordedKeyBind) this.key).getBoundChord(); + KeyChord chord = this.key.getBoundChord(); for (InputUtil.Key key : chord.keys.keySet()) { if (!protoText.isEmpty()) { diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java index f4b8ebb75b..4a44a7d296 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java @@ -18,8 +18,10 @@ import java.util.Iterator; import java.util.Map; +import java.util.SortedMap; import com.mojang.blaze3d.platform.InputUtil; +import com.mojang.blaze3d.platform.InputUtil.Key; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -29,6 +31,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import org.spongepowered.asm.mixin.injection.callback.LocalCapture; +import it.unimi.dsi.fastutil.objects.Object2BooleanAVLTreeMap; import it.unimi.dsi.fastutil.objects.Reference2ReferenceOpenHashMap; import net.minecraft.client.MinecraftClient; @@ -37,7 +40,7 @@ import net.minecraft.text.MutableText; import net.minecraft.text.Text; -import org.quiltmc.qsl.key.binds.impl.chords.ChordedKeyBind; +import org.quiltmc.qsl.key.binds.api.ChordedKeyBind; import org.quiltmc.qsl.key.binds.impl.chords.KeyChord; @Mixin(KeyBind.class) @@ -152,8 +155,8 @@ private void resetChord(CallbackInfo ci) { @Inject(at = @At("HEAD"), method = "keyEquals", cancellable = true) private void keyOrChordEquals(KeyBind other, CallbackInfoReturnable cir) { if (this.quilt$boundChord != null) { - if (((ChordedKeyBind) other).getBoundChord() != null) { - cir.setReturnValue(this.quilt$boundChord.equals(((ChordedKeyBind) other).getBoundChord())); + if (other.getBoundChord() != null) { + cir.setReturnValue(this.quilt$boundChord.equals(other.getBoundChord())); } else { cir.setReturnValue(false); } @@ -197,7 +200,25 @@ public KeyChord getBoundChord() { @Override public void setBoundChord(KeyChord chord) { - this.quilt$boundChord = chord; this.boundKey = InputUtil.UNKNOWN_KEY; + this.quilt$boundChord = chord; + } + + @Override + public KeyBind withChord(InputUtil.Key... keys) { + // TODO - Perhaps have cases for length 0 and 1? + if (keys.length > 1) { + SortedMap protoChord = new Object2BooleanAVLTreeMap<>(); + for (Key key : keys) { + protoChord.put(key, false); + } + + KeyChord chord = new KeyChord(protoChord); + this.setBoundChord(chord); + this.quilt$defaultChord = chord; + KeyBind.updateBoundKeys(); + } + + return (KeyBind) (Object) this; } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java index e2a2712c70..08c2e31322 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java @@ -39,7 +39,6 @@ import net.minecraft.text.Text; import net.minecraft.util.Util; -import org.quiltmc.qsl.key.binds.impl.chords.ChordedKeyBind; import org.quiltmc.qsl.key.binds.impl.chords.KeyChord; import org.quiltmc.qsl.key.binds.impl.config.QuiltKeyBindsConfigManager; @@ -119,7 +118,7 @@ public boolean keyReleased(int keyCode, int scanCode, int modifiers) { map.put(quilt$focusedProtoChord.get(i), false); } - ((ChordedKeyBind) this.focusedKey).setBoundChord(new KeyChord(map)); + this.focusedKey.setBoundChord(new KeyChord(map)); QuiltKeyBindsConfigManager.populateConfig(); QuiltKeyBindsConfigManager.saveModConfig(); } @@ -146,7 +145,7 @@ public boolean mouseReleased(double mouseX, double mouseY, int button) { map.put(quilt$focusedProtoChord.get(i), false); } - ((ChordedKeyBind) this.focusedKey).setBoundChord(new KeyChord(map)); + this.focusedKey.setBoundChord(new KeyChord(map)); QuiltKeyBindsConfigManager.populateConfig(); QuiltKeyBindsConfigManager.saveModConfig(); } diff --git a/library/gui/key_binds/src/main/resources/fabric.mod.json b/library/gui/key_binds/src/main/resources/fabric.mod.json index cb434c2200..94d283a721 100644 --- a/library/gui/key_binds/src/main/resources/fabric.mod.json +++ b/library/gui/key_binds/src/main/resources/fabric.mod.json @@ -15,11 +15,34 @@ "QuiltMC" ], "depends": { - "fabricloader": ">=0.12", - "minecraft": ">=1.18.2-alpha.22.3.a" + "minecraft": ">=1.18.2", + "quilt_base": "*", + "quilt_resource_loader": "*", + "fabricloader": ">=0.12" }, "description": "Key binds registration and utilities.", "mixins": [ "quilt_key_binds.mixins.json" - ] + ], + "custom": { + "loom:injected_interfaces": { + "net/minecraft/class_304": [ + "org/quiltmc/qsl/key/binds/api/ChordedKeyBind" + ] + }, + "modmenu": { + "badges": [ + "library" + ], + "parent": { + "id": "qsl", + "name": "Quilt Standard Libraries", + "description": "A set of libraries to assist in making Quilt mods.", + "icon": "assets/quilt_item_group/icon.png", + "badges": [ + "library" + ] + } + } + } } diff --git a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java index 41e2fb336c..452b0e7567 100644 --- a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java +++ b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java @@ -16,12 +16,16 @@ package org.quiltmc.qsl.key.binds.test.client; +import com.mojang.blaze3d.platform.InputUtil; import org.lwjgl.glfw.GLFW; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; + import net.minecraft.client.MinecraftClient; import net.minecraft.client.option.KeyBind; +import net.minecraft.sound.SoundEvents; import net.minecraft.text.LiteralText; +import net.minecraft.util.Hand; import org.quiltmc.qsl.key.binds.api.KeyBindRegistry; import org.quiltmc.qsl.lifecycle.api.client.event.ClientTickEvents; @@ -31,50 +35,54 @@ public class KeyBindsTestMod implements ClientTickEvents.Start { public static final String KEY_CATEGORY = "key.qsl.category"; // A conflicting key test - public static final KeyBind CONFLICT_TEST_KEY_BIND = KeyBindRegistry.registerKeyBind( + public static final KeyBind CONFLICT_TEST_KEY = KeyBindRegistry.registerKeyBind( new KeyBind("key.qsl.conflict_test", GLFW.GLFW_KEY_H, KEY_CATEGORY) ); - public static final KeyBind DISABLE_KEY_BIND = KeyBindRegistry.registerKeyBind( + public static final KeyBind DISABLE_KEY_BIND_KEY = KeyBindRegistry.registerKeyBind( new KeyBind("key.qsl.disable_key_bind", GLFW.GLFW_KEY_H, KEY_CATEGORY), true ); - public static final KeyBind ENABLE_KEY_BIND = KeyBindRegistry.registerKeyBind( + public static final KeyBind ENABLE_KEY_BIND_KEY = KeyBindRegistry.registerKeyBind( new KeyBind("key.qsl.enable_key_bind", GLFW.GLFW_KEY_I, KEY_CATEGORY), true ); - public static final KeyBind DISABLED_CONFLICT_TEST_KEY_BIND = KeyBindRegistry.registerKeyBind( + public static final KeyBind DISABLED_CONFLICT_TEST_KEY = KeyBindRegistry.registerKeyBind( new KeyBind("key.qsl.disabled_conflict_test", GLFW.GLFW_KEY_H, KEY_CATEGORY), false ); + public static final KeyBind CLAP_KEY = KeyBindRegistry.registerKeyBind( + new KeyBind("key.qsl.clap", InputUtil.UNKNOWN_KEY.getKeyCode(), KEY_CATEGORY).withChord( + InputUtil.Type.MOUSE.createFromKeyCode(GLFW.GLFW_MOUSE_BUTTON_LEFT), + InputUtil.Type.MOUSE.createFromKeyCode(GLFW.GLFW_MOUSE_BUTTON_RIGHT) + ) + ); + @Override public void startClientTick(MinecraftClient client) { - if (DISABLE_KEY_BIND.isPressed()) { - if (client.player != null) { - client.player.sendMessage(new LiteralText("The key has disappeared! Bye bye, key!"), true); - } + if (client.player == null) return; - KeyBindRegistry.setEnabled(DISABLE_KEY_BIND, false); + if (DISABLE_KEY_BIND_KEY.isPressed()) { + client.player.sendMessage(new LiteralText("The key has disappeared! Bye bye, key!"), true); + KeyBindRegistry.setEnabled(DISABLE_KEY_BIND_KEY, false); } - if (ENABLE_KEY_BIND.isPressed()) { - if (client.player != null) { - client.player.sendMessage(new LiteralText("The key is back!"), true); - } + if (ENABLE_KEY_BIND_KEY.isPressed()) { + client.player.sendMessage(new LiteralText("The key is back!"), true); + KeyBindRegistry.setEnabled(DISABLE_KEY_BIND_KEY, true); + } - KeyBindRegistry.setEnabled(DISABLE_KEY_BIND, true); + if (CONFLICT_TEST_KEY.isPressed()) { + client.player.sendMessage(new LiteralText("This is the conflict key being pressed"), false); } - if (CONFLICT_TEST_KEY_BIND.isPressed()) { - if (client.player != null) { - client.player.sendMessage(new LiteralText("This is the conflict key being pressed"), false); - } + if (DISABLED_CONFLICT_TEST_KEY.isPressed()) { + client.player.sendMessage(new LiteralText("I'm not supposed to do things! Why am I enabled?"), false); } - if (DISABLED_CONFLICT_TEST_KEY_BIND.isPressed()) { - if (client.player != null) { - client.player.sendMessage(new LiteralText("I'm not supposed to do things! Why am I enabled?"), false); - } + if (CLAP_KEY.wasPressed()) { + client.player.sendMessage(new LiteralText("*clap*"), true); + client.player.playSound(SoundEvents.ENTITY_GENERIC_SMALL_FALL, 1.5F, 1.5F); } } } diff --git a/library/gui/key_binds/src/testmod/resources/assets/quilt_key_binds_testmod/lang/en_us.json b/library/gui/key_binds/src/testmod/resources/assets/quilt_key_binds_testmod/lang/en_us.json index c9ea46b28b..4d8f257ca4 100644 --- a/library/gui/key_binds/src/testmod/resources/assets/quilt_key_binds_testmod/lang/en_us.json +++ b/library/gui/key_binds/src/testmod/resources/assets/quilt_key_binds_testmod/lang/en_us.json @@ -3,5 +3,6 @@ "key.qsl.disable_key_bind": "Disable Key Bind", "key.qsl.enable_key_bind": "Enable Key Bind", "key.qsl.conflict_test": "Conflict Test", - "key.qsl.disabled_conflict_test": "Disabled Conflict Test" + "key.qsl.disabled_conflict_test": "Disabled Conflict Test", + "key.qsl.clap": "Clap" } \ No newline at end of file From 1009b0f281a452b5339a3826b618aed8472b1193 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Sun, 10 Apr 2022 21:18:53 -0300 Subject: [PATCH 45/56] Quiltify Key Binds API, tackle down config issues --- library/gui/key_binds/build.gradle | 6 ++ .../key/binds/impl/KeyBindRegistryImpl.java | 5 ++ .../impl/config/QuiltKeyBindsConfig.java | 13 ++-- .../config/QuiltKeyBindsConfigManager.java | 63 ++++++++++++++----- .../src/main/resources/fabric.mod.json | 48 -------------- .../test/client/KeyBindRegistryTestMod.java | 26 ++++---- .../binds/test/client/KeyBindsTestMod.java | 1 - .../src/testmod/resources/fabric.mod.json | 17 ----- .../src/testmod/resources/quilt.mod.json | 25 ++++++++ 9 files changed, 101 insertions(+), 103 deletions(-) delete mode 100644 library/gui/key_binds/src/main/resources/fabric.mod.json delete mode 100644 library/gui/key_binds/src/testmod/resources/fabric.mod.json create mode 100644 library/gui/key_binds/src/testmod/resources/quilt.mod.json diff --git a/library/gui/key_binds/build.gradle b/library/gui/key_binds/build.gradle index 134e500def..d50817ea79 100644 --- a/library/gui/key_binds/build.gradle +++ b/library/gui/key_binds/build.gradle @@ -3,7 +3,10 @@ plugins { } qslModule { + name = "Quilt Key Binds API" moduleName = "key_binds" + id = "quilt_key_binds" + description = "Key binds registration and utilities." version = "1.0.0" library = "gui" moduleDependencies { @@ -13,4 +16,7 @@ qslModule { testmodOnly("lifecycle_events") } } + injectedInterface("net/minecraft/class_304") { + values = ["org/quiltmc/qsl/key/binds/api/ChordedKeyBind"] + } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java index 1fbef48766..464e4570da 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java @@ -32,6 +32,7 @@ import net.minecraft.client.option.KeyBind; +import org.quiltmc.qsl.key.binds.impl.config.QuiltKeyBindsConfigManager; import org.quiltmc.qsl.key.binds.mixin.client.KeyBindAccessor; @Environment(EnvType.CLIENT) @@ -138,6 +139,10 @@ public static void applyChanges() { if (keyBindManager != null) { keyBindManager.addModdedKeyBinds(); } + + // TODO - Perhaps this should be executed somewhere else? + QuiltKeyBindsConfigManager.populateConfig(); + QuiltKeyBindsConfigManager.saveModConfig(); } public static KeyBind[] getKeyBinds() { diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java index 8933094f4d..9abacca914 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java @@ -20,6 +20,7 @@ import java.util.List; import java.util.Map; +import com.mojang.datafixers.util.Either; import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; @@ -28,14 +29,14 @@ public class QuiltKeyBindsConfig { instance -> instance.group( // TODO - Implement me! Codec.BOOL.fieldOf("show_tutorial_toast").forGetter(QuiltKeyBindsConfig::getShowTutorialToast), - // TODO - Ideally, it would be a list for chords, a single string for single keys, and an empty list for unbound - Codec.unboundedMap(Codec.STRING, Codec.list(Codec.STRING)).fieldOf("key_binds").forGetter(QuiltKeyBindsConfig::getKeyBinds) + Codec.unboundedMap(Codec.STRING, Codec.either(Codec.STRING, Codec.list(Codec.STRING))).fieldOf("key_binds").forGetter(QuiltKeyBindsConfig::getKeyBinds) ) .apply(instance, QuiltKeyBindsConfig::new) ); private boolean showTutorialToast; - private Map> keyBinds; + // TODO - This type is super long; Use an object for this + private Map>> keyBinds; public QuiltKeyBindsConfig() { this.keyBinds = new HashMap<>(); @@ -44,7 +45,7 @@ public QuiltKeyBindsConfig() { public QuiltKeyBindsConfig( boolean showTutorialToast, - Map> keyBinds + Map>> keyBinds ) { this.showTutorialToast = showTutorialToast; this.keyBinds = keyBinds; @@ -58,11 +59,11 @@ public void setShowTutorialToast(boolean showTutorialToast) { this.showTutorialToast = showTutorialToast; } - public Map> getKeyBinds() { + public Map>> getKeyBinds() { return keyBinds; } - public void setKeyBinds(Map> keyBinds) { + public void setKeyBinds(Map>> keyBinds) { this.keyBinds = keyBinds; } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java index 8305a047bf..d9b25b4134 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java @@ -36,12 +36,13 @@ import com.google.gson.JsonParser; import com.google.gson.stream.JsonWriter; import com.mojang.blaze3d.platform.InputUtil; +import com.mojang.datafixers.util.Either; import com.mojang.serialization.JsonOps; -import net.fabricmc.loader.api.FabricLoader; import net.minecraft.client.MinecraftClient; import net.minecraft.client.option.KeyBind; +import org.quiltmc.loader.api.QuiltLoader; import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; import org.quiltmc.qsl.key.binds.impl.chords.KeyChord; @@ -49,7 +50,7 @@ public class QuiltKeyBindsConfigManager { public static Optional isConfigLoaded = Optional.empty(); public static final QuiltKeyBindsConfig CONFIG = new QuiltKeyBindsConfig(); - public static final Path QSL_CONFIG_PATH = FabricLoader.getInstance().getConfigDir().resolve("qsl"); + public static final Path QSL_CONFIG_PATH = QuiltLoader.getConfigDir().resolve("qsl"); public static final Path KEY_BINDS_CONFIG_PATH = QSL_CONFIG_PATH.resolve("key_binds.json"); public static final Gson GSON = new GsonBuilder().setPrettyPrinting().serializeNulls().disableHtmlEscaping().create(); @@ -89,17 +90,28 @@ public static void registerConfigChanges(QuiltKeyBindsConfig newConfig) { MinecraftClient client = MinecraftClient.getInstance(); for (KeyBind key : client.options.allKeys) { if (newConfig.getKeyBinds().containsKey(key.getTranslationKey())) { - List keyList = newConfig.getKeyBinds().get(key.getTranslationKey()); - if (keyList.size() == 1) { - key.setBoundKey(InputUtil.fromTranslationKey(keyList.get(0))); - } else if (keyList.size() > 1) { - SortedMap map = new TreeMap<>(); - for (String string : keyList) { - map.put(InputUtil.fromTranslationKey(string), false); + Either> keyEither = newConfig.getKeyBinds().get(key.getTranslationKey()); + keyEither.ifLeft(singleKey -> { + key.setBoundKey(InputUtil.fromTranslationKey(singleKey)); + }); + keyEither.ifRight(keyList -> { + switch (keyList.size()) { + case 0 -> { + key.setBoundKey(InputUtil.UNKNOWN_KEY); } + case 1 -> { + key.setBoundKey(InputUtil.fromTranslationKey(keyList.get(0))); + } + default -> { + SortedMap map = new TreeMap<>(); + for (String string : keyList) { + map.put(InputUtil.fromTranslationKey(string), false); + } - key.setBoundChord(new KeyChord(map)); - } + key.setBoundChord(new KeyChord(map)); + } + }; + }); } }; CONFIG.setKeyBinds(newConfig.getKeyBinds()); @@ -120,18 +132,35 @@ public static void saveModConfig() { } public static void populateConfig() { - Map> keyBindMap = new HashMap<>(); + Map>> keyBindMap = new HashMap<>(); KeyBindRegistryImpl.getAllKeyBinds(true).forEach((keyBind, disabled) -> { if (keyBind.getBoundChord() != null) { - List list = new ArrayList<>(); - for (InputUtil.Key key : keyBind.getBoundChord().keys.keySet()) { - list.add(key.getTranslationKey()); + Either> either = switch (keyBind.getBoundChord().keys.size()) { + case 0 -> { + yield Either.right(List.of()); + } + case 1 -> { + InputUtil.Key key = keyBind.getBoundChord().keys.firstKey(); + yield Either.left(key.getTranslationKey()); } + default -> { + List list = new ArrayList<>(); + for (InputUtil.Key key : keyBind.getBoundChord().keys.keySet()) { + list.add(key.getTranslationKey()); + } + + yield Either.right(list); + } }; - keyBindMap.put(keyBind.getTranslationKey(), list); + keyBindMap.put(keyBind.getTranslationKey(), either); } else { - keyBindMap.put(keyBind.getTranslationKey(), List.of(keyBind.getKeyTranslationKey())); + // TODO - Rushed in order to debug issue; Clean this up! + if (keyBind.getKeyTranslationKey().equals("key.keyboard.unknown")) { + keyBindMap.put(keyBind.getTranslationKey(), Either.right(List.of())); + } else { + keyBindMap.put(keyBind.getTranslationKey(), Either.left(keyBind.getKeyTranslationKey())); + } } /* diff --git a/library/gui/key_binds/src/main/resources/fabric.mod.json b/library/gui/key_binds/src/main/resources/fabric.mod.json deleted file mode 100644 index 94d283a721..0000000000 --- a/library/gui/key_binds/src/main/resources/fabric.mod.json +++ /dev/null @@ -1,48 +0,0 @@ -{ - "schemaVersion": 1, - "id": "quilt_key_binds", - "name": "Quilt Key Binds API", - "version": "${version}", - "environment": "client", - "license": "Apache-2.0", - "icon": "assets/quilt_key_binds/icon.png", - "contact": { - "homepage": "https://quiltmc.org", - "issues": "https://github.com/QuiltMC/quilt-standard-libraries/issues", - "sources": "https://github.com/QuiltMC/quilt-standard-libraries" - }, - "authors": [ - "QuiltMC" - ], - "depends": { - "minecraft": ">=1.18.2", - "quilt_base": "*", - "quilt_resource_loader": "*", - "fabricloader": ">=0.12" - }, - "description": "Key binds registration and utilities.", - "mixins": [ - "quilt_key_binds.mixins.json" - ], - "custom": { - "loom:injected_interfaces": { - "net/minecraft/class_304": [ - "org/quiltmc/qsl/key/binds/api/ChordedKeyBind" - ] - }, - "modmenu": { - "badges": [ - "library" - ], - "parent": { - "id": "qsl", - "name": "Quilt Standard Libraries", - "description": "A set of libraries to assist in making Quilt mods.", - "icon": "assets/quilt_item_group/icon.png", - "badges": [ - "library" - ] - } - } - } -} diff --git a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java index 3ba8c4b37a..b53596d3f2 100644 --- a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java +++ b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java @@ -18,7 +18,6 @@ import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,19 +36,18 @@ public class KeyBindRegistryTestMod implements ClientLifecycleEvents.Ready { public void readyClient(MinecraftClient client) { KeyBind enableKeyBindKey = KeyBindRegistry.getKeyBind("key.qsl.enable_key_bind"); - if (enableKeyBindKey != null) { - LOGGER.info("Successfully got the \"Enable Key Bind\" key!"); - - ClientTickEvents.END.register(tickClient -> { - if (enableKeyBindKey.wasPressed()) { - LOGGER.info("I can add behavior to other keys!"); - } - }); - } - - LOGGER.info("The registry has the following keys registered:"); - KeyBindRegistry.getAllKeyBinds(true).forEach((key, value) -> { - LOGGER.info(String.format("%s: %s", key.getTranslationKey(), value)); + if (enableKeyBindKey != null) { + LOGGER.info("Successfully got the \"Enable Key Bind\" key!"); + ClientTickEvents.END.register(tickClient -> { + if (enableKeyBindKey.wasPressed()) { + LOGGER.info("I can add behavior to other keys!"); + } }); + } + + LOGGER.info("The registry has the following keys registered:"); + KeyBindRegistry.getAllKeyBinds(true).forEach((key, value) -> { + LOGGER.info(String.format("%s: %s", key.getTranslationKey(), value)); + }); } } diff --git a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java index 452b0e7567..cae4a99cd0 100644 --- a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java +++ b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java @@ -25,7 +25,6 @@ import net.minecraft.client.option.KeyBind; import net.minecraft.sound.SoundEvents; import net.minecraft.text.LiteralText; -import net.minecraft.util.Hand; import org.quiltmc.qsl.key.binds.api.KeyBindRegistry; import org.quiltmc.qsl.lifecycle.api.client.event.ClientTickEvents; diff --git a/library/gui/key_binds/src/testmod/resources/fabric.mod.json b/library/gui/key_binds/src/testmod/resources/fabric.mod.json deleted file mode 100644 index ecbfbaa899..0000000000 --- a/library/gui/key_binds/src/testmod/resources/fabric.mod.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "schemaVersion": 1, - "id": "quilt_key_binds_testmod", - "name": "Quilt Key Binds API Test Mod", - "version": "1.0.0", - "environment": "client", - "license": "Apache-2.0", - "depends": { - "quilt_key_binds": "*" - }, - "entrypoints": { - "client_events": [ - "org.quiltmc.qsl.key.binds.test.client.KeyBindRegistryTestMod", - "org.quiltmc.qsl.key.binds.test.client.KeyBindsTestMod" - ] - } -} diff --git a/library/gui/key_binds/src/testmod/resources/quilt.mod.json b/library/gui/key_binds/src/testmod/resources/quilt.mod.json new file mode 100644 index 0000000000..1f62d66ce1 --- /dev/null +++ b/library/gui/key_binds/src/testmod/resources/quilt.mod.json @@ -0,0 +1,25 @@ +{ + "schema_version": 1, + "quilt_loader": { + "group": "org.quiltmc", + "id": "quilt_key_binds_testmod", + "version": "1.0.0", + "intermediate_mappings": "net.fabricmc:intermediary", + "depends": [ + "quilt_key_binds" + ], + "entrypoints": { + "client_events": [ + "org.quiltmc.qsl.key.binds.test.client.KeyBindRegistryTestMod", + "org.quiltmc.qsl.key.binds.test.client.KeyBindsTestMod" + ] + }, + "metadata": { + "name": "Quilt Key Binds API Test Mod", + "license": "Apache-2.0" + } + }, + "minecraft": { + "environment": "client" + } +} From 6b19c807abbeff3bfdf1163166d080834f72eb12 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Sun, 17 Apr 2022 01:04:16 -0300 Subject: [PATCH 46/56] Push changes done to config I have a plan to properly fix it, but now, it's icon time --- .../key/binds/impl/KeyBindRegistryImpl.java | 4 +-- .../config/QuiltKeyBindsConfigManager.java | 28 ++++++------------- .../client/chords/KeyBindsScreenMixin.java | 4 +-- .../mixin/client/config/GameOptionsMixin.java | 2 +- 4 files changed, 13 insertions(+), 25 deletions(-) diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java index 464e4570da..89e3a534d7 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java @@ -140,9 +140,7 @@ public static void applyChanges() { keyBindManager.addModdedKeyBinds(); } - // TODO - Perhaps this should be executed somewhere else? - QuiltKeyBindsConfigManager.populateConfig(); - QuiltKeyBindsConfigManager.saveModConfig(); + QuiltKeyBindsConfigManager.saveConfig(); } public static KeyBind[] getKeyBinds() { diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java index d9b25b4134..6314e31c22 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java @@ -45,6 +45,7 @@ import org.quiltmc.loader.api.QuiltLoader; import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; import org.quiltmc.qsl.key.binds.impl.chords.KeyChord; +import org.quiltmc.qsl.key.binds.mixin.client.KeyBindAccessor; // TODO - Understand this mess that you have written down public class QuiltKeyBindsConfigManager { @@ -64,7 +65,7 @@ public static void loadConfig() { if (result.isPresent()) { registerConfigChanges(result.get()); populateConfig(); - saveModConfig(); + saveConfig(); isConfigLoaded = Optional.of(true); } else { isConfigLoaded = Optional.of(false); @@ -82,7 +83,7 @@ public static void loadConfig() { } populateConfig(); - saveModConfig(); + saveConfig(); } } @@ -117,7 +118,7 @@ public static void registerConfigChanges(QuiltKeyBindsConfig newConfig) { CONFIG.setKeyBinds(newConfig.getKeyBinds()); } - public static void saveModConfig() { + public static void saveConfig() { var result = QuiltKeyBindsConfig.CODEC.encodeStart(JsonOps.INSTANCE, CONFIG).result(); if (result.isPresent()) { try { @@ -155,23 +156,12 @@ public static void populateConfig() { keyBindMap.put(keyBind.getTranslationKey(), either); } else { - // TODO - Rushed in order to debug issue; Clean this up! - if (keyBind.getKeyTranslationKey().equals("key.keyboard.unknown")) { - keyBindMap.put(keyBind.getTranslationKey(), Either.right(List.of())); - } else { - keyBindMap.put(keyBind.getTranslationKey(), Either.left(keyBind.getKeyTranslationKey())); - } - } - - /* - if (!CONFIG.getKeyBinds().containsKey(key.getTranslationKey())) { - keyBindMap.put(key.getTranslationKey(), List.of(key.getKeyTranslationKey())); - } else { - keyBindMap.put(key.getTranslationKey(), List.of(key.getKeyTranslationKey())); - //key.setBoundKey(InputUtil.fromTranslationKey(CONFIG.getKeyBinds().get(key.getTranslationKey()))); + Either> either = + ((KeyBindAccessor) keyBind).getBoundKey().equals(InputUtil.UNKNOWN_KEY) + ? Either.right(List.of()) + : Either.left(keyBind.getKeyTranslationKey()); + keyBindMap.put(keyBind.getTranslationKey(), either); } - - */ }); CONFIG.setKeyBinds(keyBindMap); } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java index 08c2e31322..426e6eae91 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java @@ -120,7 +120,7 @@ public boolean keyReleased(int keyCode, int scanCode, int modifiers) { this.focusedKey.setBoundChord(new KeyChord(map)); QuiltKeyBindsConfigManager.populateConfig(); - QuiltKeyBindsConfigManager.saveModConfig(); + QuiltKeyBindsConfigManager.saveConfig(); } quilt$focusedProtoChord.clear(); @@ -147,7 +147,7 @@ public boolean mouseReleased(double mouseX, double mouseY, int button) { this.focusedKey.setBoundChord(new KeyChord(map)); QuiltKeyBindsConfigManager.populateConfig(); - QuiltKeyBindsConfigManager.saveModConfig(); + QuiltKeyBindsConfigManager.saveConfig(); } quilt$focusedProtoChord.clear(); diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/GameOptionsMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/GameOptionsMixin.java index 89f9a89920..e8e6f5217a 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/GameOptionsMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/GameOptionsMixin.java @@ -39,7 +39,7 @@ public abstract class GameOptionsMixin { ) private void writeToKeyBindConfig(KeyBind key, InputUtil.Key code, CallbackInfo ci) { QuiltKeyBindsConfigManager.populateConfig(); - QuiltKeyBindsConfigManager.saveModConfig(); + QuiltKeyBindsConfigManager.saveConfig(); } @Redirect( From a48e4d1a4d9e23f61995d278babaab830d3ebbb2 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Wed, 27 Apr 2022 19:49:01 -0300 Subject: [PATCH 47/56] Fix stuff no thoughts, head empty --- library/gui/key_binds/build.gradle | 1 - .../main/java/org/quiltmc/qsl/key/binds/api/ChordedKeyBind.java | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/library/gui/key_binds/build.gradle b/library/gui/key_binds/build.gradle index d50817ea79..ec72da9fbc 100644 --- a/library/gui/key_binds/build.gradle +++ b/library/gui/key_binds/build.gradle @@ -7,7 +7,6 @@ qslModule { moduleName = "key_binds" id = "quilt_key_binds" description = "Key binds registration and utilities." - version = "1.0.0" library = "gui" moduleDependencies { core { diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ChordedKeyBind.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ChordedKeyBind.java index a3981b8ec7..577e55c94d 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ChordedKeyBind.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ChordedKeyBind.java @@ -20,8 +20,10 @@ import net.minecraft.client.option.KeyBind; +import org.quiltmc.qsl.base.api.util.InjectedInterface; import org.quiltmc.qsl.key.binds.impl.chords.KeyChord; +@InjectedInterface(KeyBind.class) public interface ChordedKeyBind { default KeyChord getBoundChord() { System.out.println("This should never happen"); From 110d0c6496c028eb5e88db67a40e376f218d5503 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Thu, 28 Apr 2022 22:23:43 -0300 Subject: [PATCH 48/56] Begin to overhaul toggling Also, begin to implement unused_key_binds --- library/gui/key_binds/build.gradle | 8 +- .../qsl/key/binds/api/ChordedKeyBind.java | 4 + .../qsl/key/binds/api/KeyBindRegistry.java | 58 +++----------- .../qsl/key/binds/api/ToggleableKeyBind.java | 52 +++++++++++++ .../key/binds/impl/KeyBindRegistryImpl.java | 73 +++++------------- .../impl/config/QuiltKeyBindsConfig.java | 21 ++++- .../config/QuiltKeyBindsConfigManager.java | 6 +- .../mixin/client/chords/KeyBindMixin.java | 2 +- .../mixin/client/toggle/KeyBindMixin.java | 77 +++++++++++++++++++ .../resources/quilt_key_binds.mixins.json | 1 + .../test/client/KeyBindRegistryTestMod.java | 6 +- .../binds/test/client/KeyBindsTestMod.java | 60 ++++++++------- .../src/testmod/resources/quilt.mod.json | 1 + 13 files changed, 230 insertions(+), 139 deletions(-) create mode 100644 library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ToggleableKeyBind.java create mode 100644 library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/toggle/KeyBindMixin.java diff --git a/library/gui/key_binds/build.gradle b/library/gui/key_binds/build.gradle index ec72da9fbc..c21c5e5a73 100644 --- a/library/gui/key_binds/build.gradle +++ b/library/gui/key_binds/build.gradle @@ -10,12 +10,16 @@ qslModule { library = "gui" moduleDependencies { core { - api("qsl_base") api("resource_loader") + impl("qsl_base") testmodOnly("lifecycle_events") } } injectedInterface("net/minecraft/class_304") { - values = ["org/quiltmc/qsl/key/binds/api/ChordedKeyBind"] + values = [ + "org/quiltmc/qsl/key/binds/api/ChordedKeyBind", + "org/quiltmc/qsl/key/binds/api/ToggleableKeyBind" + ] } + clientOnly() } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ChordedKeyBind.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ChordedKeyBind.java index 577e55c94d..9f245e351c 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ChordedKeyBind.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ChordedKeyBind.java @@ -17,12 +17,16 @@ package org.quiltmc.qsl.key.binds.api; import com.mojang.blaze3d.platform.InputUtil; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; import net.minecraft.client.option.KeyBind; import org.quiltmc.qsl.base.api.util.InjectedInterface; import org.quiltmc.qsl.key.binds.impl.chords.KeyChord; +// TODO - Add Javadocs +@Environment(EnvType.CLIENT) @InjectedInterface(KeyBind.class) public interface ChordedKeyBind { default KeyChord getBoundChord() { diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java index 5904ee4f5e..196bb750e4 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java @@ -16,7 +16,7 @@ package org.quiltmc.qsl.key.binds.api; -import java.util.Map; +import java.util.List; import com.mojang.blaze3d.platform.InputUtil; import net.fabricmc.api.EnvType; @@ -52,20 +52,14 @@ public class KeyBindRegistry { * @throws IllegalArgumentException if either {@code key} or it's translation key is already registered */ public static KeyBind registerKeyBind(KeyBind key) { - return registerKeyBind(key, true); + return KeyBindRegistryImpl.registerKeyBind(key); } - /** - * Registers a key bind with a specified initial state. - * - * @param key the key bind to be registered - * @param enabled the key bind's initial state - * @return the key bind if successfully registered, {@code null} otherwise - * @throws NullPointerException if {@code key} is null - * @throws IllegalArgumentException if either {@code key} or it's translation key is already registered - */ - public static KeyBind registerKeyBind(KeyBind key, boolean enabled) { - return KeyBindRegistryImpl.registerKeyBind(key, enabled); + // TODO - doc me! + public static void registerKeyBinds(KeyBind... keys) { + for (KeyBind key : keys) { + KeyBindRegistryImpl.registerKeyBind(key); + } } /** @@ -92,44 +86,12 @@ public static InputUtil.Key getBoundKey(KeyBind key) { } /** - * Gets the state of the key bind. - * - *

The state of a key bind determines whenever it's disabled or not. - * - *

When a key bind is disabled, it is effectively hidden from the game, - * being non-existent to it. options.txt is the only exception, who still - * stores the disabled key binds. - * - * @param key the key bind - * @return {@code true} if the key bind is enabled, {@code false} otherwise - * @throws IllegalArgumentException if {@code key} is either unregistered or a Vanilla key bind - */ - public static boolean isEnabled(KeyBind key) { - return KeyBindRegistryImpl.isEnabled(key); - } - - /** - * Sets the state of the key bind. - * - *

This method allows for disabling the key bind, making it invisible to - * the game. options.txt is the only exception, who still stores the disabled - * key binds. - * - * @param key the key bind - * @param enabled the new state - * @throws IllegalArgumentException if {@code key} is either unregistered or a Vanilla key bind - */ - public static void setEnabled(KeyBind key, boolean enabled) { - KeyBindRegistryImpl.setEnabled(key, enabled); - } - - /** - * Returns a map containing all modded key binds (and vanilla ones if specified). + * Returns a list containing all modded key binds (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 binds + * @return a list containing all modded (and optionally vanilla) key binds */ - public static Map getAllKeyBinds(boolean includeVanilla) { + public static List getAllKeyBinds(boolean includeVanilla) { return KeyBindRegistryImpl.getAllKeyBinds(includeVanilla); } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ToggleableKeyBind.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ToggleableKeyBind.java new file mode 100644 index 0000000000..421ee97748 --- /dev/null +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ToggleableKeyBind.java @@ -0,0 +1,52 @@ +package org.quiltmc.qsl.key.binds.api; + +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; + +import net.minecraft.client.option.KeyBind; + +import org.quiltmc.qsl.base.api.util.InjectedInterface; + +// TODO - Add Javadocs; You can nab the ones from KeyBindRegistry +@Environment(EnvType.CLIENT) +@InjectedInterface(KeyBind.class) +public interface ToggleableKeyBind { + /** + * Gets whenever the key bind is enabled or not. + * + * @return {@code true} if the key bind is enabled, {@code false} otherwise + */ + default boolean isEnabled() { + return true; + } + + /** + * Gets whenever the key bind is disabled or not. + * + * @return {@code true} if the key bind is disabled, {@code false} otherwise + */ + default boolean isDisabled() { + return false; + } + + /** + * Enables the key bind. + * + *

If the key bind has been disabled more than once, this method will only + * decrement its internal counter instead of enabling the key bind. + */ + default void enable() { } + + /** + * Disables the key bind. + * + *

When a key bind is disabled, it is effectively hidden from the game, + * being non-existent to it. config/quilt/key_binds.json, however, will + * still remember the key bind's bound keys, similar to non-existent key binds. + * + *

If the key bind is disabled while already disabled, it will be increment + * an internal counter, making the next enable only decrement it instead of + * enabling the key bind. + */ + default void disable() { } +} diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java index 89e3a534d7..0f2b4b16f2 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java @@ -18,9 +18,7 @@ import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.Objects; import org.apache.commons.lang3.ArrayUtils; @@ -33,40 +31,37 @@ import net.minecraft.client.option.KeyBind; import org.quiltmc.qsl.key.binds.impl.config.QuiltKeyBindsConfigManager; -import org.quiltmc.qsl.key.binds.mixin.client.KeyBindAccessor; @Environment(EnvType.CLIENT) @ApiStatus.Internal public class KeyBindRegistryImpl { public static final Logger LOGGER = LoggerFactory.getLogger("KeyBindRegistry"); - private static Map quiltKeys = new HashMap<>(); + // TODO - FastUtils + private static final List QUILT_KEY_BINDS = new ArrayList<>(); + private static final List DISABLED_KEYS = new ArrayList<>(0); private static KeyBind[] enabledQuiltKeysArray = new KeyBind[] {}; - private static List disabledQuiltKeys = new ArrayList<>(0); private static KeyBindManager keyBindManager = null; - public static KeyBind registerKeyBind(KeyBind key, boolean enabled) { + public static KeyBind registerKeyBind(KeyBind key) { Objects.requireNonNull(key, "Attempted to register a null key bind!"); - for (KeyBind otherKey : quiltKeys.keySet()) { - if (key == otherKey) { + for (KeyBind otherKey : QUILT_KEY_BINDS) { + if (key.equals(otherKey)) { throw new IllegalArgumentException(String.format("%s has already been registered!", key.getTranslationKey())); } else if (key.getTranslationKey().equals(otherKey.getTranslationKey())) { throw new IllegalArgumentException(String.format("Attempted to register {}, but a key bind with the same translation key has already been registered!", key.getTranslationKey())); } } - quiltKeys.put(key, enabled); + QUILT_KEY_BINDS.add(key); applyChanges(); - if (!enabled) { - KeyBindAccessor.getKeyBinds().remove(key.getTranslationKey()); - } return key; } public static KeyBind getKeyBind(String translationKey) { - for (KeyBind key : quiltKeys.keySet()) { + for (KeyBind key : QUILT_KEY_BINDS) { if (key.getTranslationKey().equals(translationKey)) { return key; } @@ -83,58 +78,30 @@ public static boolean throwUnregisteredKeyException(KeyBind key) { throw new IllegalArgumentException(String.format("%s isn't a registered key!", key.getTranslationKey())); } - public static boolean isEnabled(KeyBind key) { - if (quiltKeys.containsKey(key)) { - return quiltKeys.get(key); - } else { - return throwUnregisteredKeyException(key); - } - } - - public static void setEnabled(KeyBind key, boolean enabled) { - if (quiltKeys.containsKey(key)) { - quiltKeys.replace(key, enabled); - - applyChanges(); - if (enabled) { - KeyBindAccessor.getKeyBinds().put(key.getTranslationKey(), key); - } else { - KeyBindAccessor.getKeyBinds().remove(key.getTranslationKey(), key); - } - - ((KeyBindAccessor) key).callReset(); - KeyBind.updateBoundKeys(); - } else { - throwUnregisteredKeyException(key); - } - } - - public static Map getAllKeyBinds(boolean includeVanilla) { - Map allKeys = new HashMap<>(); + // TODO - includeVanilla is bad; Let's change it to something else + public static List getAllKeyBinds(boolean includeVanilla) { + List allKeys = new ArrayList<>(); if (includeVanilla) { for (int i = 0; i < keyBindManager.getAllKeys().length; i++) { - allKeys.put(keyBindManager.getAllKeys()[i], false); + allKeys.add(keyBindManager.getAllKeys()[i]); } } - allKeys.putAll(quiltKeys); + allKeys.addAll(QUILT_KEY_BINDS); return allKeys; } + // TODO - Wait a hecking minute, this isn't applying the changes as intended! public static void applyChanges() { - List enabledQuiltKeys = new ArrayList<>(); - disabledQuiltKeys.clear(); - for (var entry : quiltKeys.entrySet()) { - if (entry.getValue()) { - enabledQuiltKeys.add(entry.getKey()); - } else { - disabledQuiltKeys.add(entry.getKey()); - } + List enabledKeys = new ArrayList<>(); + DISABLED_KEYS.clear(); + for (KeyBind key : QUILT_KEY_BINDS) { + (key.isEnabled() ? enabledKeys : DISABLED_KEYS).add(key); } - enabledQuiltKeysArray = enabledQuiltKeys.toArray(new KeyBind[enabledQuiltKeys.size()]); + enabledQuiltKeysArray = enabledKeys.toArray(new KeyBind[enabledKeys.size()]); if (keyBindManager != null) { keyBindManager.addModdedKeyBinds(); @@ -148,7 +115,7 @@ public static KeyBind[] getKeyBinds() { } public static List getDisabledKeyBinds() { - return disabledQuiltKeys; + return DISABLED_KEYS; } public static void setKeyBindManager(KeyBindManager manager) { diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java index 9abacca914..9facf9632c 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java @@ -29,7 +29,8 @@ public class QuiltKeyBindsConfig { instance -> instance.group( // TODO - Implement me! Codec.BOOL.fieldOf("show_tutorial_toast").forGetter(QuiltKeyBindsConfig::getShowTutorialToast), - Codec.unboundedMap(Codec.STRING, Codec.either(Codec.STRING, Codec.list(Codec.STRING))).fieldOf("key_binds").forGetter(QuiltKeyBindsConfig::getKeyBinds) + Codec.unboundedMap(Codec.STRING, Codec.either(Codec.STRING, Codec.list(Codec.STRING))).fieldOf("key_binds").forGetter(QuiltKeyBindsConfig::getKeyBinds), + Codec.unboundedMap(Codec.STRING, Codec.either(Codec.STRING, Codec.list(Codec.STRING))).fieldOf("unused_key_binds").forGetter(QuiltKeyBindsConfig::getUnusedKeyBinds) ) .apply(instance, QuiltKeyBindsConfig::new) ); @@ -37,18 +38,22 @@ public class QuiltKeyBindsConfig { private boolean showTutorialToast; // TODO - This type is super long; Use an object for this private Map>> keyBinds; + private Map>> unusedKeyBinds; public QuiltKeyBindsConfig() { - this.keyBinds = new HashMap<>(); this.showTutorialToast = false; + this.keyBinds = new HashMap<>(); + this.unusedKeyBinds = new HashMap<>(); } public QuiltKeyBindsConfig( boolean showTutorialToast, - Map>> keyBinds + Map>> keyBinds, + Map>> unusedKeyBinds ) { this.showTutorialToast = showTutorialToast; this.keyBinds = keyBinds; + this.unusedKeyBinds = unusedKeyBinds; } public boolean getShowTutorialToast() { @@ -60,10 +65,18 @@ public void setShowTutorialToast(boolean showTutorialToast) { } public Map>> getKeyBinds() { - return keyBinds; + return this.keyBinds; } public void setKeyBinds(Map>> keyBinds) { this.keyBinds = keyBinds; } + + public Map>> getUnusedKeyBinds() { + return this.unusedKeyBinds; + } + + public void setUnusedKeyBinds(Map>> unusedKeyBinds) { + this.unusedKeyBinds = unusedKeyBinds; + } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java index 6314e31c22..ca2cba39e9 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java @@ -132,10 +132,11 @@ public static void saveConfig() { } } + // TODO - This is the ideal place to store the unused key binds; Do that! public static void populateConfig() { Map>> keyBindMap = new HashMap<>(); - KeyBindRegistryImpl.getAllKeyBinds(true).forEach((keyBind, disabled) -> { + for (KeyBind keyBind : KeyBindRegistryImpl.getAllKeyBinds(true)) { if (keyBind.getBoundChord() != null) { Either> either = switch (keyBind.getBoundChord().keys.size()) { case 0 -> { @@ -162,7 +163,8 @@ public static void populateConfig() { : Either.left(keyBind.getKeyTranslationKey()); keyBindMap.put(keyBind.getTranslationKey(), either); } - }); + } + CONFIG.setKeyBinds(keyBindMap); } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java index 4a44a7d296..ff49723f49 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java @@ -66,7 +66,7 @@ public class KeyBindMixin implements ChordedKeyBind { at = @At("RETURN"), method = "(Ljava/lang/String;Lcom/mojang/blaze3d/platform/InputUtil$Type;ILjava/lang/String;)V" ) - private void expandInit(String string, InputUtil.Type type, int i, String string2, CallbackInfo ci) { + private void initializeChordFields(String string, InputUtil.Type type, int i, String string2, CallbackInfo ci) { quilt$defaultChord = null; quilt$boundChord = null; } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/toggle/KeyBindMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/toggle/KeyBindMixin.java new file mode 100644 index 0000000000..5e0359d4be --- /dev/null +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/toggle/KeyBindMixin.java @@ -0,0 +1,77 @@ +package org.quiltmc.qsl.key.binds.mixin.client.toggle; + +import java.util.Map; + +import com.mojang.blaze3d.platform.InputUtil; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.client.option.KeyBind; + +import org.quiltmc.qsl.key.binds.api.ToggleableKeyBind; +import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; + +@Mixin(KeyBind.class) +public abstract class KeyBindMixin implements ToggleableKeyBind { + @Shadow + @Final + private static Map KEY_BINDS; + + @Unique + private int quilt$disableCounter; + + @Shadow + public abstract java.lang.String getTranslationKey(); + + @Shadow + abstract void reset(); + + @Inject( + at = @At("RETURN"), + method = "(Ljava/lang/String;Lcom/mojang/blaze3d/platform/InputUtil$Type;ILjava/lang/String;)V" + ) + private void initializeToggleFields(String string, InputUtil.Type type, int i, String string2, CallbackInfo ci) { + quilt$disableCounter = 0; + } + + @Override + public boolean isEnabled() { + return quilt$disableCounter == 0; + } + + @Override + public boolean isDisabled() { + return quilt$disableCounter > 0; + } + + @Override + public void enable() { + // Hahahahaha no. + if (quilt$disableCounter <= 0) return; + + quilt$disableCounter--; + if (quilt$disableCounter == 0) { + KeyBindRegistryImpl.applyChanges(); + KEY_BINDS.put(this.getTranslationKey(), (KeyBind) (Object) this); + this.reset(); + KeyBind.updateBoundKeys(); + } + } + + @Override + public void disable() { + if (quilt$disableCounter == 0) { + KeyBindRegistryImpl.applyChanges(); + KEY_BINDS.remove(this.getTranslationKey(), (KeyBind) (Object) this); + this.reset(); + KeyBind.updateBoundKeys(); + } + + quilt$disableCounter++; + } +} diff --git a/library/gui/key_binds/src/main/resources/quilt_key_binds.mixins.json b/library/gui/key_binds/src/main/resources/quilt_key_binds.mixins.json index 0a3d6bb91e..2b5e8fde51 100644 --- a/library/gui/key_binds/src/main/resources/quilt_key_binds.mixins.json +++ b/library/gui/key_binds/src/main/resources/quilt_key_binds.mixins.json @@ -9,6 +9,7 @@ "client.chords.KeyBindsScreenMixin", "client.config.GameOptionsMixin", "client.config.MinecraftClientMixin", + "client.toggle.KeyBindMixin", "client.EntryListWidgetAccessor", "client.GameOptionsAccessor", "client.GameOptionsMixin", diff --git a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java index b53596d3f2..dc9e005554 100644 --- a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java +++ b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java @@ -46,8 +46,8 @@ public void readyClient(MinecraftClient client) { } LOGGER.info("The registry has the following keys registered:"); - KeyBindRegistry.getAllKeyBinds(true).forEach((key, value) -> { - LOGGER.info(String.format("%s: %s", key.getTranslationKey(), value)); - }); + for (KeyBind key : KeyBindRegistry.getAllKeyBinds(true)) { + LOGGER.info(String.format("Found key %s!", key.getTranslationKey())); + } } } diff --git a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java index cae4a99cd0..c295346902 100644 --- a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java +++ b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java @@ -26,56 +26,64 @@ import net.minecraft.sound.SoundEvents; import net.minecraft.text.LiteralText; +import org.quiltmc.loader.api.ModContainer; +import org.quiltmc.qsl.base.api.entrypoint.client.ClientModInitializer; import org.quiltmc.qsl.key.binds.api.KeyBindRegistry; import org.quiltmc.qsl.lifecycle.api.client.event.ClientTickEvents; @Environment(EnvType.CLIENT) -public class KeyBindsTestMod implements ClientTickEvents.Start { +public class KeyBindsTestMod implements ClientModInitializer, ClientTickEvents.Start { public static final String KEY_CATEGORY = "key.qsl.category"; // A conflicting key test - public static final KeyBind CONFLICT_TEST_KEY = KeyBindRegistry.registerKeyBind( - new KeyBind("key.qsl.conflict_test", GLFW.GLFW_KEY_H, KEY_CATEGORY) - ); + public static final KeyBind CONFLICT_TEST_KEY = new KeyBind("key.qsl.conflict_test", GLFW.GLFW_KEY_H, KEY_CATEGORY); - public static final KeyBind DISABLE_KEY_BIND_KEY = KeyBindRegistry.registerKeyBind( - new KeyBind("key.qsl.disable_key_bind", GLFW.GLFW_KEY_H, KEY_CATEGORY), true - ); + public static final KeyBind DISABLE_KEY_BIND_KEY = new KeyBind("key.qsl.disable_key_bind", GLFW.GLFW_KEY_H, KEY_CATEGORY); - public static final KeyBind ENABLE_KEY_BIND_KEY = KeyBindRegistry.registerKeyBind( - new KeyBind("key.qsl.enable_key_bind", GLFW.GLFW_KEY_I, KEY_CATEGORY), true - ); + public static final KeyBind ENABLE_KEY_BIND_KEY = new KeyBind("key.qsl.enable_key_bind", GLFW.GLFW_KEY_I, KEY_CATEGORY); - public static final KeyBind DISABLED_CONFLICT_TEST_KEY = KeyBindRegistry.registerKeyBind( - new KeyBind("key.qsl.disabled_conflict_test", GLFW.GLFW_KEY_H, KEY_CATEGORY), false - ); + public static final KeyBind DISABLED_CONFLICT_TEST_KEY = new KeyBind("key.qsl.disabled_conflict_test", GLFW.GLFW_KEY_H, KEY_CATEGORY); - public static final KeyBind CLAP_KEY = KeyBindRegistry.registerKeyBind( - new KeyBind("key.qsl.clap", InputUtil.UNKNOWN_KEY.getKeyCode(), KEY_CATEGORY).withChord( - InputUtil.Type.MOUSE.createFromKeyCode(GLFW.GLFW_MOUSE_BUTTON_LEFT), - InputUtil.Type.MOUSE.createFromKeyCode(GLFW.GLFW_MOUSE_BUTTON_RIGHT) - ) + public static final KeyBind CLAP_KEY = new KeyBind("key.qsl.clap", InputUtil.UNKNOWN_KEY.getKeyCode(), KEY_CATEGORY).withChord( + InputUtil.Type.MOUSE.createFromKeyCode(GLFW.GLFW_MOUSE_BUTTON_LEFT), + InputUtil.Type.MOUSE.createFromKeyCode(GLFW.GLFW_MOUSE_BUTTON_RIGHT) ); + @Override + public void onInitializeClient(ModContainer mod) { + DISABLED_CONFLICT_TEST_KEY.disable(); + + KeyBindRegistry.registerKeyBinds( + CONFLICT_TEST_KEY, + DISABLE_KEY_BIND_KEY, + ENABLE_KEY_BIND_KEY, + DISABLED_CONFLICT_TEST_KEY, + CLAP_KEY + ); + } + + // TODO - Add a test for toggling vanilla key binds; They probably don't work right now, but actually seeing that in action would help a lot @Override public void startClientTick(MinecraftClient client) { if (client.player == null) return; - if (DISABLE_KEY_BIND_KEY.isPressed()) { - client.player.sendMessage(new LiteralText("The key has disappeared! Bye bye, key!"), true); - KeyBindRegistry.setEnabled(DISABLE_KEY_BIND_KEY, false); + if (DISABLE_KEY_BIND_KEY.wasPressed()) { + client.player.sendMessage(new LiteralText("The clap key has disappeared!"), true); + CLAP_KEY.disable(); + client.player.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1.5F, 1.5F); } - if (ENABLE_KEY_BIND_KEY.isPressed()) { - client.player.sendMessage(new LiteralText("The key is back!"), true); - KeyBindRegistry.setEnabled(DISABLE_KEY_BIND_KEY, true); + if (ENABLE_KEY_BIND_KEY.wasPressed()) { + client.player.sendMessage(new LiteralText("The clap key is back!"), true); + CLAP_KEY.enable(); + client.player.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1.5F, 1.5F); } - if (CONFLICT_TEST_KEY.isPressed()) { + if (CONFLICT_TEST_KEY.wasPressed()) { client.player.sendMessage(new LiteralText("This is the conflict key being pressed"), false); } - if (DISABLED_CONFLICT_TEST_KEY.isPressed()) { + if (DISABLED_CONFLICT_TEST_KEY.wasPressed()) { client.player.sendMessage(new LiteralText("I'm not supposed to do things! Why am I enabled?"), false); } diff --git a/library/gui/key_binds/src/testmod/resources/quilt.mod.json b/library/gui/key_binds/src/testmod/resources/quilt.mod.json index 1f62d66ce1..4b7f220f1b 100644 --- a/library/gui/key_binds/src/testmod/resources/quilt.mod.json +++ b/library/gui/key_binds/src/testmod/resources/quilt.mod.json @@ -9,6 +9,7 @@ "quilt_key_binds" ], "entrypoints": { + "client_init": "org.quiltmc.qsl.key.binds.test.client.KeyBindsTestMod", "client_events": [ "org.quiltmc.qsl.key.binds.test.client.KeyBindRegistryTestMod", "org.quiltmc.qsl.key.binds.test.client.KeyBindsTestMod" From 841bdec7d3123fc6e8c2316fedf88f6425a8c710 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Sun, 1 May 2022 20:25:42 -0300 Subject: [PATCH 49/56] Recalculating mind's currently empty, but i'll find a better way to do things --- .../key/binds/impl/KeyBindRegistryImpl.java | 26 +++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java index 0f2b4b16f2..4fec50fc41 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java @@ -17,11 +17,9 @@ package org.quiltmc.qsl.key.binds.impl; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Objects; -import org.apache.commons.lang3.ArrayUtils; import org.jetbrains.annotations.ApiStatus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,9 +36,10 @@ public class KeyBindRegistryImpl { public static final Logger LOGGER = LoggerFactory.getLogger("KeyBindRegistry"); // TODO - FastUtils + // TODO - Wait a hecking minute; What's the point of QUILT_KEY_BINDS now? private static final List QUILT_KEY_BINDS = new ArrayList<>(); private static final List DISABLED_KEYS = new ArrayList<>(0); - private static KeyBind[] enabledQuiltKeysArray = new KeyBind[] {}; + private static KeyBind[] enabledKeysArray = new KeyBind[] {}; private static KeyBindManager keyBindManager = null; public static KeyBind registerKeyBind(KeyBind key) { @@ -70,21 +69,13 @@ public static KeyBind getKeyBind(String translationKey) { return null; } - public static boolean throwUnregisteredKeyException(KeyBind key) { - if (keyBindManager != null && Arrays.asList(keyBindManager.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())); - } - // TODO - includeVanilla is bad; Let's change it to something else public static List getAllKeyBinds(boolean includeVanilla) { List allKeys = new ArrayList<>(); if (includeVanilla) { - for (int i = 0; i < keyBindManager.getAllKeys().length; i++) { - allKeys.add(keyBindManager.getAllKeys()[i]); + for (KeyBind keyBind : keyBindManager.getAllKeys()) { + allKeys.add(keyBind); } } @@ -93,15 +84,18 @@ public static List getAllKeyBinds(boolean includeVanilla) { return allKeys; } - // TODO - Wait a hecking minute, this isn't applying the changes as intended! public static void applyChanges() { List enabledKeys = new ArrayList<>(); DISABLED_KEYS.clear(); + for (KeyBind key : keyBindManager.getAllKeys()) { + (key.isEnabled() ? enabledKeys : DISABLED_KEYS).add(key); + } + for (KeyBind key : QUILT_KEY_BINDS) { (key.isEnabled() ? enabledKeys : DISABLED_KEYS).add(key); } - enabledQuiltKeysArray = enabledKeys.toArray(new KeyBind[enabledKeys.size()]); + enabledKeysArray = enabledKeys.toArray(new KeyBind[enabledKeys.size()]); if (keyBindManager != null) { keyBindManager.addModdedKeyBinds(); @@ -111,7 +105,7 @@ public static void applyChanges() { } public static KeyBind[] getKeyBinds() { - return ArrayUtils.addAll(keyBindManager.getAllKeys(), enabledQuiltKeysArray); + return enabledKeysArray; } public static List getDisabledKeyBinds() { From 3b9b4254b7d6074547bb73793d34317f1e56862d Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Sun, 1 May 2022 22:36:46 -0300 Subject: [PATCH 50/56] Refactor the key bind registry It's gone! --- library/gui/key_binds/build.gradle | 3 +- .../qsl/key/binds/api/KeyBindRegistry.java | 39 ++--------- .../qsl/key/binds/api/QuiltKeyBind.java | 13 ++++ .../key/binds/impl/InternalQuiltKeyBind.java | 5 ++ .../key/binds/impl/KeyBindRegistryImpl.java | 70 +++++-------------- .../config/QuiltKeyBindsConfigManager.java | 17 +++-- .../binds/mixin/client/GameOptionsMixin.java | 6 ++ .../key/binds/mixin/client/KeyBindMixin.java | 23 +++++- .../mixin/client/toggle/KeyBindMixin.java | 34 +++++---- .../test/client/KeyBindRegistryTestMod.java | 4 +- .../binds/test/client/KeyBindsTestMod.java | 13 +--- 11 files changed, 107 insertions(+), 120 deletions(-) create mode 100644 library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/QuiltKeyBind.java create mode 100644 library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/InternalQuiltKeyBind.java diff --git a/library/gui/key_binds/build.gradle b/library/gui/key_binds/build.gradle index c21c5e5a73..3a93c984a3 100644 --- a/library/gui/key_binds/build.gradle +++ b/library/gui/key_binds/build.gradle @@ -18,7 +18,8 @@ qslModule { injectedInterface("net/minecraft/class_304") { values = [ "org/quiltmc/qsl/key/binds/api/ChordedKeyBind", - "org/quiltmc/qsl/key/binds/api/ToggleableKeyBind" + "org/quiltmc/qsl/key/binds/api/ToggleableKeyBind", + "org/quiltmc/qsl/key/binds/api/QuiltKeyBind" ] } clientOnly() diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java index 196bb750e4..def5dac861 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java @@ -27,6 +27,7 @@ import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; import org.quiltmc.qsl.key.binds.mixin.client.KeyBindAccessor; +// TODO - This isn't a registry anymore; What the heck should it be named? /** * Handles the registration of modded key binds and allows for changing their properties, * such as their state. @@ -35,33 +36,7 @@ */ @Environment(EnvType.CLIENT) public class KeyBindRegistry { - /** - * Registers a key bind, with it being initially enabled. - * - *

-	 * {@code
-	 * public static final KeyBind EXAMPLE_KEY_BIND = KeyBindRegistry.registerKeyBind(
-	 * 	new KeyBind("key.modid.example", GLFW.GLFW_KEY_M, "key.modid.category")
-	 * )
-	 * }
-	 * 
- * - * @param key the key bind to be registered - * @return the successfully-registered key bind - * @throws NullPointerException if {@code key} is null - * @throws IllegalArgumentException if either {@code key} or it's translation key is already registered - */ - public static KeyBind registerKeyBind(KeyBind key) { - return KeyBindRegistryImpl.registerKeyBind(key); - } - - // TODO - doc me! - public static void registerKeyBinds(KeyBind... keys) { - for (KeyBind key : keys) { - KeyBindRegistryImpl.registerKeyBind(key); - } - } - + // TODO - Shouldn't we use Vanilla's Map for getAllKeyBinds instead? Two birds, one stone, zero thoughts, head empty /** * Searches for a modded key bind with the specified translation key in the registry. * @@ -72,6 +47,7 @@ public static KeyBind getKeyBind(String translationKey) { return KeyBindRegistryImpl.getKeyBind(translationKey); } + // TODO - Would a transitive AW be safe for getBoundKey? /** * Gets the bound key of the key bind. * @@ -86,12 +62,11 @@ public static InputUtil.Key getBoundKey(KeyBind key) { } /** - * Returns a list containing all modded key binds (and vanilla ones if specified). + * Returns a list containing all key binds. * - * @param includeVanilla {@code true} if vanilla entries should be included, else {@code false} - * @return a list containing all modded (and optionally vanilla) key binds + * @return a list containing key binds */ - public static List getAllKeyBinds(boolean includeVanilla) { - return KeyBindRegistryImpl.getAllKeyBinds(includeVanilla); + public static List getAllKeyBinds() { + return KeyBindRegistryImpl.getAllKeyBinds(); } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/QuiltKeyBind.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/QuiltKeyBind.java new file mode 100644 index 0000000000..c63e853b46 --- /dev/null +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/QuiltKeyBind.java @@ -0,0 +1,13 @@ +package org.quiltmc.qsl.key.binds.api; + +public interface QuiltKeyBind { + /** + * Gets whenever the key bind is from Vanilla or not. + * This is automatically determined by using GameOptions' allKeys property. + * + * @return {@code true} if the key bind is from Vanilla, {@code false} otherwise + */ + default boolean isVanilla() { + return false; + } +} diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/InternalQuiltKeyBind.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/InternalQuiltKeyBind.java new file mode 100644 index 0000000000..d1b7f8e1ce --- /dev/null +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/InternalQuiltKeyBind.java @@ -0,0 +1,5 @@ +package org.quiltmc.qsl.key.binds.impl; + +public interface InternalQuiltKeyBind { + void markAsVanilla(); +} diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java index 4fec50fc41..e5d09df070 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java @@ -16,13 +16,13 @@ package org.quiltmc.qsl.key.binds.impl; -import java.util.ArrayList; import java.util.List; -import java.util.Objects; import org.jetbrains.annotations.ApiStatus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + +import it.unimi.dsi.fastutil.objects.ReferenceArrayList; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; @@ -35,32 +35,12 @@ public class KeyBindRegistryImpl { public static final Logger LOGGER = LoggerFactory.getLogger("KeyBindRegistry"); - // TODO - FastUtils - // TODO - Wait a hecking minute; What's the point of QUILT_KEY_BINDS now? - private static final List QUILT_KEY_BINDS = new ArrayList<>(); - private static final List DISABLED_KEYS = new ArrayList<>(0); - private static KeyBind[] enabledKeysArray = new KeyBind[] {}; + private static final List ALL_KEY_BINDS = new ReferenceArrayList<>(); + private static final List ENABLED_KEYS = new ReferenceArrayList<>(); private static KeyBindManager keyBindManager = null; - public static KeyBind registerKeyBind(KeyBind key) { - Objects.requireNonNull(key, "Attempted to register a null key bind!"); - - for (KeyBind otherKey : QUILT_KEY_BINDS) { - if (key.equals(otherKey)) { - throw new IllegalArgumentException(String.format("%s has already been registered!", key.getTranslationKey())); - } else if (key.getTranslationKey().equals(otherKey.getTranslationKey())) { - throw new IllegalArgumentException(String.format("Attempted to register {}, but a key bind with the same translation key has already been registered!", key.getTranslationKey())); - } - } - - QUILT_KEY_BINDS.add(key); - applyChanges(); - - return key; - } - public static KeyBind getKeyBind(String translationKey) { - for (KeyBind key : QUILT_KEY_BINDS) { + for (KeyBind key : ALL_KEY_BINDS) { if (key.getTranslationKey().equals(translationKey)) { return key; } @@ -69,34 +49,22 @@ public static KeyBind getKeyBind(String translationKey) { return null; } - // TODO - includeVanilla is bad; Let's change it to something else - public static List getAllKeyBinds(boolean includeVanilla) { - List allKeys = new ArrayList<>(); - - if (includeVanilla) { - for (KeyBind keyBind : keyBindManager.getAllKeys()) { - allKeys.add(keyBind); - } - } - - allKeys.addAll(QUILT_KEY_BINDS); - - return allKeys; + public static List getAllKeyBinds() { + return ALL_KEY_BINDS; } - public static void applyChanges() { - List enabledKeys = new ArrayList<>(); - DISABLED_KEYS.clear(); - for (KeyBind key : keyBindManager.getAllKeys()) { - (key.isEnabled() ? enabledKeys : DISABLED_KEYS).add(key); - } + public static void registerKeyBind(KeyBind key) { + ALL_KEY_BINDS.add(key); + ENABLED_KEYS.add(key); + } - for (KeyBind key : QUILT_KEY_BINDS) { - (key.isEnabled() ? enabledKeys : DISABLED_KEYS).add(key); + public static void updateKeyBindState(KeyBind key) { + if (key.isEnabled()) { + ENABLED_KEYS.add(key); + } else { + ENABLED_KEYS.remove(key); } - enabledKeysArray = enabledKeys.toArray(new KeyBind[enabledKeys.size()]); - if (keyBindManager != null) { keyBindManager.addModdedKeyBinds(); } @@ -105,11 +73,7 @@ public static void applyChanges() { } public static KeyBind[] getKeyBinds() { - return enabledKeysArray; - } - - public static List getDisabledKeyBinds() { - return DISABLED_KEYS; + return ENABLED_KEYS.toArray(new KeyBind[ENABLED_KEYS.size()]); } public static void setKeyBindManager(KeyBindManager manager) { diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java index ca2cba39e9..47d262e560 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java @@ -47,7 +47,7 @@ import org.quiltmc.qsl.key.binds.impl.chords.KeyChord; import org.quiltmc.qsl.key.binds.mixin.client.KeyBindAccessor; -// TODO - Understand this mess that you have written down +// TODO - Massively simplify the config system public class QuiltKeyBindsConfigManager { public static Optional isConfigLoaded = Optional.empty(); public static final QuiltKeyBindsConfig CONFIG = new QuiltKeyBindsConfig(); @@ -87,6 +87,7 @@ public static void loadConfig() { } } + // TODO - Wait, why am I using two different config objects? Just use one! public static void registerConfigChanges(QuiltKeyBindsConfig newConfig) { MinecraftClient client = MinecraftClient.getInstance(); for (KeyBind key : client.options.allKeys) { @@ -115,7 +116,9 @@ public static void registerConfigChanges(QuiltKeyBindsConfig newConfig) { }); } }; + CONFIG.setShowTutorialToast(newConfig.getShowTutorialToast()); CONFIG.setKeyBinds(newConfig.getKeyBinds()); + CONFIG.setUnusedKeyBinds(newConfig.getUnusedKeyBinds()); } public static void saveConfig() { @@ -132,11 +135,11 @@ public static void saveConfig() { } } - // TODO - This is the ideal place to store the unused key binds; Do that! public static void populateConfig() { - Map>> keyBindMap = new HashMap<>(); + Map>> keyBindsMap = new HashMap<>(); - for (KeyBind keyBind : KeyBindRegistryImpl.getAllKeyBinds(true)) { + for (KeyBind keyBind : KeyBindRegistryImpl.getAllKeyBinds()) { + if (keyBind.isDisabled()) continue; if (keyBind.getBoundChord() != null) { Either> either = switch (keyBind.getBoundChord().keys.size()) { case 0 -> { @@ -155,16 +158,16 @@ public static void populateConfig() { yield Either.right(list); } }; - keyBindMap.put(keyBind.getTranslationKey(), either); + keyBindsMap.put(keyBind.getTranslationKey(), either); } else { Either> either = ((KeyBindAccessor) keyBind).getBoundKey().equals(InputUtil.UNKNOWN_KEY) ? Either.right(List.of()) : Either.left(keyBind.getKeyTranslationKey()); - keyBindMap.put(keyBind.getTranslationKey(), either); + keyBindsMap.put(keyBind.getTranslationKey(), either); } } - CONFIG.setKeyBinds(keyBindMap); + CONFIG.setKeyBinds(keyBindsMap); } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsMixin.java index aa8af1b83e..3f24896e90 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/GameOptionsMixin.java @@ -32,6 +32,7 @@ import net.minecraft.client.option.GameOptions; import net.minecraft.client.option.KeyBind; +import org.quiltmc.qsl.key.binds.impl.InternalQuiltKeyBind; import org.quiltmc.qsl.key.binds.impl.KeyBindManager; import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; @@ -56,6 +57,11 @@ public abstract class GameOptionsMixin { ) private void modifyAllKeys(MinecraftClient client, File file, CallbackInfo ci) { if (this.optionsFile.equals(new File(file, "options.txt"))) { + // Mark the Vanilla key binds as Vanilla + for (KeyBind key : this.allKeys) { + ((InternalQuiltKeyBind) key).markAsVanilla(); + } + KeyBindRegistryImpl.setKeyBindManager(new KeyBindManager((GameOptions) (Object) this, this.allKeys)); this.allKeys = KeyBindRegistryImpl.getKeyBinds(); } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindMixin.java index 79fe285bfb..998fa90aa1 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindMixin.java @@ -23,6 +23,7 @@ import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mutable; import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @@ -31,18 +32,36 @@ import net.minecraft.client.option.KeyBind; +import org.quiltmc.qsl.key.binds.api.QuiltKeyBind; +import org.quiltmc.qsl.key.binds.impl.InternalQuiltKeyBind; + @Environment(EnvType.CLIENT) @Mixin(KeyBind.class) -public abstract class KeyBindMixin { +public abstract class KeyBindMixin implements QuiltKeyBind, InternalQuiltKeyBind { @Shadow @Mutable @Final private static Map ORDER_BY_CATEGORIES; - @Inject(method = "(Ljava/lang/String;Lcom/mojang/blaze3d/platform/InputUtil$Type;ILjava/lang/String;)V", at = @At("TAIL")) + @Unique + private boolean quilt$vanilla; + + @Inject(method = "(Ljava/lang/String;Lcom/mojang/blaze3d/platform/InputUtil$Type;ILjava/lang/String;)V", at = @At("RETURN")) private void addModdedCategory(String translationKey, InputUtil.Type type, int keyCode, String category, CallbackInfo ci) { if (!ORDER_BY_CATEGORIES.containsKey(category)) { ORDER_BY_CATEGORIES.put(category, ORDER_BY_CATEGORIES.size() + 1); } + + this.quilt$vanilla = false; + } + + @Override + public boolean isVanilla() { + return this.quilt$vanilla; + } + + @Override + public void markAsVanilla() { + this.quilt$vanilla = true; } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/toggle/KeyBindMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/toggle/KeyBindMixin.java index 5e0359d4be..22e080e102 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/toggle/KeyBindMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/toggle/KeyBindMixin.java @@ -1,5 +1,7 @@ package org.quiltmc.qsl.key.binds.mixin.client.toggle; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import com.mojang.blaze3d.platform.InputUtil; @@ -22,20 +24,29 @@ public abstract class KeyBindMixin implements ToggleableKeyBind { @Final private static Map KEY_BINDS; + @Unique + private static final List QUILT$ALL_KEY_BINDS = new ArrayList<>(); + @Unique private int quilt$disableCounter; @Shadow - public abstract java.lang.String getTranslationKey(); + public abstract String getTranslationKey(); @Shadow abstract void reset(); - @Inject( - at = @At("RETURN"), - method = "(Ljava/lang/String;Lcom/mojang/blaze3d/platform/InputUtil$Type;ILjava/lang/String;)V" - ) + @Inject(method = "(Ljava/lang/String;Lcom/mojang/blaze3d/platform/InputUtil$Type;ILjava/lang/String;)V", at = @At("RETURN")) private void initializeToggleFields(String string, InputUtil.Type type, int i, String string2, CallbackInfo ci) { + for (KeyBind otherKey : QUILT$ALL_KEY_BINDS) { + if (this.equals(otherKey)) { + throw new IllegalArgumentException(String.format("%s has already been registered!", this.getTranslationKey())); + } else if (this.getTranslationKey().equals(otherKey.getTranslationKey())) { + throw new IllegalArgumentException(String.format("Attempted to register {}, but a key bind with the same translation key has already been registered!", this.getTranslationKey())); + } + } + + KeyBindRegistryImpl.registerKeyBind((KeyBind) (Object) this); quilt$disableCounter = 0; } @@ -56,22 +67,21 @@ public void enable() { quilt$disableCounter--; if (quilt$disableCounter == 0) { - KeyBindRegistryImpl.applyChanges(); + KeyBindRegistryImpl.updateKeyBindState((KeyBind) (Object) this); KEY_BINDS.put(this.getTranslationKey(), (KeyBind) (Object) this); - this.reset(); KeyBind.updateBoundKeys(); + this.reset(); } } @Override public void disable() { - if (quilt$disableCounter == 0) { - KeyBindRegistryImpl.applyChanges(); + quilt$disableCounter++; + if (quilt$disableCounter == 1) { + KeyBindRegistryImpl.updateKeyBindState((KeyBind) (Object) this); KEY_BINDS.remove(this.getTranslationKey(), (KeyBind) (Object) this); - this.reset(); KeyBind.updateBoundKeys(); + this.reset(); } - - quilt$disableCounter++; } } diff --git a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java index dc9e005554..15ea62a0bd 100644 --- a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java +++ b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindRegistryTestMod.java @@ -46,8 +46,8 @@ public void readyClient(MinecraftClient client) { } LOGGER.info("The registry has the following keys registered:"); - for (KeyBind key : KeyBindRegistry.getAllKeyBinds(true)) { - LOGGER.info(String.format("Found key %s!", key.getTranslationKey())); + for (KeyBind key : KeyBindRegistry.getAllKeyBinds()) { + LOGGER.info(String.format("Found key %s! (Vanilla: %s)", key.getTranslationKey(), key.isVanilla())); } } } diff --git a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java index c295346902..91ec9b0f1b 100644 --- a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java +++ b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java @@ -28,7 +28,6 @@ import org.quiltmc.loader.api.ModContainer; import org.quiltmc.qsl.base.api.entrypoint.client.ClientModInitializer; -import org.quiltmc.qsl.key.binds.api.KeyBindRegistry; import org.quiltmc.qsl.lifecycle.api.client.event.ClientTickEvents; @Environment(EnvType.CLIENT) @@ -45,21 +44,13 @@ public class KeyBindsTestMod implements ClientModInitializer, ClientTickEvents.S public static final KeyBind DISABLED_CONFLICT_TEST_KEY = new KeyBind("key.qsl.disabled_conflict_test", GLFW.GLFW_KEY_H, KEY_CATEGORY); public static final KeyBind CLAP_KEY = new KeyBind("key.qsl.clap", InputUtil.UNKNOWN_KEY.getKeyCode(), KEY_CATEGORY).withChord( - InputUtil.Type.MOUSE.createFromKeyCode(GLFW.GLFW_MOUSE_BUTTON_LEFT), - InputUtil.Type.MOUSE.createFromKeyCode(GLFW.GLFW_MOUSE_BUTTON_RIGHT) + InputUtil.Type.MOUSE.createFromKeyCode(GLFW.GLFW_MOUSE_BUTTON_LEFT), + InputUtil.Type.MOUSE.createFromKeyCode(GLFW.GLFW_MOUSE_BUTTON_RIGHT) ); @Override public void onInitializeClient(ModContainer mod) { DISABLED_CONFLICT_TEST_KEY.disable(); - - KeyBindRegistry.registerKeyBinds( - CONFLICT_TEST_KEY, - DISABLE_KEY_BIND_KEY, - ENABLE_KEY_BIND_KEY, - DISABLED_CONFLICT_TEST_KEY, - CLAP_KEY - ); } // TODO - Add a test for toggling vanilla key binds; They probably don't work right now, but actually seeing that in action would help a lot From 03652688673162bbaf4eecd3d3f052dde47bd3de Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Fri, 17 Jun 2022 01:38:51 -0300 Subject: [PATCH 51/56] Begin port to 1.19 --- library/gui/key_binds/build.gradle | 4 +- .../qsl/key/binds/api/ChordedKeyBind.java | 3 +- .../key/binds/impl/KeyBindRegistryImpl.java | 7 +- .../impl/config/QuiltKeyBindsConfig.java | 85 +------ .../config/QuiltKeyBindsConfigManager.java | 208 ++++++------------ .../binds/mixin/client/KeyBindEntryMixin.java | 13 +- .../mixin/client/chords/KeyBindMixin.java | 8 +- .../client/chords/KeyBindsScreenMixin.java | 9 +- .../mixin/client/config/GameOptionsMixin.java | 6 +- .../client/config/MinecraftClientMixin.java | 4 +- .../binds/test/client/KeyBindsTestMod.java | 15 +- .../src/testmod/resources/quilt.mod.json | 1 + 12 files changed, 109 insertions(+), 254 deletions(-) diff --git a/library/gui/key_binds/build.gradle b/library/gui/key_binds/build.gradle index 3a93c984a3..4b0acdb0c1 100644 --- a/library/gui/key_binds/build.gradle +++ b/library/gui/key_binds/build.gradle @@ -18,8 +18,8 @@ qslModule { injectedInterface("net/minecraft/class_304") { values = [ "org/quiltmc/qsl/key/binds/api/ChordedKeyBind", - "org/quiltmc/qsl/key/binds/api/ToggleableKeyBind", - "org/quiltmc/qsl/key/binds/api/QuiltKeyBind" + "org/quiltmc/qsl/key/binds/api/QuiltKeyBind", + "org/quiltmc/qsl/key/binds/api/ToggleableKeyBind" ] } clientOnly() diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ChordedKeyBind.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ChordedKeyBind.java index 9f245e351c..4a2db8c347 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ChordedKeyBind.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ChordedKeyBind.java @@ -16,10 +16,11 @@ package org.quiltmc.qsl.key.binds.api; -import com.mojang.blaze3d.platform.InputUtil; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; +import com.mojang.blaze3d.platform.InputUtil; + import net.minecraft.client.option.KeyBind; import org.quiltmc.qsl.base.api.util.InjectedInterface; diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java index e5d09df070..b6fc225ec3 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java @@ -18,18 +18,15 @@ import java.util.List; +import it.unimi.dsi.fastutil.objects.ReferenceArrayList; import org.jetbrains.annotations.ApiStatus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - -import it.unimi.dsi.fastutil.objects.ReferenceArrayList; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.option.KeyBind; -import org.quiltmc.qsl.key.binds.impl.config.QuiltKeyBindsConfigManager; - @Environment(EnvType.CLIENT) @ApiStatus.Internal public class KeyBindRegistryImpl { @@ -69,7 +66,7 @@ public static void updateKeyBindState(KeyBind key) { keyBindManager.addModdedKeyBinds(); } - QuiltKeyBindsConfigManager.saveConfig(); + //QuiltKeyBindsConfigManager.CONFIG.save(); } public static KeyBind[] getKeyBinds() { diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java index 9facf9632c..93b74bc25e 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java @@ -1,82 +1,11 @@ -/* - * Copyright 2022 QuiltMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - package org.quiltmc.qsl.key.binds.impl.config; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import com.mojang.datafixers.util.Either; -import com.mojang.serialization.Codec; -import com.mojang.serialization.codecs.RecordCodecBuilder; - -public class QuiltKeyBindsConfig { - public static final Codec CODEC = RecordCodecBuilder.create( - instance -> instance.group( - // TODO - Implement me! - Codec.BOOL.fieldOf("show_tutorial_toast").forGetter(QuiltKeyBindsConfig::getShowTutorialToast), - Codec.unboundedMap(Codec.STRING, Codec.either(Codec.STRING, Codec.list(Codec.STRING))).fieldOf("key_binds").forGetter(QuiltKeyBindsConfig::getKeyBinds), - Codec.unboundedMap(Codec.STRING, Codec.either(Codec.STRING, Codec.list(Codec.STRING))).fieldOf("unused_key_binds").forGetter(QuiltKeyBindsConfig::getUnusedKeyBinds) - ) - .apply(instance, QuiltKeyBindsConfig::new) - ); - - private boolean showTutorialToast; - // TODO - This type is super long; Use an object for this - private Map>> keyBinds; - private Map>> unusedKeyBinds; - - public QuiltKeyBindsConfig() { - this.showTutorialToast = false; - this.keyBinds = new HashMap<>(); - this.unusedKeyBinds = new HashMap<>(); - } - - public QuiltKeyBindsConfig( - boolean showTutorialToast, - Map>> keyBinds, - Map>> unusedKeyBinds - ) { - this.showTutorialToast = showTutorialToast; - this.keyBinds = keyBinds; - this.unusedKeyBinds = unusedKeyBinds; - } - - public boolean getShowTutorialToast() { - return this.showTutorialToast; - } - - public void setShowTutorialToast(boolean showTutorialToast) { - this.showTutorialToast = showTutorialToast; - } - - public Map>> getKeyBinds() { - return this.keyBinds; - } - - public void setKeyBinds(Map>> keyBinds) { - this.keyBinds = keyBinds; - } - - public Map>> getUnusedKeyBinds() { - return this.unusedKeyBinds; - } +import org.quiltmc.config.api.WrappedConfig; +import org.quiltmc.config.api.values.ValueList; +import org.quiltmc.config.api.values.ValueMap; - public void setUnusedKeyBinds(Map>> unusedKeyBinds) { - this.unusedKeyBinds = unusedKeyBinds; - } +public class QuiltKeyBindsConfig extends WrappedConfig { + public final boolean show_tutorial_toast = true; + public final ValueMap> key_binds = ValueMap.builder(ValueList.create("")).build(); + public final ValueMap> unused_key_binds = ValueMap.builder(ValueList.create("")).build(); } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java index 47d262e560..9b8481701d 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java @@ -1,173 +1,99 @@ -/* - * Copyright 2022 QuiltMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - package org.quiltmc.qsl.key.binds.impl.config; -import java.io.IOException; -import java.io.Reader; -import java.io.Writer; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; -import java.util.Optional; import java.util.SortedMap; import java.util.TreeMap; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonParseException; -import com.google.gson.JsonParser; -import com.google.gson.stream.JsonWriter; import com.mojang.blaze3d.platform.InputUtil; -import com.mojang.datafixers.util.Either; -import com.mojang.serialization.JsonOps; -import net.minecraft.client.MinecraftClient; import net.minecraft.client.option.KeyBind; -import org.quiltmc.loader.api.QuiltLoader; +import org.quiltmc.config.api.values.TrackedValue; +import org.quiltmc.config.api.values.ValueList; +import org.quiltmc.config.api.values.ValueMap; +import org.quiltmc.loader.api.config.QuiltConfig; import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; import org.quiltmc.qsl.key.binds.impl.chords.KeyChord; import org.quiltmc.qsl.key.binds.mixin.client.KeyBindAccessor; -// TODO - Massively simplify the config system +@SuppressWarnings("unchecked") public class QuiltKeyBindsConfigManager { - public static Optional isConfigLoaded = Optional.empty(); - public static final QuiltKeyBindsConfig CONFIG = new QuiltKeyBindsConfig(); - public static final Path QSL_CONFIG_PATH = QuiltLoader.getConfigDir().resolve("qsl"); - public static final Path KEY_BINDS_CONFIG_PATH = QSL_CONFIG_PATH.resolve("key_binds.json"); - public static final Gson GSON = new GsonBuilder().setPrettyPrinting().serializeNulls().disableHtmlEscaping().create(); - - public static void loadConfig() { - if (Files.exists(KEY_BINDS_CONFIG_PATH)) { - try { - Reader reader = Files.newBufferedReader(KEY_BINDS_CONFIG_PATH, StandardCharsets.UTF_8); - var result = QuiltKeyBindsConfig.CODEC.parse(JsonOps.INSTANCE, JsonParser.parseReader(reader)).result(); - reader.close(); - - if (result.isPresent()) { - registerConfigChanges(result.get()); - populateConfig(); - saveConfig(); - isConfigLoaded = Optional.of(true); - } else { - isConfigLoaded = Optional.of(false); - } - } catch (IOException | JsonParseException e) { - System.err.println(e); - } - } else { - if (!Files.isDirectory(QSL_CONFIG_PATH)) { - try { - Files.createDirectory(QSL_CONFIG_PATH); - } catch (IOException e) { - e.printStackTrace(); - } - } + public static final QuiltKeyBindsConfig CONFIG = QuiltConfig.create("qsl", "key_binds", QuiltKeyBindsConfig.class); - populateConfig(); - saveConfig(); - } - } + public static final TrackedValue SHOW_TUTORIAL_TOAST = (TrackedValue) CONFIG.getValue(List.of("show_tutorial_toast")); + public static final TrackedValue>> KEY_BINDS = (TrackedValue>>) CONFIG.getValue(List.of("key_binds")); + public static final TrackedValue>> UNUSED_KEY_BINDS = (TrackedValue>>) CONFIG.getValue(List.of("unused_key_binds")); - // TODO - Wait, why am I using two different config objects? Just use one! - public static void registerConfigChanges(QuiltKeyBindsConfig newConfig) { - MinecraftClient client = MinecraftClient.getInstance(); - for (KeyBind key : client.options.allKeys) { - if (newConfig.getKeyBinds().containsKey(key.getTranslationKey())) { - Either> keyEither = newConfig.getKeyBinds().get(key.getTranslationKey()); - keyEither.ifLeft(singleKey -> { - key.setBoundKey(InputUtil.fromTranslationKey(singleKey)); - }); - keyEither.ifRight(keyList -> { + public QuiltKeyBindsConfigManager() { } + + public static void updateConfig(boolean load) { + var filteredList = new ArrayList<>(KeyBindRegistryImpl.getAllKeyBinds()); + List removalList = new ArrayList<>(); + + KEY_BINDS.value().putAll(UNUSED_KEY_BINDS.value()); + UNUSED_KEY_BINDS.value().clear(); + + for (String keyBindKey : KEY_BINDS.value().keySet()) { + System.out.println(keyBindKey); + var keyBind = KeyBindRegistryImpl.getKeyBind(keyBindKey); + var keyList = KEY_BINDS.value().get(keyBindKey); + if (keyBind != null) { + if (load) { switch (keyList.size()) { - case 0 -> { - key.setBoundKey(InputUtil.UNKNOWN_KEY); - } - case 1 -> { - key.setBoundKey(InputUtil.fromTranslationKey(keyList.get(0))); - } - default -> { - SortedMap map = new TreeMap<>(); - for (String string : keyList) { - map.put(InputUtil.fromTranslationKey(string), false); + case 0 -> keyBind.setBoundKey(InputUtil.UNKNOWN_KEY); + case 1 -> keyBind.setBoundKey(InputUtil.fromTranslationKey(keyList.get(0))); + default -> { + SortedMap map = new TreeMap<>(); + for (String string : keyList) { + map.put(InputUtil.fromTranslationKey(string), false); + } + + keyBind.setBoundChord(new KeyChord(map)); } - - key.setBoundChord(new KeyChord(map)); - } }; - }); - } - }; - CONFIG.setShowTutorialToast(newConfig.getShowTutorialToast()); - CONFIG.setKeyBinds(newConfig.getKeyBinds()); - CONFIG.setUnusedKeyBinds(newConfig.getUnusedKeyBinds()); - } + } else { + ValueList list; + if (keyBind.getBoundChord() == null) { + if (((KeyBindAccessor) keyBind).getBoundKey().equals(InputUtil.UNKNOWN_KEY)) { + list = ValueList.create("", new String[] {}); + } else { + list = ValueList.create("", keyBind.getKeyTranslationKey()); + } + } else { + var protoList = keyBind.getBoundChord().keys.keySet().stream().map(key -> key.getTranslationKey()).toList(); + var array = protoList.toArray(new String[] {}); + list = ValueList.create("", array); + } - public static void saveConfig() { - var result = QuiltKeyBindsConfig.CODEC.encodeStart(JsonOps.INSTANCE, CONFIG).result(); - if (result.isPresent()) { - try { - Writer writer = Files.newBufferedWriter(KEY_BINDS_CONFIG_PATH, StandardCharsets.UTF_8); - JsonWriter jsonWriter = GSON.newJsonWriter(writer); - GSON.toJson(result.get(), jsonWriter); - jsonWriter.close(); - } catch (IOException e) { - System.err.println(e); + keyList.clear(); + keyList.addAll(list); + } + + filteredList.remove(keyBind); + } else { + removalList.add(keyBindKey); + UNUSED_KEY_BINDS.value().put(keyBindKey, keyList); } } - } - public static void populateConfig() { - Map>> keyBindsMap = new HashMap<>(); + removalList.forEach(key -> KEY_BINDS.value().remove(key)); - for (KeyBind keyBind : KeyBindRegistryImpl.getAllKeyBinds()) { - if (keyBind.isDisabled()) continue; - if (keyBind.getBoundChord() != null) { - Either> either = switch (keyBind.getBoundChord().keys.size()) { - case 0 -> { - yield Either.right(List.of()); - } - case 1 -> { - InputUtil.Key key = keyBind.getBoundChord().keys.firstKey(); - yield Either.left(key.getTranslationKey()); + for (KeyBind keyBind : filteredList) { + ValueList list; + if (keyBind.getBoundChord() == null) { + if (((KeyBindAccessor) keyBind).getBoundKey().equals(InputUtil.UNKNOWN_KEY)) { + list = ValueList.create("", new String[] {}); + } else { + list = ValueList.create("", keyBind.getKeyTranslationKey()); } - default -> { - List list = new ArrayList<>(); - for (InputUtil.Key key : keyBind.getBoundChord().keys.keySet()) { - list.add(key.getTranslationKey()); - } - - yield Either.right(list); - } }; - - keyBindsMap.put(keyBind.getTranslationKey(), either); } else { - Either> either = - ((KeyBindAccessor) keyBind).getBoundKey().equals(InputUtil.UNKNOWN_KEY) - ? Either.right(List.of()) - : Either.left(keyBind.getKeyTranslationKey()); - keyBindsMap.put(keyBind.getTranslationKey(), either); + var protoList = keyBind.getBoundChord().keys.keySet().stream().map(key -> key.getTranslationKey()).toList(); + var array = protoList.toArray(new String[] {}); + list = ValueList.create("", array); } - } - CONFIG.setKeyBinds(keyBindsMap); + KEY_BINDS.value().put(keyBind.getTranslationKey(), list); + } } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java index 75a43de34d..9e98f6f398 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java @@ -18,7 +18,6 @@ import java.util.List; -import com.mojang.blaze3d.platform.InputUtil; import org.objectweb.asm.Opcodes; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; @@ -33,15 +32,15 @@ import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; +import com.mojang.blaze3d.platform.InputUtil; + import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.widget.ButtonWidget; import net.minecraft.client.gui.widget.option.KeyBindListWidget; import net.minecraft.client.gui.widget.option.KeyBindListWidget.KeyBindEntry; import net.minecraft.client.option.KeyBind; import net.minecraft.client.util.math.MatrixStack; -import net.minecraft.text.LiteralText; import net.minecraft.text.Text; -import net.minecraft.text.TranslatableText; import net.minecraft.util.Formatting; import org.quiltmc.qsl.key.binds.api.KeyBindRegistry; @@ -116,10 +115,10 @@ private void collectConflictTooltips(MatrixStack matrices, int index, int y, int for (KeyBind otherKey : KeyBindRegistryImpl.getKeyBinds()) { if (otherKey != this.key && this.key.keyEquals(otherKey)) { if (this.quilt$conflictTooltips.isEmpty()) { - this.quilt$conflictTooltips.add(new TranslatableText("key.qsl.key_conflict.tooltip").formatted(Formatting.RED)); + this.quilt$conflictTooltips.add(Text.translatable("key.qsl.key_conflict.tooltip").formatted(Formatting.RED)); } - this.quilt$conflictTooltips.add(new TranslatableText("key.qsl.key_conflict.tooltip.entry", new TranslatableText(otherKey.getTranslationKey())).formatted(Formatting.RED)); + this.quilt$conflictTooltips.add(Text.translatable("key.qsl.key_conflict.tooltip.entry", Text.translatable(otherKey.getTranslationKey())).formatted(Formatting.RED)); } } } @@ -178,7 +177,7 @@ private void shortenText(MatrixStack matrices, int index, int y, int x, int entr protoText += "..."; } - this.bindButton.setMessage(new LiteralText(protoText)); + this.bindButton.setMessage(Text.literal(protoText)); } } @@ -191,7 +190,7 @@ private void shortenText(MatrixStack matrices, int index, int y, int x, int entr ) ) private Text addConflictIndicator(Text originalText) { - return new TranslatableText("key.qsl.key_conflict.indicator", originalText).formatted(Formatting.RED); + return Text.translatable("key.qsl.key_conflict.indicator", originalText).formatted(Formatting.RED); } @Override diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java index ff49723f49..34a746d2b3 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java @@ -20,8 +20,6 @@ import java.util.Map; import java.util.SortedMap; -import com.mojang.blaze3d.platform.InputUtil; -import com.mojang.blaze3d.platform.InputUtil.Key; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -34,9 +32,11 @@ import it.unimi.dsi.fastutil.objects.Object2BooleanAVLTreeMap; import it.unimi.dsi.fastutil.objects.Reference2ReferenceOpenHashMap; +import com.mojang.blaze3d.platform.InputUtil; +import com.mojang.blaze3d.platform.InputUtil.Key; + import net.minecraft.client.MinecraftClient; import net.minecraft.client.option.KeyBind; -import net.minecraft.text.LiteralText; import net.minecraft.text.MutableText; import net.minecraft.text.Text; @@ -173,7 +173,7 @@ private void isChordUnbound(CallbackInfoReturnable cir) { @Inject(at = @At("HEAD"), method = "getKeyName", cancellable = true) private void useChordName(CallbackInfoReturnable cir) { if (this.quilt$boundChord != null) { - MutableText text = LiteralText.EMPTY.shallowCopy(); + MutableText text = Text.empty(); for (InputUtil.Key key : this.quilt$boundChord.keys.keySet()) { if (text.getSiblings().size() != 0) { text.append(" + "); diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java index 426e6eae91..beb48b76c6 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java @@ -19,7 +19,6 @@ import java.util.List; import java.util.SortedMap; -import com.mojang.blaze3d.platform.InputUtil; import org.jetbrains.annotations.Nullable; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -31,6 +30,8 @@ import it.unimi.dsi.fastutil.objects.Object2BooleanAVLTreeMap; import it.unimi.dsi.fastutil.objects.ObjectArrayList; +import com.mojang.blaze3d.platform.InputUtil; + import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.option.GameOptionsScreen; import net.minecraft.client.gui.screen.option.KeyBindsScreen; @@ -119,8 +120,7 @@ public boolean keyReleased(int keyCode, int scanCode, int modifiers) { } this.focusedKey.setBoundChord(new KeyChord(map)); - QuiltKeyBindsConfigManager.populateConfig(); - QuiltKeyBindsConfigManager.saveConfig(); + QuiltKeyBindsConfigManager.updateConfig(false); } quilt$focusedProtoChord.clear(); @@ -146,8 +146,7 @@ public boolean mouseReleased(double mouseX, double mouseY, int button) { } this.focusedKey.setBoundChord(new KeyChord(map)); - QuiltKeyBindsConfigManager.populateConfig(); - QuiltKeyBindsConfigManager.saveConfig(); + QuiltKeyBindsConfigManager.updateConfig(false); } quilt$focusedProtoChord.clear(); diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/GameOptionsMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/GameOptionsMixin.java index e8e6f5217a..52c4eebc83 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/GameOptionsMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/GameOptionsMixin.java @@ -16,13 +16,14 @@ package org.quiltmc.qsl.key.binds.mixin.client.config; -import com.mojang.blaze3d.platform.InputUtil; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import com.mojang.blaze3d.platform.InputUtil; + import net.minecraft.client.option.GameOptions; import net.minecraft.client.option.KeyBind; @@ -38,8 +39,7 @@ public abstract class GameOptionsMixin { method = "setKeyCode" ) private void writeToKeyBindConfig(KeyBind key, InputUtil.Key code, CallbackInfo ci) { - QuiltKeyBindsConfigManager.populateConfig(); - QuiltKeyBindsConfigManager.saveConfig(); + QuiltKeyBindsConfigManager.updateConfig(false); } @Redirect( diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/MinecraftClientMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/MinecraftClientMixin.java index e9ea334236..02df17da12 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/MinecraftClientMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/config/MinecraftClientMixin.java @@ -25,6 +25,7 @@ import net.minecraft.client.RunArgs; import net.minecraft.client.option.KeyBind; +import org.quiltmc.qsl.key.binds.impl.config.QuiltKeyBindsConfig; import org.quiltmc.qsl.key.binds.impl.config.QuiltKeyBindsConfigManager; @Mixin(MinecraftClient.class) @@ -35,7 +36,8 @@ public abstract class MinecraftClientMixin { method = "" ) private void handleQuiltKeyBindsConfig(RunArgs runArgs, CallbackInfo ci) { - QuiltKeyBindsConfigManager.loadConfig(); + new QuiltKeyBindsConfig(); + QuiltKeyBindsConfigManager.updateConfig(true); KeyBind.updateBoundKeys(); } } diff --git a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java index 91ec9b0f1b..06f34a66c7 100644 --- a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java +++ b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java @@ -16,15 +16,16 @@ package org.quiltmc.qsl.key.binds.test.client; -import com.mojang.blaze3d.platform.InputUtil; import org.lwjgl.glfw.GLFW; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; +import com.mojang.blaze3d.platform.InputUtil; + import net.minecraft.client.MinecraftClient; import net.minecraft.client.option.KeyBind; import net.minecraft.sound.SoundEvents; -import net.minecraft.text.LiteralText; +import net.minecraft.text.Text; import org.quiltmc.loader.api.ModContainer; import org.quiltmc.qsl.base.api.entrypoint.client.ClientModInitializer; @@ -59,27 +60,27 @@ public void startClientTick(MinecraftClient client) { if (client.player == null) return; if (DISABLE_KEY_BIND_KEY.wasPressed()) { - client.player.sendMessage(new LiteralText("The clap key has disappeared!"), true); + client.player.sendMessage(Text.literal("The clap key has disappeared!"), true); CLAP_KEY.disable(); client.player.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1.5F, 1.5F); } if (ENABLE_KEY_BIND_KEY.wasPressed()) { - client.player.sendMessage(new LiteralText("The clap key is back!"), true); + client.player.sendMessage(Text.literal("The clap key is back!"), true); CLAP_KEY.enable(); client.player.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1.5F, 1.5F); } if (CONFLICT_TEST_KEY.wasPressed()) { - client.player.sendMessage(new LiteralText("This is the conflict key being pressed"), false); + client.player.sendMessage(Text.literal("This is the conflict key being pressed"), false); } if (DISABLED_CONFLICT_TEST_KEY.wasPressed()) { - client.player.sendMessage(new LiteralText("I'm not supposed to do things! Why am I enabled?"), false); + client.player.sendMessage(Text.literal("I'm not supposed to do things! Why am I enabled?"), false); } if (CLAP_KEY.wasPressed()) { - client.player.sendMessage(new LiteralText("*clap*"), true); + client.player.sendMessage(Text.literal("*clap*"), true); client.player.playSound(SoundEvents.ENTITY_GENERIC_SMALL_FALL, 1.5F, 1.5F); } } diff --git a/library/gui/key_binds/src/testmod/resources/quilt.mod.json b/library/gui/key_binds/src/testmod/resources/quilt.mod.json index 4b7f220f1b..6f03df4aab 100644 --- a/library/gui/key_binds/src/testmod/resources/quilt.mod.json +++ b/library/gui/key_binds/src/testmod/resources/quilt.mod.json @@ -4,6 +4,7 @@ "group": "org.quiltmc", "id": "quilt_key_binds_testmod", "version": "1.0.0", + "load_type": "always", "intermediate_mappings": "net.fabricmc:intermediary", "depends": [ "quilt_key_binds" From f25afdc13035eba8d647253b3d34872f3dd41c02 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Fri, 17 Jun 2022 01:40:00 -0300 Subject: [PATCH 52/56] Apply licenses --- .../quiltmc/qsl/key/binds/api/QuiltKeyBind.java | 16 ++++++++++++++++ .../qsl/key/binds/api/ToggleableKeyBind.java | 16 ++++++++++++++++ .../qsl/key/binds/impl/InternalQuiltKeyBind.java | 16 ++++++++++++++++ .../qsl/key/binds/impl/KeyBindRegistryImpl.java | 2 +- .../binds/impl/config/QuiltKeyBindsConfig.java | 16 ++++++++++++++++ .../impl/config/QuiltKeyBindsConfigManager.java | 16 ++++++++++++++++ .../binds/mixin/client/KeyBindEntryMixin.java | 2 +- .../binds/mixin/client/toggle/KeyBindMixin.java | 16 ++++++++++++++++ .../key/binds/test/client/KeyBindsTestMod.java | 2 +- 9 files changed, 99 insertions(+), 3 deletions(-) diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/QuiltKeyBind.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/QuiltKeyBind.java index c63e853b46..d6775b5491 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/QuiltKeyBind.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/QuiltKeyBind.java @@ -1,3 +1,19 @@ +/* + * Copyright 2022 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.quiltmc.qsl.key.binds.api; public interface QuiltKeyBind { diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ToggleableKeyBind.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ToggleableKeyBind.java index 421ee97748..5e217baee6 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ToggleableKeyBind.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ToggleableKeyBind.java @@ -1,3 +1,19 @@ +/* + * Copyright 2022 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.quiltmc.qsl.key.binds.api; import net.fabricmc.api.EnvType; diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/InternalQuiltKeyBind.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/InternalQuiltKeyBind.java index d1b7f8e1ce..09247b317a 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/InternalQuiltKeyBind.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/InternalQuiltKeyBind.java @@ -1,3 +1,19 @@ +/* + * Copyright 2022 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.quiltmc.qsl.key.binds.impl; public interface InternalQuiltKeyBind { diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java index b6fc225ec3..76c28c63c8 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2021-2022 QuiltMC + * Copyright 2022 QuiltMC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java index 93b74bc25e..4d72023971 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java @@ -1,3 +1,19 @@ +/* + * Copyright 2022 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.quiltmc.qsl.key.binds.impl.config; import org.quiltmc.config.api.WrappedConfig; diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java index 9b8481701d..0da74ae53c 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java @@ -1,3 +1,19 @@ +/* + * Copyright 2022 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.quiltmc.qsl.key.binds.impl.config; import java.util.ArrayList; diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java index 9e98f6f398..2279236317 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java @@ -1,5 +1,5 @@ /* - * Copyright 2021-2022 QuiltMC + * Copyright 2022 QuiltMC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/toggle/KeyBindMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/toggle/KeyBindMixin.java index 22e080e102..924df0fe9e 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/toggle/KeyBindMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/toggle/KeyBindMixin.java @@ -1,3 +1,19 @@ +/* + * Copyright 2022 QuiltMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.quiltmc.qsl.key.binds.mixin.client.toggle; import java.util.ArrayList; diff --git a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java index 06f34a66c7..842f363e95 100644 --- a/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java +++ b/library/gui/key_binds/src/testmod/java/org/quiltmc/qsl/key/binds/test/client/KeyBindsTestMod.java @@ -1,5 +1,5 @@ /* - * Copyright 2021-2022 QuiltMC + * Copyright 2022 QuiltMC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From 336531bc5c13d83f737b0dcf0bf02c8fcb5e6241 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Sat, 23 Jul 2022 18:14:09 -0300 Subject: [PATCH 53/56] Fix config system --- .../impl/config/QuiltKeyBindsConfig.java | 7 +++++ .../config/QuiltKeyBindsConfigManager.java | 26 ++++++++++--------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java index 4d72023971..eebd71414c 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfig.java @@ -16,12 +16,19 @@ package org.quiltmc.qsl.key.binds.impl.config; +import org.quiltmc.config.api.Config; import org.quiltmc.config.api.WrappedConfig; +import org.quiltmc.config.api.annotations.Processor; import org.quiltmc.config.api.values.ValueList; import org.quiltmc.config.api.values.ValueMap; +@Processor("setSerializer") public class QuiltKeyBindsConfig extends WrappedConfig { public final boolean show_tutorial_toast = true; public final ValueMap> key_binds = ValueMap.builder(ValueList.create("")).build(); public final ValueMap> unused_key_binds = ValueMap.builder(ValueList.create("")).build(); + + public void setSerializer(Config.Builder builder) { + builder.format("json5"); + } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java index 0da74ae53c..8f9fe9773f 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java @@ -54,7 +54,7 @@ public static void updateConfig(boolean load) { System.out.println(keyBindKey); var keyBind = KeyBindRegistryImpl.getKeyBind(keyBindKey); var keyList = KEY_BINDS.value().get(keyBindKey); - if (keyBind != null) { + if (keyBind != null && keyBind.isEnabled()) { if (load) { switch (keyList.size()) { case 0 -> keyBind.setBoundKey(InputUtil.UNKNOWN_KEY); @@ -96,20 +96,22 @@ public static void updateConfig(boolean load) { removalList.forEach(key -> KEY_BINDS.value().remove(key)); for (KeyBind keyBind : filteredList) { - ValueList list; - if (keyBind.getBoundChord() == null) { - if (((KeyBindAccessor) keyBind).getBoundKey().equals(InputUtil.UNKNOWN_KEY)) { - list = ValueList.create("", new String[] {}); + if (keyBind.isEnabled()) { + ValueList list; + if (keyBind.getBoundChord() == null) { + if (((KeyBindAccessor) keyBind).getBoundKey().equals(InputUtil.UNKNOWN_KEY)) { + list = ValueList.create("", new String[] {}); + } else { + list = ValueList.create("", keyBind.getKeyTranslationKey()); + } } else { - list = ValueList.create("", keyBind.getKeyTranslationKey()); + var protoList = keyBind.getBoundChord().keys.keySet().stream().map(key -> key.getTranslationKey()).toList(); + var array = protoList.toArray(new String[] {}); + list = ValueList.create("", array); } - } else { - var protoList = keyBind.getBoundChord().keys.keySet().stream().map(key -> key.getTranslationKey()).toList(); - var array = protoList.toArray(new String[] {}); - list = ValueList.create("", array); - } - KEY_BINDS.value().put(keyBind.getTranslationKey(), list); + KEY_BINDS.value().put(keyBind.getTranslationKey(), list); + } } } } From a161416d337e9bc15e2f9a5f1426dd8568ef53df Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Sat, 23 Jul 2022 18:48:03 -0300 Subject: [PATCH 54/56] Reignite work on Key Binds API --- .../qsl/key/binds/api/ChordedKeyBind.java | 5 ++--- .../qsl/key/binds/api/KeyBindRegistry.java | 16 ---------------- .../quiltmc/qsl/key/binds/api/QuiltKeyBind.java | 16 +++++++++++++++- .../qsl/key/binds/impl/KeyBindManager.java | 2 +- .../impl/config/QuiltKeyBindsConfigManager.java | 5 ++--- .../key/binds/mixin/client/KeyBindAccessor.java | 4 ---- .../binds/mixin/client/KeyBindEntryMixin.java | 3 +-- .../qsl/key/binds/mixin/client/KeyBindMixin.java | 8 ++++++++ 8 files changed, 29 insertions(+), 30 deletions(-) diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ChordedKeyBind.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ChordedKeyBind.java index 4a2db8c347..7e3b42a716 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ChordedKeyBind.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ChordedKeyBind.java @@ -31,14 +31,13 @@ @InjectedInterface(KeyBind.class) public interface ChordedKeyBind { default KeyChord getBoundChord() { - System.out.println("This should never happen"); - return null; + throw new UnsupportedOperationException(); } default void setBoundChord(KeyChord chord) { } // TODO - This is a temporary measure until CHASM comes. Replace it with a proper constructor or builder default KeyBind withChord(InputUtil.Key... keys) { - return null; + throw new UnsupportedOperationException(); } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java index def5dac861..fedfa6133f 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java @@ -18,14 +18,12 @@ import java.util.List; -import com.mojang.blaze3d.platform.InputUtil; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.option.KeyBind; import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; -import org.quiltmc.qsl.key.binds.mixin.client.KeyBindAccessor; // TODO - This isn't a registry anymore; What the heck should it be named? /** @@ -47,20 +45,6 @@ public static KeyBind getKeyBind(String translationKey) { return KeyBindRegistryImpl.getKeyBind(translationKey); } - // TODO - Would a transitive AW be safe for getBoundKey? - /** - * Gets the bound key of the key bind. - * - *

The bound key is only directly used by the key bind system's internal logic. - * If possible, use the methods provided by the KeyBind class instead. - * - * @param key the key bind - * @return the key bind's bound key - */ - public static InputUtil.Key getBoundKey(KeyBind key) { - return ((KeyBindAccessor) key).getBoundKey(); - } - /** * Returns a list containing all key binds. * diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/QuiltKeyBind.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/QuiltKeyBind.java index d6775b5491..4380db429f 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/QuiltKeyBind.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/QuiltKeyBind.java @@ -16,14 +16,28 @@ package org.quiltmc.qsl.key.binds.api; +import com.mojang.blaze3d.platform.InputUtil; + public interface QuiltKeyBind { /** * Gets whenever the key bind is from Vanilla or not. * This is automatically determined by using GameOptions' allKeys property. * - * @return {@code true} if the key bind is from Vanilla, {@code false} otherwise + * @return {@code true} if the key bind is from Vanilla, or {@code false} otherwise */ default boolean isVanilla() { return false; } + + /** + * Gets the bound key of the key bind. + * + *

The bound key is only directly used by the key bind system's internal logic. + * If possible, use the methods provided by the KeyBind class instead. + * + * @return the key bind's bound key + */ + default InputUtil.Key getBoundKey() { + throw new UnsupportedOperationException(); + } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindManager.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindManager.java index 03e16d9aa5..d8dbe6502c 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindManager.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindManager.java @@ -37,7 +37,7 @@ public KeyBindManager(GameOptions options, KeyBind[] allKeys) { } public void addModdedKeyBinds() { - ((GameOptionsAccessor) (Object) this.options).setAllKeys(KeyBindRegistryImpl.getKeyBinds()); + ((GameOptionsAccessor) this.options).setAllKeys(KeyBindRegistryImpl.getKeyBinds()); } public KeyBind[] getAllKeys() { diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java index 8f9fe9773f..f9a014d1ee 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/config/QuiltKeyBindsConfigManager.java @@ -31,7 +31,6 @@ import org.quiltmc.loader.api.config.QuiltConfig; import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; import org.quiltmc.qsl.key.binds.impl.chords.KeyChord; -import org.quiltmc.qsl.key.binds.mixin.client.KeyBindAccessor; @SuppressWarnings("unchecked") public class QuiltKeyBindsConfigManager { @@ -71,7 +70,7 @@ public static void updateConfig(boolean load) { } else { ValueList list; if (keyBind.getBoundChord() == null) { - if (((KeyBindAccessor) keyBind).getBoundKey().equals(InputUtil.UNKNOWN_KEY)) { + if (keyBind.getBoundKey().equals(InputUtil.UNKNOWN_KEY)) { list = ValueList.create("", new String[] {}); } else { list = ValueList.create("", keyBind.getKeyTranslationKey()); @@ -99,7 +98,7 @@ public static void updateConfig(boolean load) { if (keyBind.isEnabled()) { ValueList list; if (keyBind.getBoundChord() == null) { - if (((KeyBindAccessor) keyBind).getBoundKey().equals(InputUtil.UNKNOWN_KEY)) { + if (keyBind.getBoundKey().equals(InputUtil.UNKNOWN_KEY)) { list = ValueList.create("", new String[] {}); } else { list = ValueList.create("", keyBind.getKeyTranslationKey()); diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindAccessor.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindAccessor.java index 1fb3ce2017..f16b475769 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindAccessor.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindAccessor.java @@ -18,7 +18,6 @@ import java.util.Map; -import com.mojang.blaze3d.platform.InputUtil; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.gen.Accessor; import org.spongepowered.asm.mixin.gen.Invoker; @@ -35,9 +34,6 @@ static Map getKeyBinds() { return null; } - @Accessor - InputUtil.Key getBoundKey(); - @Invoker void callReset(); } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java index 2279236317..abbfc9bc51 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java @@ -43,7 +43,6 @@ import net.minecraft.text.Text; import net.minecraft.util.Formatting; -import org.quiltmc.qsl.key.binds.api.KeyBindRegistry; import org.quiltmc.qsl.key.binds.impl.KeyBindTooltipHolder; import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; import org.quiltmc.qsl.key.binds.impl.chords.KeyChord; @@ -91,7 +90,7 @@ private void initPreviousBoundKey(KeyBindListWidget list, KeyBind key, Text text locals = LocalCapture.CAPTURE_FAILHARD ) private void collectConflictTooltips(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta, CallbackInfo ci, boolean bl, boolean bl2) { - InputUtil.Key boundKey = KeyBindRegistry.getBoundKey(this.key); + InputUtil.Key boundKey = this.key.getBoundKey(); KeyChord boundChord = this.key.getBoundChord(); List boundProtoChord; diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindMixin.java index 998fa90aa1..aa863e81ec 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindMixin.java @@ -43,6 +43,9 @@ public abstract class KeyBindMixin implements QuiltKeyBind, InternalQuiltKeyBind @Final private static Map ORDER_BY_CATEGORIES; + @Shadow + private InputUtil.Key boundKey; + @Unique private boolean quilt$vanilla; @@ -64,4 +67,9 @@ public boolean isVanilla() { public void markAsVanilla() { this.quilt$vanilla = true; } + + @Override + public InputUtil.Key getBoundKey() { + return this.boundKey; + }; } From 0b2e59889081ebf9b0f9290c10b1c7219b46ceda Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Sat, 27 Aug 2022 18:50:04 -0300 Subject: [PATCH 55/56] keyboard mash --- .../qsl/key/binds/api/ChordedKeyBind.java | 23 +++++++++++-- .../qsl/key/binds/api/QuiltKeyBind.java | 3 ++ .../qsl/key/binds/api/ToggleableKeyBind.java | 12 +++++++ .../key/binds/impl/InternalQuiltKeyBind.java | 3 ++ .../key/binds/impl/KeyBindRegistryImpl.java | 4 --- .../qsl/key/binds/impl/chords/KeyChord.java | 5 +-- .../binds/mixin/client/KeyBindEntryMixin.java | 10 +++--- .../key/binds/mixin/client/KeyBindMixin.java | 3 +- .../client/chords/InputUtilKeyMixin.java | 3 +- .../mixin/client/chords/KeyBindMixin.java | 16 +++++++--- .../client/chords/KeyBindsScreenMixin.java | 32 +++++++++---------- .../mixin/client/toggle/KeyBindMixin.java | 24 ++++++++------ 12 files changed, 94 insertions(+), 44 deletions(-) diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ChordedKeyBind.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ChordedKeyBind.java index 7e3b42a716..6077800164 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ChordedKeyBind.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ChordedKeyBind.java @@ -26,17 +26,36 @@ import org.quiltmc.qsl.base.api.util.InjectedInterface; import org.quiltmc.qsl.key.binds.impl.chords.KeyChord; -// TODO - Add Javadocs +// TODO - Explain what are key chords. +/** + * An interface that adds key chord support to key binds. + */ @Environment(EnvType.CLIENT) @InjectedInterface(KeyBind.class) public interface ChordedKeyBind { + /** + * Gets the bound key chord of the key bind. + * + * @return the key bind's bound key chord. + */ default KeyChord getBoundChord() { throw new UnsupportedOperationException(); } + /** + * Sets the bound key chord of the key bind. + */ default void setBoundChord(KeyChord chord) { } - // TODO - This is a temporary measure until CHASM comes. Replace it with a proper constructor or builder + // TODO - This is a temporary measure until Chasm arrives. Replace it with a proper constructor or builder + /** + * Specifies the default key chord for the key bind. + * + *

This method is to be used only on creating a key bind instance. + * + * @param keys the keys of the default key chord + * @return the original key bind instance + */ default KeyBind withChord(InputUtil.Key... keys) { throw new UnsupportedOperationException(); } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/QuiltKeyBind.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/QuiltKeyBind.java index 4380db429f..b7df79152d 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/QuiltKeyBind.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/QuiltKeyBind.java @@ -18,6 +18,9 @@ import com.mojang.blaze3d.platform.InputUtil; +/** + * An injected interface for KeyBind that adds few Quilt-relevant utility methods. + */ public interface QuiltKeyBind { /** * Gets whenever the key bind is from Vanilla or not. diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ToggleableKeyBind.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ToggleableKeyBind.java index 5e217baee6..d4dc00f0ab 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ToggleableKeyBind.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ToggleableKeyBind.java @@ -24,6 +24,9 @@ import org.quiltmc.qsl.base.api.util.InjectedInterface; // TODO - Add Javadocs; You can nab the ones from KeyBindRegistry +/** + * An interface for adding toggling capabilities to key binds. + */ @Environment(EnvType.CLIENT) @InjectedInterface(KeyBind.class) public interface ToggleableKeyBind { @@ -45,6 +48,15 @@ default boolean isDisabled() { return false; } + /** + * Gets whenever the key bind can be disabled or not. + * + * @return {@code true} if the key bind can be disabled, {@code false} otherwise + */ + default boolean canDisable() { + return false; + } + /** * Enables the key bind. * diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/InternalQuiltKeyBind.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/InternalQuiltKeyBind.java index 09247b317a..c5b8bec545 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/InternalQuiltKeyBind.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/InternalQuiltKeyBind.java @@ -16,6 +16,9 @@ package org.quiltmc.qsl.key.binds.impl; +import org.jetbrains.annotations.ApiStatus; + +@ApiStatus.Internal public interface InternalQuiltKeyBind { void markAsVanilla(); } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java index 76c28c63c8..2ba8615abe 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java @@ -20,8 +20,6 @@ import it.unimi.dsi.fastutil.objects.ReferenceArrayList; import org.jetbrains.annotations.ApiStatus; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; @@ -30,8 +28,6 @@ @Environment(EnvType.CLIENT) @ApiStatus.Internal public class KeyBindRegistryImpl { - public static final Logger LOGGER = LoggerFactory.getLogger("KeyBindRegistry"); - private static final List ALL_KEY_BINDS = new ReferenceArrayList<>(); private static final List ENABLED_KEYS = new ReferenceArrayList<>(); private static KeyBindManager keyBindManager = null; diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java index 23de75d180..62eccf53de 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java @@ -18,9 +18,10 @@ import java.util.SortedMap; -import com.mojang.blaze3d.platform.InputUtil; import it.unimi.dsi.fastutil.objects.Object2BooleanAVLTreeMap; +import com.mojang.blaze3d.platform.InputUtil; + public class KeyChord { // TODO - Private this, add methods for getting/modifying it public SortedMap keys = new Object2BooleanAVLTreeMap<>(); @@ -44,6 +45,6 @@ public boolean equals(Object o) { @Override public int hashCode() { - return keys.keySet().hashCode(); + return this.keys.keySet().hashCode(); } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java index abbfc9bc51..26c5ee6205 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java @@ -76,9 +76,9 @@ public abstract class KeyBindEntryMixin extends KeyBindListWidget.Entry implemen @Inject(method = "", at = @At("TAIL")) private void initPreviousBoundKey(KeyBindListWidget list, KeyBind key, Text text, CallbackInfo ci) { - quilt$previousProtoChord = null; + this.quilt$previousProtoChord = null; quilt$changedProtoChord = null; - quilt$addKeyNameToTooltip = false; + this.quilt$addKeyNameToTooltip = false; } @Inject( @@ -108,7 +108,7 @@ private void collectConflictTooltips(MatrixStack matrices, int index, int y, int quilt$changedProtoChord = boundProtoChord; } - quilt$addKeyNameToTooltip = true; + this.quilt$addKeyNameToTooltip = true; if (!this.key.isUnbound()) { for (KeyBind otherKey : KeyBindRegistryImpl.getKeyBinds()) { @@ -167,9 +167,9 @@ private void shortenText(MatrixStack matrices, int index, int y, int x, int entr } if (client.textRenderer.getWidth(protoText) > targetWidth) { - if (quilt$addKeyNameToTooltip) { + if (this.quilt$addKeyNameToTooltip) { this.quilt$conflictTooltips.add(0, this.key.getKeyName()); - quilt$addKeyNameToTooltip = false; + this.quilt$addKeyNameToTooltip = false; } protoText = client.textRenderer.trimToWidth(protoText, targetWidth); diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindMixin.java index aa863e81ec..805341b81f 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindMixin.java @@ -18,7 +18,6 @@ import java.util.Map; -import com.mojang.blaze3d.platform.InputUtil; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mutable; @@ -30,6 +29,8 @@ import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; +import com.mojang.blaze3d.platform.InputUtil; + import net.minecraft.client.option.KeyBind; import org.quiltmc.qsl.key.binds.api.QuiltKeyBind; diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/InputUtilKeyMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/InputUtilKeyMixin.java index f9cc027e1f..b9f2a0e7ff 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/InputUtilKeyMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/InputUtilKeyMixin.java @@ -16,10 +16,11 @@ package org.quiltmc.qsl.key.binds.mixin.client.chords; -import com.mojang.blaze3d.platform.InputUtil; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; +import com.mojang.blaze3d.platform.InputUtil; + @Mixin(InputUtil.Key.class) public abstract class InputUtilKeyMixin implements Comparable { @Override diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java index 34a746d2b3..06deb1f748 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindMixin.java @@ -67,36 +67,44 @@ public class KeyBindMixin implements ChordedKeyBind { method = "(Ljava/lang/String;Lcom/mojang/blaze3d/platform/InputUtil$Type;ILjava/lang/String;)V" ) private void initializeChordFields(String string, InputUtil.Type type, int i, String string2, CallbackInfo ci) { - quilt$defaultChord = null; - quilt$boundChord = null; + this.quilt$defaultChord = null; + this.quilt$boundChord = null; } - @Inject(at = @At("HEAD"), method = "onKeyPressed") + @Inject(at = @At("HEAD"), method = "onKeyPressed", cancellable = true) private static void detectChordsOnIncrement(InputUtil.Key startingKey, CallbackInfo ci) { + boolean cancel = false; for (KeyChord chord : KEY_BINDS_BY_CHORD.keySet()) { if (chord.keys.containsKey(startingKey) && !chord.keys.containsValue(false)) { // This ensures that the chord will only be incremented once instead of N times if (startingKey.equals(chord.keys.keySet().toArray()[0])) { KeyBind keyBind = KEY_BINDS_BY_CHORD.get(chord); ((KeyBindAccessor) keyBind).setTimesPressed(((KeyBindAccessor) keyBind).getTimesPressed() + 1); + cancel = true; } } } + + if (cancel) ci.cancel(); } - @Inject(at = @At("HEAD"), method = "setKeyPressed") + @Inject(at = @At("HEAD"), method = "setKeyPressed", cancellable = true) private static void detectChordsOnSet(InputUtil.Key startingKey, boolean pressed, CallbackInfo ci) { + boolean cancel = false; for (KeyChord chord : KEY_BINDS_BY_CHORD.keySet()) { if (chord.keys.containsKey(startingKey)) { chord.keys.put(startingKey, pressed); if (!chord.keys.containsValue(false)) { KEY_BINDS_BY_CHORD.get(chord).setPressed(true); + cancel = true; } else { KEY_BINDS_BY_CHORD.get(chord).setPressed(false); } } } + + if (cancel) ci.cancel(); } @Inject( diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java index beb48b76c6..e5c70d484c 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java @@ -78,8 +78,8 @@ private void initializeProtoChord(CallbackInfo ci) { ) private void modifyMouseClicked(double mouseX, double mouseY, int button, CallbackInfoReturnable cir) { InputUtil.Key key = InputUtil.Type.MOUSE.createFromKeyCode(button); - if (!quilt$focusedProtoChord.contains(key)) { - quilt$focusedProtoChord.add(key); + if (!this.quilt$focusedProtoChord.contains(key)) { + this.quilt$focusedProtoChord.add(key); } cir.setReturnValue(true); @@ -101,8 +101,8 @@ private void excludeFirstMouseClick(double mouseX, double mouseY, int button, Ca ) private void modifyKeyPressed(int keyCode, int scanCode, int modifiers, CallbackInfoReturnable cir) { InputUtil.Key key = InputUtil.fromKeyCode(keyCode, scanCode); - if (!quilt$focusedProtoChord.contains(key)) { - quilt$focusedProtoChord.add(key); + if (!this.quilt$focusedProtoChord.contains(key)) { + this.quilt$focusedProtoChord.add(key); } cir.setReturnValue(true); @@ -111,19 +111,19 @@ private void modifyKeyPressed(int keyCode, int scanCode, int modifiers, Callback @Override public boolean keyReleased(int keyCode, int scanCode, int modifiers) { if (this.focusedKey != null) { - if (quilt$focusedProtoChord.size() == 1) { - this.gameOptions.setKeyCode(this.focusedKey, quilt$focusedProtoChord.get(0)); - } else if (quilt$focusedProtoChord.size() > 1) { + if (this.quilt$focusedProtoChord.size() == 1) { + this.gameOptions.setKeyCode(this.focusedKey, this.quilt$focusedProtoChord.get(0)); + } else if (this.quilt$focusedProtoChord.size() > 1) { SortedMap map = new Object2BooleanAVLTreeMap<>(); - for (int i = 0; i < quilt$focusedProtoChord.size(); i++) { - map.put(quilt$focusedProtoChord.get(i), false); + for (int i = 0; i < this.quilt$focusedProtoChord.size(); i++) { + map.put(this.quilt$focusedProtoChord.get(i), false); } this.focusedKey.setBoundChord(new KeyChord(map)); QuiltKeyBindsConfigManager.updateConfig(false); } - quilt$focusedProtoChord.clear(); + this.quilt$focusedProtoChord.clear(); this.focusedKey = null; this.time = Util.getMeasuringTimeMs(); KeyBind.updateBoundKeys(); @@ -137,19 +137,19 @@ public boolean keyReleased(int keyCode, int scanCode, int modifiers) { public boolean mouseReleased(double mouseX, double mouseY, int button) { // TODO - Don't duplicate code, have a common method if (this.focusedKey != null && !this.quilt$initialMouseRelease) { - if (quilt$focusedProtoChord.size() == 1) { - this.gameOptions.setKeyCode(this.focusedKey, quilt$focusedProtoChord.get(0)); - } else if (quilt$focusedProtoChord.size() > 1) { + if (this.quilt$focusedProtoChord.size() == 1) { + this.gameOptions.setKeyCode(this.focusedKey, this.quilt$focusedProtoChord.get(0)); + } else if (this.quilt$focusedProtoChord.size() > 1) { SortedMap map = new Object2BooleanAVLTreeMap<>(); - for (int i = 0; i < quilt$focusedProtoChord.size(); i++) { - map.put(quilt$focusedProtoChord.get(i), false); + for (int i = 0; i < this.quilt$focusedProtoChord.size(); i++) { + map.put(this.quilt$focusedProtoChord.get(i), false); } this.focusedKey.setBoundChord(new KeyChord(map)); QuiltKeyBindsConfigManager.updateConfig(false); } - quilt$focusedProtoChord.clear(); + this.quilt$focusedProtoChord.clear(); this.focusedKey = null; this.time = Util.getMeasuringTimeMs(); KeyBind.updateBoundKeys(); diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/toggle/KeyBindMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/toggle/KeyBindMixin.java index 924df0fe9e..7ddea650af 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/toggle/KeyBindMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/toggle/KeyBindMixin.java @@ -20,7 +20,6 @@ import java.util.List; import java.util.Map; -import com.mojang.blaze3d.platform.InputUtil; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -29,6 +28,8 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import com.mojang.blaze3d.platform.InputUtil; + import net.minecraft.client.option.KeyBind; import org.quiltmc.qsl.key.binds.api.ToggleableKeyBind; @@ -63,26 +64,31 @@ private void initializeToggleFields(String string, InputUtil.Type type, int i, S } KeyBindRegistryImpl.registerKeyBind((KeyBind) (Object) this); - quilt$disableCounter = 0; + this.quilt$disableCounter = 0; } @Override public boolean isEnabled() { - return quilt$disableCounter == 0; + return this.quilt$disableCounter == 0; } @Override public boolean isDisabled() { - return quilt$disableCounter > 0; + return this.quilt$disableCounter > 0; + } + + @Override + public boolean canDisable() { + return true; } @Override public void enable() { // Hahahahaha no. - if (quilt$disableCounter <= 0) return; + if (this.quilt$disableCounter <= 0) return; - quilt$disableCounter--; - if (quilt$disableCounter == 0) { + this.quilt$disableCounter--; + if (this.quilt$disableCounter == 0) { KeyBindRegistryImpl.updateKeyBindState((KeyBind) (Object) this); KEY_BINDS.put(this.getTranslationKey(), (KeyBind) (Object) this); KeyBind.updateBoundKeys(); @@ -92,8 +98,8 @@ public void enable() { @Override public void disable() { - quilt$disableCounter++; - if (quilt$disableCounter == 1) { + this.quilt$disableCounter++; + if (this.quilt$disableCounter == 1) { KeyBindRegistryImpl.updateKeyBindState((KeyBind) (Object) this); KEY_BINDS.remove(this.getTranslationKey(), (KeyBind) (Object) this); KeyBind.updateBoundKeys(); From 1286571e59fcefef0b2fa057f85b5b6065acdc62 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile <85590273+EnnuiL@users.noreply.github.com> Date: Sat, 24 Jun 2023 17:15:19 -0300 Subject: [PATCH 56/56] Partially port key binds api to 1.20 --- library/gui/key_binds/build.gradle | 5 +- .../qsl/key/binds/api/ChordedKeyBind.java | 16 +- .../qsl/key/binds/api/KeyBindRegistry.java | 3 +- .../qsl/key/binds/api/QuiltKeyBind.java | 11 +- .../qsl/key/binds/api/ToggleableKeyBind.java | 29 +-- .../qsl/key/binds/impl/KeyBindManager.java | 5 +- .../key/binds/impl/KeyBindRegistryImpl.java | 6 +- .../key/binds/impl/KeyBindTooltipHolder.java | 31 --- .../qsl/key/binds/impl/chords/KeyChord.java | 7 +- .../mixin/client/EntryListWidgetAccessor.java | 31 --- .../binds/mixin/client/KeyBindEntryMixin.java | 199 ------------------ .../mixin/client/KeyBindsScreenMixin.java | 64 ------ .../client/chords/KeyBindsScreenMixin.java | 19 +- .../mixin/client/toggle/KeyBindMixin.java | 4 +- .../resources/assets/quilt_key_binds/icon.png | Bin 261 -> 252 bytes .../resources/quilt_key_binds.mixins.json | 5 +- 16 files changed, 62 insertions(+), 373 deletions(-) delete mode 100644 library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindTooltipHolder.java delete mode 100644 library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/EntryListWidgetAccessor.java delete mode 100644 library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java delete mode 100644 library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindsScreenMixin.java diff --git a/library/gui/key_binds/build.gradle b/library/gui/key_binds/build.gradle index 4b0acdb0c1..6ca3a5bd58 100644 --- a/library/gui/key_binds/build.gradle +++ b/library/gui/key_binds/build.gradle @@ -7,14 +7,14 @@ qslModule { moduleName = "key_binds" id = "quilt_key_binds" description = "Key binds registration and utilities." - library = "gui" moduleDependencies { core { api("resource_loader") - impl("qsl_base") + api("qsl_base") testmodOnly("lifecycle_events") } } + clientOnly() injectedInterface("net/minecraft/class_304") { values = [ "org/quiltmc/qsl/key/binds/api/ChordedKeyBind", @@ -22,5 +22,4 @@ qslModule { "org/quiltmc/qsl/key/binds/api/ToggleableKeyBind" ] } - clientOnly() } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ChordedKeyBind.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ChordedKeyBind.java index 6077800164..90b8b12779 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ChordedKeyBind.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ChordedKeyBind.java @@ -16,13 +16,11 @@ package org.quiltmc.qsl.key.binds.api; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; - import com.mojang.blaze3d.platform.InputUtil; import net.minecraft.client.option.KeyBind; +import org.quiltmc.loader.api.minecraft.ClientOnly; import org.quiltmc.qsl.base.api.util.InjectedInterface; import org.quiltmc.qsl.key.binds.impl.chords.KeyChord; @@ -30,7 +28,7 @@ /** * An interface that adds key chord support to key binds. */ -@Environment(EnvType.CLIENT) +@ClientOnly @InjectedInterface(KeyBind.class) public interface ChordedKeyBind { /** @@ -39,24 +37,24 @@ public interface ChordedKeyBind { * @return the key bind's bound key chord. */ default KeyChord getBoundChord() { - throw new UnsupportedOperationException(); + throw new IllegalStateException("Mixin injection failed."); } /** * Sets the bound key chord of the key bind. */ - default void setBoundChord(KeyChord chord) { } + default void setBoundChord(KeyChord chord) {} // TODO - This is a temporary measure until Chasm arrives. Replace it with a proper constructor or builder /** * Specifies the default key chord for the key bind. - * - *

This method is to be used only on creating a key bind instance. + *

+ * This method is to be used only on creating a key bind instance. * * @param keys the keys of the default key chord * @return the original key bind instance */ default KeyBind withChord(InputUtil.Key... keys) { - throw new UnsupportedOperationException(); + throw new IllegalStateException("Mixin injection failed."); } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java index fedfa6133f..9ff62342ce 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/KeyBindRegistry.java @@ -23,6 +23,7 @@ import net.minecraft.client.option.KeyBind; +import org.quiltmc.loader.api.minecraft.ClientOnly; import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; // TODO - This isn't a registry anymore; What the heck should it be named? @@ -32,7 +33,7 @@ * *

This class also allows for getting key binds registered by other mods. */ -@Environment(EnvType.CLIENT) +@ClientOnly public class KeyBindRegistry { // TODO - Shouldn't we use Vanilla's Map for getAllKeyBinds instead? Two birds, one stone, zero thoughts, head empty /** diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/QuiltKeyBind.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/QuiltKeyBind.java index b7df79152d..a967671a65 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/QuiltKeyBind.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/QuiltKeyBind.java @@ -18,9 +18,16 @@ import com.mojang.blaze3d.platform.InputUtil; +import net.minecraft.client.option.KeyBind; + +import org.quiltmc.loader.api.minecraft.ClientOnly; +import org.quiltmc.qsl.base.api.util.InjectedInterface; + /** * An injected interface for KeyBind that adds few Quilt-relevant utility methods. */ +@ClientOnly +@InjectedInterface(KeyBind.class) public interface QuiltKeyBind { /** * Gets whenever the key bind is from Vanilla or not. @@ -29,7 +36,7 @@ public interface QuiltKeyBind { * @return {@code true} if the key bind is from Vanilla, or {@code false} otherwise */ default boolean isVanilla() { - return false; + throw new IllegalStateException("Mixin injection failed."); } /** @@ -41,6 +48,6 @@ default boolean isVanilla() { * @return the key bind's bound key */ default InputUtil.Key getBoundKey() { - throw new UnsupportedOperationException(); + throw new IllegalStateException("Mixin injection failed."); } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ToggleableKeyBind.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ToggleableKeyBind.java index d4dc00f0ab..5d85a9e4b6 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ToggleableKeyBind.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/api/ToggleableKeyBind.java @@ -21,13 +21,14 @@ import net.minecraft.client.option.KeyBind; +import org.quiltmc.loader.api.minecraft.ClientOnly; import org.quiltmc.qsl.base.api.util.InjectedInterface; // TODO - Add Javadocs; You can nab the ones from KeyBindRegistry /** * An interface for adding toggling capabilities to key binds. */ -@Environment(EnvType.CLIENT) +@ClientOnly @InjectedInterface(KeyBind.class) public interface ToggleableKeyBind { /** @@ -36,7 +37,7 @@ public interface ToggleableKeyBind { * @return {@code true} if the key bind is enabled, {@code false} otherwise */ default boolean isEnabled() { - return true; + throw new IllegalStateException("Mixin injection failed."); } /** @@ -45,7 +46,7 @@ default boolean isEnabled() { * @return {@code true} if the key bind is disabled, {@code false} otherwise */ default boolean isDisabled() { - return false; + throw new IllegalStateException("Mixin injection failed."); } /** @@ -54,27 +55,31 @@ default boolean isDisabled() { * @return {@code true} if the key bind can be disabled, {@code false} otherwise */ default boolean canDisable() { - return false; + throw new IllegalStateException("Mixin injection failed."); } /** * Enables the key bind. - * - *

If the key bind has been disabled more than once, this method will only + *

+ * If the key bind has been disabled more than once, this method will only * decrement its internal counter instead of enabling the key bind. */ - default void enable() { } + default void enable() { + throw new IllegalStateException("Mixin injection failed."); + } /** * Disables the key bind. - * - *

When a key bind is disabled, it is effectively hidden from the game, + *

+ * When a key bind is disabled, it is effectively hidden from the game, * being non-existent to it. config/quilt/key_binds.json, however, will * still remember the key bind's bound keys, similar to non-existent key binds. - * - *

If the key bind is disabled while already disabled, it will be increment + *

+ * If the key bind is disabled while already disabled, it will be increment * an internal counter, making the next enable only decrement it instead of * enabling the key bind. */ - default void disable() { } + default void disable() { + throw new IllegalStateException("Mixin injection failed."); + } } diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindManager.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindManager.java index d8dbe6502c..567ee1818d 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindManager.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindManager.java @@ -17,15 +17,14 @@ package org.quiltmc.qsl.key.binds.impl; import org.jetbrains.annotations.ApiStatus; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; import net.minecraft.client.option.GameOptions; import net.minecraft.client.option.KeyBind; +import org.quiltmc.loader.api.minecraft.ClientOnly; import org.quiltmc.qsl.key.binds.mixin.client.GameOptionsAccessor; -@Environment(EnvType.CLIENT) +@ClientOnly @ApiStatus.Internal public class KeyBindManager { private final GameOptions options; diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java index 2ba8615abe..26832509d9 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindRegistryImpl.java @@ -20,12 +20,12 @@ import it.unimi.dsi.fastutil.objects.ReferenceArrayList; import org.jetbrains.annotations.ApiStatus; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; import net.minecraft.client.option.KeyBind; -@Environment(EnvType.CLIENT) +import org.quiltmc.loader.api.minecraft.ClientOnly; + +@ClientOnly @ApiStatus.Internal public class KeyBindRegistryImpl { private static final List ALL_KEY_BINDS = new ReferenceArrayList<>(); diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindTooltipHolder.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindTooltipHolder.java deleted file mode 100644 index 6475d89e90..0000000000 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/KeyBindTooltipHolder.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2021-2022 QuiltMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.quiltmc.qsl.key.binds.impl; - -import java.util.List; - -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; -import org.jetbrains.annotations.ApiStatus; - -import net.minecraft.text.Text; - -@Environment(EnvType.CLIENT) -@ApiStatus.Internal -public interface KeyBindTooltipHolder { - List getKeyBindTooltips(); -} diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java index 62eccf53de..9eded8a441 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/impl/chords/KeyChord.java @@ -22,6 +22,9 @@ import com.mojang.blaze3d.platform.InputUtil; +import org.quiltmc.loader.api.minecraft.ClientOnly; + +@ClientOnly public class KeyChord { // TODO - Private this, add methods for getting/modifying it public SortedMap keys = new Object2BooleanAVLTreeMap<>(); @@ -30,13 +33,13 @@ public KeyChord(SortedMap keys) { this.keys = keys; } - public KeyChord() { } + public KeyChord() {} @Override public boolean equals(Object o) { if (this == o) { return true; - } else if (o != null && o instanceof KeyChord keyChord) { + } else if (o instanceof KeyChord keyChord) { return this.keys.keySet().equals(keyChord.keys.keySet()); } else { return false; diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/EntryListWidgetAccessor.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/EntryListWidgetAccessor.java deleted file mode 100644 index 266398360a..0000000000 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/EntryListWidgetAccessor.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2021-2022 QuiltMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.quiltmc.qsl.key.binds.mixin.client; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.gen.Invoker; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; - -import net.minecraft.client.gui.widget.EntryListWidget; - -@Environment(EnvType.CLIENT) -@Mixin(EntryListWidget.class) -public interface EntryListWidgetAccessor> { - @Invoker - E invokeGetHoveredEntry(); -} diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java deleted file mode 100644 index 26c5ee6205..0000000000 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindEntryMixin.java +++ /dev/null @@ -1,199 +0,0 @@ -/* - * Copyright 2022 QuiltMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.quiltmc.qsl.key.binds.mixin.client; - -import java.util.List; - -import org.objectweb.asm.Opcodes; -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.ModifyArg; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import org.spongepowered.asm.mixin.injection.callback.LocalCapture; -import it.unimi.dsi.fastutil.objects.ObjectArrayList; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; - -import com.mojang.blaze3d.platform.InputUtil; - -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.widget.ButtonWidget; -import net.minecraft.client.gui.widget.option.KeyBindListWidget; -import net.minecraft.client.gui.widget.option.KeyBindListWidget.KeyBindEntry; -import net.minecraft.client.option.KeyBind; -import net.minecraft.client.util.math.MatrixStack; -import net.minecraft.text.Text; -import net.minecraft.util.Formatting; - -import org.quiltmc.qsl.key.binds.impl.KeyBindTooltipHolder; -import org.quiltmc.qsl.key.binds.impl.KeyBindRegistryImpl; -import org.quiltmc.qsl.key.binds.impl.chords.KeyChord; - -@Environment(EnvType.CLIENT) -@Mixin(KeyBindEntry.class) -public abstract class KeyBindEntryMixin extends KeyBindListWidget.Entry implements KeyBindTooltipHolder { - @Shadow - @Final - private KeyBind key; - - @Shadow - @Final - private ButtonWidget bindButton; - - @Unique - private List quilt$conflictTooltips = new ObjectArrayList<>(); - - @Unique - private List quilt$previousProtoChord; - - @Unique - private static List quilt$changedProtoChord; - - @Unique - private boolean quilt$addKeyNameToTooltip; - - @Shadow(aliases = "field_2742", remap = false) - @Final - KeyBindListWidget field_2742; - - @Inject(method = "", at = @At("TAIL")) - private void initPreviousBoundKey(KeyBindListWidget list, KeyBind key, Text text, CallbackInfo ci) { - this.quilt$previousProtoChord = null; - quilt$changedProtoChord = null; - this.quilt$addKeyNameToTooltip = false; - } - - @Inject( - method = "render", - at = @At( - value = "INVOKE", - target = "Lnet/minecraft/client/option/KeyBind;isUnbound()Z" - ), - locals = LocalCapture.CAPTURE_FAILHARD - ) - private void collectConflictTooltips(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta, CallbackInfo ci, boolean bl, boolean bl2) { - InputUtil.Key boundKey = this.key.getBoundKey(); - KeyChord boundChord = this.key.getBoundChord(); - List boundProtoChord; - - if (boundChord == null) { - boundProtoChord = List.of(boundKey); - } else { - boundProtoChord = List.copyOf(boundChord.keys.keySet()); - } - - if (!boundProtoChord.equals(this.quilt$previousProtoChord) || quilt$changedProtoChord != null) { - this.quilt$conflictTooltips.clear(); - if (quilt$changedProtoChord != null && quilt$changedProtoChord.equals(boundProtoChord)) { - quilt$changedProtoChord = null; - } else { - quilt$changedProtoChord = boundProtoChord; - } - - this.quilt$addKeyNameToTooltip = true; - - if (!this.key.isUnbound()) { - for (KeyBind otherKey : KeyBindRegistryImpl.getKeyBinds()) { - if (otherKey != this.key && this.key.keyEquals(otherKey)) { - if (this.quilt$conflictTooltips.isEmpty()) { - this.quilt$conflictTooltips.add(Text.translatable("key.qsl.key_conflict.tooltip").formatted(Formatting.RED)); - } - - this.quilt$conflictTooltips.add(Text.translatable("key.qsl.key_conflict.tooltip.entry", Text.translatable(otherKey.getTranslationKey())).formatted(Formatting.RED)); - } - } - } - } - - this.quilt$previousProtoChord = boundProtoChord; - } - - @Inject( - method = "render", - at = @At( - value = "JUMP", - opcode = Opcodes.IFEQ, - ordinal = 1, - shift = At.Shift.BEFORE - ), - locals = LocalCapture.CAPTURE_FAILHARD - ) - private void shortenText(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta, CallbackInfo ci, boolean bl, boolean bl2) { - // TODO - Get client from the parent screen instead - MinecraftClient client = MinecraftClient.getInstance(); - Text text = this.bindButton.getMessage(); - int targetWidth = bl || bl2 ? 50 - 10 : 75 - 10; - if (client.textRenderer.getWidth(text) > targetWidth) { - String protoText = text.getString(); - if (this.key.getBoundChord() != null) { - protoText = ""; - KeyChord chord = this.key.getBoundChord(); - - for (InputUtil.Key key : chord.keys.keySet()) { - if (!protoText.isEmpty()) { - protoText += " + "; - } - - String keyString = key.getDisplayText().getString(); - - if (keyString.length() > 3) { - String[] keySegments = keyString.split(" "); - keyString = ""; - for (String keySegment : keySegments) { - keyString += keySegment.substring(0, 1); - } - } - - protoText += keyString; - } - } - - if (client.textRenderer.getWidth(protoText) > targetWidth) { - if (this.quilt$addKeyNameToTooltip) { - this.quilt$conflictTooltips.add(0, this.key.getKeyName()); - this.quilt$addKeyNameToTooltip = false; - } - - protoText = client.textRenderer.trimToWidth(protoText, targetWidth); - protoText += "..."; - } - - this.bindButton.setMessage(Text.literal(protoText)); - } - } - - @ModifyArg( - method = "render", - at = @At( - value = "INVOKE", - target = "Lnet/minecraft/client/gui/widget/ButtonWidget;setMessage(Lnet/minecraft/text/Text;)V", - ordinal = 2 - ) - ) - private Text addConflictIndicator(Text originalText) { - return Text.translatable("key.qsl.key_conflict.indicator", originalText).formatted(Formatting.RED); - } - - @Override - public List getKeyBindTooltips() { - return this.quilt$conflictTooltips; - } -} diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindsScreenMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindsScreenMixin.java deleted file mode 100644 index eb6d07adc0..0000000000 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/KeyBindsScreenMixin.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright 2021-2022 QuiltMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.quiltmc.qsl.key.binds.mixin.client; - -import java.util.List; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; - -import net.minecraft.client.gui.screen.Screen; -import net.minecraft.client.gui.screen.option.GameOptionsScreen; -import net.minecraft.client.gui.screen.option.KeyBindsScreen; -import net.minecraft.client.gui.widget.option.KeyBindListWidget; -import net.minecraft.client.gui.widget.option.KeyBindListWidget.KeyBindEntry; -import net.minecraft.client.option.GameOptions; -import net.minecraft.client.util.math.MatrixStack; -import net.minecraft.text.Text; - -import org.quiltmc.qsl.key.binds.impl.KeyBindTooltipHolder; - -@Environment(EnvType.CLIENT) -@Mixin(KeyBindsScreen.class) -public abstract class KeyBindsScreenMixin extends GameOptionsScreen { - @Shadow - private KeyBindListWidget keyBindList; - - private KeyBindsScreenMixin(Screen screen, GameOptions gameOptions, Text text) { - super(screen, gameOptions, text); - } - - @SuppressWarnings("unchecked") - @Inject(method = "render", at = @At("TAIL")) - private void renderConflictTooltips(MatrixStack matrices, int mouseX, int mouseY, float delta, CallbackInfo ci) { - // TODO - Somehow extend the hover area to include the label too - KeyBindListWidget.Entry entry = ((EntryListWidgetAccessor) this.keyBindList).invokeGetHoveredEntry(); - if (entry != null && entry instanceof KeyBindEntry keyBindEntry) { - List tooltipLines = ((KeyBindTooltipHolder) keyBindEntry).getKeyBindTooltips(); - - if (tooltipLines != null) { - // TODO - With key names, it's getting too big! Add a maximum width - this.renderTooltip(matrices, tooltipLines, mouseX, mouseY); - } - } - } -} diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java index e5c70d484c..efca89912a 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/chords/KeyBindsScreenMixin.java @@ -35,6 +35,7 @@ import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.option.GameOptionsScreen; import net.minecraft.client.gui.screen.option.KeyBindsScreen; +import net.minecraft.client.gui.widget.option.KeyBindListWidget; import net.minecraft.client.option.GameOptions; import net.minecraft.client.option.KeyBind; import net.minecraft.text.Text; @@ -51,6 +52,8 @@ public abstract class KeyBindsScreenMixin extends GameOptionsScreen { @Shadow public long time; + @Shadow + private KeyBindListWidget keyBindList; @Unique private List quilt$focusedProtoChord; @@ -100,7 +103,7 @@ private void excludeFirstMouseClick(double mouseX, double mouseY, int button, Ca cancellable = true ) private void modifyKeyPressed(int keyCode, int scanCode, int modifiers, CallbackInfoReturnable cir) { - InputUtil.Key key = InputUtil.fromKeyCode(keyCode, scanCode); + var key = InputUtil.fromKeyCode(keyCode, scanCode); if (!this.quilt$focusedProtoChord.contains(key)) { this.quilt$focusedProtoChord.add(key); } @@ -115,8 +118,8 @@ public boolean keyReleased(int keyCode, int scanCode, int modifiers) { this.gameOptions.setKeyCode(this.focusedKey, this.quilt$focusedProtoChord.get(0)); } else if (this.quilt$focusedProtoChord.size() > 1) { SortedMap map = new Object2BooleanAVLTreeMap<>(); - for (int i = 0; i < this.quilt$focusedProtoChord.size(); i++) { - map.put(this.quilt$focusedProtoChord.get(i), false); + for (InputUtil.Key key : this.quilt$focusedProtoChord) { + map.put(key, false); } this.focusedKey.setBoundChord(new KeyChord(map)); @@ -126,7 +129,8 @@ public boolean keyReleased(int keyCode, int scanCode, int modifiers) { this.quilt$focusedProtoChord.clear(); this.focusedKey = null; this.time = Util.getMeasuringTimeMs(); - KeyBind.updateBoundKeys(); + this.keyBindList.update(); + return true; } else { return super.keyReleased(keyCode, scanCode, modifiers); @@ -141,8 +145,8 @@ public boolean mouseReleased(double mouseX, double mouseY, int button) { this.gameOptions.setKeyCode(this.focusedKey, this.quilt$focusedProtoChord.get(0)); } else if (this.quilt$focusedProtoChord.size() > 1) { SortedMap map = new Object2BooleanAVLTreeMap<>(); - for (int i = 0; i < this.quilt$focusedProtoChord.size(); i++) { - map.put(this.quilt$focusedProtoChord.get(i), false); + for (InputUtil.Key key : this.quilt$focusedProtoChord) { + map.put(key, false); } this.focusedKey.setBoundChord(new KeyChord(map)); @@ -152,7 +156,8 @@ public boolean mouseReleased(double mouseX, double mouseY, int button) { this.quilt$focusedProtoChord.clear(); this.focusedKey = null; this.time = Util.getMeasuringTimeMs(); - KeyBind.updateBoundKeys(); + this.keyBindList.update(); + return true; } else { this.quilt$initialMouseRelease = false; diff --git a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/toggle/KeyBindMixin.java b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/toggle/KeyBindMixin.java index 7ddea650af..481118de13 100644 --- a/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/toggle/KeyBindMixin.java +++ b/library/gui/key_binds/src/main/java/org/quiltmc/qsl/key/binds/mixin/client/toggle/KeyBindMixin.java @@ -51,11 +51,11 @@ public abstract class KeyBindMixin implements ToggleableKeyBind { public abstract String getTranslationKey(); @Shadow - abstract void reset(); + protected abstract void reset(); @Inject(method = "(Ljava/lang/String;Lcom/mojang/blaze3d/platform/InputUtil$Type;ILjava/lang/String;)V", at = @At("RETURN")) private void initializeToggleFields(String string, InputUtil.Type type, int i, String string2, CallbackInfo ci) { - for (KeyBind otherKey : QUILT$ALL_KEY_BINDS) { + for (var otherKey : QUILT$ALL_KEY_BINDS) { if (this.equals(otherKey)) { throw new IllegalArgumentException(String.format("%s has already been registered!", this.getTranslationKey())); } else if (this.getTranslationKey().equals(otherKey.getTranslationKey())) { diff --git a/library/gui/key_binds/src/main/resources/assets/quilt_key_binds/icon.png b/library/gui/key_binds/src/main/resources/assets/quilt_key_binds/icon.png index 8d54ad0021bbf0258acfe2ba452ccc0e6f36f566..c2e0d4364c4e4cf3c3891647145be988570413f4 100644 GIT binary patch delta 219 zcmV<103`o~0{j7xDSxNg{L`cV+LzmO#QfQ7+q!W7fQtVoqWxWK|J*6vmm>cHZo4x? z|BV}uB{IALL2q9GxLXli8zqi60Bai&D_;rA&j0`bcu7P-RCwCt(ZvCSPyhr`+~M2y z-)eN??!p||hex{5_kcL3Mv77@w?N*ImC};h@qxxtkjj?KQ&j&&l2I)xc0pEC&*7@8 zP674~aRjg VKmWt+>frzY002ovPDHLkV1nV$YsCNn delta 228 zcmVeC+5Eb2|I?%Y*=yU{m)qPa-CH5t_y7O^fk{L`R5;7!li3P@AP_}u(kWYP z^Z&m!syi*D1<^y$d2{YTWG*VYL48K(THuJ95}2LPI#3Xm23;io6I=l@B9j31Y8OKf zyv(bk(0fp0Y}sB4mRXFTFy&%|ge7Oz5kd0BI13eTq>pY0zQNZZpO1RWBdgYH9_vC& e9xW?<@9T}T!xgPB&AY1r0000