From 150e8281aeca398abba303a2caa26bf4a5204f10 Mon Sep 17 00:00:00 2001 From: Random832 Date: Sat, 15 Jul 2023 09:27:51 -0700 Subject: [PATCH] Fix conflicting keybindings not having the correct click count set. #9360 --- .../minecraft/client/KeyMapping.java.patch | 9 +++ .../client/settings/KeyMappingLookup.java | 10 ++++ .../minecraftforge/debug/KeyMappingTest.java | 55 +++++++++++++++++++ src/test/resources/META-INF/mods.toml | 2 + 4 files changed, 76 insertions(+) create mode 100644 src/test/java/net/minecraftforge/debug/KeyMappingTest.java diff --git a/patches/minecraft/net/minecraft/client/KeyMapping.java.patch b/patches/minecraft/net/minecraft/client/KeyMapping.java.patch index 0ba9b755a7..b9a00bab66 100644 --- a/patches/minecraft/net/minecraft/client/KeyMapping.java.patch +++ b/patches/minecraft/net/minecraft/client/KeyMapping.java.patch @@ -12,6 +12,15 @@ private static final Set f_90811_ = Sets.newHashSet(); public static final String f_167805_ = "key.categories.movement"; public static final String f_167806_ = "key.categories.misc"; +@@ -41,7 +_,7 @@ + private int f_90818_; + + public static void m_90835_(InputConstants.Key p_90836_) { +- KeyMapping keymapping = f_90810_.get(p_90836_); ++ for (KeyMapping keymapping : f_90810_.getAll(p_90836_)) + if (keymapping != null) { + ++keymapping.f_90818_; + } @@ -49,7 +_,7 @@ } diff --git a/src/main/java/net/minecraftforge/client/settings/KeyMappingLookup.java b/src/main/java/net/minecraftforge/client/settings/KeyMappingLookup.java index 795d3486ed..f95f1b9de3 100644 --- a/src/main/java/net/minecraftforge/client/settings/KeyMappingLookup.java +++ b/src/main/java/net/minecraftforge/client/settings/KeyMappingLookup.java @@ -27,6 +27,8 @@ public class KeyMappingLookup } } + /** Replaced by {@link #getAll(InputConstants.Key) getAll} */ + @Deprecated(forRemoval = true, since = "1.20.1") @Nullable public KeyMapping get(InputConstants.Key keyCode) { @@ -43,6 +45,7 @@ public class KeyMappingLookup } @Nullable + @Deprecated(forRemoval = true, since = "1.20.1") private KeyMapping get(InputConstants.Key keyCode, KeyModifier keyModifier) { Collection bindings = map.get(keyModifier).get(keyCode); @@ -59,6 +62,13 @@ public class KeyMappingLookup return null; } + /** + * Returns all active keys associated with the given key code and the active + * modifiers and conflict context. + * + * @param keyCode the key being pressed + * @return the list of key mappings + */ public List getAll(InputConstants.Key keyCode) { List matchingBindings = new ArrayList(); diff --git a/src/test/java/net/minecraftforge/debug/KeyMappingTest.java b/src/test/java/net/minecraftforge/debug/KeyMappingTest.java new file mode 100644 index 0000000000..c11579b607 --- /dev/null +++ b/src/test/java/net/minecraftforge/debug/KeyMappingTest.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) Forge Development LLC and contributors + * SPDX-License-Identifier: LGPL-2.1-only + */ + +package net.minecraftforge.debug; + +import com.mojang.blaze3d.platform.InputConstants; +import net.minecraft.client.KeyMapping; +import net.minecraft.client.Minecraft; +import net.minecraft.network.chat.Component; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.Items; +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.client.event.RegisterKeyMappingsEvent; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.TickEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.event.lifecycle.FMLConstructModEvent; + +@Mod("keymapping_test") +public class KeyMappingTest +{ + @Mod.EventBusSubscriber(modid = "keymapping_test", value = Dist.CLIENT, bus = Mod.EventBusSubscriber.Bus.MOD) + public static class ClientStuff { + // these are two separate keys to stand in for keys added by different + // mods that each do something similar with a held item from the + // respective mod, so the user wants them on the same physical key. + static KeyMapping stickKey = new KeyMapping("stick_key", InputConstants.KEY_BACKSLASH, KeyMapping.CATEGORY_MISC); + static KeyMapping rockKey = new KeyMapping("rock_key", InputConstants.KEY_BACKSLASH, KeyMapping.CATEGORY_MISC); + @SubscribeEvent + public static void init(FMLConstructModEvent event) { + MinecraftForge.EVENT_BUS.addListener(ClientStuff::tick); + } + @SubscribeEvent + public static void initKeys(RegisterKeyMappingsEvent event) { + event.register(stickKey); + event.register(rockKey); + } + public static void tick(TickEvent.ClientTickEvent event) { + if(event.phase != TickEvent.Phase.START) return; + if(stickKey.consumeClick()) { + Player player = Minecraft.getInstance().player; + if(player != null && player.getMainHandItem().is(Items.STICK)) + player.sendSystemMessage(Component.literal("stick found!")); + } + if(rockKey.consumeClick()) { + Player player = Minecraft.getInstance().player; + if(player != null && player.getMainHandItem().is(Items.COBBLESTONE)) + player.sendSystemMessage(Component.literal("rock found!")); + } + } + } +} \ No newline at end of file diff --git a/src/test/resources/META-INF/mods.toml b/src/test/resources/META-INF/mods.toml index ca5d5a1a94..c0066344f0 100644 --- a/src/test/resources/META-INF/mods.toml +++ b/src/test/resources/META-INF/mods.toml @@ -297,5 +297,7 @@ modId="chunk_event_load_new_chunk_test" modId="custom_fluid_container_test" [[mods]] modId="alter_ground_event_test" +[[mods]] +modId="keymapping_test" # ADD ABOVE THIS LINE