silk-network

Send any serializable class as a packet - using kotlinx.serialization. Provides utilities for sending and receiving packets.

Dependency

modImplementation("net.silkmc:silk-network:1.10.4")

Define a packet

Create your packet class

You have to create a serializable class representing your packet first.

@Serializable
data class Person(val name: String, val age: Int)

Packet definition instance

Create a packet definition instance, this class holds information about the packet type and the packet id, and provides you with functions for sending and receiving packets.

Server to client

Use the s2cPacket function.

val personPacket = s2cPacket<Person>(Identifier("mymod", "personpacket"))

Client to server

Use the c2sPacket function.

val personPacket = c2sPacket<Person>(Identifier("mymod", "personpacket"))

Send a packet

Once you have packet definition instance, you can send packets.

Server to client

Send to a specific player:

personPacket.send(Person("John", 21), player)

Send to all players:

personPacket.sendToAll(Person("Maria", 21))

Client to server

personPacket.send(Person("Holger", 52))

Receive a packet

Using the packet instance, you can also register a packet receiver.

This can be done using the receiveOnClient or receiveOnServer function.

Server to client

personPacket.receiveOnClient { packet, context ->
println(packet)
}

Client to server

personPacket.receiveOnServer { packet, context ->
println(packet)
}

Packages

Link copied to clipboard

Utilities for creating client-to-server and server-to-client packets