package com.carpentersblocks.util.handler;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.network.NetHandlerPlayServer;
import net.minecraft.network.play.client.C17PacketCustomPayload;
import com.carpentersblocks.CarpentersBlocks;
import com.carpentersblocks.network.ICarpentersPacket;
import com.carpentersblocks.network.PacketEnrichPlant;
import com.carpentersblocks.network.PacketSlopeSelect;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.network.FMLNetworkEvent.ServerCustomPacketEvent;
import cpw.mods.fml.common.network.internal.FMLProxyPacket;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.Unpooled;

public class PacketHandler {

    private final static List<Class> packetCarrier;
    static {
        packetCarrier = new ArrayList<Class>();
        packetCarrier.add(PacketEnrichPlant.class);
        packetCarrier.add(PacketSlopeSelect.class);
    }

    @SubscribeEvent
    public void onServerPacket(ServerCustomPacketEvent event) throws IOException
    {
        ByteBufInputStream bbis = new ByteBufInputStream(event.packet.payload());
        EntityPlayer entityPlayer = ((NetHandlerPlayServer) event.handler).playerEntity;
        int packetId = bbis.readInt();

        try {
            ICarpentersPacket packetClass = (ICarpentersPacket) packetCarrier.get(packetId).newInstance();
            packetClass.processData(entityPlayer, bbis);
        } catch (Exception e) {
            e.printStackTrace();
        }

        bbis.close();
    }

    public static void sendPacketToServer(ICarpentersPacket packet)
    {
        ByteBuf buffer = Unpooled.buffer();
        buffer.writeInt(packetCarrier.indexOf(packet.getClass()));

        try {
            packet.appendData(buffer);
        } catch (IOException e) { }

        CarpentersBlocks.channel.sendToServer(new FMLProxyPacket(new C17PacketCustomPayload(CarpentersBlocks.MODID, buffer)));
    }

}

@@ -281,16 +281,55 @@
 ayload;%0A
+import org.apache.logging.log4j.Level;%0A
 import c
@@ -531,16 +531,60 @@
 Select;%0A
+import com.carpentersblocks.util.ModLogger;%0A
 import c
@@ -1447,25 +1447,72 @@
 dInt();%0A
-%0A
+        if (packetId %3C packetCarrier.size()) %7B%0A 
         
 try %7B%0A  
@@ -1495,38 +1495,45 @@
 e()) %7B%0A         
+   
 try %7B%0A
+    
             ICar
@@ -1631,24 +1631,28 @@
             
+    
 packetClass.
@@ -1684,32 +1684,36 @@
  bbis);%0A        
+ 
+   
 %7D catch (Excepti
@@ -1732,16 +1732,20 @@
         
+    
 e.printS
@@ -1753,24 +1753,28 @@
 ackTrace();%0A
+    
         %7D%0A%0A 
@@ -1767,24 +1767,140 @@
           %7D%0A
+        %7D else %7B%0A            ModLogger.log(Level.WARN, %22Encountered out of range packet Id: %22 + packetId);%0A        %7D
 %0A        bbi

Check for out of range packet Ids.