-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
163 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
eco-core/core-modern/src/main/kotlin/com/willfp/ecoitems/compat/modern/ModifierHelperImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
eco-core/core-plugin/src/main/kotlin/com/willfp/ecoitems/compat/ModernCompatibility.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters