mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 08:14:00 +02:00
progress on using pure component in paper
This commit is contained in:
parent
2967d500eb
commit
11f7bf8602
17 changed files with 502 additions and 167 deletions
|
|
@ -1,10 +1,35 @@
|
|||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
||||
|
||||
group = rootProject.group
|
||||
version = rootProject.version
|
||||
|
||||
plugins {
|
||||
id("io.papermc.paperweight.userdev")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
// Spigot api
|
||||
compileOnly("org.spigotmc:spigot-api:1.18-R0.1-SNAPSHOT")
|
||||
// Used for nms
|
||||
paperweight.paperDevBundle("1.21.10-R0.1-SNAPSHOT")
|
||||
|
||||
// Protocolib
|
||||
compileOnly("net.dmulloy2:ProtocolLib:5.4.0")
|
||||
}
|
||||
}
|
||||
|
||||
repositories {
|
||||
maven("https://repo.papermc.io/repository/maven-public/")
|
||||
}
|
||||
|
||||
// Set target version
|
||||
tasks.withType<JavaCompile>().configureEach {
|
||||
sourceCompatibility = "21"
|
||||
targetCompatibility = "21"
|
||||
|
||||
options.encoding = "UTF-8"
|
||||
}
|
||||
|
||||
kotlin {
|
||||
compilerOptions {
|
||||
apiVersion.set(org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_2_0)
|
||||
jvmTarget.set(JvmTarget.JVM_21)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue