Skip to content

Instantly share code, notes, and snippets.

@TelepathicGrunt
Last active June 7, 2024 11:40
Show Gist options
  • Save TelepathicGrunt/4d301c67a42cd5fd4181a635ac2efcff to your computer and use it in GitHub Desktop.
Save TelepathicGrunt/4d301c67a42cd5fd4181a635ac2efcff to your computer and use it in GitHub Desktop.
Forge 1.19.2 networking vs Fabric 1.19.2 networking

Forge

Packet class

public record CrystallineFlowerEnchantmentPacket(int containerId, List<EnchantmentSkeleton> enchantmentSkeletons) {
    public static Gson gson = new GsonBuilder().create();

    public static void sendToClient(ServerPlayer entity, int containerId, List<EnchantmentSkeleton> enchantmentSkeletons) {
        MessageHandler.DEFAULT_CHANNEL.send(PacketDistributor.PLAYER.with(() -> entity),
                new CrystallineFlowerEnchantmentPacket(containerId, enchantmentSkeletons));
    }

    /*
     * How the client will read the packet.
     */
    public static CrystallineFlowerEnchantmentPacket parse(final FriendlyByteBuf buf) {
        int containerId = buf.readInt();
        List<EnchantmentSkeleton> enchantmentSkeletons = new ArrayList<>();
        int elements = buf.readInt();
        for (int i = 0; i < elements; i++) {
            String jsonData = buf.readUtf();
            enchantmentSkeletons.add(gson.fromJson(jsonData, EnchantmentSkeleton.class));
        }
        return new CrystallineFlowerEnchantmentPacket(containerId, enchantmentSkeletons);
    }

    /*
     * creates the packet buffer and sets its values
     */
    public static void compose(final CrystallineFlowerEnchantmentPacket pkt, final FriendlyByteBuf buf) {
        buf.writeInt(pkt.containerId());
        buf.writeInt(pkt.enchantmentSkeletons().size());
        for (EnchantmentSkeleton enchantmentSkeleton : pkt.enchantmentSkeletons()) {
            buf.writeUtf(gson.toJson(enchantmentSkeleton));
        }
    }

    /*
     * What the client will do with the packet
     */
    public static class Handler {
        //this is what gets run on the client
        public static void handle(final CrystallineFlowerEnchantmentPacket pkt, final Supplier<NetworkEvent.Context> ctx) {
            ctx.get().enqueueWork(() -> {
                if (Minecraft.getInstance().player != null && Minecraft.getInstance().player.containerMenu.containerId == pkt.containerId()) {
                    CrystallineFlowerScreen.enchantmentsAvailable = pkt.enchantmentSkeletons;
                }
            });
            ctx.get().setPacketHandled(true);
        }
    }
}

Setup

public class MessageHandler {

    //setup channel to send packages through
    private static final String PROTOCOL_VERSION = "1";
    public static final SimpleChannel DEFAULT_CHANNEL = NetworkRegistry.newSimpleChannel(
            new ResourceLocation(Bumblezone.MODID, "networking"),
            () -> PROTOCOL_VERSION, PROTOCOL_VERSION::equals, PROTOCOL_VERSION::equals
    );

    /*
     * Register the channel so it exists
     */
    public static void init() {
        int channelID = -1; // must increment channelID or issues occur when adding more packets to this setup.
        DEFAULT_CHANNEL.registerMessage(++channelID, CrystallineFlowerEnchantmentPacket.class, CrystallineFlowerEnchantmentPacket::compose, CrystallineFlowerEnchantmentPacket::parse, CrystallineFlowerEnchantmentPacket.Handler::handle);
    }
}

MessageHandler.init() is called in mod init.

Calling

CrystallineFlowerEnchantmentPacket.sendToClient(serverPlayer, this.containerId, availableEnchantmentsSkeletons);

Fabric

Packet

public record CrystallineFlowerEnchantmentPacket(int containerId, List<EnchantmentSkeleton> enchantmentSkeletons) {

    public static ResourceLocation PACKET_ID = new ResourceLocation(Bumblezone.MODID, "crystalline_flower_packet");
    public static Gson gson = new GsonBuilder().create();

    public static void sendToClient(ServerPlayer serverPlayer, int containerId, List<EnchantmentSkeleton> enchantmentSkeletons) {
        FriendlyByteBuf packetData = compose(containerId, enchantmentSkeletons);
        ServerPlayNetworking.send(serverPlayer, CrystallineFlowerEnchantmentPacket.PACKET_ID, packetData);
    }
    
    /*
     * creates the packet buffer and sets its values
     */
    private static FriendlyByteBuf compose(int containerId, List<EnchantmentSkeleton> enchantmentSkeletons) {
        FriendlyByteBuf passedData = new FriendlyByteBuf(Unpooled.buffer());
        passedData.writeInt(containerId);
        passedData.writeInt(enchantmentSkeletons.size());
        for (EnchantmentSkeleton skeleton : enchantmentSkeletons) {
            passedData.writeUtf(CrystallineFlowerEnchantmentPacket.gson.toJson(skeleton));
        }
        return passedData;
    }

    /*
     * How the client will read the packet.
     */
    private static CrystallineFlowerEnchantmentPacket parse(FriendlyByteBuf passedData) {
        int containerId = passedData.readInt();

        List<EnchantmentSkeleton> enchantmentSkeletons = new ArrayList<>();
        int elements = passedData.readInt();
        for (int i = 0; i < elements; i++) {
            String jsonData = passedData.readUtf();
            enchantmentSkeletons.add(gson.fromJson(jsonData, EnchantmentSkeleton.class));
        }

        return new CrystallineFlowerEnchantmentPacket(containerId, enchantmentSkeletons);
    }

    /*
     * What the client will do with the packet
     */
    public static void registerPacket() {
        ClientPlayNetworking.registerGlobalReceiver(PACKET_ID,
                (client, handler, buf, responseSender) -> {
                    CrystallineFlowerEnchantmentPacket packetData = parse(buf);

                    client.execute(() -> {
                        if(Minecraft.getInstance().player != null &&
                            Minecraft.getInstance().player.containerMenu.containerId == packetData.containerId())
                        {
                            CrystallineFlowerScreen.enchantmentsAvailable = packetData.enchantmentSkeletons();
                        }
                    });
                });
    }
}

Setup

CrystallineFlowerEnchantmentPacket.registerPacket() is called in mod init.

Calling

CrystallineFlowerEnchantmentPacket.sendToClient(serverPlayer, this.containerId, availableEnchantmentsSkeletons);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment