Skip to content

Commit

Permalink
Fix Keybindings not appearing or crashing in some versions (#158)
Browse files Browse the repository at this point in the history
* Fix keybinding common module mixin config name

* Test custom keybinding categories

* Closes #153

* Closes #132

* Add missing fabric.mod.json for keybinding versionned modules

* Bump keybindings-api-v1 version

* Apply spotless

* Fix style
  • Loading branch information
thecatcore committed Feb 10, 2024
1 parent fbfdc1c commit 8b01b57
Show file tree
Hide file tree
Showing 16 changed files with 244 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ legacy-fabric-crash-report-info-v1.version = 1.0.0
legacy-fabric-entity-events-v1.version = 1.0.0
legacy-fabric-gamerule-api-v1.version = 1.0.0
legacy-fabric-item-groups-v1.version = 2.0.0
legacy-fabric-keybindings-api-v1.version = 1.0.0
legacy-fabric-keybindings-api-v1.version = 1.0.1
legacy-fabric-lifecycle-events-v1.version = 1.0.1
legacy-fabric-logger-api-v1.version = 1.0.4
legacy-fabric-networking-api-v1.version = 2.0.0
Expand Down
Empty file.
2 changes: 2 additions & 0 deletions legacy-fabric-keybindings-api-v1/1.12.2/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minVersionIncluded=1.12.2
maxVersionIncluded=1.12.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2020 - 2022 Legacy Fabric
* Copyright (c) 2016 - 2022 FabricMC
*
* 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 net.legacyfabric.fabric.mixin.client.keybinding;

import java.util.Map;

import org.spongepowered.asm.mixin.Final;
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.minecraft.client.option.KeyBinding;

@Mixin(KeyBinding.class)
public class KeyBindingMixin {
@Shadow
@Final
private static Map<String, Integer> field_15867;
private static int lf$category_count = 7;
@Inject(method = "<init>", at = @At("RETURN"))
private void registerCategory(String string, int i, String category, CallbackInfo ci) {
if (!field_15867.containsKey(category)) {
while (field_15867.containsValue(lf$category_count)) lf$category_count++;
field_15867.put(category, lf$category_count);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"schemaVersion": 1,
"id": "legacy-fabric-keybinding-api-v1",
"name": "Legacy Fabric Keybinding API (V1)",
"version": "${version}",
"environment": "client",
"license": "Apache-2.0",
"icon": "assets/legacy-fabric-api/icon.png",
"contact": {
"homepage": "https://legacyfabric.net/",
"issues": "https://github.com/Legacy-Fabric/fabric/issues",
"sources": "https://github.com/Legacy-Fabric/fabric"
},
"authors": [
"Legacy Fabric",
"FabricMC"
],
"depends": {
"minecraft": "${minecraft_version}"
},
"mixins": [
"legacy-fabric-keybinding-api-v1.mixins.json"
],
"description": "Hooks that help adding keybindings",
"custom": {
"modmenu": {
"badges": [ "library" ],
"parent": {
"id": "legacy-fabric-api",
"name": "Legacy Fabric API",
"badges": [ "library" ],
"description": "Core API module providing key hooks and inter-compatibility features for Minecraft 1.7.10-1.12.2.",
"icon": "assets/legacy-fabric-api/icon.png"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"required": true,
"package": "net.legacyfabric.fabric.mixin.client.keybinding",
"compatibilityLevel": "JAVA_8",
"client": [
"KeyBindingMixin"
],
"injectors": {
"defaultRequire": 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2020 - 2022 Legacy Fabric
* Copyright (c) 2016 - 2022 FabricMC
*
* 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 net.legacyfabric.fabric.test.client.keybinding;

import org.lwjgl.input.Keyboard;

import net.minecraft.client.option.KeyBinding;

import net.fabricmc.api.ClientModInitializer;

import net.legacyfabric.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.legacyfabric.fabric.api.client.keybinding.v1.KeyBindingHelper;

public class KeybindingTest implements ClientModInitializer {
@Override
public void onInitializeClient() {
KeyBinding keyBinding = KeyBindingHelper.registerKeyBinding(new KeyBinding("api.keybinding.testTranslationKey", Keyboard.KEY_F, "key.categories.lftesting"));
ClientTickEvents.END_CLIENT_TICK.register(client -> {
if (keyBinding.wasPressed()) {
System.out.printf("The key %s was pressed%n", Keyboard.getKeyName(keyBinding.getCode()));
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"schemaVersion": 1,
"id": "legacy-fabric-keybinding-api-v1-testmod",
"description": "Tests for keybindings",
"version": "1.0.0",
"entrypoints": {
"client": [
"net.legacyfabric.fabric.test.client.keybinding.KeybindingTest"
]
},
"depends": {
"minecraft": "${minecraft_version}"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"schemaVersion": 1,
"id": "legacy-fabric-keybinding-api-v1",
"name": "Legacy Fabric Keybinding API (V1)",
"version": "${version}",
"environment": "client",
"license": "Apache-2.0",
"icon": "assets/legacy-fabric-api/icon.png",
"contact": {
"homepage": "https://legacyfabric.net/",
"issues": "https://github.com/Legacy-Fabric/fabric/issues",
"sources": "https://github.com/Legacy-Fabric/fabric"
},
"authors": [
"Legacy Fabric",
"FabricMC"
],
"depends": {
"minecraft": "${minecraft_version}"
},
"mixins": [],
"description": "Hooks that help adding keybindings",
"custom": {
"modmenu": {
"badges": [ "library" ],
"parent": {
"id": "legacy-fabric-api",
"name": "Legacy Fabric API",
"badges": [ "library" ],
"description": "Core API module providing key hooks and inter-compatibility features for Minecraft 1.7.10-1.12.2.",
"icon": "assets/legacy-fabric-api/icon.png"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
public class KeybindingTest implements ClientModInitializer {
@Override
public void onInitializeClient() {
KeyBinding keyBinding = KeyBindingHelper.registerKeyBinding(new KeyBinding("api.keybinding.testTranslationKey", Keyboard.KEY_F, "key.categories.misc"));
KeyBinding keyBinding = KeyBindingHelper.registerKeyBinding(new KeyBinding("api.keybinding.testTranslationKey", Keyboard.KEY_F, "key.categories.lftesting"));
ClientTickEvents.END_CLIENT_TICK.register(client -> {
if (keyBinding.wasPressed()) {
System.out.printf("The key %s was pressed%n", Keyboard.getKeyName(keyBinding.getCode()));
Expand Down
2 changes: 1 addition & 1 deletion legacy-fabric-keybindings-api-v1/1.8.9/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
minVersionIncluded=1.8
maxVersionIncluded=1.12.2
maxVersionIncluded=1.11.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"schemaVersion": 1,
"id": "legacy-fabric-keybinding-api-v1",
"name": "Legacy Fabric Keybinding API (V1)",
"version": "${version}",
"environment": "client",
"license": "Apache-2.0",
"icon": "assets/legacy-fabric-api/icon.png",
"contact": {
"homepage": "https://legacyfabric.net/",
"issues": "https://github.com/Legacy-Fabric/fabric/issues",
"sources": "https://github.com/Legacy-Fabric/fabric"
},
"authors": [
"Legacy Fabric",
"FabricMC"
],
"depends": {
"minecraft": "${minecraft_version}"
},
"mixins": [],
"description": "Hooks that help adding keybindings",
"custom": {
"modmenu": {
"badges": [ "library" ],
"parent": {
"id": "legacy-fabric-api",
"name": "Legacy Fabric API",
"badges": [ "library" ],
"description": "Core API module providing key hooks and inter-compatibility features for Minecraft 1.7.10-1.12.2.",
"icon": "assets/legacy-fabric-api/icon.png"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
public class KeybindingTest implements ClientModInitializer {
@Override
public void onInitializeClient() {
KeyBinding keyBinding = KeyBindingHelper.registerKeyBinding(new KeyBinding("api.keybinding.testTranslationKey", Keyboard.KEY_F, "key.categories.misc"));
KeyBinding keyBinding = KeyBindingHelper.registerKeyBinding(new KeyBinding("api.keybinding.testTranslationKey", Keyboard.KEY_F, "key.categories.lftesting"));
ClientTickEvents.END_CLIENT_TICK.register(client -> {
if (keyBinding.wasPressed()) {
System.out.printf("The key %s was pressed%n", Keyboard.getKeyName(keyBinding.getCode()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@

import com.google.common.collect.Lists;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.GameOptions;
import net.minecraft.client.option.KeyBinding;

public final class KeyBindingRegistryImpl {
private static final List<KeyBinding> moddedKeyBindings = Lists.newArrayList();
private static boolean processed = false;

private KeyBindingRegistryImpl() {
}
Expand All @@ -39,6 +42,10 @@ public static KeyBinding registerKeyBinding(KeyBinding binding) {
}

moddedKeyBindings.add(binding);

// In 1.7.10 Game Options are loaded before any client entrypoint, so we need to reload when a new keybinding is registered.
if (processed) reloadGameOptions();

return binding;
}

Expand All @@ -49,6 +56,21 @@ public static KeyBinding[] process(KeyBinding[] keysAll) {
List<KeyBinding> newKeysAll = Lists.newArrayList(keysAll);
newKeysAll.removeAll(moddedKeyBindings);
newKeysAll.addAll(moddedKeyBindings);

processed = true;

return newKeysAll.toArray(new KeyBinding[0]);
}

/**
* Update keybinding list and reload game options file.
*/
private static void reloadGameOptions() {
final GameOptions options = MinecraftClient.getInstance().options;

if (options != null) {
options.allKeys = process(options.allKeys);
options.load();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"minecraft": "${minecraft_version}"
},
"mixins": [
"legacy-fabric-keybinding-api-v1.mixins.json"
"legacy-fabric-keybinding-api-v1-common.mixins.json"
],
"description": "Hooks that help adding keybindings",
"custom": {
Expand Down

0 comments on commit 8b01b57

Please sign in to comment.