From dd3a066a47b7f4d03530952bce3286d9bf35f5a0 Mon Sep 17 00:00:00 2001 From: Hare Date: Tue, 9 Dec 2025 10:52:35 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20placeholder=20API=20=E4=BB=AE=E5=AF=BE?= =?UTF-8?q?=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 10 ++++++++++ .../ghostdisplays/display/DisplayManager.kt | 17 ++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index c46bccd..ae34eee 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,3 +1,5 @@ +import net.minecrell.pluginyml.paper.PaperPluginDescription + group = "net.hareworks" version = "1.0" @@ -9,9 +11,11 @@ plugins { repositories { mavenCentral() maven("https://repo.papermc.io/repository/maven-public/") + maven("https://repo.extendedclip.com/content/repositories/placeholderapi/") } dependencies { + compileOnly("me.clip:placeholderapi:2.11.6") compileOnly("io.papermc.paper:paper-api:1.21.10-R0.1-SNAPSHOT") implementation("org.jetbrains.kotlin:kotlin-stdlib") implementation("net.hareworks:kommand-lib:1.1") @@ -31,6 +35,12 @@ paper { apiVersion = "1.21" description = "Invisible display entity controller library." authors = listOf("Hareworks") + serverDependencies { + register("PlaceholderAPI") { + required = false + load = PaperPluginDescription.RelativeLoadOrder.BEFORE + } + } } tasks { withType { diff --git a/src/main/kotlin/net/hareworks/ghostdisplays/display/DisplayManager.kt b/src/main/kotlin/net/hareworks/ghostdisplays/display/DisplayManager.kt index cfea6bc..1f68e0f 100644 --- a/src/main/kotlin/net/hareworks/ghostdisplays/display/DisplayManager.kt +++ b/src/main/kotlin/net/hareworks/ghostdisplays/display/DisplayManager.kt @@ -38,7 +38,7 @@ class DisplayManager( ensureIdAvailable(normalized) val safeLocation = location.clone() val controller = service.createTextDisplay(safeLocation, INTERACTION_DEFAULT) - val component = parseMiniMessage(initialContent.ifBlank { "${normalized}" }) + val component = parseMiniMessage(initialContent.ifBlank { "${normalized}" }, creator) controller.applyEntityUpdate { textDisplay -> textDisplay.text(component) } @@ -98,9 +98,9 @@ class DisplayManager( fun findDisplay(id: String): ManagedDisplay<*>? = displays[normalizeId(id)] - fun updateText(id: String, content: String): Component { + fun updateText(id: String, content: String, playerContext: Player? = null): Component { val display = requireText(id) - val component = parseMiniMessage(content) + val component = parseMiniMessage(content, playerContext) display.controller.applyEntityUpdate { it.text(component) } display.rawContent = content display.component = component @@ -253,11 +253,18 @@ class DisplayManager( } } - private fun parseMiniMessage(raw: String): Component = - runCatching { miniMessage.deserialize(if (raw.isBlank()) "empty" else raw) } + private fun parseMiniMessage(raw: String, playerContext: Player? = null): Component { + val rawOrEmpty = if (raw.isBlank()) "empty" else raw + val withPlaceholders = if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) { + me.clip.placeholderapi.PlaceholderAPI.setPlaceholders(playerContext, rawOrEmpty) + } else { + rawOrEmpty + } + return runCatching { miniMessage.deserialize(withPlaceholders) } .getOrElse { ex -> throw DisplayOperationException("MiniMessage parse error: ${ex.message ?: ex.javaClass.simpleName}") } + } companion object { private val INTERACTION_DEFAULT = InteractionOptions.enabled(width = 0.8, height = 0.8)