Backport contextual key bindings to 1.12.2 and below (closes #478)
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
//#if MC>=11400
|
||||
package com.replaymod.core.mixin;
|
||||
|
||||
import com.replaymod.core.ReplayMod;
|
||||
@@ -13,6 +12,7 @@ import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -24,16 +24,26 @@ import java.util.Set;
|
||||
*/
|
||||
@Mixin(KeyBinding.class)
|
||||
public class Mixin_ContextualKeyBindings {
|
||||
//#if MC>=11200
|
||||
@Shadow @Final private static Map<String, KeyBinding> keysById;
|
||||
@Unique private static Collection<KeyBinding> keyBindings() { return Mixin_ContextualKeyBindings.keysById.values(); }
|
||||
//#else
|
||||
//$$ @Shadow @Final private static List<KeyBinding> KEYBIND_ARRAY;
|
||||
//$$ @Unique private static Collection<KeyBinding> keyBindings() { return Mixin_ContextualKeyBindings.KEYBIND_ARRAY; }
|
||||
//#endif
|
||||
|
||||
@Unique private static final List<KeyBinding> temporarilyRemoved = new ArrayList<>();
|
||||
|
||||
@Inject(method = "updateKeysByCode", at = @At("HEAD"))
|
||||
private static void preContextualKeyBindings(CallbackInfo ci) {
|
||||
Set<KeyBinding> onlyInReplay = ReplayMod.instance.getKeyBindingRegistry().getOnlyInReplay();
|
||||
ReplayMod mod = ReplayMod.instance;
|
||||
if (mod == null) {
|
||||
return;
|
||||
}
|
||||
Set<KeyBinding> onlyInReplay = mod.getKeyBindingRegistry().getOnlyInReplay();
|
||||
if (ReplayModReplay.instance.getReplayHandler() != null) {
|
||||
// In replay, remove any conflicting key bindings, so that ours are guaranteed in
|
||||
Mixin_ContextualKeyBindings.keysById.values().removeIf(keyBinding -> {
|
||||
keyBindings().removeIf(keyBinding -> {
|
||||
for (KeyBinding exclusiveBinding : onlyInReplay) {
|
||||
if (keyBinding.equals(exclusiveBinding) && keyBinding != exclusiveBinding) {
|
||||
temporarilyRemoved.add(keyBinding);
|
||||
@@ -44,20 +54,25 @@ public class Mixin_ContextualKeyBindings {
|
||||
});
|
||||
} else {
|
||||
// Not in a replay, remove all replay-exclusive keybindings
|
||||
for (KeyBinding keyBinding : onlyInReplay) {
|
||||
if (Mixin_ContextualKeyBindings.keysById.remove(keyBinding.getTranslationKey()) != null) {
|
||||
keyBindings().removeIf(keyBinding -> {
|
||||
if (onlyInReplay.contains(keyBinding)) {
|
||||
temporarilyRemoved.add(keyBinding);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Inject(method = "updateKeysByCode", at = @At("RETURN"))
|
||||
private static void postContextualKeyBindings(CallbackInfo ci) {
|
||||
for (KeyBinding keyBinding : temporarilyRemoved) {
|
||||
//#if MC>=11200
|
||||
Mixin_ContextualKeyBindings.keysById.put(keyBinding.getTranslationKey(), keyBinding);
|
||||
//#else
|
||||
//$$ keyBindings().add(keyBinding);
|
||||
//#endif
|
||||
}
|
||||
temporarilyRemoved.clear();
|
||||
}
|
||||
}
|
||||
//#endif
|
||||
|
||||
@@ -373,4 +373,13 @@ class Patterns {
|
||||
//$$ return PositionedSoundRecord.createPositionedSoundRecord(sound, pitch);
|
||||
//#endif
|
||||
}
|
||||
|
||||
@Pattern
|
||||
private static boolean isKeyBindingConflicting(KeyBinding a, KeyBinding b) {
|
||||
//#if MC>=10900
|
||||
return a.equals(b);
|
||||
//#else
|
||||
//$$ return (a.getKeyCode() == b.getKeyCode());
|
||||
//#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,18 +186,14 @@ public class ReplayModReplay implements Module {
|
||||
}
|
||||
}
|
||||
replayHandler = new ReplayHandler(replayFile, asyncMode);
|
||||
//#if MC>=11400
|
||||
KeyBinding.updateKeysByCode(); // see Mixin_ContextualKeyBindings
|
||||
//#endif
|
||||
|
||||
return replayHandler;
|
||||
}
|
||||
|
||||
public void forcefullyStopReplay() {
|
||||
replayHandler = null;
|
||||
//#if MC>=11400
|
||||
KeyBinding.updateKeysByCode(); // see Mixin_ContextualKeyBindings
|
||||
//#endif
|
||||
}
|
||||
|
||||
public ReplayMod getCore() {
|
||||
|
||||
@@ -4,9 +4,9 @@
|
||||
"mixins": [],
|
||||
"server": [],
|
||||
"client": [
|
||||
"Mixin_ContextualKeyBindings",
|
||||
//#if MC>=11400
|
||||
"AbstractButtonWidgetAccessor",
|
||||
"Mixin_ContextualKeyBindings",
|
||||
"MixinGameRenderer",
|
||||
"Mixin_HideDynamicResourcePacks",
|
||||
"Mixin_RegisterDynamicResourcePacks",
|
||||
|
||||
Reference in New Issue
Block a user