Getting Started (Network) - MeAlam1/BlueLib GitHub Wiki

Getting Started with the Network and Packet System

Overview

The Network and Packet System in BlueLib provides a streamlined approach to handling custom packet communication between the client and server. This structure allows mod developers to easily define, register, and manage network packets using simple interfaces and utility classes.


Step 1: Registering the Networks

To begin using the networking system, you'll need to create a class that implements the PacketProvider.C2SPacketProvider and PacketProvider.S2CPacketProvider interfaces. These interfaces expose methods for registering both client-to-server (C2S) and server-to-client (S2C) packets.

Here's an example of a basic setup:

public class NetworkRegistries implements PacketProvider.C2SPacketProvider, PacketProvider.S2CPacketProvider {

    @Override
    public List<PacketRegisterInfo<?>> getC2SPacketInfoList() {
        List<PacketRegisterInfo<?>> list = new ArrayList<>();

        // Register your Client-to-Server packets here
        list.add(new PacketRegisterInfo<>(TestPacket.ID, TestPacket::decode, new TestPacketHandler()));

        return list;
    }

    @Override
    public List<PacketRegisterInfo<?>> getS2CPacketInfoList() {
        List<PacketRegisterInfo<?>> list = new ArrayList<>();

        // Register your Server-to-Client packets here
        list.add(new PacketRegisterInfo<>(TestPacket.ID, TestPacket::decode, new TestPacketHandler()));

        return list;
    }
}

In this example:

  • Each packet is registered using PacketRegisterInfo, which includes:

    • The packet ID (TestPacket.ID)
    • The decoder method (TestPacket::decode)
    • The handler to execute upon receipt (TestPacketHandler)

Step 2: Registering the Packet Provider

Once you’ve defined your packet lists, you’ll need to register your packet provider during mod initialization. This is typically done in the constructor of your mod class:

    public static YourNetworkRegistry getRegistry() {
        return new YourNetworkRegistry();
    }

    public static void doServerRegistration() {
        NetworkRegistry.registerC2SPacketProvider(getRegistry());
    }

    public static void doClientRegistration() {
        NetworkRegistry.registerS2CPacketProvider(getRegistry());
    }

This ensures your packets are registered early and correctly loaded during runtime.

Make sure to call doServerRegistration() in your mods constructor and doClientRegistration in your mods Client Sided Constructor.


Key Points

  • Separation of Concerns: C2S and S2C packets are clearly separated, making packet direction easy to manage.
  • Centralized Registration: All packets are registered in a single provider class, promoting clean and maintainable code.
  • Simple Integration: Just register your provider during mod initialization, and the system handles the rest.

What’s Next?

Now that your packets are registered, the next step is to define your packet structures and handlers. For a detailed breakdown on writing packets and their handlers, refer to the Client-To-Server or Server-To-Client page.


This setup lays the foundation for robust network communication in your mod using BlueLib’s powerful packet system. For questions, examples, and community support, join the official Discord server.