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