use of gg.moonflower.pollen.api.network.packet.PollinatedPacket in project pollen by MoonflowerTeam.
the class PollinatedNetworkChannelImpl method serialize.
protected FriendlyByteBuf serialize(PollinatedPacket<?> message, PollinatedPacketDirection expectedDirection) {
Optional<PacketFactory<?, ?>> factoryOptional = this.factories.stream().filter(factory -> factory.clazz == message.getClass()).findFirst();
if (!factoryOptional.isPresent())
throw new IllegalStateException("Unregistered packet: " + message.getClass() + " on channel: " + this.channelId);
int id = this.factories.indexOf(factoryOptional.get());
if (factoryOptional.get().direction != null && factoryOptional.get().direction != expectedDirection)
throw new IllegalStateException("Attempted to send packet with id: " + id + ". Expected " + expectedDirection + ", got " + factoryOptional.get().direction);
FriendlyByteBuf buf = PacketByteBufs.create();
buf.writeVarInt(id);
try {
message.writePacketData(buf);
} catch (IOException e) {
throw new IllegalStateException("Failed to write packet data", e);
}
return buf;
}
Aggregations