package me.nallar.patched.network;

import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import me.nallar.tickthreading.Log;
import me.nallar.tickthreading.patcher.Declare;
import net.minecraft.network.TcpConnection;
import net.minecraft.network.packet.IPacketHandler;
import net.minecraft.network.packet.NetHandler;
import net.minecraft.network.packet.Packet;

public abstract class PatchTcpConnection extends TcpConnection {
	private static List<IPacketHandler> packetHandlers;

	public PatchTcpConnection(Socket par1Socket, String par2Str, NetHandler par3NetHandler) throws IOException {
		super(par1Socket, par2Str, par3NetHandler);
	}

	public void construct() {
		readPackets = Collections.synchronizedList(new LinkedList());
	}

	private boolean callPacketOut(Packet p) {
		for (IPacketHandler handler : getPacketHandlers()) {
			if (!handler.onOutgoingPacket(theNetHandler, p.getPacketId(), p)) {
				return false;
			}
		}

		return true;
	}

	@Override
	@Declare
	public void addReadPacket(Packet packet) {
		this.readPackets.add(packet);
	}

	@Override
	@Declare
	public boolean readNetworkPacket() {
		return this.readPacket();
	}

	@Override
	@Declare
	public boolean isRunning() {
		return isRunning && !isServerTerminating;
	}

	@Declare
	public static java.util.List<net.minecraft.network.packet.IPacketHandler> getPacketHandlers() {
		if (packetHandlers == null) {
			synchronized (IPacketHandler.class) {
				if (packetHandlers == null) {
					packetHandlers = new ArrayList<IPacketHandler>();
				}
			}
		}
		return packetHandlers;
	}

	@Override
	public void addToSendQueue(Packet par1Packet) {
		if (!this.isServerTerminating && callPacketOut(par1Packet)) {
			synchronized (this.sendQueueLock) {
				this.sendQueueByteLength += par1Packet.getPacketSize() + 1;
				this.dataPackets.add(par1Packet);
			}
		}
	}

	@Override
	protected void onNetworkError(Exception e) {
		if (e == null || e.toString() == null) {
			Log.severe("onNetworkError with bad exception " + e, new Throwable());
		}
		if (e instanceof SocketException) {
			this.networkShutdown("disconnected: " + e.getMessage(), e);
		} else {
			String name = "unknown";
			try {
				name = this.theNetHandler.getPlayer().getCommandSenderName();
			} catch (Throwable ignored) {
			}
			Log.severe("Failed to handle packet from " + name, e);
			this.networkShutdown("Internal error: " + e, e);
		}
	}

	@Override
	public void wakeThreads() {
		if (this.writeThread != null) {
			this.writeThread.interrupt();
		}
	}
}

@@ -179,39 +179,8 @@
 tion
-s;%0D%0Aimport java.util.LinkedList
 ;%0D%0Ai
@@ -276,23 +276,138 @@
 ing.
-patcher.Declare
+collections.ConcurrentQueueList;%0D%0Aimport me.nallar.tickthreading.patcher.Declare;%0D%0Aimport me.nallar.tickthreading.util.ReflectUtil
 ;%0D%0Ai
@@ -909,24 +909,34 @@
 struct() %7B%0D%0A
+%09%09try %7B%0D%0A%09
 %09%09readPacket
@@ -943,55 +943,126 @@
 s = 
-Collections.synchronizedList(new LinkedList());
+new ConcurrentQueueList();%0D%0A%09%09%7D catch (NoSuchFieldError ignored) %7B%0D%0A%09%09%09// Already using sane collection in MCPC+.%0D%0A%09%09%7D
 %0D%0A%09%7D
@@ -1365,35 +1365,635 @@
 %0D%0A%09%09
-this.readPackets.add(packet
+getReadPackets().add(packet);%0D%0A%09%7D%0D%0A%0D%0A%09private Collection%3CPacket%3E actualReadPackets;%0D%0A%0D%0A%09private Collection%3CPacket%3E getReadPackets() %7B%0D%0A%09%09if (actualReadPackets != null) %7B%0D%0A%09%09%09return actualReadPackets;%0D%0A%09%09%7D%0D%0A%09%09try %7B%0D%0A%09%09%09return actualReadPackets = readPackets;%0D%0A%09%09%7D catch (NoSuchFieldError e) %7B%0D%0A%09%09%09try %7B%0D%0A%09%09%09%09return actualReadPackets = (Collection %3CPacket%3E) ReflectUtil.getField(TcpConnection.class, %22o%22).get(this); // TODO: Implement reflection obfuscation%0D%0A%09%09%09%7D catch (IllegalAccessException e1) %7B%0D%0A%09%09%09%09Log.severe(%22Failed to get readPackets collection%22, e1);%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%09%09throw new Error(%22Failed to get readPackets collection%22
 );%0D%0A

Fix #440