VoidLib
Networking
Client to Server

Client to Server Packets

For when you need to send stuff from the client to the server 😨

Creating C2S Packets

You'll need an @Serializable (kotlinx.serialization) data class

Here's an example

@Serializable
data class TestData(val shouldDoSomething: Boolean)

Packet Logic (The 'Hard' Part)

You'll need an object (or class we don't discriminate (You should use an object though)) that extends either C2SVoidPacket or ChainC2SVoidPacket

The only difference being that the Chain variant provides a utility function where you don't have to put in the chain/receiver id when sending packets

Here's an example

object TestPacket: ChainC2SVoidPacket<TestData> {
    override val chainId: Identifier = Identifier("test_mod", "test_packet")
    @OptIn(ExperimentalSerializationApi::class)
    override val format: BinaryFormat = Cbor { encodeDefaults = true }
    //serializer() function auto generated by @Serializable
    override val serializer: KSerializer<TestData> = TestData.serializer()
 
    override fun handleC2S(
        server: MinecraftServer,
        player: ServerPlayerEntity,
        handler: ServerPlayNetworkHandler,
        data: TestData,
        responseSender: PacketSender
    ) {
        if (data.shouldDoSomething) {
            //do something
        }
    }
}

Now that you have you're packet you have to open the communication channel in your main entrypoint

//main entrypoint
fun commonSetup() {
    TestPacket.open()
}

Sending Packets

//main entrypoint
fun commonSetup() {
    TestPacket.sendToServer(TestData(true))
}