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

Commit 0839874

Browse files
committed
component <-> nbt serialization
1 parent 3e60406 commit 0839874

File tree

9 files changed

+206
-205
lines changed

9 files changed

+206
-205
lines changed

build.gradle.kts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ dependencies {
2929

3030
implementation("com.google.code.gson:gson:2.13.1")
3131

32-
implementation("io.github.jglrxavpok.hephaistos:common:2.6.1")
33-
implementation("io.github.jglrxavpok.hephaistos:gson:2.6.1")
32+
implementation("net.kyori:adventure-nbt:4.21.0")
3433
}
3534

3635
tasks.withType<Test> {
Lines changed: 45 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package io.github.dockyardmc.scroll
22

3-
import io.github.dockyardmc.scroll.extensions.put
43
import kotlinx.serialization.ExperimentalSerializationApi
54
import kotlinx.serialization.SerialName
65
import kotlinx.serialization.Serializable
76
import kotlinx.serialization.Transient
87
import kotlinx.serialization.json.JsonClassDiscriminator
9-
import org.jglrxavpok.hephaistos.nbt.NBT
10-
import org.jglrxavpok.hephaistos.nbt.NBTCompound
8+
import net.kyori.adventure.nbt.CompoundBinaryTag
119

1210
@Suppress("MemberVisibilityCanBePrivate")
1311
@OptIn(ExperimentalSerializationApi::class)
@@ -19,136 +17,128 @@ sealed class ClickEvent {
1917
* @throws MissingFieldException if there's a field missing
2018
* @throws UnsupportedOperationException if `action` is not supported
2119
*/
22-
fun fromNbt(nbt: NBTCompound): ClickEvent {
20+
fun fromNbt(nbt: CompoundBinaryTag): ClickEvent {
21+
// Implementation would need to be updated for the adventure-nbt library
22+
// This is a placeholder for the conversion logic
2323
return when (val action = nbt.getString("action")) {
24-
"open_url" -> OpenUrl(nbt.getString("url") ?: throw MissingFieldException("url"))
25-
"open_file" -> OpenFile(nbt.getString("path") ?: throw MissingFieldException("path"))
26-
"run_command" -> RunCommand(nbt.getString("command") ?: throw MissingFieldException("command"))
27-
"suggest_command" -> SuggestCommand(nbt.getString("command") ?: throw MissingFieldException("command"))
28-
"change_page" -> ChangePage(nbt.getInt("page") ?: throw MissingFieldException("page"))
29-
"copy_to_clipboard" -> CopyToClipboard(nbt.getString("value") ?: throw MissingFieldException("value"))
30-
"show_dialog" -> ShowDialog(nbt.getString("dialog") ?: throw MissingFieldException("dialog"))
31-
"custom" -> Custom(
32-
nbt.getString("id") ?: throw MissingFieldException("id"),
33-
nbt.getString("payload") ?: throw MissingFieldException("payload")
34-
)
35-
36-
null -> throw MissingFieldException("action")
37-
else -> throw UnsupportedOperationException("unknown `action`: `$action`")
24+
"open_url" -> OpenUrl(nbt.getString("url"))
25+
"open_file" -> OpenFile(nbt.getString("path"))
26+
"run_command" -> RunCommand(nbt.getString("command"))
27+
"suggest_command" -> SuggestCommand(nbt.getString("command"))
28+
"change_page" -> ChangePage(nbt.getInt("page"))
29+
"copy_to_clipboard" -> CopyToClipboard(nbt.getString("value"))
30+
"show_dialog" -> ShowDialog(nbt.getString("dialog"))
31+
"custom" -> Custom(nbt.getString("id"), nbt.getString("payload"))
32+
else -> throw UnsupportedOperationException("ClickEvent action '$action' is not supported")
3833
}
3934
}
35+
4036
}
4137

4238
abstract val action: String
4339

44-
open fun getNbt(): NBTCompound {
45-
return NBT.Compound { builder ->
46-
builder.put("action", action)
47-
}
40+
open fun getNbt(): CompoundBinaryTag {
41+
return CompoundBinaryTag.builder()
42+
.putString("action", action)
43+
.build()
4844
}
4945

46+
5047
@Serializable
5148
@SerialName("open_url")
5249
class OpenUrl(val url: String) : ClickEvent() {
5350
@Transient
5451
override val action: String = "open_url"
5552

56-
override fun getNbt(): NBTCompound {
57-
return super.getNbt().kmodify {
58-
put("url", url)
59-
}
53+
override fun getNbt(): CompoundBinaryTag {
54+
return super.getNbt().putString("url", url)
6055
}
6156
}
6257

58+
6359
@Serializable
6460
@SerialName("open_file")
6561
class OpenFile(val path: String) : ClickEvent() {
6662
@Transient
6763
override val action: String = "open_file"
6864

69-
override fun getNbt(): NBTCompound {
70-
return super.getNbt().kmodify {
71-
put("path", path)
72-
}
65+
override fun getNbt(): CompoundBinaryTag {
66+
return super.getNbt().putString("path", path)
7367
}
7468
}
7569

70+
7671
@Serializable
7772
@SerialName("run_command")
7873
class RunCommand(val command: String) : ClickEvent() {
7974
@Transient
8075
override val action: String = "run_command"
8176

82-
override fun getNbt(): NBTCompound {
83-
return super.getNbt().kmodify {
84-
put("command", command)
85-
}
77+
override fun getNbt(): CompoundBinaryTag {
78+
return super.getNbt().putString("command", command)
8679
}
8780
}
8881

82+
8983
@Serializable
9084
@SerialName("suggest_command")
9185
class SuggestCommand(val command: String) : ClickEvent() {
9286
@Transient
9387
override val action: String = "suggest_command"
9488

95-
override fun getNbt(): NBTCompound {
96-
return super.getNbt().kmodify {
97-
put("command", command)
98-
}
89+
override fun getNbt(): CompoundBinaryTag {
90+
return super.getNbt().putString("command", command)
9991
}
10092
}
10193

94+
10295
@Serializable
10396
@SerialName("change_page")
10497
class ChangePage(val page: Int) : ClickEvent() {
10598
@Transient
10699
override val action: String = "change_page"
107100

108-
override fun getNbt(): NBTCompound {
109-
return super.getNbt().kmodify {
110-
put("page", page)
111-
}
101+
override fun getNbt(): CompoundBinaryTag {
102+
return super.getNbt().putInt("page", page)
112103
}
113104
}
114105

106+
115107
@Serializable
116108
@SerialName("copy_to_clipboard")
117109
class CopyToClipboard(val value: String) : ClickEvent() {
118110
@Transient
119111
override val action: String = "copy_to_clipboard"
120112

121-
override fun getNbt(): NBTCompound {
122-
return super.getNbt().kmodify {
123-
put("value", value)
124-
}
113+
override fun getNbt(): CompoundBinaryTag {
114+
return super.getNbt().putString("value", value)
125115
}
126116
}
127117

118+
128119
@Serializable
129120
@SerialName("show_dialog")
130121
class ShowDialog(val dialog: String) : ClickEvent() {
131122
@Transient
132123
override val action: String = "show_dialog"
133124

134-
override fun getNbt(): NBTCompound {
135-
return super.getNbt().kmodify {
136-
put("dialog", dialog)
137-
}
125+
override fun getNbt(): CompoundBinaryTag {
126+
return super.getNbt().putString("dialog", dialog)
138127
}
139128
}
140129

130+
141131
@Serializable
142132
@SerialName("custom")
143133
class Custom(val id: String, val payload: String) : ClickEvent() {
144134
@Transient
145135
override val action: String = "custom"
146136

147-
override fun getNbt(): NBTCompound {
148-
return super.getNbt().kmodify {
149-
put("id", id)
150-
put("payload", payload)
151-
}
137+
override fun getNbt(): CompoundBinaryTag {
138+
return super.getNbt()
139+
.putString("id", id)
140+
.putString("payload", payload)
152141
}
153142
}
143+
154144
}

src/main/kotlin/io/github/dockyardmc/scroll/Component.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package io.github.dockyardmc.scroll
22

33
import io.github.dockyardmc.scroll.serializers.ComponentToJsonSerializer
4-
import io.github.dockyardmc.scroll.serializers.ComponentToNbtSerializer
4+
import io.github.dockyardmc.scroll.serializers.ComponentSerializer
55
import kotlinx.serialization.SerialName
66
import kotlinx.serialization.Serializable
7-
import org.jglrxavpok.hephaistos.nbt.NBTCompound
7+
import net.kyori.adventure.nbt.CompoundBinaryTag
88

99
@Serializable
1010
open class Component(
@@ -25,7 +25,7 @@ open class Component(
2525
@SerialName("hover_event")
2626
var hoverEvent: HoverEvent? = null,
2727
@SerialName("click_event")
28-
var clickEvent: ClickEvent? = null
28+
var clickEvent: ClickEvent? = null,
2929
) {
3030
companion object {
3131
fun compound(components: MutableList<Component>): Component {
@@ -40,8 +40,8 @@ open class Component(
4040
return this.stripStyling()
4141
}
4242

43-
fun toNBT(): NBTCompound {
44-
return ComponentToNbtSerializer.serializeComponent(this)
43+
fun toNBT(): CompoundBinaryTag {
44+
return ComponentSerializer.componentToNbt(this)
4545
}
4646

4747
fun toJson(): String {
@@ -71,14 +71,14 @@ open class Component(
7171

7272
fun resetFormatting(includingFont: Boolean, ignoreShadow: Boolean = false) {
7373
this.color = null
74-
if(!ignoreShadow) this.shadowColor = null
74+
if (!ignoreShadow) this.shadowColor = null
7575
this.strikethrough = null
7676
this.underlined = null
7777
this.font = null
7878
this.italic = null
7979
this.bold = null
8080

81-
if(includingFont) {
81+
if (includingFont) {
8282
this.font = null
8383
this.hoverEvent = null
8484
this.obfuscated = null
Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package io.github.dockyardmc.scroll
22

3-
import io.github.dockyardmc.scroll.extensions.put
43
import io.github.dockyardmc.scroll.extensions.toComponent
54
import kotlinx.serialization.ExperimentalSerializationApi
65
import kotlinx.serialization.SerialName
76
import kotlinx.serialization.Serializable
87
import kotlinx.serialization.Transient
98
import kotlinx.serialization.json.JsonClassDiscriminator
10-
import org.jglrxavpok.hephaistos.nbt.NBT
11-
import org.jglrxavpok.hephaistos.nbt.NBTCompound
9+
import net.kyori.adventure.nbt.CompoundBinaryTag
1210

1311
@Suppress("MemberVisibilityCanBePrivate")
1412
@OptIn(ExperimentalSerializationApi::class)
@@ -17,34 +15,41 @@ import org.jglrxavpok.hephaistos.nbt.NBTCompound
1715
sealed class HoverEvent {
1816
abstract val type: String
1917

20-
open fun getNbt(): NBTCompound {
21-
return NBT.Compound { builder ->
22-
builder.put("action", type)
23-
}
18+
open fun getNbt(): CompoundBinaryTag {
19+
return CompoundBinaryTag.builder()
20+
.putString("action", type)
21+
.build()
2422
}
2523

2624
companion object {
2725
/**
2826
* @throws MissingFieldException if there's a field missing
2927
* @throws UnsupportedOperationException if `action` is not supported
3028
*/
31-
fun fromNbt(nbt: NBTCompound): HoverEvent {
29+
fun fromNbt(nbt: CompoundBinaryTag): HoverEvent {
3230
return when (val action = nbt.getString("action")) {
33-
"show_text" -> ShowText(nbt.getCompound("value")?.toComponent() ?: throw MissingFieldException("value"))
34-
"show_item" -> ShowItem(
35-
nbt.getString("id") ?: throw MissingFieldException("id"),
36-
nbt.getInt("count") ?: throw MissingFieldException("count")
37-
)
38-
"show_entity" -> ShowEntity(
39-
nbt.getString("id") ?: throw MissingFieldException("id"),
40-
nbt.getString("uuid") ?: throw MissingFieldException("uuid"),
41-
nbt.getCompound("name")?.toComponent()
42-
)
43-
44-
null -> throw MissingFieldException("action")
45-
else -> throw UnsupportedOperationException("unknown `action`: `$action`")
31+
"show_text" -> {
32+
val valueTag = nbt.getCompound("value")
33+
ShowText(valueTag.toComponent())
34+
}
35+
36+
"show_item" -> {
37+
val id = nbt.getString("id")
38+
val count = nbt.getInt("count")
39+
ShowItem(id, count)
40+
}
41+
42+
"show_entity" -> {
43+
val id = nbt.getString("id")
44+
val uuid = nbt.getString("uuid")
45+
val nameTag = nbt.getCompound("name")
46+
ShowEntity(id, uuid, nameTag.toComponent())
47+
}
48+
49+
else -> throw UnsupportedOperationException("HoverEvent action '$action' is not supported")
4650
}
4751
}
52+
4853
}
4954

5055
@Serializable
@@ -55,25 +60,22 @@ sealed class HoverEvent {
5560
@Transient
5661
override val type: String = "show_text"
5762

58-
override fun getNbt(): NBTCompound {
59-
return super.getNbt().kmodify {
60-
put("value", value.toNBT())
61-
}
63+
override fun getNbt(): CompoundBinaryTag {
64+
return super.getNbt().put("value", value.toNBT())
6265
}
6366
}
6467

6568
// TODO: add components (recursive dependency? :( )
69+
// will this ever be useful? You can make your own in way simpler way
70+
// I think we can mark this unsupported -maya
6671
@Serializable
6772
@SerialName("show_item")
6873
class ShowItem(val id: String, val count: Int) : HoverEvent() {
6974
@Transient
7075
override val type: String = "show_item"
7176

72-
override fun getNbt(): NBTCompound {
73-
return super.getNbt().kmodify {
74-
put("id", id)
75-
put("count", count)
76-
}
77+
init {
78+
throw UnsupportedOperationException("ShowItem is not supported")
7779
}
7880
}
7981

@@ -82,13 +84,13 @@ sealed class HoverEvent {
8284
@Transient
8385
override val type: String = "show_entity"
8486

85-
override fun getNbt(): NBTCompound {
86-
return super.getNbt().kmodify {
87-
put("id", id)
88-
put("uuid", uuid)
89-
if(name != null)
90-
put("name", name.toNBT())
91-
}
87+
override fun getNbt(): CompoundBinaryTag {
88+
val nbt = super.getNbt()
89+
nbt.putString("id", id)
90+
nbt.putString("uuid", uuid)
91+
if (name != null) nbt.put("name", name.toNBT())
92+
93+
return nbt
9294
}
9395
}
9496
}

src/main/kotlin/io/github/dockyardmc/scroll/extensions/ExtendedNBTCompound.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package io.github.dockyardmc.scroll.extensions
22

33
import io.github.dockyardmc.scroll.Component
44
import io.github.dockyardmc.scroll.serializers.NbtToComponentSerializer
5-
import org.jglrxavpok.hephaistos.nbt.NBTCompound
5+
import net.kyori.adventure.nbt.CompoundBinaryTag
66

7-
fun NBTCompound.toComponent(): Component {
7+
fun CompoundBinaryTag.toComponent(): Component {
88
return NbtToComponentSerializer.serializeNbt(this)
99
}

0 commit comments

Comments
 (0)