Skip to content

Commit

Permalink
Merge pull request #58 from jchung01/master
Browse files Browse the repository at this point in the history
Remove deprecated transformer code
  • Loading branch information
ACGaming authored Jun 8, 2024
2 parents be666ef + ee623f5 commit 535ff35
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 145 deletions.
1 change: 0 additions & 1 deletion src/main/java/org/dimdev/jeid/JEID.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public class JEID {
+ "after:twilightforest;"
+ "after:wyrmsofnyrus;"
+ "after:worldedit";
public static final Logger LOGGER = LogManager.getLogger(JEID.NAME);

@Mod.EventHandler
public void onPreInit(FMLPreInitializationEvent event) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/dimdev/jeid/JEIDLogger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.dimdev.jeid;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import org.dimdev.jeid.jeid.Tags;

public class JEIDLogger {
public static final Logger LOGGER = LogManager.getLogger(Tags.MOD_NAME);
}
142 changes: 0 additions & 142 deletions src/main/java/org/dimdev/jeid/core/JEIDTransformer.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package org.dimdev.jeid.core;

import net.minecraft.launchwrapper.IClassTransformer;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.registry.RegistryNamespaced;
import net.minecraftforge.fml.relauncher.FMLLaunchHandler;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.*;

import java.util.Iterator;
Expand All @@ -25,9 +20,6 @@
* <a href="https://github.com/zabi94/MaxPotionIDExtender">MaxPotionIDExtender by Zabi94</a>
*/
public class JEIDTransformer implements IClassTransformer {
@Deprecated
public static RegistryNamespaced<ResourceLocation, Potion> REGISTRY;

@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {
if (transformedName.equals("net.minecraft.item.ItemStack")) {
Expand Down Expand Up @@ -107,106 +99,6 @@ private byte[] transformItemStack(byte[] basicClass) {
return cw.toByteArray();
}

/**
* This is no longer used as it has been converted into a mixin.
* @see org.dimdev.jeid.mixin.core.potion.MixinSPacketRemoveEntityEffect
*/
@Deprecated
private byte[] transformSPacketRemoveEntityEffect(byte[] basicClass) {
ClassReader cr = new ClassReader(basicClass);
ClassNode cn = new ClassNode();
cr.accept(cn, 0);

// Redirect readUnsignedByte (aka short) to readInt
String packetBuffer = "net/minecraft/network/PacketBuffer";
String descPacketData = "(L" + packetBuffer + ";)V";
MethodNode rpd = locateMethod(cn, descPacketData, "readPacketData", "func_148837_a");
AbstractInsnNode target = locateTargetInsn(rpd, n -> n.getOpcode() == Opcodes.INVOKEVIRTUAL && ((MethodInsnNode) n).name.equals("readUnsignedByte"));
rpd.instructions.insert(target, new MethodInsnNode(Opcodes.INVOKEVIRTUAL, packetBuffer, "readInt", "()I", false));
rpd.instructions.remove(target);

// Redirect writeByte to writeInt
MethodNode wpd = locateMethod(cn, descPacketData, "writePacketData", "func_148840_b");
target = locateTargetInsn(wpd, n -> n.getOpcode() == Opcodes.INVOKEVIRTUAL && ((MethodInsnNode) n).name.equals("writeByte"));
wpd.instructions.insert(target, new MethodInsnNode(Opcodes.INVOKEVIRTUAL, packetBuffer, "writeInt", "(I)Lio/netty/buffer/ByteBuf;", false));
wpd.instructions.remove(target);

ClassWriter cw = new ClassWriter(0);
cn.accept(cw);
return cw.toByteArray();
}

/**
* This is no longer used as it has been converted into a mixin.
* @see org.dimdev.jeid.mixin.core.potion.MixinSPacketEntityEffect
*/
@Deprecated
private byte[] transformSPacketEntityEffect(byte[] basicClass) {
ClassReader cr = new ClassReader(basicClass);
ClassNode cn = new ClassNode();
cr.accept(cn, 0);

String potionEffect = "net/minecraft/potion/PotionEffect";
String sPacketEntityEffect = "net/minecraft/network/play/server/SPacketEntityEffect";
String packetBuffer = "net/minecraft/network/PacketBuffer";
String descPacketData = "(L" + packetBuffer + ";)V";
// Add new field, int effectInt
cn.fields.add(new FieldNode(Opcodes.ACC_PUBLIC, "effectInt", "I", null, 0));

MethodNode mnInit = locateMethod(cn, "(IL" + potionEffect + ";)V", "<init>");
Iterator<AbstractInsnNode> i = mnInit.instructions.iterator();
AbstractInsnNode targetNode = null;
int line = 0;
while (i.hasNext() && targetNode == null) {
AbstractInsnNode node = i.next();
if (node instanceof LineNumberNode) {
if (line == 1) {
targetNode = node;
}
line++;
}
}

if (targetNode == null) {
throw new RuntimeException("Can't find target node for SPacketEntityEffect constructor");
}

// Initialize effectInt in constructors
// These are reversed, they get pushed down the stack
mnInit.instructions.insert(targetNode, new FieldInsnNode(Opcodes.PUTFIELD, sPacketEntityEffect, "effectInt", "I"));
mnInit.instructions.insert(targetNode, new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(this.getClass()), "getIdFromPotEffect", "(L" + potionEffect + ";)I", false));
mnInit.instructions.insert(targetNode, new VarInsnNode(Opcodes.ALOAD, 2));
mnInit.instructions.insert(targetNode, new VarInsnNode(Opcodes.ALOAD, 0));

MethodNode mnEmptyInit = locateMethod(cn, "()V", "<init>");
AbstractInsnNode tgt = locateTargetInsn(mnEmptyInit, n -> n.getOpcode() == Opcodes.RETURN);
mnEmptyInit.instructions.insertBefore(tgt, new VarInsnNode(Opcodes.ALOAD, 0));
mnEmptyInit.instructions.insertBefore(tgt, new LdcInsnNode(0));
mnEmptyInit.instructions.insertBefore(tgt, new FieldInsnNode(Opcodes.PUTFIELD, sPacketEntityEffect, "effectInt", "I"));

// Patch readPacketData: this.effectInt = buf.readVarInt();
MethodNode mnReadPacket = locateMethod(cn, descPacketData, "readPacketData", "func_148837_a");
AbstractInsnNode target = locateTargetInsn(mnReadPacket, n -> n.getOpcode() == Opcodes.RETURN).getPrevious().getPrevious();
mnReadPacket.instructions.insertBefore(target, new VarInsnNode(Opcodes.ALOAD, 0));
mnReadPacket.instructions.insertBefore(target, new VarInsnNode(Opcodes.ALOAD, 1));
mnReadPacket.instructions.insertBefore(target, new MethodInsnNode(Opcodes.INVOKEVIRTUAL, packetBuffer, (JEIDLoadingPlugin.isDeobf ? "readVarInt" : "func_150792_a"), "()I", false));
mnReadPacket.instructions.insertBefore(target, new FieldInsnNode(Opcodes.PUTFIELD, sPacketEntityEffect, "effectInt", "I"));

// Patch writePacketData: buf.writeVarInt(effectInt);
MethodNode mnWritePacket = locateMethod(cn, descPacketData, "writePacketData", "func_148840_b");
AbstractInsnNode wpTarget = locateTargetInsn(mnWritePacket, n -> n.getOpcode() == Opcodes.RETURN).getPrevious().getPrevious();
mnWritePacket.instructions.insertBefore(wpTarget, new VarInsnNode(Opcodes.ALOAD, 1));
mnWritePacket.instructions.insertBefore(wpTarget, new VarInsnNode(Opcodes.ALOAD, 0));
mnWritePacket.instructions.insertBefore(wpTarget, new FieldInsnNode(Opcodes.GETFIELD, sPacketEntityEffect, "effectInt", "I"));
mnWritePacket.instructions.insertBefore(wpTarget, new MethodInsnNode(Opcodes.INVOKEVIRTUAL, packetBuffer, (JEIDLoadingPlugin.isDeobf ? "writeVarInt" : "func_150787_b"), "(I)L" + packetBuffer + ";", false));
mnWritePacket.instructions.insertBefore(wpTarget, new InsnNode(Opcodes.POP));

// Max stack size has been modified
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
cn.accept(cw);
return cw.toByteArray();
}

private byte[] transformPotionEffect(byte[] basicClass) {
ClassReader cr = new ClassReader(basicClass);
ClassNode cn = new ClassNode();
Expand Down Expand Up @@ -236,39 +128,5 @@ private byte[] transformPotionEffect(byte[] basicClass) {
cn.accept(cw);
return cw.toByteArray();
}

/**
* This is no longer used as it has been converted into a mixin.
* @see org.dimdev.jeid.mixin.core.potion.client.MixinNetHandlerPlayClient
*/
@Deprecated
private byte[] transformNetHandlerPlayClient(byte[] basicClass) {
ClassReader cr = new ClassReader(basicClass);
ClassNode cn = new ClassNode();
cr.accept(cn, 0);

String sPacketEntityEffect = "net/minecraft/network/play/server/SPacketEntityEffect";
MethodNode mn = locateMethod(cn, "(L" + sPacketEntityEffect + ";)V", "handleEntityEffect", "func_147260_a");
AbstractInsnNode target = locateTargetInsn(mn, n -> n.getOpcode() == Opcodes.SIPUSH);
mn.instructions.remove(target.getPrevious());
mn.instructions.remove(target.getNext());
// Redirect getEffectID to effectInt
mn.instructions.insertBefore(target, new FieldInsnNode(Opcodes.GETFIELD, sPacketEntityEffect, "effectInt", "I"));
// Remove bitwise operation
mn.instructions.remove(target);

ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
cn.accept(cw);
return cw.toByteArray();
}

/**
* No longer used.
* @see JEIDTransformer#transformSPacketRemoveEntityEffect(byte[])
*/
@Deprecated
public static int getIdFromPotEffect(PotionEffect pe) {
return REGISTRY.getIDForObject(pe.getPotion());
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import org.dimdev.jeid.JEID;
import org.dimdev.jeid.JEIDLogger;
import org.dimdev.jeid.ducks.INewChunk;
import org.dimdev.jeid.network.MessageManager;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -27,7 +27,7 @@ public class MixinCommandSetBiome {
private void reid$setBiomeArrayElement(MinecraftServer server, ICommandSender sender, String[] args, CallbackInfo ci,
@Local BlockPos coord, @Local World world, @Local(ordinal = 0) int id,
@Local(ordinal = 2) int x, @Local(ordinal = 3) int z, @Local Chunk chunk) {
JEID.LOGGER.info("setting biome at {}, {}", x, z);
JEIDLogger.LOGGER.info("setting biome at {}, {}", x, z);
// Method calls markDirty()
((INewChunk) chunk).getIntBiomeArray()[(z & 0xF) << 4 | x & 0xF] = id;
}
Expand Down

0 comments on commit 535ff35

Please sign in to comment.