HomeJavaModsClient-Server Communication API
Client-Server Communication API
ModsJava

Client-Server Communication API

by Danrus110_ · on Modrinth

⬇ Download on Modrinth

Client-Server Communication API

how to use for developer:

public class FooBarPacket implements CscPacket{
    
    public FooBarPacket(ByteBuf buf) {
        read(buf);
    }

    private int foo;

    @Override
    public void read(ByteBuf buf) {
        foo = buf.readInt();
    }

    @Override
    public void write(ByteBuf buf) {
        buf.writeInt(foo);
    }
}
public static CscPacketType<FooBarPacket> FOO_BAR_PACKET = CscApi.registerPacket(
  "foo_bar", 
  FooBarPacket.class, 
  FooBarPacket::new
);
public class ServerClass {
    private static final Logger log = LoggerFactory.getLogger(ServerClass.class);

    public void init() {
        CscServer.getService().registerListener((packet, player) -> {
            log.info("Received packet {} from player {}", packet.getClass().getSimpleName(), player.getName());
        }, ModPackets.FOO_BAR_PACKET);
    }
    
    public void sendFooBarPacket(Player player) {
        CscServer.getService().sendPacket(new FooBarPacket(), player);
    }
}
public class ClientClass {
    private static final Logger log = LoggerFactory.getLogger(ClientClass.class);

    public void init() {
        CscClient.getService().registerListener(packet -> {
            log.info("Received packet: {}", packet);
        }, ModPackets.FOO_BAR_PACKET);
    }
    
    public void sendFooBarPacket() {
        CscClient.getService().sendPacket(new FooBarPacket());
    }
}
Quick facts

Install steps are the general flow for this file type — How to install Minecraft Java mods & modpacks walks through it step by step.

Verified by MCModsHub

These come from our own check of the pack file, not from the source page.

Explore more