Skip to content

Commit

Permalink
Fixed Java 17 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed Jul 8, 2024
1 parent 0080c32 commit 52367db
Show file tree
Hide file tree
Showing 19 changed files with 275 additions and 42 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies {
implementation(project(path = ":eco-core:core-plugin", configuration = "shadow"))
implementation(project(":eco-core:core-proxy"))
implementation(project(":eco-core:core-backend"))
implementation(project(":eco-core:core-backend-modern"))
implementation(project(path = ":eco-core:core-nms:v1_17_R1", configuration = "reobf"))
implementation(project(path = ":eco-core:core-nms:v1_18_R1", configuration = "reobf"))
implementation(project(path = ":eco-core:core-nms:v1_18_R2", configuration = "reobf"))
Expand Down
19 changes: 19 additions & 0 deletions eco-core/core-backend-modern/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
group = "com.willfp"
version = rootProject.version

dependencies {
compileOnly(project(":eco-core:core-backend"))
compileOnly("io.papermc.paper:paper-api:1.21-R0.1-SNAPSHOT")
}

tasks {
compileJava {
options.release = 21
}

compileKotlin {
kotlinOptions {
jvmTarget = "21"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.willfp.eco.internal.compat.modern.entities

import com.willfp.eco.core.entities.Entities
import com.willfp.eco.internal.compat.modern.entities.parsers.EntityArgParserJumpStrength
import com.willfp.eco.internal.compat.modern.entities.parsers.EntityArgParserScale
import com.willfp.eco.internal.entities.ModernEntityArgParsers

class ModernEntityArgParsersImpl: ModernEntityArgParsers {
override fun registerAll() {
Entities.registerArgParser(EntityArgParserScale)
Entities.registerArgParser(EntityArgParserJumpStrength)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.willfp.eco.internal.entities
package com.willfp.eco.internal.compat.modern.entities.parsers

import com.willfp.eco.core.entities.args.EntityArgParseResult
import com.willfp.eco.core.entities.args.EntityArgParser
Expand Down Expand Up @@ -40,4 +40,4 @@ object EntityArgParserJumpStrength : EntityArgParser {
}
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.willfp.eco.internal.compat.modern.entities.parsers

import com.willfp.eco.core.entities.args.EntityArgParseResult
import com.willfp.eco.core.entities.args.EntityArgParser
import org.bukkit.attribute.Attribute
import org.bukkit.entity.LivingEntity
import org.bukkit.entity.Phantom
import org.bukkit.entity.Slime

object EntityArgParserScale : EntityArgParser {
override fun parseArguments(args: Array<out String>): EntityArgParseResult? {
var attributeValue: Double? = null

for (arg in args) {
val argSplit = arg.split(":")
if (!argSplit[0].equals("scale", ignoreCase = true)) {
continue
}
if (argSplit.size < 2) {
continue
}
attributeValue = argSplit[1].toDoubleOrNull()
}

attributeValue ?: return null

return EntityArgParseResult(
{
if (it !is LivingEntity) {
return@EntityArgParseResult false
}

val inst = it.getAttribute(Attribute.GENERIC_SCALE) ?: return@EntityArgParseResult false
inst.value >= attributeValue
},
{
if (it !is LivingEntity) {
return@EntityArgParseResult
}

it.getAttribute(Attribute.GENERIC_SCALE)?.baseValue = attributeValue
}
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.willfp.eco.internal.compat.modern.items

import com.willfp.eco.core.items.Items
import com.willfp.eco.internal.compat.modern.items.parsers.ArgParserFireResistant
import com.willfp.eco.internal.compat.modern.items.parsers.ArgParserGlint
import com.willfp.eco.internal.compat.modern.items.parsers.ArgParserItemName
import com.willfp.eco.internal.compat.modern.items.parsers.ArgParserMaxDamage
import com.willfp.eco.internal.compat.modern.items.parsers.ArgParserMaxStackSize
import com.willfp.eco.internal.compat.modern.items.parsers.ArgParserTrim
import com.willfp.eco.internal.items.ModernItemArgParsers

class ModernItemArgParsersImpl : ModernItemArgParsers {
override fun registerAll() {
Items.registerArgParser(ArgParserTrim)
Items.registerArgParser(ArgParserFireResistant)
Items.registerArgParser(ArgParserGlint)
Items.registerArgParser(ArgParserItemName)
Items.registerArgParser(ArgParserMaxDamage)
Items.registerArgParser(ArgParserMaxStackSize)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.willfp.eco.internal.items.modern
package com.willfp.eco.internal.compat.modern.items.parsers

import com.willfp.eco.internal.items.templates.FlagArgParser
import org.bukkit.inventory.meta.ItemMeta
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.willfp.eco.internal.items.modern
package com.willfp.eco.internal.compat.modern.items.parsers

import com.willfp.eco.internal.items.templates.FlagArgParser
import org.bukkit.inventory.meta.ItemMeta
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.willfp.eco.internal.items.modern
package com.willfp.eco.internal.compat.modern.items.parsers

import com.willfp.eco.internal.items.templates.ValueArgParser
import com.willfp.eco.util.StringUtils
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package com.willfp.eco.internal.items.modern
package com.willfp.eco.internal.compat.modern.items.parsers

import com.willfp.eco.internal.items.templates.ValueArgParser
import com.willfp.eco.util.StringUtils
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.minimessage.MiniMessage
import org.bukkit.inventory.meta.Damageable
import org.bukkit.inventory.meta.ItemMeta
import org.checkerframework.checker.units.qual.m

object ArgParserMaxDamage : ValueArgParser<Int>("max_damage") {
override fun parse(arg: String): Int? {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package com.willfp.eco.internal.items.modern
package com.willfp.eco.internal.compat.modern.items.parsers

import com.willfp.eco.internal.items.templates.ValueArgParser
import com.willfp.eco.util.StringUtils
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.minimessage.MiniMessage
import org.bukkit.inventory.meta.Damageable
import org.bukkit.inventory.meta.ItemMeta
import org.checkerframework.checker.units.qual.m

object ArgParserMaxStackSize : ValueArgParser<Int>("max_stack_size") {
override fun parse(arg: String): Int? {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.willfp.eco.internal.items.modern
package com.willfp.eco.internal.compat.modern.items.parsers

import com.willfp.eco.core.items.args.LookupArgParser
import org.bukkit.NamespacedKey
Expand Down
6 changes: 3 additions & 3 deletions eco-core/core-backend/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ dependencies {
implementation("org.reflections:reflections:0.9.12")
implementation("org.objenesis:objenesis:3.2")

compileOnly("io.papermc.paper:paper-api:1.21-R0.1-SNAPSHOT")
compileOnly("io.papermc.paper:paper-api:1.20.2-R0.1-SNAPSHOT")
compileOnly("me.clip:placeholderapi:2.11.4")
compileOnly("net.kyori:adventure-text-minimessage:4.10.0")
compileOnly("net.kyori:adventure-platform-bukkit:4.1.0")
Expand All @@ -16,12 +16,12 @@ dependencies {

tasks {
compileJava {
options.release = 21
options.release = 17
}

compileKotlin {
kotlinOptions {
jvmTarget = "21"
jvmTarget = "17"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.willfp.eco.internal.compat

import com.willfp.eco.core.Prerequisite
import com.willfp.eco.core.proxy.exceptions.ProxyError

private const val BASE_PACKAGE = "com.willfp.eco.internal.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>()

object ModernCompatibilityScope {
inline fun <reified T> loadProxy(): T {
return loadCompatibilityProxy(T::class.java)
}

inline fun <reified T> useProxy(block: T.() -> Any?) {
val proxy = loadProxy<T>()

with(proxy) {
block()
}
}
}

fun <R> ifModern(block: ModernCompatibilityScope.() -> R) {
if (!isModern) {
return
}

block(ModernCompatibilityScope)
}

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 ProxyError(
"Modern compatibility proxy class $className does not implement ${clazz.name}",
ClassCastException()
)
}

return instance
} catch (e: ClassNotFoundException) {
throw ProxyError("Could not find modern compatibility proxy class $className", e)
} catch (e: NoSuchMethodException) {
throw ProxyError("Could not find no-args constructor for modern compatibility proxy class $className", e)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ object EntityArgParserFlySpeed : EntityArgParser {
}
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.willfp.eco.internal.entities

import com.willfp.eco.internal.compat.ModernCompatibilityProxy

@ModernCompatibilityProxy("entities.ModernEntityArgParsersImpl")
interface ModernEntityArgParsers {
fun registerAll()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.willfp.eco.internal.items

import com.willfp.eco.internal.compat.ModernCompatibilityProxy

@ModernCompatibilityProxy("items.ModernItemArgParsersImpl")
interface ModernItemArgParsers {
fun registerAll()
}
Loading

0 comments on commit 52367db

Please sign in to comment.