-
Notifications
You must be signed in to change notification settings - Fork 4
Getting Started (Network)
The Network and Packet System in BlueLib provides a clear and structured way to manage custom packet communication between the client and server. This system allows mod developers to define, register, and handle packets efficiently using straightforward interfaces and utility classes.
With the PacketProvider system, packet registration is centralized and simple, ensuring a clean separation between client and server packet logic.
The PacketProvider is a global interface that supports both client and server packet management. You implement it to define either a client-side or server-side provider, specifying the packets you want to handle:
-
getC2SPackets()– Returns packets that the client sends to the server. -
getS2CPackets()– Returns packets that the server sends to the client.
Here’s an example of a client-side packet provider:
@ApiStatus.Internal
public class ClientNetworkRegistry implements PacketProvider {
@Override
public @NotNull List<PacketRegisterInfo<?>> getC2SPackets() {
List<PacketRegisterInfo<?>> list = new ArrayList<>();
// Client-to-Server packets only need ID and decoder
list.add(new PacketRegisterInfo<>(TestPacket.ID, TestPacket::decode));
return list;
}
@Override
public @NotNull List<PacketRegisterInfo<?>> getS2CPackets() {
List<PacketRegisterInfo<?>> list = new ArrayList<>();
// Server-to-Client packets also include a handler
list.add(new PacketRegisterInfo<>(ExamplePacket.ID, ExamplePacket::decode, ExamplePacketHandler::new));
return list;
}
}And here is a corresponding server-side provider:
@ApiStatus.Internal
public class ServerNetworkRegistry implements PacketProvider {
@Override
public @NotNull List<PacketRegisterInfo<?>> getC2SPackets() {
List<PacketRegisterInfo<?>> list = new ArrayList<>();
// Server handles client-to-server packets with a handler
list.add(new PacketRegisterInfo<>(TestPacket.ID, TestPacket::decode, TestPacketHandler::new));
return list;
}
@Override
public @NotNull List<PacketRegisterInfo<?>> getS2CPackets() {
List<PacketRegisterInfo<?>> list = new ArrayList<>();
// Server-to-client packets only need ID and decoder
list.add(new PacketRegisterInfo<>(ExamplePacket.ID, ExamplePacket::decode));
return list;
}
}Once your packet providers are defined, you need to register them globally using NetworkRegistry.registerPacketProvider:
Server or Global Constructor of the Mod:
NetworkRegistry.registerPacketProvider(new ServerNetworkRegistry());Client Constructor of the Mod:
NetworkRegistry.registerPacketProvider(new ClientNetworkRegistry());This ensures that all packet definitions are loaded early and are correctly mapped during runtime.
Now that your providers are registered, you can define packet structures and handlers to manage the data flowing between client and server. Check the Client-To-Server or Server-To-Client sections for detailed examples of creating and decoding packets, as well as writing handlers.
This setup provides a robust foundation for network communication in your mod using BlueLib’s updated packet system. For additional support, examples, and community discussion, join the official Discord server.
Variant Loader
Data Pipeline
Network & Packets
Logging
Markdown
Utility Classes
- Conversion
- Math
- Minecraft
- Schedular