Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Begin updating for MM changes in adventure 4.10.0 #68

Merged
merged 5 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions gradle/libs.versions.conf
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ plugins = {

versions = {
ktor = "1.6.1"
adventure = "4.10.0-SNAPSHOT"
}

dependencies = {
adventure-minimessage = { group = "net.kyori", name = "adventure-text-minimessage", version = "4.2.0-SNAPSHOT" }
adventure-text-serializer-gson = { group = ${dependencies.adventure-minimessage.group}, name = "adventure-text-serializer-gson", version = "4.8.1" }
adventure-minimessage = { group = "net.kyori", name = "adventure-text-minimessage", version.ref = "adventure" }
adventure-text-serializer-gson = { group = ${dependencies.adventure-minimessage.group}, name = "adventure-text-serializer-gson", version.ref = "adventure" }
cache4k = { group = "io.github.reactivecircus.cache4k", name = "cache4k", version = "0.3.0" }
kotlinx-html = { group = "org.jetbrains.kotlinx", name = "kotlinx-html", version = "0.7.3" }
kotlinx-serialization-json = { group = ${dependencies.kotlinx-html.group}, name = "kotlinx-serialization-json", version = "1.2.2" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public data class Call(public val miniMessage: String? = null) : Packet
@SerialName("placeholders")
public data class Placeholders(
public val stringPlaceholders: Map<String, String>? = null,
public val componentPlaceholders: Map<String, JsonObject>? = null,
public val miniMessagePlaceholders: Map<String, String>? = null
public val componentPlaceholders: Map<String, JsonObject>? = null
) : Packet

@Serializable
Expand Down
1 change: 0 additions & 1 deletion src/commonMain/resources/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ <h1 class="title mc-font">MiniMessage Viewer</h1>
<table class="table is-striped is-hoverable is-fullwidth">
<thead>
<tr>
<th><abbr title="Tick to convert the replacement to a component using MiniMessage formatting">MM?</abbr></th>
<th>Key</th>
<th>Replacement</th>
<th></th>
Expand Down
36 changes: 3 additions & 33 deletions src/jsMain/kotlin/net/kyori/adventure/webui/js/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ private const val PARAM_INPUT: String = "input"
private const val PARAM_MODE: String = "mode"
public const val PARAM_BACKGROUND: String = "bg"
private const val PARAM_STRING_PLACEHOLDERS: String = "st"
private const val PARAM_COMPONENT_PLACEHOLDERS: String = "ct"

private var isInEditorMode: Boolean = false
private lateinit var editorInput: EditorInput
Expand Down Expand Up @@ -182,9 +181,6 @@ public fun main() {
window.localStorage[PARAM_STRING_PLACEHOLDERS] = Serializers.json.encodeToString(
newPlaceholders.stringPlaceholders
)
window.localStorage[PARAM_COMPONENT_PLACEHOLDERS] = Serializers.json.encodeToString(
newPlaceholders.miniMessagePlaceholders
)
webSocket.send(newPlaceholders)
}
}
Expand Down Expand Up @@ -269,15 +265,6 @@ public fun main() {
Serializers.json.encodeToString(placeholders.stringPlaceholders)
)
}
if (!placeholders.miniMessagePlaceholders.isNullOrEmpty()) {
link += "&$PARAM_COMPONENT_PLACEHOLDERS="
link +=
encodeURIComponent(
Serializers.json.encodeToString(
placeholders.miniMessagePlaceholders
)
)
}
window.navigator.clipboard.writeText(link).then {
bulmaToast.toast("Shareable link copied to clipboard!")
}
Expand Down Expand Up @@ -374,13 +361,10 @@ public fun main() {
private fun readPlaceholders(): Placeholders {
val userPlaceholders = UserPlaceholder.allInList()
val stringPlaceholders = mutableMapOf<String, String>()
val miniMessagePlaceholders = mutableMapOf<String, String>()
userPlaceholders.filter { t -> t.key.isNotEmpty() && t.value.isNotEmpty() }.forEach { t ->
(if (t.isMiniMessage) miniMessagePlaceholders else stringPlaceholders)[t.key] = t.value
stringPlaceholders[t.key] = t.value
}
return Placeholders(
stringPlaceholders = stringPlaceholders, miniMessagePlaceholders = miniMessagePlaceholders
)
return Placeholders(stringPlaceholders = stringPlaceholders)
}

private fun onWebsocketReady() {
Expand All @@ -392,31 +376,17 @@ private fun onWebsocketReady() {
inputBox.value = inputString
}
var stringPlaceholders: Map<String, String>? = null
var miniMessagePlaceholders: Map<String, String>? = null
urlParams.getFromParamsOrLocalStorage(PARAM_STRING_PLACEHOLDERS)?.also { inputString ->
stringPlaceholders = Serializers.json.tryDecodeFromString(inputString)
}
urlParams.getFromParamsOrLocalStorage(PARAM_COMPONENT_PLACEHOLDERS)?.also { inputString ->
miniMessagePlaceholders = Serializers.json.tryDecodeFromString(inputString)
}
stringPlaceholders?.forEach { (k, v) ->
UserPlaceholder.addToList().apply {
key = k
value = v
}
}
miniMessagePlaceholders?.forEach { (k, v) ->
UserPlaceholder.addToList().apply {
isMiniMessage = true
key = k
value = v
}
}
webSocket.send(
Placeholders(
stringPlaceholders = stringPlaceholders,
miniMessagePlaceholders = miniMessagePlaceholders
)
Placeholders(stringPlaceholders = stringPlaceholders)
)
}

Expand Down
18 changes: 3 additions & 15 deletions src/jsMain/kotlin/net/kyori/adventure/webui/js/UserPlaceholder.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package net.kyori.adventure.webui.js

import kotlinx.browser.document
import kotlinx.html.InputType
import kotlinx.html.classes
import kotlinx.html.dom.append
import kotlinx.html.js.a
import kotlinx.html.js.i
Expand All @@ -17,11 +15,9 @@ import org.w3c.dom.HTMLTableRowElement
import org.w3c.dom.asList

public class UserPlaceholder(
private val isMiniMessageCheckbox: HTMLInputElement,
private val keyInput: HTMLInputElement,
private val valueInput: HTMLInputElement
) {
public var isMiniMessage: Boolean by isMiniMessageCheckbox::checked
public var key: String by keyInput::value
public var value: String by valueInput::value

Expand All @@ -30,14 +26,10 @@ public class UserPlaceholder(
val list = document.getElementById("placeholders-list")!!
lateinit var key: HTMLInputElement
lateinit var value: HTMLInputElement
lateinit var isMM: HTMLInputElement
lateinit var row: HTMLTableRowElement
lateinit var deleteButton: HTMLAnchorElement
list.append {
row = tr {
td(classes = "control is-vcentered has-text-centered") {
isMM = input(type = InputType.checkBox, classes = "placeholder-component")
}
td(classes = "control") { key = input(classes = "input placeholder-key") }
td(classes = "control") { value = input(classes = "input placeholder-value") }
td(classes = "control") {
Expand All @@ -52,15 +44,11 @@ public class UserPlaceholder(
}
}
deleteButton.addEventListener("click", { row.remove() })
return UserPlaceholder(isMM, key, value)
return UserPlaceholder(key, value)
}

public fun allInList(): List<UserPlaceholder> {
val placeholdersBox = document.getElementById("placeholders-list")!!
val placeholderIsMM =
placeholdersBox.getElementsByClassName("placeholder-component").asList().map {
it.unsafeCast<HTMLInputElement>()
}
val placeholderKeys =
placeholdersBox.getElementsByClassName("placeholder-key").asList().map {
it.unsafeCast<HTMLInputElement>()
Expand All @@ -69,8 +57,8 @@ public class UserPlaceholder(
placeholdersBox.getElementsByClassName("placeholder-value").asList().map {
it.unsafeCast<HTMLInputElement>()
}
return placeholderIsMM.mapIndexed { i, _ ->
UserPlaceholder(placeholderIsMM[i], placeholderKeys[i], placeholderValues[i])
return placeholderKeys.mapIndexed { i, _ ->
UserPlaceholder(placeholderKeys[i], placeholderValues[i])
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,9 @@ import io.ktor.routing.route
import io.ktor.routing.routing
import io.ktor.websocket.webSocket
import kotlinx.serialization.encodeToString
import net.kyori.adventure.text.minimessage.Context
import net.kyori.adventure.text.minimessage.MiniMessage
import net.kyori.adventure.text.minimessage.Template
import net.kyori.adventure.text.minimessage.parser.ParsingException
import net.kyori.adventure.text.minimessage.parser.TokenParser
import net.kyori.adventure.text.minimessage.parser.node.TagNode
import net.kyori.adventure.text.minimessage.template.TemplateResolver
import net.kyori.adventure.text.minimessage.transformation.TransformationRegistry
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer
import net.kyori.adventure.webui.Serializers
import net.kyori.adventure.webui.URL_API
Expand All @@ -49,32 +44,23 @@ import net.kyori.adventure.webui.websocket.Packet
import net.kyori.adventure.webui.websocket.ParseResult
import net.kyori.adventure.webui.websocket.Placeholders
import net.kyori.adventure.webui.websocket.Response
import java.util.function.BiPredicate

public val Placeholders?.placeholderResolver: TemplateResolver
public val Placeholders?.tagResolver: TagResolver
get() {
if (this == null) return TemplateResolver.empty()
if (this == null) return TagResolver.empty()
val stringConverted =
this.stringPlaceholders?.map { Template.template(it.key, it.value) } ?: listOf()
this.stringPlaceholders?.map { (key, value) ->
Placeholder.parsed(key, value)
} ?: listOf()
val componentConverted =
this.componentPlaceholders?.map {
Template.template(
it.key, GsonComponentSerializer.gson().deserialize(it.value.toString())
)
}
?: listOf()
val miniMessageConverted =
this.miniMessagePlaceholders?.map {
Template.template(it.key, MiniMessage.miniMessage().deserialize(it.value))
}
?: listOf()
return TemplateResolver.templates(
stringConverted + componentConverted + miniMessageConverted
)
this.componentPlaceholders?.map { (key, value) ->
Placeholder.component(key, GsonComponentSerializer.gson().deserialize(value.toString()))
} ?: listOf()
return TagResolver.resolver(stringConverted + componentConverted)
}

/** Entry-point for MiniMessage Viewer. */
public fun Application.minimessage() {
public fun Application.miniMessage() {
// add standard renderers
HookManager.apply {
component(HOVER_EVENT_RENDER_HOOK)
Expand All @@ -101,14 +87,14 @@ public fun Application.minimessage() {
// set up other routing
route(URL_API) {
webSocket(URL_MINI_TO_HTML) {
var templateResolver = TemplateResolver.empty()
var tagResolver = TagResolver.empty()
var miniMessage: String? = null

for (frame in incoming) {
if (frame is Frame.Text) {
when (val packet = Serializers.json.tryDecodeFromString<Packet>(frame.readText())) {
is Call -> miniMessage = packet.miniMessage
is Placeholders -> templateResolver = packet.placeholderResolver
is Placeholders -> tagResolver = packet.tagResolver
null -> continue
}

Expand All @@ -122,7 +108,7 @@ public fun Application.minimessage() {
.map { line -> HookManager.render(line) }
.map { line ->
MiniMessage.miniMessage()
.deserialize(line, templateResolver)
.deserialize(line, tagResolver)
}
.map { component -> HookManager.render(component) }
.forEach { component ->
Expand Down Expand Up @@ -151,33 +137,16 @@ public fun Application.minimessage() {
GsonComponentSerializer.gson()
.serialize(
MiniMessage.miniMessage()
.deserialize(input, structure.placeholders.placeholderResolver)
.deserialize(input, structure.placeholders.tagResolver)
)
)
}

post(URL_MINI_TO_TREE) {
val structure = Serializers.json.tryDecodeFromString<Combined>(call.receiveText())
val input = structure?.miniMessage ?: return@post
val resolver = structure.placeholders.placeholderResolver
val transformationFactory = { node: TagNode ->
try {
TransformationRegistry.standard()
.get(
node.name(),
node.parts(),
resolver,
Context.of(false, input, MiniMessage.miniMessage())
)
} catch (ignored: ParsingException) {
null
}
}
val tagNameChecker = BiPredicate { name: String?, _: Boolean ->
TransformationRegistry.standard().exists(name, resolver)
}
val root =
TokenParser.parse(transformationFactory, tagNameChecker, resolver, input, false)
val resolver = structure.placeholders.tagResolver
val root = MiniMessage.miniMessage().deserializeToTree(input, resolver)
call.respondText(root.toString())
}

Expand Down
26 changes: 13 additions & 13 deletions src/jvmMain/resources/application.conf
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
ktor {
deployment {
port = 8080
}
deployment {
port = 8080
}

application {
modules = [
net.kyori.adventure.webui.jvm.ApplicationKt.main,
net.kyori.adventure.webui.jvm.minimessage.MiniMessageKt.minimessage
]
}
application {
modules = [
net.kyori.adventure.webui.jvm.ApplicationKt.main,
net.kyori.adventure.webui.jvm.minimessage.MiniMessageKt.miniMessage
]
}

config {
jsScriptFile = "$jsScriptFile"
}
}
config {
jsScriptFile = "$jsScriptFile"
}
}