package net.minecraft.network; import com.mojang.logging.LogUtils; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToByteEncoder; import net.minecraft.network.protocol.Packet; import net.minecraft.network.protocol.PacketType; import net.minecraft.util.profiling.jfr.JvmProfiler; import org.slf4j.Logger; /** * Main netty packet encoder. Writes the packet ID as a VarInt based on the current {@link ConnectionProtocol} as well as the packet's data. */ public class PacketEncoder extends MessageToByteEncoder> { private static final Logger LOGGER = LogUtils.getLogger(); private final ProtocolInfo protocolInfo; public PacketEncoder(ProtocolInfo protocolInfo) { this.protocolInfo = protocolInfo; } protected void encode(ChannelHandlerContext channelHandlerContext, Packet packet, ByteBuf byteBuf) throws Exception { PacketType> packetType = packet.type(); try { this.protocolInfo.codec().encode(byteBuf, packet); int i = byteBuf.readableBytes(); if (LOGGER.isDebugEnabled()) { LOGGER.debug(Connection.PACKET_SENT_MARKER, "OUT: [{}:{}] {} -> {} bytes", this.protocolInfo.id().id(), packetType, packet.getClass().getName(), i); } JvmProfiler.INSTANCE.onPacketSent(this.protocolInfo.id(), packetType, channelHandlerContext.channel().remoteAddress(), i); } catch (Throwable var9) { LOGGER.error("Error sending packet {}", packetType, var9); if (packet.isSkippable()) { throw new SkipPacketEncoderException(var9); } throw var9; } finally { ProtocolSwapHandler.handleOutboundTerminalPacket(channelHandlerContext, packet); } } }