Skip to content

Commit

Permalink
Fix: pull in NeoForge PR as overwrite mixin to fix key bind modifiers
Browse files Browse the repository at this point in the history
Thanks, I hate it.
  • Loading branch information
Gegy committed Nov 1, 2023
1 parent 8d628f8 commit 1325d4a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.lovetropics.extras.mixin.fix;

import com.mojang.blaze3d.platform.InputConstants;
import net.minecraft.client.KeyMapping;
import net.minecraftforge.client.settings.KeyMappingLookup;
import net.minecraftforge.client.settings.KeyModifier;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;

import java.util.*;

@Mixin(value = KeyMappingLookup.class, remap = false)
public class KeyMappingLookupMixin {
@Shadow(remap = false)
@Final
private static EnumMap<KeyModifier, Map<InputConstants.Key, Collection<KeyMapping>>> map;

/**
* @author Su5eD
* @reason https://github.com/neoforged/NeoForge/pull/171
*/
@Overwrite(remap = false)
public List<KeyMapping> getAll(final InputConstants.Key keyCode) {
final List<KeyMapping> matchingBindings = new ArrayList<>();
final KeyModifier activeModifier = KeyModifier.getActiveModifier();
// Apply active modifier only if the pressed key is not the modifier itself
// Otherwise, look for key bindings without modifiers
if (activeModifier == KeyModifier.NONE || activeModifier.matches(keyCode) || !matchingBindings.addAll(findKeybinds(keyCode, activeModifier))) {
matchingBindings.addAll(findKeybinds(keyCode, KeyModifier.NONE));
}
return matchingBindings;
}

private List<KeyMapping> findKeybinds(final InputConstants.Key keyCode, final KeyModifier modifier) {
final Collection<KeyMapping> modifierBindings = map.get(modifier).get(keyCode);
if (modifierBindings != null) {
return modifierBindings.stream().filter(binding -> binding.isActiveAndMatches(keyCode)).toList();
}
return List.of();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.lovetropics.extras.mixin;
package com.lovetropics.extras.mixin.fix;

import com.mojang.brigadier.ParseResults;
import com.mojang.brigadier.context.CommandContextBuilder;
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/ltextras.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
"LivingEntityMixin",
"PlayerListMixin",
"ScaffoldingBlockMixin",
"SignableCommandMixin",
"TeamCommandMixin",
"collectible.ItemStackMixin",
"collectible.MerchantResultSlotMixin",
"collectible.ServerPlayerGameModeMixin",
"collectible.SlotMixin",
"fix.ComponentArgumentMixin",
"fix.KeyMappingLookupMixin",
"fix.SignableCommandMixin",
"perf.BiomeManagerMixin",
"perf.ChunkManagerMixin",
"tag.MappedRegistryMixin",
Expand Down

0 comments on commit 1325d4a

Please sign in to comment.