package protocolsupport.protocol.packet.v_1_8;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import net.minecraft.server.v1_9_R1.EnumProtocol;
import net.minecraft.server.v1_9_R1.Packet;
import protocolsupport.protocol.packet.ServerBoundPacket;

public class PlayPacketReorderer {

	protected Packet<?> animatePacket;

	public List<Packet<?>> orderPackets(Collection<? extends Packet<?>> packets) {
		ArrayList<Packet<?>> ordered = new ArrayList<>();
		for (Packet<?> curPacket : packets) {
			int packetId = ServerBoundPacket.getId(EnumProtocol.PLAY, curPacket);
			//if the packet is use entity, we attempt to add a cached animate packet after it
			if (packetId == ServerBoundPacket.PLAY_USE_ENTITY.getId()) {
				if (animatePacket != null) {
					ordered.add(curPacket);
					ordered.add(animatePacket);
					animatePacket = null;
				} else {
					ordered.add(curPacket);
				}
			}
			//handle other packet types
			else {
				//attempt to add cached animate packet
				if (animatePacket != null) {
					ordered.add(animatePacket);
					animatePacket = null;
				}
				//if it is animate packet, we cache it
				if (packetId == ServerBoundPacket.PLAY_ANIMATION.getId()) {
					animatePacket = curPacket;
				}
				//any other type just gets added to ordered list
				else {
					ordered.add(curPacket);
				}
			}
		}
		return ordered;
	}

}

@@ -348,16 +348,82 @@
 acket;%0A%0A
+%09private final ArrayList%3CPacket%3C?%3E%3E ordered = new ArrayList%3C%3E();%0A%0A
 %09public 
@@ -500,54 +500,21 @@
 %7B%0A%09%09
-ArrayList%3CPacket%3C?%3E%3E ordered = new ArrayList%3C%3E
+ordered.clear
 ();%0A
@@ -771,24 +771,52 @@
 .getId()) %7B%0A
+%09%09%09%09ordered.add(curPacket);%0A
 %09%09%09%09if (anim
@@ -840,37 +840,8 @@
 ) %7B%0A
-%09%09%09%09%09ordered.add(curPacket);%0A
 %09%09%09%09
@@ -861,32 +861,32 @@
 animatePacket);%0A
+
 %09%09%09%09%09animatePack
@@ -900,50 +900,8 @@
 ll;%0A
-%09%09%09%09%7D else %7B%0A%09%09%09%09%09ordered.add(curPacket);%0A
 %09%09%09%09

Use signle field to reduce memory allocation