Skip to content

Commit e8016f1

Browse files
authored
Merge pull request #217 from KyoriPowered/feat/in-game-preview-frontend
2 parents cc3e70d + ec82846 commit e8016f1

File tree

6 files changed

+42
-36
lines changed

6 files changed

+42
-36
lines changed

src/commonMain/kotlin/net/kyori/adventure/webui/Constants.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,8 @@ public const val PARAM_EDITOR_TOKEN: String = "token"
4242
/** Path for getting a short link for a MiniMessage input. */
4343
public const val URL_MINI_SHORTEN: String = "/mini-shorten"
4444

45-
/** Path for getting a hostname for an in-game MiniMessage motd preview. */
46-
public const val URL_SETUP_MOTD_PREVIEW: String = "/setup-motd-preview"
47-
/** Path for getting a hostname for an in-game MiniMessage kick preview. */
48-
public const val URL_SETUP_KICK_PREVIEW: String = "/setup-kick-preview"
45+
/** Path for getting a hostname for an in-game MiniMessage preview. */
46+
public const val URL_IN_GAME_PREVIEW: String = "/in-game-preview"
4947

5048
/** Path for getting the configuration of this WebUI instance */
5149
public const val URL_BUILD_INFO: String = "/build"

src/commonMain/kotlin/net/kyori/adventure/webui/websocket/Packet.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,9 @@ public data class Combined(
2828
public val background: String? = null,
2929
public val mode: String? = null
3030
)
31+
32+
@Serializable
33+
public data class InGamePreview(
34+
public val miniMessage: String? = null,
35+
public val key: String? = null
36+
)

src/commonMain/resources/web/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ <h1 class="title mc-font">MiniMessage Viewer</h1>
6161
<button class="button" id="copy-button" aria-label="Copy input" title="Copy input">
6262
<span class="icon is-small"><i class="fas fa-copy"></i></span>
6363
</button>
64+
<button class="button" id="in-game-preview-button" aria-label="Try in-game" title="Try in-game">
65+
<span class="icon is-small"><i class="fas fa-gamepad"></i></span>
66+
</button>
6467
<div class="dropdown share-dropdown">
6568
<div class="dropdown-trigger">
6669
<button class="button" aria-haspopup="true" aria-controls="share-dropdown-menu">

src/jsMain/kotlin/net/kyori/adventure/webui/js/Main.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ import net.kyori.adventure.webui.URL_API
1717
import net.kyori.adventure.webui.URL_EDITOR
1818
import net.kyori.adventure.webui.URL_EDITOR_INPUT
1919
import net.kyori.adventure.webui.URL_EDITOR_OUTPUT
20+
import net.kyori.adventure.webui.URL_IN_GAME_PREVIEW
2021
import net.kyori.adventure.webui.URL_MINI_TO_HTML
2122
import net.kyori.adventure.webui.URL_MINI_TO_JSON
2223
import net.kyori.adventure.webui.URL_MINI_TO_TREE
2324
import net.kyori.adventure.webui.editor.EditorInput
2425
import net.kyori.adventure.webui.tryDecodeFromString
2526
import net.kyori.adventure.webui.websocket.Call
2627
import net.kyori.adventure.webui.websocket.Combined
28+
import net.kyori.adventure.webui.websocket.InGamePreview
2729
import net.kyori.adventure.webui.websocket.Packet
2830
import net.kyori.adventure.webui.websocket.Placeholders
2931
import net.kyori.adventure.webui.websocket.Response
@@ -323,6 +325,20 @@ public fun mainLoaded() {
323325
}
324326
)
325327

328+
var inGamePreviewKey = (1..8).map { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".random() }.joinToString("")
329+
document.getElementById("in-game-preview-button")!!.addEventListener(
330+
"click",
331+
{
332+
window.postPacket(
333+
"$URL_API$URL_IN_GAME_PREVIEW",
334+
InGamePreview(miniMessage = input.value, key = inGamePreviewKey)
335+
)
336+
.then { response -> response.text() }
337+
.then { text -> window.navigator.clipboard.writeText(text) }
338+
.then { bulmaToast.toast("Minecraft Server Hostname copied to clipboard!") }
339+
}
340+
)
341+
326342
// EDITOR
327343

328344
// BURGER MENU

src/jvmMain/kotlin/net/kyori/adventure/webui/jvm/minimessage/MiniMessage.kt

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ import net.kyori.adventure.webui.Serializers
2626
import net.kyori.adventure.webui.URL_API
2727
import net.kyori.adventure.webui.URL_BUILD_INFO
2828
import net.kyori.adventure.webui.URL_EDITOR
29+
import net.kyori.adventure.webui.URL_IN_GAME_PREVIEW
2930
import net.kyori.adventure.webui.URL_MINI_SHORTEN
3031
import net.kyori.adventure.webui.URL_MINI_TO_HTML
3132
import net.kyori.adventure.webui.URL_MINI_TO_JSON
3233
import net.kyori.adventure.webui.URL_MINI_TO_TREE
33-
import net.kyori.adventure.webui.URL_SETUP_KICK_PREVIEW
34-
import net.kyori.adventure.webui.URL_SETUP_MOTD_PREVIEW
3534
import net.kyori.adventure.webui.jvm.appendComponent
3635
import net.kyori.adventure.webui.jvm.getConfigString
3736
import net.kyori.adventure.webui.jvm.minimessage.editor.installEditor
@@ -49,6 +48,7 @@ import net.kyori.adventure.webui.jvm.minimessage.storage.BytebinStorage
4948
import net.kyori.adventure.webui.tryDecodeFromString
5049
import net.kyori.adventure.webui.websocket.Call
5150
import net.kyori.adventure.webui.websocket.Combined
51+
import net.kyori.adventure.webui.websocket.InGamePreview
5252
import net.kyori.adventure.webui.websocket.Packet
5353
import net.kyori.adventure.webui.websocket.ParseResult
5454
import net.kyori.adventure.webui.websocket.Placeholders
@@ -205,16 +205,14 @@ public fun Application.miniMessage() {
205205
}
206206
}
207207

208-
post(URL_SETUP_MOTD_PREVIEW) {
209-
val input = call.receiveText()
210-
val hostname = previewManager.initializeMotdPreview(input)
211-
call.respondText(hostname)
212-
}
213-
214-
post(URL_SETUP_KICK_PREVIEW) {
215-
val input = call.receiveText()
216-
val hostname = previewManager.initializeKickPreview(input)
217-
call.respondText(hostname)
208+
post(URL_IN_GAME_PREVIEW) {
209+
val request = Serializers.json.tryDecodeFromString<InGamePreview>(call.receiveText())
210+
if (request != null && request.miniMessage != null && request.key != null) {
211+
val hostname = previewManager.initializePreview(request.miniMessage, request.key)
212+
call.respondText(hostname)
213+
} else {
214+
call.response.status(HttpStatusCode.BadRequest)
215+
}
218216
}
219217

220218
get(URL_BUILD_INFO) {

src/jvmMain/kotlin/net/kyori/adventure/webui/jvm/minimessage/preview/ServerStatusPreviewManager.kt

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ public class ServerStatusPreviewManager(
3636
private val managerJob = SupervisorJob(application.coroutineContext.job)
3737
override val coroutineContext: CoroutineContext = application.coroutineContext + managerJob
3838

39-
private val motdPreviews = Cache.Builder<String, String>().expireAfterAccess(1.hours).build()
40-
private val kickPreviews = Cache.Builder<String, String>().expireAfterAccess(1.hours).build()
39+
private val previews = Cache.Builder<String, String>().expireAfterAccess(1.hours).build()
4140

4241
init {
4342
launch {
@@ -110,32 +109,18 @@ public class ServerStatusPreviewManager(
110109
}
111110

112111
private fun lookupKickMessage(serverAddress: String): String {
113-
return kickPreviews.get(serverAddress.split(".")[0]) ?: "<red>You cant join here!"
112+
return previews.get(serverAddress.split(".")[0]) ?: "<red>You cant join here!"
114113
}
115114

116115
private fun lookupMotd(serverAddress: String): String {
117-
return motdPreviews.get(serverAddress.split(".")[0]) ?: "<rainbow>MiniMessage is cool!"
116+
return previews.get(serverAddress.split(".")[0]) ?: "<rainbow>MiniMessage is cool!"
118117
}
119118

120-
public fun initializeKickPreview(input: String): String {
121-
val key = generateRandomString()
122-
kickPreviews.put(key, input)
119+
public fun initializePreview(input: String, key: String): String {
120+
previews.put(key, input)
123121
return "$key.webui.advntr.dev"
124122
}
125123

126-
public fun initializeMotdPreview(input: String): String {
127-
val key = generateRandomString()
128-
motdPreviews.put(key, input)
129-
return "$key.webui.advntr.dev"
130-
}
131-
132-
private fun generateRandomString(length: Int = 8): String {
133-
val allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
134-
return (1..length)
135-
.map { allowedChars.random() }
136-
.joinToString("")
137-
}
138-
139124
private suspend fun ByteWriteChannel.writeMcPacket(packetId: Int, consumer: (packet: DataOutputStream) -> Unit) {
140125
val stream = ByteArrayOutputStream()
141126
val packet = DataOutputStream(stream)

0 commit comments

Comments
 (0)