Bifrost: Implement player advancement notifications. Oh my god this was hard and it still is ugly.

This commit is contained in:
Kenneth Endfinger
2022-01-17 17:19:12 -05:00
parent d16b9b1138
commit 9395f43e40
5 changed files with 91 additions and 2 deletions

View File

@ -0,0 +1,74 @@
package cloud.kubelet.foundation.core.util
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer
import org.bukkit.advancement.Advancement
import java.lang.reflect.Field
import java.util.concurrent.ConcurrentHashMap
private fun Advancement.getInternalHandle(): Any =
javaClass.getMethod("getHandle").invoke(this)
private fun Class<*>.getDeclaredFieldAccessible(name: String): Field {
val field = getDeclaredField(name)
if (!field.trySetAccessible()) {
throw RuntimeException("Failed to set reflection permissions to accessible.")
}
return field
}
private fun Advancement.getInternalAdvancementDisplay(handle: Any = getInternalHandle()): Any? =
handle.javaClass.methods.firstOrNull {
it.returnType.simpleName == "AdvancementDisplay" &&
it.parameterCount == 0
}?.invoke(handle) ?: handle.javaClass.getDeclaredFieldAccessible("c").get(handle)
private fun Advancement.displayTitleText(): String? {
val handle = getInternalHandle()
val advancementDisplay = getInternalAdvancementDisplay(handle) ?: return null
try {
val field = advancementDisplay.javaClass.getDeclaredField("a")
field.trySetAccessible()
val message = field.get(advancementDisplay)
val title = message.javaClass.getMethod("getString").invoke(message)
return title.toString()
} catch (_: Exception) {
}
val titleComponentField = advancementDisplay.javaClass.declaredFields.firstOrNull {
it.type.simpleName == "IChatBaseComponent"
}
if (titleComponentField != null) {
titleComponentField.trySetAccessible()
val titleChatBaseComponent = titleComponentField.get(advancementDisplay)
val title = titleChatBaseComponent.javaClass.getMethod("getText").invoke(titleChatBaseComponent).toString()
if (title.isNotBlank()) {
return title
}
val chatSerializerClass = titleChatBaseComponent.javaClass.declaredClasses.firstOrNull {
it.simpleName == "ChatSerializer"
}
if (chatSerializerClass != null) {
val componentJson = chatSerializerClass
.getMethod("a", titleChatBaseComponent.javaClass)
.invoke(null, titleChatBaseComponent).toString()
val gson = GsonComponentSerializer.gson().deserialize(componentJson)
return LegacyComponentSerializer.legacySection().serialize(gson)
}
}
val rawAdvancementName = key.key
return rawAdvancementName.substring(rawAdvancementName.lastIndexOf("/") + 1)
.lowercase().split("_")
.joinToString(" ") { it.substring(0, 1).uppercase() + it.substring(1) }
}
object AdvancementTitleCache {
private val cache = ConcurrentHashMap<Advancement, String?>()
fun of(advancement: Advancement): String? =
cache.computeIfAbsent(advancement) { it.displayTitleText() }
}