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

Commit f921fa0

Browse files
committed
feat(nbt): migrate from querz.nbt to kyori.adventure.nbt for binary tag support
1 parent cd81c6f commit f921fa0

File tree

18 files changed

+391
-421
lines changed

18 files changed

+391
-421
lines changed

gradle/libs.versions.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ velocity-version = "3.4.0-SNAPSHOT"
33
aide-reflection = "1.3"
44
netty = "4.2.0.Final"
55
netty-tcnative = "2.0.70.Final"
6-
nbt = "6.1"
76
datafixerupper = "8.0.16"
87
#byte-buddy = "1.15.10"
98
exposed = "0.61.0"
@@ -34,7 +33,6 @@ jackson-module-kotlin = { module = "com.fasterxml.jackson.module:jackson-module-
3433
aide-reflection = { module = "tech.hiddenproject:aide-reflection", version.ref = "aide-reflection" }
3534
netty-all = { module = "io.netty:netty-all", version.ref = "netty" }
3635
netty-tcnative = { module = "io.netty:netty-tcnative", version.ref = "netty-tcnative" }
37-
nbt = { module = "com.github.Querz:NBT", version.ref = "nbt" }
3836
datafixerupper = { module = "com.mojang:datafixerupper", version.ref = "datafixerupper" }
3937
byte-buddy = { module = "net.bytebuddy:byte-buddy" }
4038
exposed-spring-boot-starter = { module = "org.jetbrains.exposed:exposed-spring-boot-starter", version.ref = "exposed" }

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import dev.slne.surf.surfapi.gradle.util.slneReleases
2+
import jdk.tools.jlink.resources.plugins
23

34
plugins {
45
id("dev.slne.surf.surfapi.gradle.core")
@@ -15,7 +16,6 @@ dependencies {
1516
exclude(group = "io.netty")
1617
}
1718

18-
api(libs.nbt)
1919
api(libs.datafixerupper) {
2020
isTransitive = false
2121
}

surf-cloud-api/surf-cloud-api-common/src/main/kotlin/dev/slne/surf/cloud/api/common/netty/network/codec/kotlinx/nbt/CompoundTagSerializer.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@ import dev.slne.surf.cloud.api.common.netty.protocol.buffer.SurfByteBuf
55
import kotlinx.serialization.Serializable
66
import kotlinx.serialization.builtins.ByteArraySerializer
77
import kotlinx.serialization.descriptors.SerialDescriptor
8-
import net.querz.nbt.tag.CompoundTag
8+
import net.kyori.adventure.nbt.CompoundBinaryTag
99

10-
typealias SerializableCompoundTag = @Serializable(with = CompoundTagSerializer::class) CompoundTag
10+
typealias SerializableCompoundTag = @Serializable(with = CompoundTagSerializer::class) CompoundBinaryTag
1111

12-
object CompoundTagSerializer : CloudBufSerializer<CompoundTag>() {
13-
override val descriptor = SerialDescriptor("CompoundTag", ByteArraySerializer().descriptor)
12+
object CompoundTagSerializer : CloudBufSerializer<CompoundBinaryTag>() {
13+
override val descriptor =
14+
SerialDescriptor("CompoundBinaryTag", ByteArraySerializer().descriptor)
1415

1516
override fun serialize0(
1617
buf: SurfByteBuf,
17-
value: CompoundTag
18+
value: CompoundBinaryTag
1819
) {
1920
buf.writeCompoundTag(value)
2021
}
2122

22-
override fun deserialize0(buf: SurfByteBuf): CompoundTag {
23+
override fun deserialize0(buf: SurfByteBuf): CompoundBinaryTag {
2324
return buf.readCompoundTag()
2425
}
2526
}

surf-cloud-api/surf-cloud-api-common/src/main/kotlin/dev/slne/surf/cloud/api/common/netty/protocol/buffer/SurfByteBuf.kt

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap
2828
import it.unimi.dsi.fastutil.objects.ObjectArrayList
2929
import kotlinx.serialization.KSerializer
3030
import net.kyori.adventure.key.Key
31+
import net.kyori.adventure.nbt.CompoundBinaryTag
3132
import net.kyori.adventure.sound.Sound
3233
import net.kyori.adventure.text.Component
33-
import net.querz.nbt.tag.CompoundTag
3434
import java.io.*
3535
import java.net.Inet4Address
3636
import java.net.InetSocketAddress
@@ -97,7 +97,7 @@ open class SurfByteBuf(source: ByteBuf) : WrappedByteBuf(source) {
9797

9898
private val GSON = Gson()
9999

100-
fun<T> streamCodecFromKotlin(serializer: KSerializer<T>): StreamCodec<SurfByteBuf, T> {
100+
fun <T> streamCodecFromKotlin(serializer: KSerializer<T>): StreamCodec<SurfByteBuf, T> {
101101
return SerializerCodec(serializer)
102102
}
103103

@@ -591,7 +591,8 @@ open class SurfByteBuf(source: ByteBuf) : WrappedByteBuf(source) {
591591
buf: B,
592592
decodeFactory: DecodeLongFactory<in B> =
593593
DecodeLongFactory { buf -> buf.readLong() }
594-
): OptionalLong = if (buf.readBoolean()) OptionalLong.of(decodeFactory.decodeLong(buf)) else OptionalLong.empty()
594+
): OptionalLong =
595+
if (buf.readBoolean()) OptionalLong.of(decodeFactory.decodeLong(buf)) else OptionalLong.empty()
595596

596597

597598
@Deprecated("Use codec instead")
@@ -651,18 +652,18 @@ open class SurfByteBuf(source: ByteBuf) : WrappedByteBuf(source) {
651652
fun <B : ByteBuf> readInetSocketAddress(buf: B) =
652653
createUnresolvedInetSocketAddress(readUtf(buf), buf.readVarInt())
653654

654-
fun <B : ByteBuf> writeCompoundTag(buf: B, tag: CompoundTag) {
655+
fun <B : ByteBuf> writeCompoundTag(buf: B, tag: CompoundBinaryTag) {
655656
ExtraCodecs.COMPOUND_TAG_CODEC.encode(buf, tag)
656657
}
657658

658659
fun <B : ByteBuf> readCompoundTag(buf: B) = ExtraCodecs.COMPOUND_TAG_CODEC.decode(buf)
659660

660-
fun <B: ByteBuf> writeZonedDateTime(buf: B, time: ZonedDateTime) {
661+
fun <B : ByteBuf> writeZonedDateTime(buf: B, time: ZonedDateTime) {
661662
buf.writeLong(time.toInstant().toEpochMilli())
662663
writeUtf(buf, time.zone.id)
663664
}
664665

665-
fun <B: ByteBuf> readZonedDateTime(buf: B): ZonedDateTime {
666+
fun <B : ByteBuf> readZonedDateTime(buf: B): ZonedDateTime {
666667
val epoch = buf.readLong()
667668
val zone = readUtf(buf)
668669
return ZonedDateTime.ofInstant(Instant.ofEpochMilli(epoch), ZoneId.of(zone))
@@ -688,11 +689,11 @@ open class SurfByteBuf(source: ByteBuf) : WrappedByteBuf(source) {
688689
?: throw DecoderException("Failed to read singleton: $className")
689690
}
690691

691-
fun <B: ByteBuf> writeDuration(buf: B, duration: Duration) {
692+
fun <B : ByteBuf> writeDuration(buf: B, duration: Duration) {
692693
buf.writeLong(duration.inWholeMilliseconds)
693694
}
694695

695-
fun <B: ByteBuf> readDuration(buf: B): Duration {
696+
fun <B : ByteBuf> readDuration(buf: B): Duration {
696697
return buf.readLong().milliseconds
697698
}
698699
}
@@ -862,7 +863,7 @@ open class SurfByteBuf(source: ByteBuf) : WrappedByteBuf(source) {
862863
fun readWithCount(reader: Consumer<SurfByteBuf>) = readWithCount(this, reader)
863864
fun writeInetSocketAddress(address: InetSocketAddress) = writeInetSocketAddress(this, address)
864865
fun readInetSocketAddress() = readInetSocketAddress(this)
865-
fun writeCompoundTag(tag: CompoundTag) = writeCompoundTag(this, tag)
866+
fun writeCompoundTag(tag: CompoundBinaryTag) = writeCompoundTag(this, tag)
866867
fun readCompoundTag() = readCompoundTag(this)
867868
fun writeZonedDateTime(time: ZonedDateTime) = writeZonedDateTime(this, time)
868869
fun readZonedDateTime() = readZonedDateTime(this)
@@ -1093,15 +1094,19 @@ fun <B : ByteBuf> B.writeInetSocketAddress(address: InetSocketAddress) =
10931094
SurfByteBuf.writeInetSocketAddress(this, address)
10941095

10951096
fun <B : ByteBuf> B.readCompoundTag() = SurfByteBuf.readCompoundTag(this)
1096-
fun <B : ByteBuf> B.writeCompoundTag(tag: CompoundTag) = SurfByteBuf.writeCompoundTag(this, tag)
1097+
fun <B : ByteBuf> B.writeCompoundTag(tag: CompoundBinaryTag) = SurfByteBuf.writeCompoundTag(this, tag)
10971098

10981099
fun <B : ByteBuf> B.readZonedDateTime() = SurfByteBuf.readZonedDateTime(this)
1099-
fun <B : ByteBuf> B.writeZonedDateTime(time: ZonedDateTime) = SurfByteBuf.writeZonedDateTime(this, time)
1100+
fun <B : ByteBuf> B.writeZonedDateTime(time: ZonedDateTime) =
1101+
SurfByteBuf.writeZonedDateTime(this, time)
11001102

11011103
fun <B : ByteBuf> B.readInet4Address() = SurfByteBuf.readInet4Address(this)
1102-
fun <B : ByteBuf> B.writeInet4Address(address: Inet4Address) = SurfByteBuf.writeInet4Address(this, address)
1104+
fun <B : ByteBuf> B.writeInet4Address(address: Inet4Address) =
1105+
SurfByteBuf.writeInet4Address(this, address)
1106+
1107+
fun <B : ByteBuf> B.readSingleton(classLoader: ClassLoader) =
1108+
SurfByteBuf.readSingleton(this, classLoader)
11031109

1104-
fun <B : ByteBuf> B.readSingleton(classLoader: ClassLoader) = SurfByteBuf.readSingleton(this, classLoader)
11051110
fun <B : ByteBuf> B.writeSingleton(singleton: Any) = SurfByteBuf.writeSingleton(this, singleton)
11061111

11071112
fun <B : ByteBuf> B.writeDuration(duration: Duration) = SurfByteBuf.writeDuration(this, duration)

surf-cloud-api/surf-cloud-api-common/src/main/kotlin/dev/slne/surf/cloud/api/common/util/codec/codec-util.kt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet
1717
import net.kyori.adventure.bossbar.BossBar
1818
import net.kyori.adventure.inventory.Book
1919
import net.kyori.adventure.key.Key
20+
import net.kyori.adventure.nbt.BinaryTagIO
21+
import net.kyori.adventure.nbt.CompoundBinaryTag
2022
import net.kyori.adventure.resource.ResourcePackInfo
2123
import net.kyori.adventure.resource.ResourcePackRequest
2224
import net.kyori.adventure.sound.Sound
@@ -26,12 +28,6 @@ import net.kyori.adventure.text.TextComponent
2628
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer
2729
import net.kyori.adventure.title.Title
2830
import net.kyori.adventure.title.TitlePart
29-
import net.querz.nbt.io.NBTDeserializer
30-
import net.querz.nbt.io.NBTInputStream
31-
import net.querz.nbt.io.NBTOutputStream
32-
import net.querz.nbt.io.NBTSerializer
33-
import net.querz.nbt.io.NamedTag
34-
import net.querz.nbt.tag.CompoundTag
3531
import java.io.ByteArrayInputStream
3632
import java.io.ByteArrayOutputStream
3733
import java.time.Duration
@@ -385,11 +381,16 @@ object ExtraCodecs {
385381

386382
// endregion
387383
// region nbt
388-
val COMPOUND_TAG_CODEC = streamCodec<ByteBuf, CompoundTag>({ buf, tag ->
389-
val bytes = NBTSerializer(true).toBytes(NamedTag("", tag))
390-
buf.writeByteArray(bytes)
384+
val COMPOUND_TAG_CODEC = streamCodec<ByteBuf, CompoundBinaryTag>({ buf, tag ->
385+
ByteArrayOutputStream().use {out ->
386+
BinaryTagIO.writer().write(tag, out, BinaryTagIO.Compression.GZIP)
387+
buf.writeByteArray(out.toByteArray())
388+
}
391389
}, { buf ->
392-
NBTDeserializer(true).fromBytes(buf.readByteArray()).tag as CompoundTag
390+
val bytes = buf.readByteArray()
391+
ByteArrayInputStream(bytes).use { input ->
392+
BinaryTagIO.unlimitedReader().read(input, BinaryTagIO.Compression.GZIP)
393+
}
393394
})
394395
// endregion
395396

surf-cloud-api/surf-cloud-api-common/src/main/kotlin/dev/slne/surf/cloud/api/common/util/nbt/FastNbtIo.kt

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ package dev.slne.surf.cloud.api.common.util.nbt
22

33
import it.unimi.dsi.fastutil.io.FastBufferedInputStream
44
import it.unimi.dsi.fastutil.io.FastBufferedOutputStream
5-
import net.querz.nbt.io.NBTInputStream
6-
import net.querz.nbt.io.NBTOutputStream
7-
import net.querz.nbt.tag.CompoundTag
5+
import net.kyori.adventure.nbt.BinaryTagIO
6+
import net.kyori.adventure.nbt.CompoundBinaryTag
87
import java.io.InputStream
98
import java.io.OutputStream
109
import java.nio.file.Files
@@ -22,42 +21,37 @@ object FastNbtIo {
2221
StandardOpenOption.TRUNCATE_EXISTING
2322
)
2423

25-
fun writeCompressed(nbt: CompoundTag, path: Path) {
24+
fun writeCompressed(nbt: CompoundBinaryTag, path: Path) {
2625
Files.newOutputStream(path, *outputOptions).use { output ->
2726
FastBufferedOutputStream(output).use { fastOutput ->
2827
writeCompressed(nbt, fastOutput)
2928
}
3029
}
3130
}
3231

33-
fun writeCompressed(nbt: CompoundTag, out: OutputStream) {
32+
fun writeCompressed(nbt: CompoundBinaryTag, out: OutputStream) {
3433
createCompressorStream(out).use { output ->
3534
writeCompoundTag(output, nbt)
3635
}
3736
}
3837

39-
fun readCompressed(path: Path, maxDepth: Int) = Files.newInputStream(path).use { input ->
38+
fun readCompressed(path: Path) = Files.newInputStream(path).use { input ->
4039
FastBufferedInputStream(input).use { fastInput ->
41-
readCompressed(fastInput, maxDepth)
40+
readCompressed(fastInput)
4241
}
4342
}
4443

4544

46-
fun readCompressed(stream: InputStream, maxDepth: Int) =
45+
fun readCompressed(stream: InputStream) =
4746
createDecompressorStream(stream).use { input ->
48-
readCompoundTag(input, maxDepth)
47+
readCompoundTag(input)
4948
}
5049

5150

52-
fun readCompoundTag(read: InputStream, maxDepth: Int) =
53-
NBTInputStream(read).use { input ->
54-
input.readTag(maxDepth).tag as? CompoundTag ?: error("Root tag is not a CompoundTag")
55-
}
51+
fun readCompoundTag(read: InputStream) = BinaryTagIO.unlimitedReader().read(read)
5652

57-
fun writeCompoundTag(out: OutputStream, tag: CompoundTag) {
58-
NBTOutputStream(out).use { output ->
59-
output.writeTag(tag, Int.MAX_VALUE)
60-
}
53+
fun writeCompoundTag(out: OutputStream, tag: CompoundBinaryTag) {
54+
BinaryTagIO.writer().write(tag, out)
6155
}
6256

6357
private fun createDecompressorStream(stream: InputStream) =

surf-cloud-api/surf-cloud-api-common/src/main/kotlin/dev/slne/surf/cloud/api/common/util/nbt/nbt-extensions.kt

Lines changed: 0 additions & 60 deletions
This file was deleted.
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package dev.slne.surf.cloud.core.common.data
22

3-
import net.querz.nbt.tag.LongTag
3+
import net.kyori.adventure.nbt.BinaryTagTypes
4+
import net.kyori.adventure.nbt.LongBinaryTag
45

56

67
object CloudPersistentData {
78
const val SERVER_ID_NOT_SET = -1L
89

910
var SERVER_ID by persistentData(
1011
"server_id",
11-
{ LongTag(it) },
12-
{ asLong() },
12+
BinaryTagTypes.LONG,
13+
{ value() },
14+
{ LongBinaryTag.longBinaryTag(it) },
1315
SERVER_ID_NOT_SET
1416
).nonNull()
1517
}

0 commit comments

Comments
 (0)