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

Commit 9d48197

Browse files
committed
update hover event
1 parent 5b7559d commit 9d48197

File tree

10 files changed

+110
-40
lines changed

10 files changed

+110
-40
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,4 @@ sealed class ClickEvent {
151151
}
152152
}
153153
}
154-
}
155-
156-
class MissingFieldException(field: String) :
157-
RuntimeException("failed to parse: field `$field` is missing")
154+
}

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

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,94 @@
11
package io.github.dockyardmc.scroll
22

3+
import io.github.dockyardmc.scroll.extensions.put
4+
import io.github.dockyardmc.scroll.extensions.toComponent
5+
import kotlinx.serialization.ExperimentalSerializationApi
6+
import kotlinx.serialization.SerialName
37
import kotlinx.serialization.Serializable
8+
import kotlinx.serialization.Transient
9+
import kotlinx.serialization.json.JsonClassDiscriminator
10+
import org.jglrxavpok.hephaistos.nbt.NBT
11+
import org.jglrxavpok.hephaistos.nbt.NBTCompound
412

13+
@Suppress("MemberVisibilityCanBePrivate")
14+
@OptIn(ExperimentalSerializationApi::class)
515
@Serializable
6-
class HoverEvent(
7-
val action: HoverAction,
8-
val contents: Component? = null
9-
)
16+
@JsonClassDiscriminator("action")
17+
sealed class HoverEvent {
18+
abstract val type: String
19+
20+
open fun getNbt(): NBTCompound {
21+
return NBT.Compound { builder ->
22+
builder.put("action", type)
23+
}
24+
}
25+
26+
companion object {
27+
/**
28+
* @throws MissingFieldException if there's a field missing
29+
* @throws UnsupportedOperationException if `action` is not supported
30+
*/
31+
fun fromNbt(nbt: NBTCompound): HoverEvent {
32+
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() ?: throw MissingFieldException("name")
42+
)
43+
44+
null -> throw MissingFieldException("action")
45+
else -> throw UnsupportedOperationException("unknown `action`: `$action`")
46+
}
47+
}
48+
}
49+
50+
@Serializable
51+
@SerialName("show_text")
52+
class ShowText(val value: Component) : HoverEvent() {
53+
constructor(value: String) : this(value.toComponent())
54+
55+
@Transient
56+
override val type: String = "show_text"
57+
58+
override fun getNbt(): NBTCompound {
59+
return super.getNbt().kmodify {
60+
put("value", value.toNBT())
61+
}
62+
}
63+
}
64+
65+
// TODO: add components (recursive dependency? :( )
66+
@Serializable
67+
@SerialName("show_item")
68+
class ShowItem(val id: String, val count: Int) : HoverEvent() {
69+
@Transient
70+
override val type: String = "show_item"
71+
72+
override fun getNbt(): NBTCompound {
73+
return super.getNbt().kmodify {
74+
put("id", id)
75+
put("count", count)
76+
}
77+
}
78+
}
79+
80+
@SerialName("show_entity")
81+
class ShowEntity(val id: String, val uuid: String, val name: Component? = null) : HoverEvent() {
82+
@Transient
83+
override val type: String = "show_entity"
84+
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+
}
92+
}
93+
}
94+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package io.github.dockyardmc.scroll
2+
3+
class MissingFieldException(field: String) :
4+
RuntimeException("failed to parse: field `$field` is missing")
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
package io.github.dockyardmc.scroll.providers.default
22

33
import io.github.dockyardmc.scroll.Component
4-
import io.github.dockyardmc.scroll.HoverAction
54
import io.github.dockyardmc.scroll.HoverEvent
6-
import io.github.dockyardmc.scroll.Scroll
5+
import io.github.dockyardmc.scroll.extensions.toComponent
76
import io.github.dockyardmc.scroll.providers.ClosingNamedFormatProvider
87
import io.github.dockyardmc.scroll.providers.FormatProviderContext
98

109
class HoverEventProvider: ClosingNamedFormatProvider("hover", listOf()) {
1110

1211
override fun formatNormal(context: FormatProviderContext, component: Component) {
13-
val action = HoverAction.valueOf(context.getArgument(0).uppercase())
14-
val value = Scroll.parse(context.getArgument(1))
15-
val hoverEvent = HoverEvent(action, value)
12+
component.hoverEvent = when (context.getArgument(0)) {
13+
"show_text" -> HoverEvent.ShowText(context.getArgument(1).toComponent())
14+
"show_item" -> HoverEvent.ShowItem(context.getArgument(1), context.getArgumentOrNull(2)?.toIntOrNull() ?: 1)
15+
"show_entity" -> HoverEvent.ShowEntity(context.getArgument(1), context.getArgument(2), context.getArgumentOrNull(3)?.toComponent())
1616

17-
component.hoverEvent = hoverEvent
17+
else -> return
18+
}
1819
}
1920

2021
override fun formatClosing(context: FormatProviderContext, component: Component) {
2122
component.hoverEvent = null
2223
}
23-
}
24+
}

src/main/kotlin/io/github/dockyardmc/scroll/serializers/ComponentToNbtSerializer.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ object ComponentToNbtSerializer {
3131

3232
val hover = c.hoverEvent
3333
if(hover != null) {
34-
nbtWriter.put("hover_event", NBT.Compound { hoverWriter ->
35-
hoverWriter.put("action", hover.action.name.lowercase())
36-
hoverWriter.put("contents", serializeComponent(hover.contents!!))
37-
})
34+
nbtWriter.put("hover_event", hover.getNbt())
3835
}
3936

4037
val click = c.clickEvent

src/main/kotlin/io/github/dockyardmc/scroll/serializers/KyoriToScrollSerializer.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ object KyoriToScrollSerializer {
4949
}
5050

5151
private fun net.kyori.adventure.text.event.HoverEvent<*>.toScroll(): HoverEvent? {
52-
val value = this.value()
53-
return when(this.value()) {
54-
is net.kyori.adventure.text.Component -> HoverEvent(HoverAction.SHOW_TEXT, serializeComponent(value as net.kyori.adventure.text.Component))
52+
return when(val value = this.value()) {
53+
is net.kyori.adventure.text.Component -> HoverEvent.ShowText(value.toScroll())
54+
is net.kyori.adventure.text.event.HoverEvent.ShowItem -> HoverEvent.ShowItem(value.item().asString(), value.count())
55+
is net.kyori.adventure.text.event.HoverEvent.ShowEntity -> HoverEvent.ShowEntity(value.type().asString(), value.id().toString(), value.name()?.toScroll())
5556
else -> null
5657
}
5758
}

src/main/kotlin/io/github/dockyardmc/scroll/serializers/NbtToComponentSerializer.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ object NbtToComponentSerializer {
2727

2828
val hover = nbt.getCompound("hover_event")
2929
if (hover != null) {
30-
val action = hover.getString("action")!!
31-
val content = hover.getCompound("contents")!!
32-
33-
component.hoverEvent = HoverEvent(HoverAction.valueOf(action.uppercase()), serializeNbt(content))
30+
component.hoverEvent = HoverEvent.fromNbt(hover)
3431
}
3532

3633
val click = nbt.getCompound("click_event")

src/test/kotlin/NbtSerializationTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class NbtSerializationTests {
1515
val nbt = NBT.Compound {
1616
it.put("hover_event", NBT.Compound { hover ->
1717
hover.put("action", "show_text")
18-
hover.put("contents", NBT.Compound { hoverIn ->
18+
hover.put("value", NBT.Compound { hoverIn ->
1919
hoverIn.put("extra", NBT.Compound {
2020
it.put("extra", NBT.Compound { extrahover ->
2121
extrahover.put("text", "Click to open the store URL")

src/test/kotlin/StringSerializationTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class StringSerializationTests {
141141
fun testHover() {
142142
val input = "<hover:show_text:'<red><bold><i>bucket o' fish'>hover over meeeee <r>do not hover over me"
143143
val expected = Component.compound(mutableListOf(
144-
Component(text = "hover over meeeee ", hoverEvent = HoverEvent(HoverAction.SHOW_TEXT, "<red><bold><i>bucket o' fish".toComponent())),
144+
Component(text = "hover over meeeee ", hoverEvent = HoverEvent.ShowText("<red><bold><i>bucket o' fish".toComponent())),
145145
Component(text = "do not hover over me")
146146
))
147147

0 commit comments

Comments
 (0)