Skip to content

Commit

Permalink
Add PlayerAPI support (1.2.1)
Browse files Browse the repository at this point in the history
  • Loading branch information
My-Name-Is-Jeff committed Jun 27, 2021
1 parent 89caa62 commit 50e0630
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 7 deletions.
10 changes: 8 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
id "java"
}

version = "1.2"
version = "1.2.1"
group = "mynameisjeff.simpletogglesprint"
archivesBaseName = "SimpleToggleSprint"

Expand All @@ -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"
Expand Down Expand Up @@ -62,6 +67,7 @@ jar {
shadowJar {
archiveFileName = jar.archiveFileName
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
configurations = [project.configurations.bundle]

exclude "**/module-info.class"
exclude "dummyThing"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,15 +33,13 @@

@Mixin(EntityPlayerSP.class)
public abstract class MixinEntityPlayerSP extends AbstractClientPlayer {
@Shadow
protected Minecraft mc;

public MixinEntityPlayerSP(World worldIn, GameProfile playerProfile) {
super(worldIn, 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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}}
Original file line number Diff line number Diff line change
@@ -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<String, List<String>> 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<String> myTargets, Set<String> otherTargets) {

}

@Override
public List<String> 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) {

}
}
6 changes: 5 additions & 1 deletion src/main/resources/mixins.simpletogglesprint.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}

0 comments on commit 50e0630

Please sign in to comment.