package codechicken.lib.packet;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

import codechicken.lib.asm.ObfMapping;

import net.minecraft.network.packet.NetHandler;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet250CustomPayload;

/**
 * A class that facilitates sending of multiple packets one after the other.
 * It first sends an empty 250 packet and then it's payloads.
 */
public class MetaPacket extends Packet250CustomPayload
{
    static
    {
        try
        {
            String fieldName = new ObfMapping("net/minecraft/network/packet/Packet", "packetClassToIdMap", "Ljava/util/Map;").toRuntime().s_name;
            Field field = Packet.class.getDeclaredField(fieldName);
            field.setAccessible(true);
            Map<Class<? extends Packet>, Integer> packetClassToIdMap = (Map<Class<? extends Packet>, Integer>) field.get(null);
            packetClassToIdMap.put(MetaPacket.class, 250);
        }
        catch(Exception e)
        {
            throw new RuntimeException(e);
        }
    }
    
    public ArrayList<Packet> packets = new ArrayList<Packet>();
    
    public MetaPacket(Packet... packets)
    {
        super("", null);
        
        for(Packet p : packets)
            this.packets.add(p);
    }
    
    public MetaPacket(Collection<? extends Packet> packets)
    {
        this.packets.addAll(packets);
    }
    
    @Override
    public void readPacketData(DataInput datain)
    {
        throw new IllegalStateException("Meta packets can't be read");
    }

    @Override
    public void writePacketData(DataOutput dataout) {
        //send a dummy 250
        super.writePacketData(dataout);

        for(Packet p : packets)//TODO: fix extends
            try {
                Packet.writePacket(p, dataout);
            } catch (IOException e) {
                e.printStackTrace();
            }

        //adjust sent size for Packet.writePacket
        Packet.sentSize-=getPacketSize()-super.getPacketSize();
    }

    @Override
    public void processPacket(NetHandler nethandler)
    {
        for(Packet p : packets)//Memory connection
            p.processPacket(nethandler);
    }

    @Override
    public int getPacketSize()
    {
        int size = 0;
        for(Packet p : packets)
            size+=p.getPacketSize()+1;
        return size;
    }
}

@@ -1758,24 +1758,71 @@
  dataout) %7B%0A
+        try %7B//TODO: fix super IOException%0A    
         //se
@@ -1840,24 +1840,28 @@
 250%0A        
+  
+  
 super.writeP
@@ -1881,32 +1881,36 @@
 aout);%0A%0A        
+    
 for(Packet p : p
@@ -1920,45 +1920,8 @@
 ets)
-//TODO: fix extends%0A            try %7B
 %0A   
@@ -1969,98 +1969,13 @@
 t);%0A
+%0A
     
-        %7D catch (IOException e) %7B%0A                e.printStackTrace();%0A            %7D%0A%0A
     
@@ -2020,16 +2020,20 @@
 ePacket%0A
+    
         
@@ -2084,24 +2084,101 @@
 cketSize();%0A
+        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D%0A
     %7D%0A%0A    @

Fix compile time compatibility with new and old forge