Archived
0

refactoring: ProtocolEncoder

This commit is contained in:
2021-05-10 03:21:12 +03:00
parent f9a71250b1
commit 38918f5eaf
3 changed files with 16 additions and 22 deletions

View File

@@ -1,18 +0,0 @@
package mc.protocol;
import io.netty.util.AttributeKey;
import lombok.experimental.UtilityClass;
import java.util.Map;
@UtilityClass
public class NetworkAttributes {
public static final AttributeKey<State> STATE = AttributeKey.newInstance("STATE");
/**
* @deprecated костыль
*/
@Deprecated
public static final AttributeKey<Map<String, Object>> CUSTOM_PROPERTIES = AttributeKey.newInstance("CUSTOM_PROPERTIES");
}

View File

@@ -1,35 +0,0 @@
package mc.protocol.io.codec;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import lombok.extern.slf4j.Slf4j;
import mc.protocol.NetworkAttributes;
import mc.protocol.State;
import mc.protocol.io.NetByteBuf;
import mc.protocol.packets.ServerSidePacket;
@Slf4j
public class ProtocolEncoder extends MessageToByteEncoder<ServerSidePacket> {
@Override
protected void encode(ChannelHandlerContext ctx, ServerSidePacket packet, ByteBuf out) {
State state = ctx.channel().attr(NetworkAttributes.STATE).get();
Integer packetId = state.getServerSidePacketId(packet.getClass());
if (packetId == null) {
log.error("Unknown send packet: State {} ; Class {}", state, packet.getClass());
return;
}
log.debug("OUT: {}:{}", state, packet);
NetByteBuf buffer = new NetByteBuf(Unpooled.buffer());
buffer.writeVarInt(packetId);
packet.writeSelf(buffer);
NetByteBuf netByteBuf = new NetByteBuf(out);
netByteBuf.writeVarInt(buffer.readableBytes());
netByteBuf.writeBytes(buffer);
}
}