package protocolsupport.api;

import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.List;

import org.bukkit.craftbukkit.v1_11_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;

import net.minecraft.server.v1_11_R1.EntityPlayer;
import protocolsupport.api.remapper.BlockRemapperControl;
import protocolsupport.api.remapper.ItemRemapperControl;
import protocolsupport.protocol.ConnectionImpl;
import protocolsupport.protocol.storage.ProtocolStorage;
import protocolsupport.utils.Utils;
import protocolsupport.utils.Utils.Converter;

public class ProtocolSupportAPI {

	private static final boolean getconnectioncheckall = Utils.getJavaPropertyValue("api.getconnection.checkall", false, Converter.STRING_TO_BOOLEAN);

	public static ProtocolVersion getProtocolVersion(Player player) {
		Connection connection = getConnection(player);
		return connection != null ? connection.getVersion() : ProtocolVersion.UNKNOWN;
	}

	public static ProtocolVersion getProtocolVersion(SocketAddress address) {
		Connection connection = getConnection(address);
		return connection != null ? connection.getVersion() : ProtocolVersion.UNKNOWN;
	}

	public static List<Connection> getConnections() {
		return new ArrayList<>(ProtocolStorage.getConnections());
	}

	public static Connection getConnection(Player player) {
		if (player instanceof CraftPlayer) {
			EntityPlayer nmsplayer = ((CraftPlayer) player).getHandle();
			if (nmsplayer.playerConnection != null) {
				return ConnectionImpl.getFromChannel(nmsplayer.playerConnection.networkManager.channel);
			}
		}
		return getConnection(player.getAddress());
	}

	public static Connection getConnection(SocketAddress address) {
		Connection connection = ProtocolStorage.getConnection(address);
		if (connection != null) {
			return connection;
		}
		if (getconnectioncheckall) {
			for (Connection econnection : getConnections()) {
				if (econnection.getAddress().equals(address)) {
					return econnection;
				}
			}
		}
		return null;
	}

	public static ItemRemapperControl getItemRemapper(ProtocolVersion version) {
		return new ItemRemapperControl(version);
	}

	public static BlockRemapperControl getBlockRemapper(ProtocolVersion version) {
		return new BlockRemapperControl(version);
	}

}

@@ -128,133 +128,23 @@
 kit.
-craftbukkit.v1_11_R1.entity.CraftPlayer;%0Aimport org.bukkit.entity.Player;%0A%0Aimport net.minecraft.server.v1_11_R1.EntityPlayer;
+entity.Player;%0A
 %0Aimp
@@ -259,56 +259,8 @@
 ol;%0A
-import protocolsupport.protocol.ConnectionImpl;%0A
 impo
@@ -316,273 +316,42 @@
 ge;%0A
-import protocolsupport.utils.Utils;%0Aimport protocolsupport.utils.Utils.Converter;%0A%0Apublic class ProtocolSupportAPI %7B%0A%0A%09private static final boolean getconnectioncheckall = Utils.getJavaPropertyValue(%22api.getconnection.checkall%22, false, Converter.STRING_TO_BOOLEAN);
+%0Apublic class ProtocolSupportAPI %7B
 %0A%0A%09p
@@ -935,258 +935,8 @@
 ) %7B%0A
-%09%09if (player instanceof CraftPlayer) %7B%0A%09%09%09EntityPlayer nmsplayer = ((CraftPlayer) player).getHandle();%0A%09%09%09if (nmsplayer.playerConnection != null) %7B%0A%09%09%09%09return ConnectionImpl.getFromChannel(nmsplayer.playerConnection.networkManager.channel);%0A%09%09%09%7D%0A%09%09%7D%0A
 %09%09re
@@ -1047,39 +1047,22 @@
 ss) %7B%0A%09%09
-Connection connection =
+return
  Protoco
@@ -1098,253 +1098,8 @@
 s);%0A
-%09%09if (connection != null) %7B%0A%09%09%09return connection;%0A%09%09%7D%0A%09%09if (getconnectioncheckall) %7B%0A%09%09%09for (Connection econnection : getConnections()) %7B%0A%09%09%09%09if (econnection.getAddress().equals(address)) %7B%0A%09%09%09%09%09return econnection;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return null;%0A
 %09%7D%0A%0A

Remove some workarounds. There is a separate event is ps that allows changing remote address if needed.