Fix conflicting keybindings not having the correct click count set. #9360

This commit is contained in:
Random832 authored and LexManos committed 2023-07-15 10:44:29 -07:00
1 parent 03a505a3f2
commit cf3d42ef38
4 files changed
+82 -1

No files matched your search

@@ -27,6 +27,7 @@ public class KeyMappingLookup
}
}
@Deprecated(forRemoval = true)
@Nullable
public KeyMapping get(InputConstants.Key keyCode)
{
@@ -59,6 +60,16 @@ public class KeyMappingLookup
return null;
}
/**
* Returns all active keys associated with the given key code and the active
* modifiers and conflict context.
*
* This could probably be renamed to something like getActiveMappings, but
* that would be a breaking change.
*
* @param keyCode the key being pressed
* @return the list of key mappings
*/
public List<KeyMapping> getAll(InputConstants.Key keyCode)
{
List<KeyMapping> matchingBindings = new ArrayList<KeyMapping>();
@@ -67,7 +78,11 @@ public class KeyMappingLookup
Collection<KeyMapping> bindings = bindingsMap.get(keyCode);
if (bindings != null)
{
matchingBindings.addAll(bindings);
for (KeyMapping binding : bindings)
{
if (binding.isConflictContextAndModifierActive())
matchingBindings.add(binding);
}
}
}
return matchingBindings;
@@ -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!"));
}
}
}
}
+2
View File
@@ -297,6 +297,8 @@ modId="chunk_event_load_new_chunk_test"
modId="alter_ground_event_test"
[[mods]]
modId="custom_fluid_container_test"
[[mods]]
modId="keymapping_test"
# ADD ABOVE THIS LINE