package protocolsupport.protocol.transformer.v_1_7.clientboundtransformer;

import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.AttributeKey;

import java.io.IOException;

import net.minecraft.server.v1_8_R3.EnumProtocol;
import net.minecraft.server.v1_8_R3.EnumProtocolDirection;
import net.minecraft.server.v1_8_R3.NetworkManager;
import net.minecraft.server.v1_8_R3.Packet;
import net.minecraft.server.v1_8_R3.PacketListener;
import protocolsupport.api.ProtocolVersion;
import protocolsupport.protocol.PacketDataSerializer;
import protocolsupport.protocol.core.IPacketEncoder;

public class PacketEncoder implements IPacketEncoder {

	private static final EnumProtocolDirection direction = EnumProtocolDirection.CLIENTBOUND;
	private static final AttributeKey<EnumProtocol> currentStateAttrKey = NetworkManager.c;

	private static boolean[] blockedPlayPackets = new boolean[256];
	static {
		//packet from 1.8
		for (int i = 0x41; i < 0x49; i++) {
			if (i == 0x48) {
				continue;
			}
			blockedPlayPackets[i] = true;
		}
	}

	private ProtocolVersion version;
	public PacketEncoder(ProtocolVersion version) {
		this.version = version;
	}

	private final PacketTransformer[] transformers = new PacketTransformer[] {
		new HandshakePacketTransformer(),
		new PlayPacketTransformer(),
		new StatusPacketTransformer(),
		new LoginPacketTransformer()
	};

	@Override
	public void encode(ChannelHandlerContext ctx, Packet<PacketListener> packet, ByteBuf output) throws Exception {
		Channel channel = ctx.channel();
		EnumProtocol currentProtocol = channel.attr(currentStateAttrKey).get();
		final Integer packetId = currentProtocol.a(direction, packet);
		if (packetId == null) {
			throw new IOException("Can't serialize unregistered packet");
		}
		if ((currentProtocol == EnumProtocol.PLAY) && blockedPlayPackets[packetId]) {
			return;
		}
		PacketDataSerializer serializer = new PacketDataSerializer(output, version);
		transformers[currentProtocol.ordinal()].tranform(ctx, packetId, packet, serializer);
	}

}

@@ -351,24 +351,77 @@
 lDirection;%0A
+import net.minecraft.server.v1_8_R3.MinecraftServer;%0A
 import net.m
@@ -2050,16 +2050,25 @@
 rsion);%0A
+%09%09try %7B%0A%09
 %09%09transf
@@ -2146,14 +2146,138 @@
 lizer);%0A
+%09%09%7D catch (Throwable t) %7B%0A%09%09%09if (MinecraftServer.getServer().isDebugging()) %7B%0A%09%09%09%09t.printStackTrace();%0A%09%09%09%7D%0A%09%09%09throw t;%0A%09%09%7D%0A
 %09%7D%0A%0A%7D%0A

Print exception for sure