package cpw.mods.fml.common.asm.transformers;

import java.util.ListIterator;

import net.minecraft.launchwrapper.IClassTransformer;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldInsnNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;

public class ItemStackTransformer implements IClassTransformer {
    @Override
    public byte[] transform(String name, String transformedName, byte[] basicClass) {
        if (!"net.minecraft.item.ItemStack".equals(name))
            return basicClass;
        ClassNode classNode = new ClassNode();
        ClassReader classReader = new ClassReader(basicClass);
        classReader.accept(classNode, 0);

        for (MethodNode m: classNode.methods)
        {
            for (ListIterator<AbstractInsnNode> it = m.instructions.iterator(); it.hasNext(); )
            {
                AbstractInsnNode insnNode = it.next();
                if (insnNode.getType() == AbstractInsnNode.FIELD_INSN)
                {
                    FieldInsnNode fi = (FieldInsnNode)insnNode;
                    if ("field_151002_e".equals(fi.name) && fi.getOpcode() == Opcodes.GETFIELD)
                    {
                        it.remove();
                        MethodInsnNode replace = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "net/minecraft/item/ItemStack","getItem", "()Lnet/minecraft/item/Item;");
                        it.add(replace);
                    }
                }
            }
        }
        ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        classNode.accept(writer);
        return writer.toByteArray();
    }

}

@@ -363,32 +363,73 @@
 .FieldInsnNode;%0A
+import org.objectweb.asm.tree.FieldNode;%0A
 import org.objec
@@ -566,16 +566,154 @@
 ormer %7B%0A
+    private static final String ITEM_TYPE = %22Lnet/minecraft/item/Item;%22;%0A    private static final String GETITEM_DESC = %22()%22+ ITEM_TYPE;%0A%0A
     @Ove
@@ -1046,16 +1046,1211 @@
 e, 0);%0A%0A
+        FieldNode itemField = null;%0A        for (FieldNode f : classNode.fields)%0A        %7B%0A            if (ITEM_TYPE.equals(f.desc) && itemField == null)%0A            %7B%0A                itemField = f;%0A            %7D%0A            else if (ITEM_TYPE.equals(f.desc))%0A            %7B%0A                throw new RuntimeException(%22Error processing ItemStack - found a duplicate Item field%22);%0A            %7D%0A        %7D%0A        if (itemField == null)%0A        %7B%0A            throw new RuntimeException(%22Error processing ItemStack - no Item field declared (is the code somehow obfuscated?)%22);%0A        %7D%0A%0A        MethodNode getItemMethod = null;%0A        for (MethodNode m: classNode.methods)%0A        %7B%0A            if (GETITEM_DESC.equals(m.desc) && getItemMethod == null)%0A            %7B%0A                getItemMethod = m;%0A            %7D%0A            else if (GETITEM_DESC.equals(m.desc))%0A            %7B%0A                throw new RuntimeException(%22Error processing ItemStack - duplicate getItem method found%22);%0A            %7D%0A        %7D%0A        if (getItemMethod == null)%0A        %7B%0A            throw new RuntimeException(%22Error processing ItemStack - no getItem method found (is the code somehow obfuscated?)%22);%0A        %7D%0A%0A
         
@@ -2643,24 +2643,22 @@
 if (
-%22field_151002_e%22
+itemField.name
 .equ
@@ -2894,48 +2894,46 @@
 ck%22,
-%22
 getItem
-%22, %22()Lnet/minecraft/item/Item;%22
+Method.name, getItemMethod.desc
 );%0A 

Fix the ItemStack transformer to find the method and field so it works with srg and mcp naming.