Skip to content

Commit

Permalink
Primal's Steppe Wolves now fear fish
Browse files Browse the repository at this point in the history
This is to prevent further fish massacre
  • Loading branch information
sam-kirby committed Jun 7, 2020
1 parent 71c751c commit c05ab91
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public class SevPatchesLoadingPlugin implements IFMLLoadingPlugin {
public static String VEC_3I_DISTANCE_SQ;
public static String VEC_3I_DISTANCE_SQ_DESC;

public static String INIT_ENTITY_AI;
public static String ENTITY_TASKS;
public static String ENTITY_TASKS_ADD_TASK;

public SevPatchesLoadingPlugin() {
LOGGER.info("setting up mixin environment");
MixinBootstrap.init();
Expand Down Expand Up @@ -79,6 +83,10 @@ public void injectData(Map<String, Object> data) {
SevPatchesLoadingPlugin.FIND_CHUNKS_FOR_SPAWNING_DESC = dev ? "(Lnet/minecraft/world/WorldServer;ZZZ)I" : "(Loo;ZZZ)I";
SevPatchesLoadingPlugin.VEC_3I_DISTANCE_SQ = dev ? "distanceSq" : "f";
SevPatchesLoadingPlugin.VEC_3I_DISTANCE_SQ_DESC = "(DDD)D";

SevPatchesLoadingPlugin.INIT_ENTITY_AI = dev ? "initEntityAI" : "func_184651_r";
SevPatchesLoadingPlugin.ENTITY_TASKS = dev ? "tasks" : "field_70714_bg";
SevPatchesLoadingPlugin.ENTITY_TASKS_ADD_TASK = dev ? "addTask" : "func_75776_a";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,21 @@
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Label;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.*;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldInsnNode;
import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.InsnNode;
import org.objectweb.asm.tree.JumpInsnNode;
import org.objectweb.asm.tree.LabelNode;
import org.objectweb.asm.tree.LdcInsnNode;
import org.objectweb.asm.tree.LineNumberNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.tree.TypeInsnNode;
import org.objectweb.asm.tree.VarInsnNode;

import java.util.Arrays;
import java.util.ListIterator;
Expand Down Expand Up @@ -36,6 +50,8 @@ public byte[] transform(String name, String transformedName, byte[] basicClass)
return this.fishAreFish(basicClass);
case "nmd.primal.core.common.entities.living.EntityHammerHead":
return this.nicerHammerHeads(basicClass);
case "nmd.primal.core.common.entities.living.EntityCanisCampestris":
return this.scaredyCat(basicClass);
default:
return basicClass;
}
Expand All @@ -60,6 +76,86 @@ private void setEventSubPriority(ClassNode input, String targetMethod, String pr
}
}

/**
* Make Primal's Steppe Wolves scared of fish
*/
private byte[] scaredyCat(byte[] basicClass) {
ClassReader classReader = new ClassReader(basicClass);

ClassNode classNode = new ClassNode();
classReader.accept(classNode, 0);

MethodNode initEntityAI = null;

for (MethodNode methodNode : classNode.methods) {
if (!methodNode.name.equals(SevPatchesLoadingPlugin.INIT_ENTITY_AI)) continue;
initEntityAI = methodNode;
break;
}

if (initEntityAI == null) {
SevPatchesLoadingPlugin.LOGGER.warn("Did not find target method (scaredyCat)");
return basicClass;
}

InsnNode returnInsn = null;

for (ListIterator<AbstractInsnNode> it = initEntityAI.instructions.iterator(); it.hasNext(); ) {
AbstractInsnNode insnNode = it.next();

if (insnNode.getOpcode() == Opcodes.RETURN) {
returnInsn = (InsnNode) insnNode;
break;
}
}

if (returnInsn == null) {
SevPatchesLoadingPlugin.LOGGER.warn("This method does not return?");
return basicClass;
}

InsnList scaredWolf = new InsnList();

scaredWolf.add(new VarInsnNode(Opcodes.ALOAD, 0));
scaredWolf.add(new FieldInsnNode(
Opcodes.GETFIELD,
"nmd/primal/core/common/entities/living/EntityCanisCampestris",
SevPatchesLoadingPlugin.ENTITY_TASKS,
"Lnet/minecraft/entity/ai/EntityAITasks;"
));
scaredWolf.add(new InsnNode(Opcodes.ICONST_1));
scaredWolf.add(new TypeInsnNode(
Opcodes.NEW,
"net/minecraft/entity/ai/EntityAIAvoidEntity"
));
scaredWolf.add(new InsnNode(Opcodes.DUP));
scaredWolf.add(new VarInsnNode(Opcodes.ALOAD, 0));
scaredWolf.add(new LdcInsnNode(Type.getObjectType("com/tmtravlr/jaff/entities/EntityFish")));
scaredWolf.add(new LdcInsnNode(6.0F));
scaredWolf.add(new InsnNode(Opcodes.DCONST_1));
scaredWolf.add(new LdcInsnNode(1.2D));
scaredWolf.add(new MethodInsnNode(
Opcodes.INVOKESPECIAL,
"net/minecraft/entity/ai/EntityAIAvoidEntity",
"<init>",
"(Lnet/minecraft/entity/EntityCreature;Ljava/lang/Class;FDD)V",
false
));
scaredWolf.add(new MethodInsnNode(
Opcodes.INVOKEVIRTUAL,
"net/minecraft/entity/ai/EntityAITasks",
SevPatchesLoadingPlugin.ENTITY_TASKS_ADD_TASK,
"(ILnet/minecraft/entity/ai/EntityAIBase;)V",
false
));

initEntityAI.instructions.insertBefore(returnInsn, scaredWolf);

ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
classNode.accept(classWriter);
return classWriter.toByteArray();
}

/**
* Prevent Primal's Hammerhead from murdering all the fish
*/
Expand Down

0 comments on commit c05ab91

Please sign in to comment.