Skip to content

Commit

Permalink
Fixed 1.21 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed Jul 8, 2024
1 parent 16dfd0d commit 6406d3f
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 22 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ allprojects {

java {
withSourcesJar()
toolchain.languageVersion.set(JavaLanguageVersion.of(17))
toolchain.languageVersion.set(JavaLanguageVersion.of(21))
}

tasks {
Expand Down
23 changes: 23 additions & 0 deletions eco-core/core-modern/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
Original file line number Diff line number Diff line change
@@ -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"))
}
}
6 changes: 5 additions & 1 deletion eco-core/core-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ group = "com.willfp"
version = rootProject.version

dependencies {
compileOnly("io.papermc.paper:paper-api:1.21-R0.1-SNAPSHOT")
compileOnly("io.papermc.paper:paper-api:1.20-R0.1-SNAPSHOT")
}

publishing {
Expand Down Expand Up @@ -30,6 +30,10 @@ publishing {
}
}

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(17))
}

tasks {
build {
dependsOn(publishToMavenLocal)
Expand Down
Original file line number Diff line number Diff line change
@@ -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")
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.willfp.ecoitems.items

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 org.bukkit.attribute.Attribute
import org.bukkit.attribute.AttributeInstance
Expand All @@ -10,7 +11,6 @@ import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerItemHeldEvent
import org.bukkit.inventory.EquipmentSlotGroup
import java.util.UUID

class ItemAttributeListener(private val plugin: EcoPlugin) : Listener {
Expand Down Expand Up @@ -65,32 +65,30 @@ class ItemAttributeListener(private val plugin: EcoPlugin) : Listener {
return true
}

if (Prerequisite.HAS_1_21.isMet) {
return this.key.namespace == "ecoitems" && (this.key.key.startsWith("damage")
|| this.key.key.startsWith("speed"))
var isFromEcoItems = false

ifModern {
useProxy<ModifierHelper> {
isFromEcoItems = isFromEcoItems(this@isFromEcoItems)
}
}

return false
return isFromEcoItems
}

private fun AttributeInstance.addCompatibleModifier(
attributeType: String,
amount: Double,
offset: Int
) {
if (Prerequisite.HAS_1_21.isMet) {
this.addModifier(
@Suppress("UnstableApiUsage")
AttributeModifier(
plugin.createNamespacedKey("${attributeType.lowercase()}_$offset"),
amount,
AttributeModifier.Operation.ADD_NUMBER,
EquipmentSlotGroup.ANY
ifModern {
useProxy<ModifierHelper> {
addModifier(
createModifier(plugin, attributeType, amount, offset)
)
)
} else {
this.addModifier(
@Suppress("DEPRECATION", "REMOVAL")
}
}.otherwise {
addModifier(
AttributeModifier(
UUID.nameUUIDFromBytes("ecoitems_$offset".toByteArray()),
"EcoItems $attributeType $offset",
Expand All @@ -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
}
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#libreforge-updater
#Fri Jul 05 13:02:23 BST 2024
#Mon Jul 08 15:31:28 BST 2024
kotlin.code.style=official
libreforge-version=4.64.1
libreforge-version=4.65.0
version=5.50.1
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ rootProject.name = "EcoItems"
// Core
include(":eco-core")
include(":eco-core:core-plugin")
include(":eco-core:core-modern")

0 comments on commit 6406d3f

Please sign in to comment.