Optional Late Mixins

This commit is contained in:
KAMKEEL committed 2026-09-17 19:45:03 -04:00
1 parent 3d530a207a
commit 3ee0df1865
9 files changed
+468 -21

No files matched your search

@@ -0,0 +1,35 @@
package noppes.npcs;
import com.gtnewhorizon.gtnhmixins.ILateMixinLoader;
import com.gtnewhorizon.gtnhmixins.LateMixin;
import cpw.mods.fml.relauncher.FMLLaunchHandler;
import noppes.npcs.config.ConfigMixin;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
* The mixins into other mods' player models, loaded by UniMixins after every
* mod jar is on the class path. Only found when UniMixins is installed; the
* early plugin then leaves these out.
*/
@LateMixin
public class CustomNPCsLateMixins implements ILateMixinLoader {
@Override
public String getMixinConfig() {
return "mixins.customnpcs.late.json";
}
@Override
public List<String> getMixins(Set<String> loadedMods) {
List<String> mixins = new ArrayList<>();
if (!FMLLaunchHandler.side().isClient() || !ConfigMixin.AnimationMixin) {
return mixins;
}
mixins.addAll(OptionalModelMixins.playerModelMixins(true));
System.out.println("[CustomNPC+] Late player model mixins: " + mixins);
return mixins;
}
}
@@ -0,0 +1,115 @@
package noppes.npcs;
import net.minecraft.launchwrapper.Launch;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipFile;
/**
* Which mixins into other mods' player models to load, each only when its
* class is there. With UniMixins the late loader asks after every mod jar is
* on the class path. Without it the early plugin asks before that, finds the
* jar in the mods folder and lends it to the class loader so Mixin can read
* the target.
*/
public final class OptionalModelMixins {
/** Jars lent to the class loader early, taken off the source list again once read. */
private static final List<URL> lentJars = new ArrayList<URL>();
private OptionalModelMixins() {
}
/** UniMixins is a coremod, so its late loader interface is readable this early. */
public static boolean lateLoaderAvailable() {
try {
return Launch.classLoader.getClassBytes("com.gtnewhorizon.gtnhmixins.ILateMixinLoader") != null;
} catch (IOException exception) {
return false;
}
}
/** The mixin names, relative to the late package. */
public static List<String> playerModelMixins(boolean late) {
List<String> mixins = new ArrayList<String>();
if (hasClass("noppes.mpm.client.model.ModelMPM", late)) {
mixins.add("MixinMPMModel");
}
if (hasClass("noppes.mpm.client.model.ModelScaleRenderer", late)) {
mixins.add("MixinMPMModelScaleRenderer");
}
if (hasClass("micdoodle8.mods.galacticraft.core.client.model.ModelPlayerGC", late)) {
mixins.add("MixinGalacticraftPlayerModel");
}
if (hasClass("api.player.model.ModelPlayerAPI", late)) {
mixins.add("MixinPlayerAPIModel");
}
return mixins;
}
/**
* FML lists every class-path source as a mod source, so a lent jar left
* on the list is found twice and refused as a duplicate. Off the list the
* jar stays readable, and FML adds it once itself.
*/
public static void releaseLentJars() {
if (lentJars.isEmpty()) return;
Launch.classLoader.getSources().removeAll(lentJars);
System.out.println("[CustomNPC+] Took " + lentJars.size() + " optional mod jar(s) back off the source list");
lentJars.clear();
}
private static boolean hasClass(String className, boolean late) {
try {
if (Launch.classLoader.getClassBytes(className) != null) {
System.out.println("[CustomNPC+] Optional class " + className + ": on the class path");
return true;
}
} catch (IOException exception) {
System.out.println("[CustomNPC+] Could not inspect optional class " + className + ": " + exception);
}
File jar = late ? null : jarHolding(className);
if (jar == null) {
System.out.println("[CustomNPC+] Optional class " + className + ": not found");
return false;
}
// Mixin drops a @Pseudo target it cannot read when the config is
// prepared. Lending the jar now lets it read the class.
try {
URL url = jar.toURI().toURL();
Launch.classLoader.addURL(url);
lentJars.add(url);
System.out.println("[CustomNPC+] Optional class " + className + ": in mods/"
+ jar.getName() + ", lent to the class path");
return true;
} catch (MalformedURLException exception) {
System.out.println("[CustomNPC+] Optional class " + className + ": in mods/"
+ jar.getName() + " but it could not be lent to the class path: " + exception);
return false;
}
}
/** The jar under mods/ that holds the class, or null. */
private static File jarHolding(String className) {
String entry = className.replace('.', '/') + ".class";
File[] folders = {new File("mods"), new File("mods" + File.separator + "1.7.10")};
for (File folder : folders) {
File[] jars = folder.listFiles();
if (jars == null) continue;
for (File jar : jars) {
if (!jar.getName().toLowerCase().endsWith(".jar")) continue;
try (ZipFile zip = new ZipFile(jar)) {
if (zip.getEntry(entry) != null) return jar;
} catch (IOException ignored) {
// Not a readable jar; nothing to find in it.
}
}
}
return null;
}
}
@@ -0,0 +1,84 @@
package noppes.npcs.client.model;
import net.minecraft.client.model.ModelBiped;
import net.minecraft.client.model.ModelRenderer;
import noppes.npcs.AnimationMixinFunctions;
import java.util.HashSet;
import java.util.Set;
import noppes.npcs.client.ClientEventHandler;
public final class PlayerModelAnimation {
private PlayerModelAnimation() {
}
public static Pose capture(ModelBiped model) {
return new Pose(parts(model));
}
private static final Set<String> REPORTED = new HashSet<String>();
public static void apply(ModelBiped model) {
boolean applied = false;
for (ModelRenderer part : parts(model)) {
if (part != null && AnimationMixinFunctions.applyValues(part)) {
applied = true;
}
}
// Once per model class and outcome: whether the hook had an animation to put on.
String key = model.getClass().getName() + (applied ? " applied" : " nothing");
if (REPORTED.add(key)) {
System.out.println("[CustomNPC+] Player animation on " + model.getClass().getName()
+ ": " + (applied ? "values applied" : "nothing to apply")
+ " (renderingPlayer=" + (ClientEventHandler.renderingPlayer != null)
+ ", renderingNpc=" + (ClientEventHandler.renderingNpc != null) + ")");
}
}
private static ModelRenderer[] parts(ModelBiped model) {
return new ModelRenderer[]{
model.bipedHead,
model.bipedBody,
model.bipedRightArm,
model.bipedLeftArm,
model.bipedRightLeg,
model.bipedLeftLeg
};
}
public static final class Pose {
private final ModelRenderer[] parts;
private final float[][] values;
private Pose(ModelRenderer[] parts) {
this.parts = parts;
this.values = new float[parts.length][6];
for (int i = 0; i < parts.length; i++) {
ModelRenderer part = parts[i];
if (part == null) {
continue;
}
values[i][0] = part.rotationPointX;
values[i][1] = part.rotationPointY;
values[i][2] = part.rotationPointZ;
values[i][3] = part.rotateAngleX;
values[i][4] = part.rotateAngleY;
values[i][5] = part.rotateAngleZ;
}
}
public void restore() {
for (int i = 0; i < parts.length; i++) {
ModelRenderer part = parts[i];
if (part == null) {
continue;
}
part.rotationPointX = values[i][0];
part.rotationPointY = values[i][1];
part.rotationPointZ = values[i][2];
part.rotateAngleX = values[i][3];
part.rotateAngleY = values[i][4];
part.rotateAngleZ = values[i][5];
}
}
}
}
@@ -1,6 +1,7 @@
package noppes.npcs.mixin;
import cpw.mods.fml.relauncher.FMLLaunchHandler;
import noppes.npcs.OptionalModelMixins;
import noppes.npcs.config.ConfigMixin;
import org.spongepowered.asm.lib.tree.ClassNode;
import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
@@ -29,7 +30,8 @@ public class CustomNPCsMixinPlugin implements IMixinConfigPlugin {
@Override
public void acceptTargets(Set<String> myTargets, Set<String> otherTargets) {
// The targets are read by now; a jar lent so Mixin could read one goes back.
OptionalModelMixins.releaseLentJars();
}
@Override
@@ -52,10 +54,16 @@ public class CustomNPCsMixinPlugin implements IMixinConfigPlugin {
}
if (ConfigMixin.AnimationMixin) {
mixins.add("MixinModelRenderer");
mixins.add("MixinMPMModel");
mixins.add("MixinMPMModelScaleRenderer");
if (OptionalModelMixins.lateLoaderAvailable()) {
System.out.println("[CustomNPC+] UniMixins is here; the other mods' model mixins load late");
} else {
for (String name : OptionalModelMixins.playerModelMixins(false)) {
mixins.add("late." + name);
}
}
mixins.add("MixinRendererLivingEntity");
}
System.out.println("[CustomNPC+] Client mixins chosen: " + mixins);
if (ConfigMixin.FirstPersonAnimationMixin) {
mixins.add("MixinItemRenderer");
}
@@ -76,15 +84,21 @@ public class CustomNPCsMixinPlugin implements IMixinConfigPlugin {
@Override
public void preApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {
if (mixinClassName.contains("MixinMPMModel")) {
System.out.println("[CustomNPC+] Applying MPM animation mixin to " + targetClassName);
if (isPlayerCompatibilityMixin(mixinClassName)) {
System.out.println("[CustomNPC+] Applying player animation mixin to " + targetClassName);
}
}
@Override
public void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) {
if (mixinClassName.contains("MixinMPMModel")) {
System.out.println("[CustomNPC+] Applied MPM animation mixin to " + targetClassName);
if (isPlayerCompatibilityMixin(mixinClassName)) {
System.out.println("[CustomNPC+] Applied player animation mixin to " + targetClassName);
}
}
private static boolean isPlayerCompatibilityMixin(String mixinClassName) {
return mixinClassName.contains("MixinMPMModel")
|| mixinClassName.contains("MixinGalacticraftPlayerModel")
|| mixinClassName.contains("MixinPlayerAPIModel");
}
}
@@ -0,0 +1,79 @@
package noppes.npcs.mixin.late;
import net.minecraft.client.model.ModelBiped;
import noppes.npcs.client.model.PlayerModelAnimation;
import net.minecraft.entity.Entity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Pseudo;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
/** Applies player animations after Galacticraft calculates its player pose. */
@Pseudo
@Mixin(targets = "micdoodle8.mods.galacticraft.core.client.model.ModelPlayerGC", remap = false)
public abstract class MixinGalacticraftPlayerModel {
@Unique private PlayerModelAnimation.Pose cnpc$pose;
@Unique private boolean cnpc$rendering;
@Unique private static boolean cnpc$loggedHook;
@Inject(
method = {
"render(Lnet/minecraft/entity/Entity;FFFFFF)V",
"func_78088_a(Lnet/minecraft/entity/Entity;FFFFFF)V"
},
at = @At("HEAD"),
remap = false,
require = 1
)
private void cnpc$beginRender(Entity entity, float limbSwing, float limbSwingAmount,
float age, float yaw, float pitch, float scale,
CallbackInfo callbackInfo) {
ModelBiped model = (ModelBiped) (Object) this;
cnpc$pose = PlayerModelAnimation.capture(model);
cnpc$rendering = true;
}
@Inject(
method = {
"setRotationAngles(FFFFFFLnet/minecraft/entity/Entity;)V",
"func_78087_a(FFFFFFLnet/minecraft/entity/Entity;)V"
},
at = @At("RETURN"),
remap = false,
require = 1
)
private void cnpc$applyAnimation(float limbSwing, float limbSwingAmount, float age,
float yaw, float pitch, float scale, Entity entity,
CallbackInfo callbackInfo) {
if (!cnpc$rendering) {
return;
}
ModelBiped model = (ModelBiped) (Object) this;
PlayerModelAnimation.apply(model);
if (!cnpc$loggedHook) {
cnpc$loggedHook = true;
System.out.println("[CustomNPC+] Galacticraft ModelPlayerGC animation hook is running");
}
}
@Inject(
method = {
"render(Lnet/minecraft/entity/Entity;FFFFFF)V",
"func_78088_a(Lnet/minecraft/entity/Entity;FFFFFF)V"
},
at = @At("RETURN"),
remap = false,
require = 1
)
private void cnpc$endRender(Entity entity, float limbSwing, float limbSwingAmount,
float age, float yaw, float pitch, float scale,
CallbackInfo callbackInfo) {
if (cnpc$pose != null) {
cnpc$pose.restore();
}
cnpc$pose = null;
cnpc$rendering = false;
}
}
@@ -1,9 +1,8 @@
package noppes.npcs.mixin;
package noppes.npcs.mixin.late;
import net.minecraft.client.model.ModelBiped;
import net.minecraft.client.model.ModelRenderer;
import noppes.npcs.client.model.PlayerModelAnimation;
import net.minecraft.entity.Entity;
import noppes.npcs.AnimationMixinFunctions;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Pseudo;
import org.spongepowered.asm.mixin.Unique;
@@ -21,6 +20,7 @@ public abstract class MixinMPMModel {
@Unique private static boolean cnpc$loggedModelEntry;
@Unique private static boolean cnpc$loggedModelHook;
@Unique private PlayerModelAnimation.Pose cnpc$pose;
@Inject(
method = {
@@ -34,6 +34,7 @@ public abstract class MixinMPMModel {
private void cnpc$logModelEntry(Entity entity, float limbSwing, float limbSwingAmount,
float age, float yaw, float pitch, float scale,
CallbackInfo callbackInfo) {
cnpc$pose = PlayerModelAnimation.capture((ModelBiped) (Object) this);
if (!cnpc$loggedModelEntry) {
cnpc$loggedModelEntry = true;
System.out.println("[CustomNPC+] MPM ModelMPM render method is running");
@@ -64,16 +65,24 @@ public abstract class MixinMPMModel {
+ model.bipedRightArm.getClass().getName());
}
ModelRenderer[] parts = new ModelRenderer[]{
model.bipedHead,
model.bipedBody,
model.bipedRightArm,
model.bipedLeftArm,
model.bipedRightLeg,
model.bipedLeftLeg
};
for (ModelRenderer part : parts) {
AnimationMixinFunctions.applyValues(part);
PlayerModelAnimation.apply(model);
}
@Inject(
method = {
"render(Lnet/minecraft/entity/Entity;FFFFFF)V",
"func_78088_a(Lnet/minecraft/entity/Entity;FFFFFF)V"
},
at = @At("RETURN"),
remap = false,
require = 1
)
private void cnpc$restorePlayerPose(Entity entity, float limbSwing, float limbSwingAmount,
float age, float yaw, float pitch, float scale,
CallbackInfo callbackInfo) {
if (cnpc$pose != null) {
cnpc$pose.restore();
cnpc$pose = null;
}
}
}
@@ -1,4 +1,4 @@
package noppes.npcs.mixin;
package noppes.npcs.mixin.late;
import net.minecraft.client.model.ModelRenderer;
import noppes.npcs.AnimationMixinFunctions;
@@ -0,0 +1,100 @@
package noppes.npcs.mixin.late;
import net.minecraft.client.model.ModelBiped;
import noppes.npcs.client.model.PlayerModelAnimation;
import net.minecraft.entity.Entity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Pseudo;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.lang.reflect.Field;
/** Applies player animations after PlayerAPI and its model bases calculate the pose. */
@Pseudo
@Mixin(targets = "api.player.model.ModelPlayerAPI", remap = false)
public abstract class MixinPlayerAPIModel {
@Unique private static Field cnpc$modelField;
@Unique private static boolean cnpc$modelLookupFailed;
@Unique private static boolean cnpc$loggedHook;
@Unique private PlayerModelAnimation.Pose cnpc$pose;
@Unique private boolean cnpc$rendering;
@Inject(
method = "render(Lnet/minecraft/entity/Entity;FFFFFF)V",
at = @At("HEAD"),
remap = false,
require = 1
)
private void cnpc$beginRender(Entity entity, float limbSwing, float limbSwingAmount,
float age, float yaw, float pitch, float scale,
CallbackInfo callbackInfo) {
ModelBiped model = cnpc$model();
if (model == null) {
return;
}
cnpc$pose = PlayerModelAnimation.capture(model);
cnpc$rendering = true;
}
@Inject(
method = "setRotationAngles(FFFFFFLnet/minecraft/entity/Entity;)V",
at = @At("RETURN"),
remap = false,
require = 1
)
private void cnpc$applyAnimation(float limbSwing, float limbSwingAmount, float age,
float yaw, float pitch, float scale, Entity entity,
CallbackInfo callbackInfo) {
if (!cnpc$rendering) {
return;
}
ModelBiped model = cnpc$model();
if (model == null) {
return;
}
PlayerModelAnimation.apply(model);
if (!cnpc$loggedHook) {
cnpc$loggedHook = true;
System.out.println("[CustomNPC+] PlayerAPI model animation hook is running; model="
+ model.getClass().getName());
}
}
@Inject(
method = "render(Lnet/minecraft/entity/Entity;FFFFFF)V",
at = @At("RETURN"),
remap = false,
require = 1
)
private void cnpc$endRender(Entity entity, float limbSwing, float limbSwingAmount,
float age, float yaw, float pitch, float scale,
CallbackInfo callbackInfo) {
if (cnpc$pose != null) {
cnpc$pose.restore();
}
cnpc$pose = null;
cnpc$rendering = false;
}
@Unique
private ModelBiped cnpc$model() {
if (cnpc$modelLookupFailed) {
return null;
}
try {
if (cnpc$modelField == null) {
cnpc$modelField = this.getClass().getDeclaredField("modelPlayer");
cnpc$modelField.setAccessible(true);
}
Object model = cnpc$modelField.get(this);
return model instanceof ModelBiped ? (ModelBiped) model : null;
} catch (ReflectiveOperationException exception) {
cnpc$modelLookupFailed = true;
System.out.println("[CustomNPC+] PlayerAPI model lookup failed: " + exception);
return null;
}
}
}
@@ -0,0 +1,11 @@
{
"required": true,
"minVersion": "0.7.11",
"package": "noppes.npcs.mixin.late",
"refmap": "mixins.customnpcs.refmap.json",
"target": "@env(DEFAULT)",
"compatibilityLevel": "JAVA_8",
"mixins": [],
"client": [],
"server": []
}