Fixed 1.21 compatibility

This commit is contained in:
Auxilor
2024-07-08 15:50:04 +01:00
parent 16dfd0d163
commit 6406d3f8d6
8 changed files with 163 additions and 22 deletions

View File

@@ -44,7 +44,7 @@ allprojects {
java { java {
withSourcesJar() withSourcesJar()
toolchain.languageVersion.set(JavaLanguageVersion.of(17)) toolchain.languageVersion.set(JavaLanguageVersion.of(21))
} }
tasks { tasks {

View File

@@ -0,0 +1,23 @@
group = "com.willfp"
version = rootProject.version
dependencies {
compileOnly(project(":eco-core:core-plugin"))
compileOnly("io.papermc.paper:paper-api:1.21-R0.1-SNAPSHOT")
}
tasks {
build {
dependsOn(publishToMavenLocal)
}
compileJava {
options.release = 21
}
compileKotlin {
kotlinOptions {
jvmTarget = "21"
}
}
}

View File

@@ -0,0 +1,28 @@
package com.willfp.ecoitems.compat.modern
import com.willfp.eco.core.EcoPlugin
import com.willfp.ecoitems.items.ModifierHelper
import org.bukkit.attribute.AttributeModifier
import org.bukkit.inventory.EquipmentSlotGroup
class ModifierHelperImpl : ModifierHelper {
override fun createModifier(
plugin: EcoPlugin,
attributeType: String,
amount: Double,
offset: Int
): AttributeModifier {
@Suppress("UnstableApiUsage")
return AttributeModifier(
plugin.createNamespacedKey("${attributeType.lowercase()}_$offset"),
amount,
AttributeModifier.Operation.ADD_NUMBER,
EquipmentSlotGroup.ANY
)
}
override fun isFromEcoItems(modifier: AttributeModifier): Boolean {
return modifier.key.namespace == "ecoitems" && (modifier.key.key.startsWith("damage")
|| modifier.key.key.startsWith("speed"))
}
}

View File

@@ -2,7 +2,7 @@ group = "com.willfp"
version = rootProject.version version = rootProject.version
dependencies { dependencies {
compileOnly("io.papermc.paper:paper-api:1.21-R0.1-SNAPSHOT") compileOnly("io.papermc.paper:paper-api:1.20-R0.1-SNAPSHOT")
} }
publishing { publishing {
@@ -30,6 +30,10 @@ publishing {
} }
} }
java {
toolchain.languageVersion.set(JavaLanguageVersion.of(17))
}
tasks { tasks {
build { build {
dependsOn(publishToMavenLocal) dependsOn(publishToMavenLocal)

View File

@@ -0,0 +1,75 @@
package com.willfp.ecoitems.compat
import com.willfp.eco.core.Prerequisite
import com.willfp.libreforge.proxy.InvalidProxyException
private const val BASE_PACKAGE = "com.willfp.ecoitems.compat.modern"
private val isModern = Prerequisite.HAS_PAPER.isMet && Prerequisite.HAS_1_21.isMet
internal annotation class ModernCompatibilityProxy(
val location: String
)
private val cache = mutableMapOf<Class<*>, Any>()
internal object ModernCompatibilityScope {
inline fun <reified T> loadProxy(): T {
return loadCompatibilityProxy(T::class.java)
}
inline fun <reified T> useProxy(block: T.() -> Unit) {
val proxy = loadProxy<T>()
with(proxy) {
block()
}
}
}
internal object PotentiallyModernScope {
fun otherwise(block: () -> Any?) {
if (!isModern) {
block()
}
}
}
internal fun <R1> ifModern(
block: ModernCompatibilityScope.() -> R1
): PotentiallyModernScope {
if (isModern) {
block(ModernCompatibilityScope)
}
return PotentiallyModernScope
}
internal fun <T> loadCompatibilityProxy(clazz: Class<T>): T {
@Suppress("UNCHECKED_CAST")
return cache.getOrPut(clazz) {
loadProxyUncached(clazz)
} as T
}
private fun loadProxyUncached(clazz: Class<*>): Any {
val proxy = clazz.getAnnotation(ModernCompatibilityProxy::class.java)
val location = proxy?.location ?: throw IllegalArgumentException("Class ${clazz.name} is not a proxy")
val className = "$BASE_PACKAGE.$location"
try {
val found = Class.forName(className)
val constructor = found.getConstructor()
val instance = constructor.newInstance()
if (!clazz.isInstance(instance)) {
throw InvalidProxyException("Modern compatibility proxy class $className does not implement ${clazz.name}")
}
return instance
} catch (e: ClassNotFoundException) {
throw InvalidProxyException("Could not find modern compatibility proxy class $className")
} catch (e: NoSuchMethodException) {
throw InvalidProxyException("Could not find no-args constructor for modern compatibility proxy class $className")
}
}

View File

@@ -1,7 +1,8 @@
package com.willfp.ecoitems.items package com.willfp.ecoitems.items
import com.willfp.eco.core.EcoPlugin import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.core.Prerequisite import com.willfp.ecoitems.compat.ModernCompatibilityProxy
import com.willfp.ecoitems.compat.ifModern
import com.willfp.libreforge.toDispatcher import com.willfp.libreforge.toDispatcher
import org.bukkit.attribute.Attribute import org.bukkit.attribute.Attribute
import org.bukkit.attribute.AttributeInstance import org.bukkit.attribute.AttributeInstance
@@ -10,7 +11,6 @@ import org.bukkit.entity.Player
import org.bukkit.event.EventHandler import org.bukkit.event.EventHandler
import org.bukkit.event.Listener import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerItemHeldEvent import org.bukkit.event.player.PlayerItemHeldEvent
import org.bukkit.inventory.EquipmentSlotGroup
import java.util.UUID import java.util.UUID
class ItemAttributeListener(private val plugin: EcoPlugin) : Listener { class ItemAttributeListener(private val plugin: EcoPlugin) : Listener {
@@ -65,12 +65,15 @@ class ItemAttributeListener(private val plugin: EcoPlugin) : Listener {
return true return true
} }
if (Prerequisite.HAS_1_21.isMet) { var isFromEcoItems = false
return this.key.namespace == "ecoitems" && (this.key.key.startsWith("damage")
|| this.key.key.startsWith("speed")) ifModern {
useProxy<ModifierHelper> {
isFromEcoItems = isFromEcoItems(this@isFromEcoItems)
}
} }
return false return isFromEcoItems
} }
private fun AttributeInstance.addCompatibleModifier( private fun AttributeInstance.addCompatibleModifier(
@@ -78,19 +81,14 @@ class ItemAttributeListener(private val plugin: EcoPlugin) : Listener {
amount: Double, amount: Double,
offset: Int offset: Int
) { ) {
if (Prerequisite.HAS_1_21.isMet) { ifModern {
this.addModifier( useProxy<ModifierHelper> {
@Suppress("UnstableApiUsage") addModifier(
AttributeModifier( createModifier(plugin, attributeType, amount, offset)
plugin.createNamespacedKey("${attributeType.lowercase()}_$offset"),
amount,
AttributeModifier.Operation.ADD_NUMBER,
EquipmentSlotGroup.ANY
) )
) }
} else { }.otherwise {
this.addModifier( addModifier(
@Suppress("DEPRECATION", "REMOVAL")
AttributeModifier( AttributeModifier(
UUID.nameUUIDFromBytes("ecoitems_$offset".toByteArray()), UUID.nameUUIDFromBytes("ecoitems_$offset".toByteArray()),
"EcoItems $attributeType $offset", "EcoItems $attributeType $offset",
@@ -101,3 +99,15 @@ class ItemAttributeListener(private val plugin: EcoPlugin) : Listener {
} }
} }
} }
@ModernCompatibilityProxy("ModifierHelperImpl")
interface ModifierHelper {
fun createModifier(
plugin: EcoPlugin,
attributeType: String,
amount: Double,
offset: Int
): AttributeModifier
fun isFromEcoItems(modifier: AttributeModifier): Boolean
}

View File

@@ -1,5 +1,5 @@
#libreforge-updater #libreforge-updater
#Fri Jul 05 13:02:23 BST 2024 #Mon Jul 08 15:31:28 BST 2024
kotlin.code.style=official kotlin.code.style=official
libreforge-version=4.64.1 libreforge-version=4.65.0
version=5.50.1 version=5.50.1

View File

@@ -16,3 +16,4 @@ rootProject.name = "EcoItems"
// Core // Core
include(":eco-core") include(":eco-core")
include(":eco-core:core-plugin") include(":eco-core:core-plugin")
include(":eco-core:core-modern")