package invtweaks.forge;

import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;
import cpw.mods.fml.common.network.IPacketHandler;
import cpw.mods.fml.common.network.Player;
import invtweaks.InvTweaks;
import invtweaks.InvTweaksConst;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.inventory.Slot;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet250CustomPayload;

import java.util.ArrayList;

public class PacketHandler implements IPacketHandler {
    @Override
    public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) {
        if(packet.data[0] == InvTweaksConst.PACKET_LOGIN) {
            if(packet.data.length == 2 && packet.data[1] == InvTweaksConst.PROTOCOL_VERSION) {
                InvTweaksMod.proxy.setServerHasInvTweaks(true);
            }
        } else {
            EntityPlayerMP realPlayer = (EntityPlayerMP) player;
            ByteArrayDataInput packetData = ByteStreams.newDataInput(packet.data);
            byte packetId = packetData.readByte();
            if (packetId == InvTweaksConst.PACKET_CLICK) {
                int slot = packetData.readInt();
                int buttonPressed = packetData.readInt();
                int modiferKeys = packetData.readInt();

                realPlayer.openContainer.slotClick(slot, buttonPressed, modiferKeys, realPlayer);

                ArrayList arraylist = new ArrayList();

                for (int i = 0; i < realPlayer.openContainer.inventorySlots.size(); ++i) {
                    arraylist.add(((Slot) realPlayer.openContainer.inventorySlots.get(i)).getStack());
                }

                realPlayer.sendContainerAndContentsToPlayer(realPlayer.openContainer, arraylist);
            }
        }
    }
}

@@ -658,34 +658,38 @@
 ayer) %7B%0A        
-if
+switch
 (packet.data%5B0%5D 
@@ -687,19 +687,36 @@
 .data%5B0%5D
- ==
+) %7B%0A            case
  InvTwea
@@ -739,34 +739,338 @@
 OGIN
-) %7B%0A            if(packet.
+:%0A                onLoginPacket(manager, packet.data, player);%0A                break;%0A            case InvTweaksConst.PACKET_CLICK:%0A                onClickPacket(manager, packet.data, player);%0A                break;%0A        %7D%0A    %7D%0A%0A    private void onLoginPacket(INetworkManager manager, byte%5B%5D data, Player player) %7B%0A        if(
 data
@@ -1085,23 +1085,16 @@
 == 2 && 
-packet.
 data%5B1%5D 
@@ -1127,28 +1127,24 @@
 _VERSION) %7B%0A
-    
             
@@ -1203,35 +1203,103 @@
     
+%7D%0A
     %7D%0A
+%0A
     
-    %7D else %7B%0A    
+private void onClickPacket(INetworkManager manager, byte%5B%5D data, Player player) %7B%0A
     
@@ -1363,20 +1363,16 @@
         
-  
-  
 ByteArra
@@ -1420,23 +1420,16 @@
 taInput(
-packet.
 data);%0A 
@@ -1439,28 +1439,8 @@
     
-  
-  byte packetId = 
 pack
@@ -1450,87 +1450,22 @@
 ata.
-read
+skip
 Byte
-(
+s(1
 );%0A
-            if (packetId == InvTweaksConst.PACKET_CLICK) %7B%0A        
     
@@ -1497,32 +1497,24 @@
 .readInt();%0A
-        
         int 
@@ -1559,24 +1559,16 @@
         
-   
-     
 int modi
@@ -1596,32 +1596,24 @@
 readInt();%0A%0A
-        
         real
@@ -1699,24 +1699,16 @@
         
-       
- 
 ArrayLis
@@ -1739,24 +1739,16 @@
 ist();%0A%0A
-        
         
@@ -1834,24 +1834,16 @@
         
- 
-       
 arraylis
@@ -1929,27 +1929,11 @@
     
-  
-      %7D%0A%0A        
+%7D%0A%0A
     
@@ -2022,32 +2022,8 @@
 t);%0A
-            %7D%0A        %7D%0A
     

Refactor the packet handler to make it easier to handle multiple packets.