Networking
How-To use the ModernNetworking
This page describes how to use the ‘ModernNetworking’ of CraftedCore.
For Minecraft 1.20.5+, you need to additionally register the packets the server might receive (C2S): ModernNetworking.registerType(packetId);
Sides
Valid sides are S2C
and C2S
, where S2C
describes Server-To-Client and C2S
describes Client-To-Server.
Receiver
Code, that will be handled once the package is received.
class ImplReceiver implements Receiver {
@Override
public void receive(Context context, CompoundTag data) {
// read data from the data tag and handle it
context.getPlayer(); // returns the player that either send or received the package, depending on:
context.getEnv();
}
}
Registering Receiver
Must be run for every packet, no matter if it’s C2S or S2C.
ModernNetworking.registerReceiver(side, packetId, receiver);
Sending Data
Sending data to a player (server only):
ModernNetworking.sendToPlayer(serverPlayer, packetId, data);
Sending data to the server (client only):
ModernNetworking.sendToServer(packetId, data);
Continue with Platform Tools