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")
Content copied to clipboard
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)
Content copied to clipboard
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"))
Content copied to clipboard
Client to server
Use the c2sPacket function.
val personPacket = c2sPacket<Person>(Identifier("mymod", "personpacket"))
Content copied to clipboard
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)
Content copied to clipboard
Send to all players:
personPacket.sendToAll(Person("Maria", 21))
Content copied to clipboard
Client to server
personPacket.send(Person("Holger", 52))
Content copied to clipboard
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)
}
Content copied to clipboard
Client to server
personPacket.receiveOnServer { packet, context ->
println(packet)
}
Content copied to clipboard