Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.r4g3baby.simplescore.bukkit.protocol.modern.chat

import com.r4g3baby.simplescore.bukkit.util.Adventure
import com.r4g3baby.simplescore.bukkit.util.NMS
import com.r4g3baby.simplescore.bukkit.util.OBC
import com.r4g3baby.simplescore.core.util.Reflection

class WrappedChatComponent(val handle: Any) {
class WrappedChatComponent private constructor(val handle: Any) {
companion object {
val clazz: Class<*>

Expand All @@ -27,6 +28,10 @@ class WrappedChatComponent(val handle: Any) {
}

fun fromString(message: String): WrappedChatComponent {
val component = Adventure.parseToComponent(message)
if (component != null) return WrappedChatComponent(component)

val message = Adventure.parseToString(message) ?: message
val components = fromString.invoke(null, message) as Array<*>
return WrappedChatComponent(components[0]!!)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.r4g3baby.simplescore.bukkit.scoreboard

import com.r4g3baby.simplescore.BukkitPlugin
import com.r4g3baby.simplescore.api.scoreboard.VarReplacer
import com.r4g3baby.simplescore.bukkit.util.Adventure
import com.r4g3baby.simplescore.bukkit.util.getPlayerPing
import com.r4g3baby.simplescore.bukkit.util.lazyReplace
import com.r4g3baby.simplescore.core.util.translateColorCodes
Expand Down Expand Up @@ -36,7 +35,6 @@ class VarReplacer(plugin: BukkitPlugin) : VarReplacer<Player> {
.lazyReplace("%server_online%") { viewer.server.onlinePlayers.size.toString() }
.lazyReplace("%server_maxplayers%") { viewer.server.maxPlayers.toString() }

result = Adventure.parseToString(result)
return translateColorCodes(result)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.r4g3baby.simplescore.bukkit.util

import com.r4g3baby.simplescore.core.util.Reflection
import com.r4g3baby.simplescore.core.util.Reflection.classExists
import net.kyori.adventure.text.minimessage.MiniMessage
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer
Expand All @@ -8,31 +9,43 @@ import org.bukkit.ChatColor.COLOR_CHAR
object Adventure {
private val miniMessage: MiniMessage?
private val textSerializer: LegacyComponentSerializer?
private val componentConstructor: Reflection.ConstructorInvoker?

init {
val isAdventureSupported = classExists("net.kyori.adventure.text.minimessage.MiniMessage")
&& classExists("net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer")

if (isAdventureSupported) {
miniMessage = MiniMessage.builder().strict(false).build()
val isMiniMessageSupported = classExists("net.kyori.adventure.text.minimessage.MiniMessage")
miniMessage = if (isMiniMessageSupported) MiniMessage.miniMessage() else null

val isLegacyTextSupported = classExists("net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer")
textSerializer = if (isLegacyTextSupported) {
val legacySerializerBuilder = LegacyComponentSerializer.builder().character(COLOR_CHAR)
val hexColorsSupported = ServerVersion.atOrAbove(ServerVersion.netherUpdate)
textSerializer = if (hexColorsSupported) {

if (hexColorsSupported) {
legacySerializerBuilder.useUnusualXRepeatedCharacterHexFormat().hexColors().build()
} else legacySerializerBuilder.build()
} else {
miniMessage = null
textSerializer = null
}
} else null

componentConstructor = try {
val component = Reflection.getClass("net.kyori.adventure.text.Component")
val adventureComponent = Reflection.getClass("io.papermc.paper.adventure.AdventureComponent")
Reflection.getConstructor(adventureComponent, component)
} catch (_: IllegalArgumentException) { null }
}

fun parseToString(text: String): String {
fun parseToString(text: String): String? {
return if (textSerializer != null && miniMessage != null) {
textSerializer.serialize(
miniMessage.deserialize(vanillaToMini(text))
)
} else text
} else null
}

fun parseToComponent(text: String): Any? {
return if (componentConstructor != null && miniMessage != null) {
componentConstructor.invoke(
miniMessage.deserialize(vanillaToMini(text))
)
} else null
}

private val namedToMiniTag = mapOf(
Expand Down