package me.nallar.patched.network;

import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

import cpw.mods.fml.common.FMLLog;
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);
	}

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

		return true;
	}

	@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 instanceof SocketException) {
			this.networkShutdown("disconnected: " + e.getMessage(), e);
		} else {
			String name = "unknown";
			try {
				name = this.theNetHandler.getPlayer().getCommandSenderName();
			} catch (Throwable ignored) {
			}
			FMLLog.log(Level.SEVERE, e, "Failed to handle packet from " + name);
			this.networkShutdown("Internal error: " + e.getMessage(), e);
		}
	}

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

@@ -243,16 +243,53 @@
 MLLog;%0D%0A
+import me.nallar.tickthreading.Log;%0D%0A
 import m
@@ -1905,24 +1905,148 @@
 ption e) %7B%0D%0A
+%09%09if (e == null %7C%7C e.toString() == null) %7B%0D%0A%09%09%09Log.severe(%22onNetworkError with bad exception %22 + e, new Throwable());%0D%0A%09%09%7D%0D%0A
 %09%09if (e inst
@@ -2301,36 +2301,19 @@
 %0A%09%09%09
-FML
 Log.
-log(Level.SEVERE, e, 
+severe(
 %22Fai
@@ -2346,16 +2346,19 @@
 %22 + name
+, e
 );%0D%0A%09%09%09t
@@ -2395,37 +2395,24 @@
 error: %22 + e
-.getMessage()
 , e);%0D%0A%09%09%7D%0D%0A

Improve logging for #403