-
Notifications
You must be signed in to change notification settings - Fork 56
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
19 changed files
with
275 additions
and
42 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,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" | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
.../main/kotlin/com/willfp/eco/internal/compat/modern/entities/ModernEntityArgParsersImpl.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,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) | ||
} | ||
} |
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
45 changes: 45 additions & 0 deletions
45
...ain/kotlin/com/willfp/eco/internal/compat/modern/entities/parsers/EntityArgParserScale.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,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 | ||
} | ||
) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...n/src/main/kotlin/com/willfp/eco/internal/compat/modern/items/ModernItemArgParsersImpl.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,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) | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...al/items/modern/ArgParserFireResistant.kt → ...n/items/parsers/ArgParserFireResistant.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
2 changes: 1 addition & 1 deletion
2
...o/internal/items/modern/ArgParserGlint.kt → ...at/modern/items/parsers/ArgParserGlint.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
2 changes: 1 addition & 1 deletion
2
...nternal/items/modern/ArgParserItemName.kt → ...modern/items/parsers/ArgParserItemName.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
6 changes: 1 addition & 5 deletions
6
...ternal/items/modern/ArgParserMaxDamage.kt → ...odern/items/parsers/ArgParserMaxDamage.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
7 changes: 1 addition & 6 deletions
7
...nal/items/modern/ArgParserMaxStackSize.kt → ...rn/items/parsers/ArgParserMaxStackSize.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
2 changes: 1 addition & 1 deletion
2
...co/internal/items/modern/ArgParserTrim.kt → ...pat/modern/items/parsers/ArgParserTrim.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
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
68 changes: 68 additions & 0 deletions
68
eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/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,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) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -40,4 +40,4 @@ object EntityArgParserFlySpeed : EntityArgParser { | |
} | ||
) | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...e/core-backend/src/main/kotlin/com/willfp/eco/internal/entities/ModernEntityArgParsers.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,8 @@ | ||
package com.willfp.eco.internal.entities | ||
|
||
import com.willfp.eco.internal.compat.ModernCompatibilityProxy | ||
|
||
@ModernCompatibilityProxy("entities.ModernEntityArgParsersImpl") | ||
interface ModernEntityArgParsers { | ||
fun registerAll() | ||
} |
8 changes: 8 additions & 0 deletions
8
eco-core/core-backend/src/main/kotlin/com/willfp/eco/internal/items/ModernItemArgParsers.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,8 @@ | ||
package com.willfp.eco.internal.items | ||
|
||
import com.willfp.eco.internal.compat.ModernCompatibilityProxy | ||
|
||
@ModernCompatibilityProxy("items.ModernItemArgParsersImpl") | ||
interface ModernItemArgParsers { | ||
fun registerAll() | ||
} |
Oops, something went wrong.