diff --git a/README.md b/README.md index d59e2ff..a9094e2 100644 --- a/README.md +++ b/README.md @@ -113,18 +113,28 @@ The following tags are valid: - `` - `` - Events - - `` for hover-able text. Formatting applies to inner text as well + - `` for hover-able text. Formatting applies to inner text as well. Actions: + - `show_text`: 1 text argument + - `show_item`: item id and optional item count + - `show_entity`: entity type id, entity uuid, and optional name (text component) + - *you can skip type, in this case `show_text` will be used as default.* Like this: `Hover!` - `` for clickable text. Actions are following - `open_url` - following text needs to start with "https://" - `run_command` - following text needs to start with "/" - `suggest_command` - following text needs to start with "/" - `copy_to_clipboard` + - `show_dialog` + - `custom` - send custom click action. two arguments: `id` and `payload` - Other - `` to change font - - `` to make the text after rainbow. (Resets when another color is applied or when is reached) - `` - Color Interpolation, step is float between 0 and 1 - `` to reset formatting +Some tags (like ``) have multiple arguments. Those are separated by `:`. +Like this: +- `Click for custom click!!!` +- `Hover to see a book (not really)` + In some cases (format and reset) you can use shortened versions - `` is short of `` - `` is short of `` diff --git a/build.gradle.kts b/build.gradle.kts index 86e5700..04dc91c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,8 +5,8 @@ import java.net.http.HttpResponse plugins { `maven-publish` - kotlin("jvm") version "1.9.22" - kotlin("plugin.serialization") version "1.9.23" + kotlin("jvm") version "2.1.21" + kotlin("plugin.serialization") version "2.1.21" application } @@ -19,14 +19,18 @@ repositories { dependencies { testImplementation(kotlin("test")) - testImplementation("net.kyori:adventure-api:4.18.0") - testImplementation("net.kyori:adventure-text-minimessage:4.18.0") - implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.3.0") - implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.0") - implementation("com.google.code.gson:gson:2.10.1") - implementation("io.github.jglrxavpok.hephaistos:common:2.2.0") - implementation("io.github.jglrxavpok.hephaistos:gson:2.2.0") - compileOnly("net.kyori:adventure-api:4.18.0") + + testImplementation("net.kyori:adventure-api:4.21.0") + testImplementation("net.kyori:adventure-text-minimessage:4.21.0") + compileOnly("net.kyori:adventure-api:4.21.0") + + implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.8.1") + implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.1") + + implementation("com.google.code.gson:gson:2.13.1") + + implementation("io.github.jglrxavpok.hephaistos:common:2.6.1") + implementation("io.github.jglrxavpok.hephaistos:gson:2.6.1") } tasks.withType { @@ -142,4 +146,4 @@ fun embed(): String { "attachments": [] } """.trimIndent() -} \ No newline at end of file +} diff --git a/src/main/kotlin/io/github/dockyardmc/scroll/ClickAction.kt b/src/main/kotlin/io/github/dockyardmc/scroll/ClickAction.kt deleted file mode 100644 index da7a371..0000000 --- a/src/main/kotlin/io/github/dockyardmc/scroll/ClickAction.kt +++ /dev/null @@ -1,20 +0,0 @@ -package io.github.dockyardmc.scroll - -import kotlinx.serialization.SerialName -import kotlinx.serialization.Serializable - -@Serializable -enum class ClickAction { - @SerialName("open_url") - OPEN_URL, - @SerialName("open_file") - OPEN_FILE, - @SerialName("run_command") - RUN_COMMAND, - @SerialName("suggest_command") - SUGGEST_COMMAND, - @SerialName("change_page") - CHANGE_PAGE, - @SerialName("copy_to_clipboard") - COPY_TO_CLIPBOARD -} \ No newline at end of file diff --git a/src/main/kotlin/io/github/dockyardmc/scroll/ClickEvent.kt b/src/main/kotlin/io/github/dockyardmc/scroll/ClickEvent.kt index 6468318..0fb6f54 100644 --- a/src/main/kotlin/io/github/dockyardmc/scroll/ClickEvent.kt +++ b/src/main/kotlin/io/github/dockyardmc/scroll/ClickEvent.kt @@ -1,9 +1,154 @@ package io.github.dockyardmc.scroll +import io.github.dockyardmc.scroll.extensions.put +import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable +import kotlinx.serialization.Transient +import kotlinx.serialization.json.JsonClassDiscriminator +import org.jglrxavpok.hephaistos.nbt.NBT +import org.jglrxavpok.hephaistos.nbt.NBTCompound +@Suppress("MemberVisibilityCanBePrivate") +@OptIn(ExperimentalSerializationApi::class) @Serializable -class ClickEvent( - val action: ClickAction, - val value: String? = null -) \ No newline at end of file +@JsonClassDiscriminator("action") +sealed class ClickEvent { + companion object { + /** + * @throws MissingFieldException if there's a field missing + * @throws UnsupportedOperationException if `action` is not supported + */ + fun fromNbt(nbt: NBTCompound): ClickEvent { + return when (val action = nbt.getString("action")) { + "open_url" -> OpenUrl(nbt.getString("url") ?: throw MissingFieldException("url")) + "open_file" -> OpenFile(nbt.getString("path") ?: throw MissingFieldException("path")) + "run_command" -> RunCommand(nbt.getString("command") ?: throw MissingFieldException("command")) + "suggest_command" -> SuggestCommand(nbt.getString("command") ?: throw MissingFieldException("command")) + "change_page" -> ChangePage(nbt.getInt("page") ?: throw MissingFieldException("page")) + "copy_to_clipboard" -> CopyToClipboard(nbt.getString("value") ?: throw MissingFieldException("value")) + "show_dialog" -> ShowDialog(nbt.getString("dialog") ?: throw MissingFieldException("dialog")) + "custom" -> Custom( + nbt.getString("id") ?: throw MissingFieldException("id"), + nbt.getString("payload") ?: throw MissingFieldException("payload") + ) + + null -> throw MissingFieldException("action") + else -> throw UnsupportedOperationException("unknown `action`: `$action`") + } + } + } + + abstract val action: String + + open fun getNbt(): NBTCompound { + return NBT.Compound { builder -> + builder.put("action", action) + } + } + + @Serializable + @SerialName("open_url") + class OpenUrl(val url: String) : ClickEvent() { + @Transient + override val action: String = "open_url" + + override fun getNbt(): NBTCompound { + return super.getNbt().kmodify { + put("url", url) + } + } + } + + @Serializable + @SerialName("open_file") + class OpenFile(val path: String) : ClickEvent() { + @Transient + override val action: String = "open_file" + + override fun getNbt(): NBTCompound { + return super.getNbt().kmodify { + put("path", path) + } + } + } + + @Serializable + @SerialName("run_command") + class RunCommand(val command: String) : ClickEvent() { + @Transient + override val action: String = "run_command" + + override fun getNbt(): NBTCompound { + return super.getNbt().kmodify { + put("command", command) + } + } + } + + @Serializable + @SerialName("suggest_command") + class SuggestCommand(val command: String) : ClickEvent() { + @Transient + override val action: String = "suggest_command" + + override fun getNbt(): NBTCompound { + return super.getNbt().kmodify { + put("command", command) + } + } + } + + @Serializable + @SerialName("change_page") + class ChangePage(val page: Int) : ClickEvent() { + @Transient + override val action: String = "change_page" + + override fun getNbt(): NBTCompound { + return super.getNbt().kmodify { + put("page", page) + } + } + } + + @Serializable + @SerialName("copy_to_clipboard") + class CopyToClipboard(val value: String) : ClickEvent() { + @Transient + override val action: String = "copy_to_clipboard" + + override fun getNbt(): NBTCompound { + return super.getNbt().kmodify { + put("value", value) + } + } + } + + @Serializable + @SerialName("show_dialog") + class ShowDialog(val dialog: String) : ClickEvent() { + @Transient + override val action: String = "show_dialog" + + override fun getNbt(): NBTCompound { + return super.getNbt().kmodify { + put("dialog", dialog) + } + } + } + + @Serializable + @SerialName("custom") + class Custom(val id: String, val payload: String) : ClickEvent() { + @Transient + override val action: String = "custom" + + override fun getNbt(): NBTCompound { + return super.getNbt().kmodify { + put("id", id) + put("payload", payload) + } + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/io/github/dockyardmc/scroll/Component.kt b/src/main/kotlin/io/github/dockyardmc/scroll/Component.kt index 04796bc..7d43b82 100644 --- a/src/main/kotlin/io/github/dockyardmc/scroll/Component.kt +++ b/src/main/kotlin/io/github/dockyardmc/scroll/Component.kt @@ -22,7 +22,9 @@ open class Component( var obfuscated: Boolean? = null, var font: String? = null, var insertion: String? = null, + @SerialName("hover_event") var hoverEvent: HoverEvent? = null, + @SerialName("click_event") var clickEvent: ClickEvent? = null ) { companion object { diff --git a/src/main/kotlin/io/github/dockyardmc/scroll/HoverAction.kt b/src/main/kotlin/io/github/dockyardmc/scroll/HoverAction.kt deleted file mode 100644 index a840d3d..0000000 --- a/src/main/kotlin/io/github/dockyardmc/scroll/HoverAction.kt +++ /dev/null @@ -1,12 +0,0 @@ -package io.github.dockyardmc.scroll - -import kotlinx.serialization.SerialName -import kotlinx.serialization.Serializable - -@Serializable -enum class HoverAction { - @SerialName("show_text") - SHOW_TEXT, - @SerialName("show_item") - SHOW_ITEM -} \ No newline at end of file diff --git a/src/main/kotlin/io/github/dockyardmc/scroll/HoverEvent.kt b/src/main/kotlin/io/github/dockyardmc/scroll/HoverEvent.kt index 1e1ba25..3d3d78c 100644 --- a/src/main/kotlin/io/github/dockyardmc/scroll/HoverEvent.kt +++ b/src/main/kotlin/io/github/dockyardmc/scroll/HoverEvent.kt @@ -1,9 +1,94 @@ package io.github.dockyardmc.scroll +import io.github.dockyardmc.scroll.extensions.put +import io.github.dockyardmc.scroll.extensions.toComponent +import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable +import kotlinx.serialization.Transient +import kotlinx.serialization.json.JsonClassDiscriminator +import org.jglrxavpok.hephaistos.nbt.NBT +import org.jglrxavpok.hephaistos.nbt.NBTCompound +@Suppress("MemberVisibilityCanBePrivate") +@OptIn(ExperimentalSerializationApi::class) @Serializable -class HoverEvent( - val action: HoverAction, - val contents: Component? = null -) \ No newline at end of file +@JsonClassDiscriminator("action") +sealed class HoverEvent { + abstract val type: String + + open fun getNbt(): NBTCompound { + return NBT.Compound { builder -> + builder.put("action", type) + } + } + + companion object { + /** + * @throws MissingFieldException if there's a field missing + * @throws UnsupportedOperationException if `action` is not supported + */ + fun fromNbt(nbt: NBTCompound): HoverEvent { + return when (val action = nbt.getString("action")) { + "show_text" -> ShowText(nbt.getCompound("value")?.toComponent() ?: throw MissingFieldException("value")) + "show_item" -> ShowItem( + nbt.getString("id") ?: throw MissingFieldException("id"), + nbt.getInt("count") ?: throw MissingFieldException("count") + ) + "show_entity" -> ShowEntity( + nbt.getString("id") ?: throw MissingFieldException("id"), + nbt.getString("uuid") ?: throw MissingFieldException("uuid"), + nbt.getCompound("name")?.toComponent() + ) + + null -> throw MissingFieldException("action") + else -> throw UnsupportedOperationException("unknown `action`: `$action`") + } + } + } + + @Serializable + @SerialName("show_text") + class ShowText(val value: Component) : HoverEvent() { + constructor(value: String) : this(value.toComponent()) + + @Transient + override val type: String = "show_text" + + override fun getNbt(): NBTCompound { + return super.getNbt().kmodify { + put("value", value.toNBT()) + } + } + } + + // TODO: add components (recursive dependency? :( ) + @Serializable + @SerialName("show_item") + class ShowItem(val id: String, val count: Int) : HoverEvent() { + @Transient + override val type: String = "show_item" + + override fun getNbt(): NBTCompound { + return super.getNbt().kmodify { + put("id", id) + put("count", count) + } + } + } + + @SerialName("show_entity") + class ShowEntity(val id: String, val uuid: String, val name: Component? = null) : HoverEvent() { + @Transient + override val type: String = "show_entity" + + override fun getNbt(): NBTCompound { + return super.getNbt().kmodify { + put("id", id) + put("uuid", uuid) + if(name != null) + put("name", name.toNBT()) + } + } + } +} diff --git a/src/main/kotlin/io/github/dockyardmc/scroll/MissingFieldException.kt b/src/main/kotlin/io/github/dockyardmc/scroll/MissingFieldException.kt new file mode 100644 index 0000000..7f0c461 --- /dev/null +++ b/src/main/kotlin/io/github/dockyardmc/scroll/MissingFieldException.kt @@ -0,0 +1,4 @@ +package io.github.dockyardmc.scroll + +class MissingFieldException(field: String) : + RuntimeException("failed to parse: field `$field` is missing") \ No newline at end of file diff --git a/src/main/kotlin/io/github/dockyardmc/scroll/providers/default/ClickEventProvider.kt b/src/main/kotlin/io/github/dockyardmc/scroll/providers/default/ClickEventProvider.kt index 69ba857..642f701 100644 --- a/src/main/kotlin/io/github/dockyardmc/scroll/providers/default/ClickEventProvider.kt +++ b/src/main/kotlin/io/github/dockyardmc/scroll/providers/default/ClickEventProvider.kt @@ -1,6 +1,5 @@ package io.github.dockyardmc.scroll.providers.default -import io.github.dockyardmc.scroll.ClickAction import io.github.dockyardmc.scroll.ClickEvent import io.github.dockyardmc.scroll.Component import io.github.dockyardmc.scroll.providers.ClosingNamedFormatProvider @@ -9,14 +8,24 @@ import io.github.dockyardmc.scroll.providers.FormatProviderContext class ClickEventProvider : ClosingNamedFormatProvider("click", listOf()) { override fun formatNormal(context: FormatProviderContext, component: Component) { - val action = ClickAction.valueOf(context.getArgument(0).uppercase()) - val value = context.getArgument(1) - val clickEvent = ClickEvent(action, value) + component.clickEvent = when (context.getArgument(0)) { + "open_url" -> ClickEvent.OpenUrl(context.getArgument(1)) + "open_file" -> ClickEvent.OpenFile(context.getArgument(1)) + "run_command" -> ClickEvent.RunCommand(context.getArgument(1)) + "suggest_command" -> ClickEvent.SuggestCommand(context.getArgument(1)) + "change_page" -> ClickEvent.ChangePage(context.getArgument(1).toInt()) + "copy_to_clipboard" -> ClickEvent.CopyToClipboard(context.getArgument(1)) + "show_dialog" -> ClickEvent.ShowDialog(context.getArgument(1)) + "custom" -> ClickEvent.Custom( + context.getArgument(1), + context.getArgument(2) + ) - component.clickEvent = clickEvent + else -> return + } } override fun formatClosing(context: FormatProviderContext, component: Component) { component.clickEvent = null } -} \ No newline at end of file +} diff --git a/src/main/kotlin/io/github/dockyardmc/scroll/providers/default/HoverEventProvider.kt b/src/main/kotlin/io/github/dockyardmc/scroll/providers/default/HoverEventProvider.kt index 3a9ea3f..f5f88e5 100644 --- a/src/main/kotlin/io/github/dockyardmc/scroll/providers/default/HoverEventProvider.kt +++ b/src/main/kotlin/io/github/dockyardmc/scroll/providers/default/HoverEventProvider.kt @@ -1,23 +1,24 @@ package io.github.dockyardmc.scroll.providers.default import io.github.dockyardmc.scroll.Component -import io.github.dockyardmc.scroll.HoverAction import io.github.dockyardmc.scroll.HoverEvent -import io.github.dockyardmc.scroll.Scroll +import io.github.dockyardmc.scroll.extensions.toComponent import io.github.dockyardmc.scroll.providers.ClosingNamedFormatProvider import io.github.dockyardmc.scroll.providers.FormatProviderContext class HoverEventProvider: ClosingNamedFormatProvider("hover", listOf()) { override fun formatNormal(context: FormatProviderContext, component: Component) { - val action = HoverAction.valueOf(context.getArgument(0).uppercase()) - val value = Scroll.parse(context.getArgument(1)) - val hoverEvent = HoverEvent(action, value) + component.hoverEvent = when (val action = context.getArgument(0)) { + "show_text" -> HoverEvent.ShowText(context.getArgument(1).toComponent()) + "show_item" -> HoverEvent.ShowItem(context.getArgument(1), context.getArgumentOrNull(2)?.toIntOrNull() ?: 1) + "show_entity" -> HoverEvent.ShowEntity(context.getArgument(1), context.getArgument(2), context.getArgumentOrNull(3)?.toComponent()) - component.hoverEvent = hoverEvent + else -> HoverEvent.ShowText(action.toComponent()) + } } override fun formatClosing(context: FormatProviderContext, component: Component) { component.hoverEvent = null } -} \ No newline at end of file +} diff --git a/src/main/kotlin/io/github/dockyardmc/scroll/serializers/ComponentToNbtSerializer.kt b/src/main/kotlin/io/github/dockyardmc/scroll/serializers/ComponentToNbtSerializer.kt index 147882e..1279777 100644 --- a/src/main/kotlin/io/github/dockyardmc/scroll/serializers/ComponentToNbtSerializer.kt +++ b/src/main/kotlin/io/github/dockyardmc/scroll/serializers/ComponentToNbtSerializer.kt @@ -2,7 +2,10 @@ package io.github.dockyardmc.scroll.serializers import io.github.dockyardmc.scroll.Component import io.github.dockyardmc.scroll.extensions.put -import org.jglrxavpok.hephaistos.nbt.* +import org.jglrxavpok.hephaistos.nbt.NBT +import org.jglrxavpok.hephaistos.nbt.NBTCompound +import org.jglrxavpok.hephaistos.nbt.NBTDouble +import org.jglrxavpok.hephaistos.nbt.NBTType import org.jglrxavpok.hephaistos.nbt.mutable.MutableNBTCompound object ComponentToNbtSerializer { @@ -28,18 +31,12 @@ object ComponentToNbtSerializer { val hover = c.hoverEvent if(hover != null) { - nbtWriter.put("hoverEvent", NBT.Compound { hoverWriter -> - hoverWriter.put("action", hover.action.name.lowercase()) - hoverWriter.put("contents", serializeComponent(hover.contents!!)) - }) + nbtWriter.put("hover_event", hover.getNbt()) } val click = c.clickEvent if(click != null) { - nbtWriter.put("clickEvent", NBT.Compound { clickWriter -> - clickWriter.put("action", click.action.name.lowercase()) - clickWriter.put("value", click.value!!) - }) + nbtWriter.put("click_event", click.getNbt()) } val list = c.extra diff --git a/src/main/kotlin/io/github/dockyardmc/scroll/serializers/JsonToComponentSerializer.kt b/src/main/kotlin/io/github/dockyardmc/scroll/serializers/JsonToComponentSerializer.kt index adf0bd2..e712581 100644 --- a/src/main/kotlin/io/github/dockyardmc/scroll/serializers/JsonToComponentSerializer.kt +++ b/src/main/kotlin/io/github/dockyardmc/scroll/serializers/JsonToComponentSerializer.kt @@ -1,23 +1,10 @@ package io.github.dockyardmc.scroll.serializers -import io.github.dockyardmc.scroll.ClickEvent import io.github.dockyardmc.scroll.Component -import io.github.dockyardmc.scroll.HoverEvent -import kotlinx.serialization.Serializable -import kotlinx.serialization.decodeFromString import kotlinx.serialization.json.Json -import javax.lang.model.type.UnionType object JsonToComponentSerializer { - fun serialize(json: String): Component { - val serializer = Json { - ignoreUnknownKeys = true - isLenient = true - } - -// return Json.decodeFromJsonElement(serializer.parseToJsonElement(json)) return Json.decodeFromString(json) - } } \ No newline at end of file diff --git a/src/main/kotlin/io/github/dockyardmc/scroll/serializers/KyoriToScrollSerializer.kt b/src/main/kotlin/io/github/dockyardmc/scroll/serializers/KyoriToScrollSerializer.kt index f6852a0..ea1d292 100644 --- a/src/main/kotlin/io/github/dockyardmc/scroll/serializers/KyoriToScrollSerializer.kt +++ b/src/main/kotlin/io/github/dockyardmc/scroll/serializers/KyoriToScrollSerializer.kt @@ -4,6 +4,7 @@ import io.github.dockyardmc.scroll.* import net.kyori.adventure.text.KeybindComponent import net.kyori.adventure.text.TextComponent import net.kyori.adventure.text.TranslatableComponent +import net.kyori.adventure.text.event.ClickEvent.Action import net.kyori.adventure.text.format.TextDecoration object KyoriToScrollSerializer { @@ -36,15 +37,22 @@ object KyoriToScrollSerializer { } private fun net.kyori.adventure.text.event.ClickEvent.toScroll(): ClickEvent { - val action = ClickAction.entries[this.action().ordinal] - val value = this.value() - return ClickEvent(action, value) + return when (this.action()) { + Action.OPEN_URL -> ClickEvent.OpenUrl(this.value()) + Action.OPEN_FILE -> ClickEvent.OpenFile(this.value()) + Action.RUN_COMMAND -> ClickEvent.RunCommand(this.value()) + Action.SUGGEST_COMMAND -> ClickEvent.SuggestCommand(this.value()) + Action.CHANGE_PAGE -> ClickEvent.ChangePage(this.value().toInt()) + Action.COPY_TO_CLIPBOARD -> ClickEvent.CopyToClipboard(this.value()) + } + // kyori doesn't support other ones yet. } private fun net.kyori.adventure.text.event.HoverEvent<*>.toScroll(): HoverEvent? { - val value = this.value() - return when(this.value()) { - is net.kyori.adventure.text.Component -> HoverEvent(HoverAction.SHOW_TEXT, serializeComponent(value as net.kyori.adventure.text.Component)) + return when(val value = this.value()) { + is net.kyori.adventure.text.Component -> HoverEvent.ShowText(value.toScroll()) + is net.kyori.adventure.text.event.HoverEvent.ShowItem -> HoverEvent.ShowItem(value.item().asString(), value.count()) + is net.kyori.adventure.text.event.HoverEvent.ShowEntity -> HoverEvent.ShowEntity(value.type().asString(), value.id().toString(), value.name()?.toScroll()) else -> null } } diff --git a/src/main/kotlin/io/github/dockyardmc/scroll/serializers/NbtToComponentSerializer.kt b/src/main/kotlin/io/github/dockyardmc/scroll/serializers/NbtToComponentSerializer.kt index 288b961..d36dc83 100644 --- a/src/main/kotlin/io/github/dockyardmc/scroll/serializers/NbtToComponentSerializer.kt +++ b/src/main/kotlin/io/github/dockyardmc/scroll/serializers/NbtToComponentSerializer.kt @@ -25,20 +25,14 @@ object NbtToComponentSerializer { component.translate = nbt.getString("translate") component.underlined = nbt.getBoolean("underlined") - val hover = nbt.getCompound("hoverEvent") + val hover = nbt.getCompound("hover_event") if (hover != null) { - val action = hover.getString("action")!! - val content = hover.getCompound("contents")!! - - component.hoverEvent = HoverEvent(HoverAction.valueOf(action.uppercase()), serializeNbt(content)) + component.hoverEvent = HoverEvent.fromNbt(hover) } - val click = nbt.getCompound("clickEvent") + val click = nbt.getCompound("click_event") if (click != null) { - val action = click.getString("action")!! - val value = click.getString("value")!! - - component.clickEvent = ClickEvent(ClickAction.valueOf(action.uppercase()), value) + component.clickEvent = ClickEvent.fromNbt(click) } val list = nbt.getList("extra") diff --git a/src/test/kotlin/JsonSerializationTests.kt b/src/test/kotlin/JsonSerializationTests.kt index 1444532..64a58a0 100644 --- a/src/test/kotlin/JsonSerializationTests.kt +++ b/src/test/kotlin/JsonSerializationTests.kt @@ -1,3 +1,4 @@ +import io.github.dockyardmc.scroll.ClickEvent import io.github.dockyardmc.scroll.Component import io.github.dockyardmc.scroll.serializers.JsonToComponentSerializer import io.github.dockyardmc.scroll.extensions.toComponent @@ -17,17 +18,29 @@ class JsonSerializationTests { @Test fun testComponentToJson() { - val input = "Osmanthus wine tastes the same as I remember... But where are those who share the memory" - val expected = "{\"extra\":[{\"text\":\"Osmanthus wine tastes \",\"color\":\"#FFFF55\"},{\"text\":\"the same as \",\"color\":\"#FFFF55\",\"italic\":true},{\"text\":\"I remember\",\"color\":\"#FFAA00\",\"bold\":true},{\"text\":\"... \",\"color\":\"#AAAAAA\",\"italic\":true},{\"text\":\"But where are those who share \",\"color\":\"#FFFF55\"},{\"text\":\"the memory\",\"color\":\"#55FFFF\",\"bold\":true,\"underlined\":true}],\"text\":\"\"}" + val input = + "Osmanthus wine tastes the same as I remember... But where are those who share the memory" + val expected = + "{\"extra\":[{\"text\":\"Osmanthus wine tastes \",\"color\":\"#FFFF55\"},{\"text\":\"the same as \",\"color\":\"#FFFF55\",\"italic\":true},{\"text\":\"I remember\",\"color\":\"#FFAA00\",\"bold\":true},{\"text\":\"... \",\"color\":\"#AAAAAA\",\"italic\":true},{\"text\":\"But where are those who share \",\"color\":\"#FFFF55\"},{\"text\":\"the memory\",\"color\":\"#55FFFF\",\"bold\":true,\"underlined\":true}],\"text\":\"\"}" assertEquals(expected, input.toComponent().toJson()) } @Test fun testJsonToComponent() { - val input = "{\"extra\":[{\"text\":\"Osmanthus wine tastes \",\"color\":\"#FFFF55\"},{\"text\":\"the same as \",\"color\":\"#FFFF55\",\"italic\":true},{\"text\":\"I remember\",\"color\":\"#FFAA00\",\"bold\":true},{\"text\":\"... \",\"color\":\"#AAAAAA\",\"italic\":true},{\"text\":\"But where are those who share \",\"color\":\"#FFFF55\"},{\"text\":\"the memory\",\"color\":\"#55FFFF\",\"bold\":true,\"underlined\":true}],\"text\":\"\"}" - val expected = "Osmanthus wine tastes the same as I remember... But where are those who share the memory" + val input = + "{\"extra\":[{\"text\":\"Osmanthus wine tastes \",\"color\":\"#FFFF55\"},{\"text\":\"the same as \",\"color\":\"#FFFF55\",\"italic\":true},{\"text\":\"I remember\",\"color\":\"#FFAA00\",\"bold\":true},{\"text\":\"... \",\"color\":\"#AAAAAA\",\"italic\":true},{\"text\":\"But where are those who share \",\"color\":\"#FFFF55\"},{\"text\":\"the memory\",\"color\":\"#55FFFF\",\"bold\":true,\"underlined\":true}],\"text\":\"\"}" + val expected = + "Osmanthus wine tastes the same as I remember... But where are those who share the memory" assertEquals(expected.toComponent().toJson(), JsonToComponentSerializer.serialize(input).toJson()) } + + @Test + fun testClickEvent() { + val input = Component(text = "", clickEvent = ClickEvent.ShowDialog("my_dialog")) + val expected = """{"text":"","click_event":{"action":"show_dialog","dialog":"my_dialog"}}""" + + assertEquals(input.toJson(), expected) + } } \ No newline at end of file diff --git a/src/test/kotlin/NbtSerializationTests.kt b/src/test/kotlin/NbtSerializationTests.kt index 3cdb1f1..2317fca 100644 --- a/src/test/kotlin/NbtSerializationTests.kt +++ b/src/test/kotlin/NbtSerializationTests.kt @@ -13,9 +13,9 @@ class NbtSerializationTests { @Test fun nbtToComponentTest(){ val nbt = NBT.Compound { - it.put("hoverEvent", NBT.Compound { hover -> + it.put("hover_event", NBT.Compound { hover -> hover.put("action", "show_text") - hover.put("contents", NBT.Compound { hoverIn -> + hover.put("value", NBT.Compound { hoverIn -> hoverIn.put("extra", NBT.Compound { it.put("extra", NBT.Compound { extrahover -> extrahover.put("text", "Click to open the store URL") @@ -28,9 +28,9 @@ class NbtSerializationTests { }) it.put("text", "CLICK HERE") it.put("bold", true) - it.put("clickEvent", NBT.Compound { click -> + it.put("click_event", NBT.Compound { click -> click.put("action", "open_url") - click.put("value", "https://store.mccisland.net") + click.put("url", "https://store.mccisland.net") }) it.put("underline", true) it.put("color", ScrollUtil.colorTags[""]) @@ -49,7 +49,7 @@ class NbtSerializationTests { val input = "Click to open the store URL'>CLICK HERE" val expected = mutableListOf( NBT.Compound { - it.put("hoverEvent", NBT.Compound { hover -> + it.put("hover_event", NBT.Compound { hover -> hover.put("action", "show_text") hover.put("contents", NBT.Compound { hoverIn -> hoverIn.put("extra", NBT.Compound { @@ -64,9 +64,9 @@ class NbtSerializationTests { }) it.put("text", "CLICK HERE") it.put("bold", true) - it.put("clickEvent", NBT.Compound { click -> + it.put("click_event", NBT.Compound { click -> click.put("action", "open_url") - click.put("value", "https://store.mccisland.net") + click.put("url", "https://store.mccisland.net") }) it.put("underline", true) it.put("color", ScrollUtil.colorTags[""]) diff --git a/src/test/kotlin/StringSerializationTests.kt b/src/test/kotlin/StringSerializationTests.kt index 4f13647..cb0309d 100644 --- a/src/test/kotlin/StringSerializationTests.kt +++ b/src/test/kotlin/StringSerializationTests.kt @@ -1,7 +1,6 @@ import io.github.dockyardmc.scroll.* import io.github.dockyardmc.scroll.extensions.scrollSanitized import io.github.dockyardmc.scroll.extensions.toComponent -import kotlin.math.exp import kotlin.test.Test import kotlin.test.assertEquals @@ -132,7 +131,7 @@ class StringSerializationTests { fun testClick() { val input = "Click for Seed" val expected = Component.compound(mutableListOf( - Component(text = "Click for Seed", clickEvent = ClickEvent(ClickAction.RUN_COMMAND, "/seed")) + Component(text = "Click for Seed", clickEvent = ClickEvent.RunCommand("/seed")) )) assertEquals(expected.toJson(), Scroll.parse(input).toJson()) @@ -142,13 +141,26 @@ class StringSerializationTests { fun testHover() { val input = "bucket o' fish'>hover over meeeee do not hover over me" val expected = Component.compound(mutableListOf( - Component(text = "hover over meeeee ", hoverEvent = HoverEvent(HoverAction.SHOW_TEXT, "bucket o' fish".toComponent())), + Component(text = "hover over meeeee ", hoverEvent = HoverEvent.ShowText("bucket o' fish".toComponent())), Component(text = "do not hover over me") )) assertEquals(expected.toJson(), Scroll.parse(input).toJson()) } + @Test + fun testDefaultHover() { + val input = "Hover for colon three" + val expected = Component.compound(mutableListOf( + Component( + text = "Hover for colon three", + hoverEvent = HoverEvent.ShowText(":3") + ) + )) + + assertEquals(expected.toJson(), input.toComponent().toJson()) + } + @Test fun testClickWithLink() { val input = "https://lukynka.cloud" @@ -156,7 +168,7 @@ class StringSerializationTests { val expected = Component.compound(mutableListOf( Component( text = "https://lukynka.cloud", - clickEvent = ClickEvent(ClickAction.OPEN_URL, "https://lukynka.cloud"), + clickEvent = ClickEvent.OpenUrl("https://lukynka.cloud"), color = "#55FFFF", underlined = true ), @@ -165,6 +177,22 @@ class StringSerializationTests { assertEquals(expected.toJson(), input.toComponent().toJson()) } + @Test + fun testClickWithCustom() { + val input = "Omg!! click for custom action!!" + + val expected = Component.compound( + mutableListOf( + Component( + text = "Omg!! click for custom action!!", + clickEvent = ClickEvent.Custom("my_id", "my_payload") + ) + ) + ) + + assertEquals(expected.toJson(), input.toComponent().toJson()) + } + // @Test // fun testGay() { // val input = "gayyyy"