Skip to content

Commit

Permalink
fix: Fix non-conflicting key mappings still taking priority on Fabric…
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Sep 16, 2023
1 parent 76bde0e commit 1674cb2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package net.blay09.mods.balm.mixin;

import com.mojang.blaze3d.platform.InputConstants;
import net.blay09.mods.balm.api.client.BalmClient;
import net.minecraft.client.KeyMapping;
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.ModifyArg;

import java.util.Map;

@Mixin(KeyMapping.class)
public class FabricKeyMappingMixin {

@Final
@Shadow
private static Map<InputConstants.Key, KeyMapping> MAP;

@ModifyArg(method = "<init>(Ljava/lang/String;Lcom/mojang/blaze3d/platform/InputConstants$Type;ILjava/lang/String;)V", at = @At(value = "INVOKE", target = "Ljava/util/Map;put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", ordinal = 1), index = 1)
private Object ctor(Object key, Object value) {
if (value instanceof KeyMapping keyMapping) {
if (key instanceof InputConstants.Key && BalmClient.getKeyMappings().shouldIgnoreConflicts(keyMapping)) {
return MAP.get(key);
}
}
return value;
}

@ModifyArg(method = "resetMapping()V", at = @At(value = "INVOKE", target = "Ljava/util/Map;put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"), index = 1)
private static Object resetMappings(Object key, Object value) {
if (value instanceof KeyMapping keyMapping) {
if (key instanceof InputConstants.Key && BalmClient.getKeyMappings().shouldIgnoreConflicts(keyMapping)) {
return MAP.get(key);
}
}
return value;
}
}
1 change: 1 addition & 0 deletions fabric/src/main/resources/balm.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"KeyboardHandlerMixin",
"KeyMappingAccessor",
"KeyMappingMixin",
"FabricKeyMappingMixin",
"LevelRendererMixin",
"MinecraftMixin",
"MouseHandlerAccessor",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ default boolean isActiveAndMatches(KeyMapping keyMapping, InputConstants.Type ty
Optional<Boolean> conflictsWith(KeyMapping first, KeyMapping second);

void ignoreConflicts(KeyMapping keyMapping);

boolean shouldIgnoreConflicts(KeyMapping keyMapping);
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ public void ignoreConflicts(KeyMapping keyMapping) {
ignoreConflicts.addAll(multiModifierKeyMappings.getOrDefault(keyMapping, Collections.emptySet()));
}

@Override
public boolean shouldIgnoreConflicts(KeyMapping keyMapping) {
return ignoreConflicts.contains(keyMapping);
}

protected abstract boolean isContextActive(KeyMapping keyMapping);

protected boolean isContextActive(KeyConflictContext conflictContext) {
Expand Down

0 comments on commit 1674cb2

Please sign in to comment.