mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 16:16:17 +02:00
Merge branch 'v1.x.x-part' into v2.x.x
# Conflicts: # nms/nms-common/build.gradle.kts # src/main/kotlin/xyz/alexcrea/cuanvil/dependency/DependencyManager.kt # src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt
This commit is contained in:
commit
529e781a72
21 changed files with 620 additions and 223 deletions
|
|
@ -0,0 +1,102 @@
|
|||
package xyz.alexcrea.cuanvil.dependency.util
|
||||
|
||||
import net.kyori.adventure.text.Component
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer
|
||||
import org.bukkit.inventory.ItemStack
|
||||
import org.bukkit.inventory.meta.ItemMeta
|
||||
|
||||
// Mostly made for paper, spigot and folia support
|
||||
@Suppress("DEPRECATION")
|
||||
object PlatformUtil {
|
||||
|
||||
private fun hasClass(className: String): Boolean {
|
||||
try {
|
||||
Class.forName(className)
|
||||
return true
|
||||
} catch (_: ClassNotFoundException) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
private fun hasMethod(clazz: Class<*>, name: String, vararg parameterTypes: Class<*>): Boolean {
|
||||
try {
|
||||
clazz.getDeclaredMethod(name, *parameterTypes)
|
||||
return true
|
||||
} catch (_: NoSuchMethodException) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
val isPaper = hasClass("com.destroystokyo.paper.PaperConfig") ||
|
||||
hasClass("io.papermc.paper.configuration.Configuration")
|
||||
|
||||
val isFolia = hasClass("io.papermc.paper.threadedregions.RegionizedServer")
|
||||
|
||||
private val legacy_mm = LegacyComponentSerializer.legacySection()
|
||||
|
||||
// Lore
|
||||
fun ItemMeta.componentLore(): MutableList<Component> {
|
||||
val lore: List<Component>?
|
||||
if(isPaper){
|
||||
lore = this.lore()
|
||||
} else {
|
||||
val legacyLores = this.lore ?: return ArrayList()
|
||||
|
||||
lore = ArrayList(legacyLores.size)
|
||||
for (legacyLore in legacyLores) {
|
||||
lore.add(legacy_mm.deserialize(legacyLore))
|
||||
}
|
||||
}
|
||||
|
||||
return lore ?: ArrayList()
|
||||
}
|
||||
|
||||
fun ItemMeta.setComponentLore(lore: List<Component?>) {
|
||||
if(isPaper){
|
||||
this.lore(lore)
|
||||
} else {
|
||||
val legacyLore = ArrayList<String?>(lore.size)
|
||||
for (component in lore) {
|
||||
legacyLore.add(if(component == null) null
|
||||
else legacy_mm.serialize(component))
|
||||
}
|
||||
|
||||
this.lore = legacyLore
|
||||
}
|
||||
}
|
||||
|
||||
// Display name
|
||||
private val useCustomName = hasMethod(ItemStack::class.java, "customName")
|
||||
|
||||
fun ItemMeta.componentDisplayName(): Component? {
|
||||
if(useCustomName){
|
||||
if(!this.hasCustomName()) return null
|
||||
return this.customName()
|
||||
}else if(isPaper){
|
||||
if(!this.hasDisplayName()) return null
|
||||
return this.displayName()
|
||||
} else {
|
||||
if(!this.hasDisplayName()) return null
|
||||
|
||||
val legacy = this.displayName
|
||||
return legacy_mm.deserialize(legacy)
|
||||
}
|
||||
}
|
||||
|
||||
fun ItemMeta.setComponentDisplayName(component: Component?) {
|
||||
if(useCustomName){
|
||||
this.customName(component)
|
||||
}else if(isPaper){
|
||||
this.displayName(component)
|
||||
} else {
|
||||
if(component == null){
|
||||
this.setDisplayName(null)
|
||||
return
|
||||
}
|
||||
|
||||
val legacy = legacy_mm.serialize(component)
|
||||
this.setDisplayName(legacy)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -11,7 +11,7 @@ dependencies {
|
|||
implementation(project(":nms:nms-common"))
|
||||
|
||||
// Used for nms
|
||||
paperweight.paperDevBundle("1.21.9-R0.1-SNAPSHOT")
|
||||
paperweight.paperDevBundle("1.21.10-R0.1-SNAPSHOT")
|
||||
}
|
||||
|
||||
repositories {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue