package net.md_5.bungee;

import io.netty.buffer.ByteBuf;

/**
 * Class to rewrite integers within packets.
 */
public class EntityMap
{

    public final static int[][] entityIds = new int[ 256 ][];

    static
    {
        entityIds[0x05] = new int[]
        {
            1
        };
        entityIds[0x07] = new int[]
        {
            1, 5
        };
        entityIds[0x11] = new int[]
        {
            1
        };
        entityIds[0x12] = new int[]
        {
            1
        };
        entityIds[0x13] = new int[]
        {
            1
        };
        entityIds[0x14] = new int[]
        {
            1
        };
        entityIds[0x16] = new int[]
        {
            1, 5
        };
        entityIds[0x17] = new int[]
        {
            1, 20
        };
        entityIds[0x18] = new int[]
        {
            1
        };
        entityIds[0x19] = new int[]
        {
            1
        };
        entityIds[0x1A] = new int[]
        {
            1
        };
        entityIds[0x1C] = new int[]
        {
            1
        };
        entityIds[0x1E] = new int[]
        {
            1
        };
        entityIds[0x1F] = new int[]
        {
            1
        };
        entityIds[0x20] = new int[]
        {
            1
        };
        entityIds[0x21] = new int[]
        {
            1
        };
        entityIds[0x22] = new int[]
        {
            1
        };
        entityIds[0x23] = new int[]
        {
            1
        };
        entityIds[0x26] = new int[]
        {
            1
        };
        entityIds[0x27] = new int[]
        {
            1, 5
        };
        entityIds[0x28] = new int[]
        {
            1
        };
        entityIds[0x29] = new int[]
        {
            1
        };
        entityIds[0x2A] = new int[]
        {
            1
        };
        entityIds[0x37] = new int[]
        {
            1
        };

        entityIds[0x47] = new int[]
        {
            1
        };
    }

    public static void rewrite(ByteBuf packet, int oldId, int newId)
    {
        int packetId = packet.getUnsignedByte( 0 );
        if ( packetId == 0x1D )
        { // bulk entity
            for ( int pos = 2; pos < packet.writerIndex(); pos += 4 )
            {
                int readId = packet.getInt( pos );
                if ( readId == oldId )
                {
                    packet.setInt( pos, newId );
                } else if ( readId == newId )
                {
                    packet.setInt( pos, oldId );
                }
            }
        } else
        {
            int[] idArray = entityIds[packetId];
            if ( idArray != null )
            {
                for ( int pos : idArray )
                {
                    int readId = packet.getInt( pos );
                    if ( readId == oldId )
                    {
                        packet.setInt( pos, newId );
                    } else if ( readId == newId )
                    {
                        packet.setInt( pos, oldId );
                    }
                }
            }
        }
    }
}

@@ -773,16 +773,19 @@
        1
+ //
 , 20%0A   
@@ -3121,20 +3121,324 @@
 %7D%0A        %7D%0A
+        if ( packetId == 0x17 )%0A        %7B%0A            int type = packet.getByte( 5 );%0A            if ( type %3E= 60 && type %3C= 62 )%0A            %7B%0A                if ( packet.getInt( 20 ) == oldId )%0A                %7B%0A                    packet.setInt( 20, newId );%0A                %7D%0A            %7D%0A        %7D%0A
     %7D%0A%7D%0A

Fix entity rewrites on falling blocks.