From 50e0630652c6656a706b28d3429be37646b67cf9 Mon Sep 17 00:00:00 2001 From: My-Name-Is-Jeff <37018278+My-Name-Is-Jeff@users.noreply.github.com> Date: Sat, 26 Jun 2021 20:17:15 -0400 Subject: [PATCH] Add PlayerAPI support (1.2.1) --- build.gradle | 10 +++- .../SimpleToggleSprint.java | 5 +- .../mixins/MixinEntityPlayerSP.java | 5 +- .../playerapi/MixinClientPlayerAPI.java | 23 ++++++++ .../tweaker/MixinPlugin.java | 59 +++++++++++++++++++ .../resources/mixins.simpletogglesprint.json | 6 +- 6 files changed, 101 insertions(+), 7 deletions(-) create mode 100644 src/main/java/mynameisjeff/simpletogglesprint/mixins/playerapi/MixinClientPlayerAPI.java create mode 100644 src/main/java/mynameisjeff/simpletogglesprint/tweaker/MixinPlugin.java diff --git a/build.gradle b/build.gradle index b26678c..99f1c61 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { id "java" } -version = "1.2" +version = "1.2.1" group = "mynameisjeff.simpletogglesprint" archivesBaseName = "SimpleToggleSprint" @@ -25,8 +25,13 @@ repositories { maven { url "https://repo.spongepowered.org/repository/maven-public/" } } +configurations { + bundle + implementation.extendsFrom(bundle) +} + dependencies { - implementation("org.spongepowered:mixin:0.7.11-SNAPSHOT") { + bundle("org.spongepowered:mixin:0.7.11-SNAPSHOT") { transitive = false exclude module: "guava" exclude module: "commons-io" @@ -62,6 +67,7 @@ jar { shadowJar { archiveFileName = jar.archiveFileName duplicatesStrategy = DuplicatesStrategy.EXCLUDE + configurations = [project.configurations.bundle] exclude "**/module-info.class" exclude "dummyThing" diff --git a/src/main/java/mynameisjeff/simpletogglesprint/SimpleToggleSprint.java b/src/main/java/mynameisjeff/simpletogglesprint/SimpleToggleSprint.java index 80a65c9..89988c5 100644 --- a/src/main/java/mynameisjeff/simpletogglesprint/SimpleToggleSprint.java +++ b/src/main/java/mynameisjeff/simpletogglesprint/SimpleToggleSprint.java @@ -47,7 +47,7 @@ public class SimpleToggleSprint { public static final String MODID = "simpletogglesprint"; public static final String MOD_NAME = "SimpleToggleSprint"; - public static final String VERSION = "1.2"; + public static final String VERSION = "1.2.1"; public static final Minecraft mc = Minecraft.getMinecraft(); public static final KeyBinding keySprint = new KeyBinding("Toggle Sprint", Keyboard.KEY_NONE, "SimpleToggleSprint"); public static final KeyBinding keySneak = new KeyBinding("Toggle Sneak", Keyboard.KEY_NONE, "SimpleToggleSprint"); @@ -174,4 +174,7 @@ public boolean isActive() { } } + public static boolean shouldSetSprint(KeyBinding keyBinding) { + return keyBinding.isKeyDown() || (Config.enabledToggleSprint && Config.toggleSprintState && keyBinding == mc.gameSettings.keyBindSprint && mc.currentScreen == null); + } } \ No newline at end of file diff --git a/src/main/java/mynameisjeff/simpletogglesprint/mixins/MixinEntityPlayerSP.java b/src/main/java/mynameisjeff/simpletogglesprint/mixins/MixinEntityPlayerSP.java index 9cad950..6aca20e 100644 --- a/src/main/java/mynameisjeff/simpletogglesprint/mixins/MixinEntityPlayerSP.java +++ b/src/main/java/mynameisjeff/simpletogglesprint/mixins/MixinEntityPlayerSP.java @@ -19,6 +19,7 @@ package mynameisjeff.simpletogglesprint.mixins; import com.mojang.authlib.GameProfile; +import mynameisjeff.simpletogglesprint.SimpleToggleSprint; import mynameisjeff.simpletogglesprint.core.Config; import net.minecraft.client.Minecraft; import net.minecraft.client.entity.AbstractClientPlayer; @@ -32,8 +33,6 @@ @Mixin(EntityPlayerSP.class) public abstract class MixinEntityPlayerSP extends AbstractClientPlayer { - @Shadow - protected Minecraft mc; public MixinEntityPlayerSP(World worldIn, GameProfile playerProfile) { super(worldIn, playerProfile); @@ -41,6 +40,6 @@ public MixinEntityPlayerSP(World worldIn, GameProfile playerProfile) { @Redirect(method = "onLivingUpdate", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/settings/KeyBinding;isKeyDown()Z")) private boolean setSprintState(KeyBinding keyBinding) { - return keyBinding.isKeyDown() || (Config.enabledToggleSprint && Config.toggleSprintState && keyBinding == this.mc.gameSettings.keyBindSprint && mc.currentScreen == null); + return SimpleToggleSprint.shouldSetSprint(keyBinding); } } diff --git a/src/main/java/mynameisjeff/simpletogglesprint/mixins/playerapi/MixinClientPlayerAPI.java b/src/main/java/mynameisjeff/simpletogglesprint/mixins/playerapi/MixinClientPlayerAPI.java new file mode 100644 index 0000000..a6bb9d3 --- /dev/null +++ b/src/main/java/mynameisjeff/simpletogglesprint/mixins/playerapi/MixinClientPlayerAPI.java @@ -0,0 +1,23 @@ +package mynameisjeff.simpletogglesprint.mixins.playerapi; + +import com.mojang.authlib.GameProfile; +import mynameisjeff.simpletogglesprint.SimpleToggleSprint; +import net.minecraft.client.entity.AbstractClientPlayer; +import net.minecraft.client.entity.EntityPlayerSP; +import net.minecraft.client.settings.KeyBinding; +import net.minecraft.world.World; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Pseudo; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +@Mixin(EntityPlayerSP.class) +public abstract class MixinClientPlayerAPI extends AbstractClientPlayer { + public MixinClientPlayerAPI(World worldIn, GameProfile playerProfile) { + super(worldIn, playerProfile); + } + + @Redirect(method = "localOnLivingUpdate", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/settings/KeyBinding;isKeyDown()Z")) + private boolean setSprintState(KeyBinding keyBinding) { + return SimpleToggleSprint.shouldSetSprint(keyBinding); + }} diff --git a/src/main/java/mynameisjeff/simpletogglesprint/tweaker/MixinPlugin.java b/src/main/java/mynameisjeff/simpletogglesprint/tweaker/MixinPlugin.java new file mode 100644 index 0000000..bb27afc --- /dev/null +++ b/src/main/java/mynameisjeff/simpletogglesprint/tweaker/MixinPlugin.java @@ -0,0 +1,59 @@ +package mynameisjeff.simpletogglesprint.tweaker; + +import net.minecraftforge.fml.relauncher.CoreModManager; +import org.spongepowered.asm.lib.tree.ClassNode; +import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; +import org.spongepowered.asm.mixin.extensibility.IMixinInfo; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class MixinPlugin implements IMixinConfigPlugin { + + private boolean hasPlayerAPI = false; + + @Override + public void onLoad(String mixinPackage) { + for (Map.Entry> e : CoreModManager.getTransformers().entrySet()) { + if (e.getKey().startsWith("PlayerAPIPlugin") && e.getValue().contains("api.player.forge.PlayerAPITransformer")) { + System.out.println("PlayerAPI detected."); + hasPlayerAPI = true; + break; + } + } + } + + @Override + public String getRefMapperConfig() { + return null; + } + + @Override + public boolean shouldApplyMixin(String targetClassName, String mixinClassName) { + if (mixinClassName.startsWith("mynameisjeff.simpletogglesprint.mixins.playerapi.")) { + return hasPlayerAPI; + } + return true; + } + + @Override + public void acceptTargets(Set myTargets, Set otherTargets) { + + } + + @Override + public List getMixins() { + return null; + } + + @Override + public void preApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) { + + } + + @Override + public void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) { + + } +} diff --git a/src/main/resources/mixins.simpletogglesprint.json b/src/main/resources/mixins.simpletogglesprint.json index 821dea5..b938684 100644 --- a/src/main/resources/mixins.simpletogglesprint.json +++ b/src/main/resources/mixins.simpletogglesprint.json @@ -2,10 +2,14 @@ "compatibilityLevel": "JAVA_8", "package": "mynameisjeff.simpletogglesprint.mixins", "refmap": "mixins.simpletogglesprint.refmap.json", + "plugin": "mynameisjeff.simpletogglesprint.tweaker.MixinPlugin", "mixins": [ "MixinEntityPlayerSP", "MixinGuiIngameMenu", "MixinMovementInputFromOptions" ], - "verbose": true + "verbose": true, + "client": [ + "playerapi.MixinClientPlayerAPI" + ] } \ No newline at end of file