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();
}
}
}
}
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();
}
}
Aggregations