Search in sources :

Example 1 with ClientboundCustomPayloadPacket

use of net.minecraft.network.protocol.game.ClientboundCustomPayloadPacket in project MinecraftForge by MinecraftForge.

the class VanillaPacketSplitter method appendPackets.

/**
 * Append the given packet to the given list. If the packet needs to be split, multiple packets will be appened.
 * Otherwise only the packet itself.
 */
public static void appendPackets(ConnectionProtocol protocol, PacketFlow direction, Packet<?> packet, List<? super Packet<?>> out) {
    if (heuristicIsDefinitelySmallEnough(packet)) {
        out.add(packet);
    } else {
        FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
        packet.write(buf);
        if (buf.readableBytes() <= PROTOCOL_MAX) {
            buf.release();
            out.add(packet);
        } else {
            int parts = (int) Math.ceil(((double) buf.readableBytes()) / PART_SIZE);
            if (parts == 1) {
                buf.release();
                out.add(packet);
            } else {
                for (int part = 0; part < parts; part++) {
                    ByteBuf partPrefix;
                    if (part == 0) {
                        partPrefix = Unpooled.buffer(5);
                        partPrefix.writeByte(STATE_FIRST);
                        new FriendlyByteBuf(partPrefix).writeVarInt(protocol.getPacketId(direction, packet));
                    } else {
                        partPrefix = Unpooled.buffer(1);
                        partPrefix.writeByte(part == parts - 1 ? STATE_LAST : 0);
                    }
                    int partSize = Math.min(PART_SIZE, buf.readableBytes());
                    ByteBuf partBuf = Unpooled.wrappedBuffer(partPrefix, buf.retainedSlice(buf.readerIndex(), partSize));
                    buf.skipBytes(partSize);
                    out.add(new ClientboundCustomPayloadPacket(CHANNEL, new FriendlyByteBuf(partBuf)));
                }
                // we retained all the slices, so we do not need this one anymore
                buf.release();
            }
        }
    }
}
Also used : ClientboundCustomPayloadPacket(net.minecraft.network.protocol.game.ClientboundCustomPayloadPacket) ByteBuf(io.netty.buffer.ByteBuf)

Example 2 with ClientboundCustomPayloadPacket

use of net.minecraft.network.protocol.game.ClientboundCustomPayloadPacket in project SpongeCommon by SpongePowered.

the class PacketUtil method createPlayPayload.

public static net.minecraft.network.protocol.Packet<?> createPlayPayload(final ResourceKey channel, final ChannelBuf payload, final EngineConnectionSide<?> side) {
    if (side == EngineConnectionSide.CLIENT) {
        final ServerboundCustomPayloadPacketAccessor packet = (ServerboundCustomPayloadPacketAccessor) new ServerboundCustomPayloadPacket();
        packet.accessor$identifier((ResourceLocation) (Object) channel);
        packet.accessor$data((FriendlyByteBuf) payload);
        return (net.minecraft.network.protocol.Packet<?>) packet;
    } else if (side == EngineConnectionSide.SERVER) {
        final ClientboundCustomPayloadPacketAccessor packet = (ClientboundCustomPayloadPacketAccessor) new ClientboundCustomPayloadPacket();
        packet.accessor$identifier((ResourceLocation) (Object) channel);
        packet.accessor$data((FriendlyByteBuf) payload);
        return (net.minecraft.network.protocol.Packet<?>) packet;
    } else {
        throw new UnsupportedOperationException();
    }
}
Also used : FriendlyByteBuf(net.minecraft.network.FriendlyByteBuf) ServerboundCustomQueryPacket(net.minecraft.network.protocol.login.ServerboundCustomQueryPacket) ClientboundCustomPayloadPacket(net.minecraft.network.protocol.game.ClientboundCustomPayloadPacket) ServerboundCustomPayloadPacket(net.minecraft.network.protocol.game.ServerboundCustomPayloadPacket) Packet(org.spongepowered.api.network.channel.packet.Packet) ClientboundCustomQueryPacket(net.minecraft.network.protocol.login.ClientboundCustomQueryPacket) ServerboundCustomPayloadPacketAccessor(org.spongepowered.common.accessor.network.protocol.game.ServerboundCustomPayloadPacketAccessor) ClientboundCustomPayloadPacket(net.minecraft.network.protocol.game.ClientboundCustomPayloadPacket) ServerboundCustomPayloadPacket(net.minecraft.network.protocol.game.ServerboundCustomPayloadPacket) ClientboundCustomPayloadPacketAccessor(org.spongepowered.common.accessor.network.protocol.game.ClientboundCustomPayloadPacketAccessor) ResourceLocation(net.minecraft.resources.ResourceLocation)

Aggregations

ClientboundCustomPayloadPacket (net.minecraft.network.protocol.game.ClientboundCustomPayloadPacket)2 ByteBuf (io.netty.buffer.ByteBuf)1 FriendlyByteBuf (net.minecraft.network.FriendlyByteBuf)1 ServerboundCustomPayloadPacket (net.minecraft.network.protocol.game.ServerboundCustomPayloadPacket)1 ClientboundCustomQueryPacket (net.minecraft.network.protocol.login.ClientboundCustomQueryPacket)1 ServerboundCustomQueryPacket (net.minecraft.network.protocol.login.ServerboundCustomQueryPacket)1 ResourceLocation (net.minecraft.resources.ResourceLocation)1 Packet (org.spongepowered.api.network.channel.packet.Packet)1 ClientboundCustomPayloadPacketAccessor (org.spongepowered.common.accessor.network.protocol.game.ClientboundCustomPayloadPacketAccessor)1 ServerboundCustomPayloadPacketAccessor (org.spongepowered.common.accessor.network.protocol.game.ServerboundCustomPayloadPacketAccessor)1