Search in sources :

Example 1 with PacketFlow

use of net.minecraft.network.protocol.PacketFlow in project MinecraftForge by MinecraftForge.

the class VanillaPacketSplitter method onClientPacket.

@SuppressWarnings("unchecked")
private static void onClientPacket(NetworkEvent.ServerCustomPayloadEvent event) {
    NetworkEvent.Context ctx = event.getSource().get();
    PacketFlow direction = ctx.getDirection() == NetworkDirection.PLAY_TO_CLIENT ? PacketFlow.CLIENTBOUND : PacketFlow.SERVERBOUND;
    ConnectionProtocol protocol = ConnectionProtocol.PLAY;
    ctx.setPacketHandled(true);
    FriendlyByteBuf buf = event.getPayload();
    byte state = buf.readByte();
    if (state == STATE_FIRST) {
        if (!receivedBuffers.isEmpty()) {
            LOGGER.warn("forge:split received out of order - inbound buffer not empty when receiving first");
            receivedBuffers.clear();
        }
    }
    // retain the buffer, it is released after this handler otherwise
    buf.retain();
    receivedBuffers.add(buf);
    if (state == STATE_LAST) {
        FriendlyByteBuf full = new FriendlyByteBuf(Unpooled.wrappedBuffer(receivedBuffers.toArray(new FriendlyByteBuf[0])));
        int packetId = full.readVarInt();
        Packet<?> packet = protocol.createPacket(direction, packetId, full);
        if (packet == null) {
            LOGGER.error("Received invalid packet ID {} in forge:split", packetId);
        } else {
            receivedBuffers.clear();
            full.release();
            ctx.enqueueWork(() -> ((Packet<ClientPacketListener>) packet).handle(Minecraft.getInstance().getConnection()));
        }
    }
}
Also used : ClientPacketListener(net.minecraft.client.multiplayer.ClientPacketListener) PacketFlow(net.minecraft.network.protocol.PacketFlow)

Aggregations

ClientPacketListener (net.minecraft.client.multiplayer.ClientPacketListener)1 PacketFlow (net.minecraft.network.protocol.PacketFlow)1