Search in sources :

Example 1 with PollinatedPacket

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;
}
Also used : PollinatedPacket(gg.moonflower.pollen.api.network.packet.PollinatedPacket) ResourceLocation(net.minecraft.resources.ResourceLocation) IOException(java.io.IOException) FriendlyByteBuf(net.minecraft.network.FriendlyByteBuf) PollinatedPacketDirection(gg.moonflower.pollen.api.network.packet.PollinatedPacketDirection) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Nullable(org.jetbrains.annotations.Nullable) PacketDeserializer(gg.moonflower.pollen.api.network.PacketDeserializer) List(java.util.List) Optional(java.util.Optional) PacketByteBufs(net.fabricmc.fabric.api.networking.v1.PacketByteBufs) LazyLoadedValue(net.minecraft.util.LazyLoadedValue) ApiStatus(org.jetbrains.annotations.ApiStatus) FriendlyByteBuf(net.minecraft.network.FriendlyByteBuf) IOException(java.io.IOException)

Aggregations

PacketDeserializer (gg.moonflower.pollen.api.network.PacketDeserializer)1 PollinatedPacket (gg.moonflower.pollen.api.network.packet.PollinatedPacket)1 PollinatedPacketDirection (gg.moonflower.pollen.api.network.packet.PollinatedPacketDirection)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1 Supplier (java.util.function.Supplier)1 PacketByteBufs (net.fabricmc.fabric.api.networking.v1.PacketByteBufs)1 FriendlyByteBuf (net.minecraft.network.FriendlyByteBuf)1 ResourceLocation (net.minecraft.resources.ResourceLocation)1 LazyLoadedValue (net.minecraft.util.LazyLoadedValue)1 ApiStatus (org.jetbrains.annotations.ApiStatus)1 Nullable (org.jetbrains.annotations.Nullable)1