#Failing to deserialize packet id correct
1 messages · Page 1 of 1 (latest)
Hey, @verbal meteor!
Please remember to /close this post once your question has been answered!
public static List<IncomingPacket> deserialize(ByteBuf data, IncomingPacketRegistry incomingPacketRegistry)
throws InstantiationException, IllegalAccessException {
List<IncomingPacket> packets = new GlueList<>();
for (ByteBuf packetBuf : splitPackets(data)) {
int id = ByteBufUtil.readVarInt(packetBuf);
System.out.println(id); // For some reason this is always zero.
Class<? extends IncomingPacket> packetClass = incomingPacketRegistry.get(id);
IncomingPacket packet = packetClass.newInstance();
packet.deserialize(packetBuf);
packets.add(packet);
}
data.discardReadBytes();
return packets;
}
public static List<ByteBuf> splitPackets(ByteBuf buf) {
List<ByteBuf> bufList = new GlueList<>();
buf.markReaderIndex();
byte[] lengthBytes = new byte[3];
for (int position = 0; position < lengthBytes.length; ++position) {
if (!buf.isReadable()) {
buf.resetReaderIndex();
return bufList;
}
lengthBytes[position] = buf.readByte();
if (lengthBytes[position] < 0) {
continue;
}
ByteBuf byteBuf = Unpooled.wrappedBuffer(lengthBytes);
try {
int length = ByteBufUtil.readVarInt(byteBuf);
if (buf.readableBytes() < length) {
buf.resetReaderIndex();
return bufList;
}
bufList.add(buf.readBytes(length));
} finally {
byteBuf.release();
}
return bufList;
}
return bufList;
}
The code that comes before it:
Method Inside Connection.java
public void processPackets(ByteBuf buf) {
List<IncomingPacket> receivedPackets;
try {
receivedPackets = PacketSerializer.deserialize(buf, INCOMING_PACKET_REGISTRY);
for (IncomingPacket received : receivedPackets) {
if (MinecraftServer.DEBUG_MESSAGES)
System.out.println("[Mineral] Packet received: " + received.getClass().getSimpleName());
received.received(this);
}
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
}
Method inside ServerHandler.java (extends ChannelInboundHandlerAdapter)
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ByteBuf in = (ByteBuf) msg;
Connection connection = connected.getOrCreate(ctx);
connection.processPackets(in);
}