package net.minecraft.network;

import org.apache.logging.log4j.MarkerManager;
import org.apache.logging.log4j.LogManager;
import net.minecraft.network.protocol.Packet;
import java.io.IOException;
import io.netty.util.AttributeKey;
import java.util.List;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import net.minecraft.network.protocol.PacketFlow;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.Logger;
import io.netty.handler.codec.ByteToMessageDecoder;

public class PacketDecoder extends ByteToMessageDecoder {
    private static final Logger LOGGER;
    private static final Marker MARKER;
    private final PacketFlow flow;
    
    public PacketDecoder(final PacketFlow lu) {
        this.flow = lu;
    }
    
    protected void decode(final ChannelHandlerContext channelHandlerContext, final ByteBuf byteBuf, final List<Object> list) throws Exception {
        if (byteBuf.readableBytes() == 0) {
            return;
        }
        final FriendlyByteBuf kv5 = new FriendlyByteBuf(byteBuf);
        final int integer6 = kv5.readVarInt();
        final Packet<?> lt7 = ((ConnectionProtocol)channelHandlerContext.channel().attr((AttributeKey)Connection.ATTRIBUTE_PROTOCOL).get()).createPacket(this.flow, integer6);
        if (lt7 == null) {
            throw new IOException("Bad packet id " + integer6);
        }
        lt7.read(kv5);
        if (kv5.readableBytes() > 0) {
            throw new IOException("Packet " + ((ConnectionProtocol)channelHandlerContext.channel().attr((AttributeKey)Connection.ATTRIBUTE_PROTOCOL).get()).getId() + "/" + integer6 + " (" + lt7.getClass().getSimpleName() + ") was larger than I expected, found " + kv5.readableBytes() + " bytes extra whilst reading packet " + integer6);
        }
        list.add(lt7);
        if (PacketDecoder.LOGGER.isDebugEnabled()) {
            PacketDecoder.LOGGER.debug(PacketDecoder.MARKER, " IN: [{}:{}] {}", channelHandlerContext.channel().attr((AttributeKey)Connection.ATTRIBUTE_PROTOCOL).get(), integer6, lt7.getClass().getName());
        }
    }
    
    static {
        LOGGER = LogManager.getLogger();
        MARKER = MarkerManager.getMarker("PACKET_RECEIVED", Connection.PACKET_MARKER);
    }
}
