Server to Client Packets
For when you need to send stuff from the client to the server 😨
Creating S2C 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 S2CVoidPacket or ChainS2CVoidPacket
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: ChainS2CVoidPacket<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 handleS2C(
client: MinecraftClient,
handler: ClientPlayNetworkHandler,
data: PacketData,
responseSender: PacketSender
) {
if (data.shouldDoSomething) {
//do something
}
}
}Now that you have you're packet you have to open the communication channel in your client entrypoint
//client entrypoint
fun clientSetup() {
TestPacket.open()
}Sending Packets
//client entrypoint
fun clientSetup() {
val player: ServerPlayerEntity = //get player
TestPacket.sendToClient(player, TestData(true))
}