Skip to content
This repository was archived by the owner on Dec 10, 2025. It is now read-only.

Commit 20e675e

Browse files
committed
refactor: add kotlin-byte-buf-serializer dependency and update packet codec handling
1 parent 539d8fe commit 20e675e

File tree

7 files changed

+116
-2
lines changed

7 files changed

+116
-2
lines changed

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ zstd-jni = "1.5.6-9"
1717
luckperms-api = "5.4"
1818
reactive-streams = "1.0.4"
1919
ehcache = "3.10.8"
20+
kotlin-byte-buf-serializer = "1.0.0"
2021

2122

2223
[libraries]
@@ -65,6 +66,7 @@ spring-aspects = { module = "org.springframework:spring-aspects" }
6566
flyway-core = { module = "org.flywaydb:flyway-core" }
6667
flyway-mysql = { module = "org.flywaydb:flyway-mysql" }
6768
spring-instrument = { module = "org.springframework:spring-instrument" }
69+
kotlin-byte-buf-serializer = { module = "dev.slne.surf:kotlin-byte-buf-serializer", version.ref = "kotlin-byte-buf-serializer" }
6870

6971

7072
[plugins]

surf-cloud-api/surf-cloud-api-common/api/surf-cloud-api-common.api

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,47 @@ public final class dev/slne/surf/cloud/api/common/CloudInstanceKt {
1919
public abstract interface annotation class dev/slne/surf/cloud/api/common/SurfCloudApplication : java/lang/annotation/Annotation {
2020
}
2121

22+
public final class dev/slne/surf/cloud/api/common/TestPacket : dev/slne/surf/cloud/api/common/netty/packet/NettyPacket {
23+
public static final field Companion Ldev/slne/surf/cloud/api/common/TestPacket$Companion;
24+
public fun <init> (Ljava/lang/String;IZLjava/util/List;Ljava/util/Map;Ljava/lang/String;Ljava/util/List;Ljava/util/Map;)V
25+
public final fun component1 ()Ljava/lang/String;
26+
public final fun component2 ()I
27+
public final fun component3 ()Z
28+
public final fun component4 ()Ljava/util/List;
29+
public final fun component5 ()Ljava/util/Map;
30+
public final fun component6 ()Ljava/lang/String;
31+
public final fun component7 ()Ljava/util/List;
32+
public final fun component8 ()Ljava/util/Map;
33+
public final fun copy (Ljava/lang/String;IZLjava/util/List;Ljava/util/Map;Ljava/lang/String;Ljava/util/List;Ljava/util/Map;)Ldev/slne/surf/cloud/api/common/TestPacket;
34+
public static synthetic fun copy$default (Ldev/slne/surf/cloud/api/common/TestPacket;Ljava/lang/String;IZLjava/util/List;Ljava/util/Map;Ljava/lang/String;Ljava/util/List;Ljava/util/Map;ILjava/lang/Object;)Ldev/slne/surf/cloud/api/common/TestPacket;
35+
public fun equals (Ljava/lang/Object;)Z
36+
public final fun getBoolean ()Z
37+
public final fun getData ()Ljava/lang/String;
38+
public final fun getList ()Ljava/util/List;
39+
public final fun getMap ()Ljava/util/Map;
40+
public final fun getNullable ()Ljava/lang/String;
41+
public final fun getNullableList ()Ljava/util/List;
42+
public final fun getNullableMap ()Ljava/util/Map;
43+
public final fun getNumber ()I
44+
public fun hashCode ()I
45+
public fun toString ()Ljava/lang/String;
46+
}
47+
48+
public synthetic class dev/slne/surf/cloud/api/common/TestPacket$$serializer : kotlinx/serialization/internal/GeneratedSerializer {
49+
public static final field INSTANCE Ldev/slne/surf/cloud/api/common/TestPacket$$serializer;
50+
public final fun childSerializers ()[Lkotlinx/serialization/KSerializer;
51+
public final fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Ldev/slne/surf/cloud/api/common/TestPacket;
52+
public synthetic fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Ljava/lang/Object;
53+
public final fun getDescriptor ()Lkotlinx/serialization/descriptors/SerialDescriptor;
54+
public final fun serialize (Lkotlinx/serialization/encoding/Encoder;Ldev/slne/surf/cloud/api/common/TestPacket;)V
55+
public synthetic fun serialize (Lkotlinx/serialization/encoding/Encoder;Ljava/lang/Object;)V
56+
}
57+
58+
public final class dev/slne/surf/cloud/api/common/TestPacket$Companion {
59+
public final fun random ()Ldev/slne/surf/cloud/api/common/TestPacket;
60+
public final fun serializer ()Lkotlinx/serialization/KSerializer;
61+
}
62+
2263
public class dev/slne/surf/cloud/api/common/config/auto/AdventureAutoConfiguration {
2364
public fun <init> ()V
2465
public fun miniMessage ()Lnet/kyori/adventure/text/minimessage/MiniMessage;

surf-cloud-api/surf-cloud-api-common/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ dependencies {
1010

1111
api(libs.aide.reflection)
1212
api(libs.netty.all)
13+
api(libs.kotlin.byte.buf.serializer) {
14+
exclude(group = "io.netty")
15+
}
16+
1317
api(libs.nbt)
1418
api(libs.datafixerupper) {
1519
isTransitive = false
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package dev.slne.surf.cloud.api.common
2+
3+
import dev.slne.surf.cloud.api.common.meta.SurfNettyPacket
4+
import dev.slne.surf.cloud.api.common.netty.network.protocol.PacketFlow
5+
import dev.slne.surf.cloud.api.common.netty.packet.NettyPacket
6+
import kotlinx.serialization.Serializable
7+
8+
@Serializable
9+
@SurfNettyPacket("cloud:serverbound:test", PacketFlow.SERVERBOUND)
10+
data class TestPacket(
11+
val data: String,
12+
val number: Int,
13+
val boolean: Boolean,
14+
val list: List<String>,
15+
val map: Map<String, String>,
16+
val nullable: String?,
17+
val nullableList: List<String>?,
18+
val nullableMap: Map<String, String>?,
19+
) : NettyPacket() {
20+
companion object {
21+
fun random() = TestPacket(
22+
data = "test",
23+
number = 42,
24+
boolean = true,
25+
list = listOf("one", "two", "three"),
26+
map = mapOf("key1" to "value1", "key2" to "value2"),
27+
nullable = null,
28+
nullableList = null,
29+
nullableMap = null
30+
)
31+
}
32+
}

surf-cloud-api/surf-cloud-api-common/src/main/kotlin/dev/slne/surf/cloud/api/common/netty/packet/packet-extension.kt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package dev.slne.surf.cloud.api.common.netty.packet
22

3+
import dev.slne.surf.bytebufserializer.Buf
34
import dev.slne.surf.cloud.api.common.meta.PacketCodec
45
import dev.slne.surf.cloud.api.common.meta.SurfNettyPacket
56
import dev.slne.surf.cloud.api.common.netty.network.codec.StreamCodec
67
import dev.slne.surf.cloud.api.common.netty.network.codec.StreamDecoder
78
import dev.slne.surf.cloud.api.common.netty.network.codec.StreamMemberEncoder
89
import dev.slne.surf.cloud.api.common.util.mutableObject2ObjectMapOf
910
import io.netty.buffer.ByteBuf
11+
import kotlinx.serialization.InternalSerializationApi
12+
import kotlinx.serialization.KSerializer
13+
import kotlinx.serialization.serializerOrNull
1014
import kotlin.reflect.KClass
1115
import kotlin.reflect.full.companionObject
1216
import kotlin.reflect.full.companionObjectInstance
@@ -69,10 +73,24 @@ private val codecCache = mutableObject2ObjectMapOf<KClass<out NettyPacket>, Stre
6973
*
7074
* @return The [StreamCodec] for the packet type, or `null` if not found.
7175
*/
76+
@OptIn(InternalSerializationApi::class)
7277
@Suppress("UNCHECKED_CAST")
73-
fun <B : Any, V : Any> KClass<out NettyPacket>.findPacketCodec(): StreamCodec<in B, out V>? {
78+
fun <B : ByteBuf, V : NettyPacket> KClass<out V>.findPacketCodec(): StreamCodec<in B, out V>? {
7479
codecCache[this]?.let { return it as? StreamCodec<in B, out V> }
7580

81+
val serializer = serializerOrNull()
82+
if (serializer != null) {
83+
return object : StreamCodec<B, V> {
84+
override fun decode(buf: B): V {
85+
return Buf.decodeFromBuf(buf, serializer)
86+
}
87+
88+
override fun encode(buf: B, value: V) {
89+
Buf.encodeToBuf(buf, serializer as KSerializer<V>, value)
90+
}
91+
}
92+
}
93+
7694
val properties =
7795
declaredMemberProperties + (companionObject?.declaredMemberProperties ?: emptyList())
7896

@@ -92,4 +110,4 @@ fun <B : Any, V : Any> KClass<out NettyPacket>.findPacketCodec(): StreamCodec<in
92110
codec?.let { codecCache[this] = it }
93111

94112
return codec
95-
}
113+
}

surf-cloud-bukkit/src/main/kotlin/dev/slne/surf/cloud/bukkit/BukkitMain.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import com.github.shynixn.mccoroutine.folia.launch
66
import com.github.shynixn.mccoroutine.folia.ticks
77
import dev.jorel.commandapi.CommandAPIBukkit
88
import dev.jorel.commandapi.kotlindsl.*
9+
import dev.slne.surf.cloud.api.client.netty.packet.fireAndForget
910
import dev.slne.surf.cloud.api.client.paper.player.toCloudOfflinePlayer
11+
import dev.slne.surf.cloud.api.common.TestPacket
1012
import dev.slne.surf.cloud.api.common.player.teleport.TeleportCause
1113
import dev.slne.surf.cloud.api.common.player.teleport.fineLocation
1214
import dev.slne.surf.cloud.api.common.player.toCloudPlayer
@@ -270,6 +272,13 @@ class BukkitMain : SuspendingJavaPlugin() {
270272
}
271273
}
272274
}
275+
276+
commandAPICommand("send-test-packet") {
277+
anyExecutor { sender, args ->
278+
TestPacket.random().fireAndForget()
279+
sender.sendPlainMessage("Test packet sent")
280+
}
281+
}
273282
}
274283

275284
@OptIn(ExperimentalContracts::class)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
package dev.slne.surf.cloud.standalone.test
22

3+
import dev.slne.surf.cloud.api.common.TestPacket
4+
import dev.slne.surf.cloud.api.common.meta.SurfNettyPacketHandler
35
import dev.slne.surf.surfapi.core.api.util.logger
46
import org.springframework.stereotype.Component
57

68

79
@Component
810
class TestPacketListener {
911
private val log = logger()
12+
13+
@SurfNettyPacketHandler
14+
fun onTestPacket(packet: TestPacket) {
15+
log.atInfo()
16+
.log("Received TestPacket: $packet")
17+
}
1018
}

0 commit comments

Comments
 (0)