diff --git a/project/module-item/build.gradle.kts b/project/module-item/build.gradle.kts index 05035f16..dcfeaa3d 100644 --- a/project/module-item/build.gradle.kts +++ b/project/module-item/build.gradle.kts @@ -11,5 +11,6 @@ dependencies { compileTaboo("module-bukkit-hook") compileModule("runtime-bukkit") compileModule("module-script") + compileModule("module-compat-core") compileOnly(fileTree("libs")) } \ No newline at end of file diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/feature/action/ActionManager.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/feature/action/ActionManager.kt index 5e8429ba..d2ceefe2 100644 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/feature/action/ActionManager.kt +++ b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/feature/action/ActionManager.kt @@ -1,12 +1,11 @@ package cn.fd.ratziel.module.item.impl.feature.action import cn.fd.ratziel.core.Identifier -import cn.fd.ratziel.function.SimpleArgumentContext import cn.fd.ratziel.module.item.api.feature.ItemTrigger import cn.fd.ratziel.module.item.impl.feature.action.triggers.* import cn.fd.ratziel.module.item.impl.service.NativeServiceRegistry import cn.fd.ratziel.script.api.ScriptEnvironment -import cn.fd.ratziel.script.impl.SimpleScriptEnvironment +import cn.fd.ratziel.script.impl.SimpleScriptEnv import java.util.concurrent.ConcurrentHashMap /** @@ -53,7 +52,7 @@ object ActionManager { @JvmStatic fun trigger(identifier: Identifier, trigger: ItemTrigger, function: ScriptEnvironment.() -> Unit) = - trigger(identifier, trigger, SimpleScriptEnvironment().apply(function)) + trigger(identifier, trigger, SimpleScriptEnv().apply(function)) /** * 匹配 [TriggerMap] diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/feature/action/triggers/InteractTrigger.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/feature/action/triggers/InteractTrigger.kt index 9fc51ca8..b81b5bd1 100644 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/feature/action/triggers/InteractTrigger.kt +++ b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/impl/feature/action/triggers/InteractTrigger.kt @@ -2,7 +2,7 @@ package cn.fd.ratziel.module.item.impl.feature.action.triggers import cn.fd.ratziel.module.item.api.feature.ItemTrigger import cn.fd.ratziel.module.item.impl.feature.action.ActionManager -import cn.fd.ratziel.script.impl.SimpleScriptEnvironment +import cn.fd.ratziel.script.impl.SimpleScriptEnv import org.bukkit.event.block.Action.* import org.bukkit.event.player.PlayerInteractEvent import taboolib.common.platform.event.SubscribeEvent @@ -25,7 +25,7 @@ object InteractTrigger { val item = event.item ?: return val neoItem = AttackTrigger.asNeo(item) ?: return // 环境 - val env = SimpleScriptEnvironment().apply { + val env = SimpleScriptEnv().apply { set("event", event) set("item", item) set("neoItem", neoItem) diff --git a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/nms/RefItemMeta.kt b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/nms/RefItemMeta.kt index 17cf0054..a18bd30e 100644 --- a/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/nms/RefItemMeta.kt +++ b/project/module-item/src/main/kotlin/cn/fd/ratziel/module/item/nms/RefItemMeta.kt @@ -97,7 +97,7 @@ class RefItemMeta(raw: T) { internal fun new(tag: NBTCompound): T { val handled = if (MinecraftVersion.majorLegacy >= 12005) - InternalUtil.nbtToDcp(tag.getData()) + NMS12005.INSTANCE.parsePatch(tag.getData())!! else tag return uncheck(constructor.instance(handled)!!) } @@ -135,35 +135,23 @@ class RefItemMeta(raw: T) { obcClass("inventory.CraftMetaItem\$Applicator") } - /** - * NBTTagCompound to DataComponentPatch - */ - fun nbtToDcp(tag: Any): Any = applicatorToDcp(nbtToApplicator(tag)) - fun applicatorToDcp(applicator: Any): Any = applicatorBuildMethod.invoke(applicator)!! - fun nbtToApplicator(nbt: Any): Any = - applicatorPutMethod.invoke( - applicatorConstructor.instance()!!, - customDataKey, - NMS12005.customDataConstructor.instance(nbt) - )!! - val customDataKey by lazy { - META_ITEM.getProperty("CUSTOM_DATA", isStatic = true) + ReflexClass.of(META_ITEM.clazz, false).getField("CUSTOM_DATA") } val applicatorConstructor by lazy { - ReflexClass.of(applicatorClass).getConstructor() + ReflexClass.of(applicatorClass, false).getConstructor() } val applicatorPutMethod by lazy { - ReflexClass.of(applicatorClass).structure.methods.firstOrNull { it.name == "put" } + ReflexClass.of(applicatorClass, false).structure.methods.firstOrNull { it.name == "put" } ?: throw NoSuchMethodException("${applicatorClass.name}#put") } val applicatorBuildMethod by lazy { - ReflexClass.of(applicatorClass).getMethod("build") + ReflexClass.of(applicatorClass, false).getMethod("build") } } diff --git a/project/module-script/src/main/java/cn/fd/ratziel/script/impl/SimpleScriptEnvironment.java b/project/module-script/src/main/java/cn/fd/ratziel/script/impl/SimpleScriptEnv.java similarity index 74% rename from project/module-script/src/main/java/cn/fd/ratziel/script/impl/SimpleScriptEnvironment.java rename to project/module-script/src/main/java/cn/fd/ratziel/script/impl/SimpleScriptEnv.java index 37424b8a..880d5643 100644 --- a/project/module-script/src/main/java/cn/fd/ratziel/script/impl/SimpleScriptEnvironment.java +++ b/project/module-script/src/main/java/cn/fd/ratziel/script/impl/SimpleScriptEnv.java @@ -9,26 +9,26 @@ import javax.script.SimpleBindings; /** - * SimpleScriptEnvironment + * SimpleScriptEnv * * @author TheFloodDragon * @since 2024/7/15 13:41 */ -public class SimpleScriptEnvironment implements ScriptEnvironment { +public class SimpleScriptEnv implements ScriptEnvironment { - public SimpleScriptEnvironment() { + public SimpleScriptEnv() { this(new SimpleBindings()); } - public SimpleScriptEnvironment(@NotNull Bindings bindings) { + public SimpleScriptEnv(@NotNull Bindings bindings) { this(bindings, new SimpleArgumentContext()); } - public SimpleScriptEnvironment(@NotNull ArgumentContext context) { + public SimpleScriptEnv(@NotNull ArgumentContext context) { this(new SimpleBindings(), context); } - public SimpleScriptEnvironment(@NotNull Bindings bindings, @NotNull ArgumentContext context) { + public SimpleScriptEnv(@NotNull Bindings bindings, @NotNull ArgumentContext context) { this.bindings = bindings; this.context = context; } diff --git a/project/module-script/src/main/kotlin/cn/fd/ratziel/script/ScriptManager.kt b/project/module-script/src/main/kotlin/cn/fd/ratziel/script/ScriptManager.kt index 75a119f9..1d9d7ce9 100644 --- a/project/module-script/src/main/kotlin/cn/fd/ratziel/script/ScriptManager.kt +++ b/project/module-script/src/main/kotlin/cn/fd/ratziel/script/ScriptManager.kt @@ -1,6 +1,8 @@ package cn.fd.ratziel.script import cn.fd.ratziel.script.api.ScriptType +import taboolib.common.env.RuntimeEnv +import taboolib.common.platform.Awake /** * ScriptManager @@ -13,4 +15,12 @@ object ScriptManager { var defaultScriptLanguage: ScriptType = ScriptTypes.KETHER internal set + @Awake + private fun initLanguages() { + // Load Env + for (lang in ScriptTypes.entries) { + if (lang.enabled) RuntimeEnv.ENV.loadDependency(lang.executor::class.java) + } + } + } \ No newline at end of file diff --git a/project/module-script/src/main/kotlin/cn/fd/ratziel/script/ScriptTypes.kt b/project/module-script/src/main/kotlin/cn/fd/ratziel/script/ScriptTypes.kt index ce27d7f0..5967fa24 100644 --- a/project/module-script/src/main/kotlin/cn/fd/ratziel/script/ScriptTypes.kt +++ b/project/module-script/src/main/kotlin/cn/fd/ratziel/script/ScriptTypes.kt @@ -3,10 +3,10 @@ package cn.fd.ratziel.script import cn.fd.ratziel.script.api.ScriptEnvironment import cn.fd.ratziel.script.api.ScriptExecutor import cn.fd.ratziel.script.api.ScriptType -import cn.fd.ratziel.script.executors.JavaScriptExecutor -import cn.fd.ratziel.script.executors.JexlExecutor -import cn.fd.ratziel.script.executors.KetherExecutor -import cn.fd.ratziel.script.executors.KotlinScriptExecutor +import cn.fd.ratziel.script.js.JavaScriptExecutor +import cn.fd.ratziel.script.jexl.JexlExecutor +import cn.fd.ratziel.script.kether.KetherExecutor +import cn.fd.ratziel.script.kts.KotlinScriptExecutor import java.util.concurrent.CopyOnWriteArrayList /** diff --git a/project/module-script/src/main/kotlin/cn/fd/ratziel/script/impl/CompilableScriptExecutor.kt b/project/module-script/src/main/kotlin/cn/fd/ratziel/script/api/CompilableScriptExecutor.kt similarity index 81% rename from project/module-script/src/main/kotlin/cn/fd/ratziel/script/impl/CompilableScriptExecutor.kt rename to project/module-script/src/main/kotlin/cn/fd/ratziel/script/api/CompilableScriptExecutor.kt index 69506aca..b251e3ba 100644 --- a/project/module-script/src/main/kotlin/cn/fd/ratziel/script/impl/CompilableScriptExecutor.kt +++ b/project/module-script/src/main/kotlin/cn/fd/ratziel/script/api/CompilableScriptExecutor.kt @@ -1,6 +1,5 @@ -package cn.fd.ratziel.script.impl +package cn.fd.ratziel.script.api -import cn.fd.ratziel.script.api.ScriptExecutor import java.io.Reader import javax.script.Compilable import javax.script.CompiledScript diff --git a/project/module-script/src/main/kotlin/cn/fd/ratziel/script/internal/applier/BasicAppliers.kt b/project/module-script/src/main/kotlin/cn/fd/ratziel/script/internal/applier/BasicAppliers.kt index be8ed106..06787389 100644 --- a/project/module-script/src/main/kotlin/cn/fd/ratziel/script/internal/applier/BasicAppliers.kt +++ b/project/module-script/src/main/kotlin/cn/fd/ratziel/script/internal/applier/BasicAppliers.kt @@ -8,6 +8,8 @@ import taboolib.common.platform.ProxyCommandSender /** * BasicAppliers * + * 依托这设计 + * * @author TheFloodDragon * @since 2024/7/16 12:21 */ @@ -17,6 +19,15 @@ private object BasicAppliers { @Awake fun register() { for (lang in ScriptTypes.entries) { + // Sender from Bindings + lang.appliers.add(ScriptEnvironment.Applier { + val sender = it.context.popOrNull(ProxyCommandSender::class.java) + if (sender == null) { + val newSender = it.bindings["player"] ?: it.bindings["sender"] + if (newSender != null) it.context.add(newSender) + } + }) + // Sender from Context if (lang == ScriptTypes.KETHER) { lang.appliers.add(fetch { bindings["@Sender"] = it }) } else lang.appliers.add(fetch { bindings["sender"] = it }) diff --git a/project/module-script/src/main/kotlin/cn/fd/ratziel/script/executors/JexlExecutor.kt b/project/module-script/src/main/kotlin/cn/fd/ratziel/script/jexl/JexlExecutor.kt similarity index 86% rename from project/module-script/src/main/kotlin/cn/fd/ratziel/script/executors/JexlExecutor.kt rename to project/module-script/src/main/kotlin/cn/fd/ratziel/script/jexl/JexlExecutor.kt index 4422a9c7..fea51f2d 100644 --- a/project/module-script/src/main/kotlin/cn/fd/ratziel/script/executors/JexlExecutor.kt +++ b/project/module-script/src/main/kotlin/cn/fd/ratziel/script/jexl/JexlExecutor.kt @@ -1,9 +1,10 @@ -package cn.fd.ratziel.script.executors +package cn.fd.ratziel.script.jexl +import cn.fd.ratziel.script.api.CompilableScriptExecutor import cn.fd.ratziel.script.api.ScriptContent import cn.fd.ratziel.script.api.ScriptEnvironment -import cn.fd.ratziel.script.impl.CompilableScriptExecutor import taboolib.common.env.RuntimeDependency +import taboolib.common.platform.Ghost import javax.script.Compilable import javax.script.CompiledScript import javax.script.ScriptEngine @@ -15,6 +16,7 @@ import javax.script.ScriptEngineManager * @author TheFloodDragon * @since 2024/7/14 21:41 */ +@Ghost // 避免依赖注入, 主动触发 @RuntimeDependency( value = "!org.apache.commons:commons-jexl3:3.4.0", test = "!org.apache.commons.jexl3.JexlEngine", diff --git a/project/module-script/src/main/kotlin/cn/fd/ratziel/script/executors/JavaScriptExecutor.kt b/project/module-script/src/main/kotlin/cn/fd/ratziel/script/js/JavaScriptExecutor.kt similarity index 86% rename from project/module-script/src/main/kotlin/cn/fd/ratziel/script/executors/JavaScriptExecutor.kt rename to project/module-script/src/main/kotlin/cn/fd/ratziel/script/js/JavaScriptExecutor.kt index a0a70505..44ac09d5 100644 --- a/project/module-script/src/main/kotlin/cn/fd/ratziel/script/executors/JavaScriptExecutor.kt +++ b/project/module-script/src/main/kotlin/cn/fd/ratziel/script/js/JavaScriptExecutor.kt @@ -1,9 +1,10 @@ -package cn.fd.ratziel.script.executors +package cn.fd.ratziel.script.js +import cn.fd.ratziel.script.api.CompilableScriptExecutor import cn.fd.ratziel.script.api.ScriptContent import cn.fd.ratziel.script.api.ScriptEnvironment -import cn.fd.ratziel.script.impl.CompilableScriptExecutor import taboolib.common.env.RuntimeDependency +import taboolib.common.platform.Ghost import javax.script.Compilable import javax.script.CompiledScript import javax.script.ScriptEngine @@ -15,6 +16,7 @@ import javax.script.ScriptEngineManager * @author TheFloodDragon * @since 2024/7/14 21:40 */ +@Ghost // 避免依赖注入, 主动触发 @RuntimeDependency( value = "!org.openjdk.nashorn:nashorn-core:15.4", test = "!org.openjdk.nashorn.api.scripting.NashornScriptEngine" diff --git a/project/module-script/src/main/kotlin/cn/fd/ratziel/script/executors/KetherExecutor.kt b/project/module-script/src/main/kotlin/cn/fd/ratziel/script/kether/KetherExecutor.kt similarity index 93% rename from project/module-script/src/main/kotlin/cn/fd/ratziel/script/executors/KetherExecutor.kt rename to project/module-script/src/main/kotlin/cn/fd/ratziel/script/kether/KetherExecutor.kt index f7327ebe..f232231d 100644 --- a/project/module-script/src/main/kotlin/cn/fd/ratziel/script/executors/KetherExecutor.kt +++ b/project/module-script/src/main/kotlin/cn/fd/ratziel/script/kether/KetherExecutor.kt @@ -1,4 +1,4 @@ -package cn.fd.ratziel.script.executors +package cn.fd.ratziel.script.kether import cn.fd.ratziel.script.api.ScriptContent import cn.fd.ratziel.script.api.ScriptEnvironment diff --git a/project/module-script/src/main/kotlin/cn/fd/ratziel/script/executors/KotlinScriptExecutor.kt b/project/module-script/src/main/kotlin/cn/fd/ratziel/script/kts/KotlinScriptExecutor.kt similarity index 93% rename from project/module-script/src/main/kotlin/cn/fd/ratziel/script/executors/KotlinScriptExecutor.kt rename to project/module-script/src/main/kotlin/cn/fd/ratziel/script/kts/KotlinScriptExecutor.kt index b69277ad..e449550d 100644 --- a/project/module-script/src/main/kotlin/cn/fd/ratziel/script/executors/KotlinScriptExecutor.kt +++ b/project/module-script/src/main/kotlin/cn/fd/ratziel/script/kts/KotlinScriptExecutor.kt @@ -1,11 +1,12 @@ -package cn.fd.ratziel.script.executors +package cn.fd.ratziel.script.kts import cn.fd.ratziel.core.env.CoreEnv +import cn.fd.ratziel.script.api.CompilableScriptExecutor import cn.fd.ratziel.script.api.ScriptContent import cn.fd.ratziel.script.api.ScriptEnvironment -import cn.fd.ratziel.script.impl.CompilableScriptExecutor import taboolib.common.env.RuntimeDependencies import taboolib.common.env.RuntimeDependency +import taboolib.common.platform.Ghost import javax.script.Compilable import javax.script.CompiledScript import javax.script.ScriptEngine @@ -17,6 +18,7 @@ import javax.script.ScriptEngineManager * @author TheFloodDragon * @since 2024/7/14 21:41 */ +@Ghost // 避免依赖注入, 主动触发 @RuntimeDependencies( RuntimeDependency( value = "!org.jetbrains.kotlin:kotlin-reflect:" + CoreEnv.KOTLIN_VERSION, diff --git a/project/runtime-bukkit/src/main/kotlin/cn/fd/ratziel/bukkit/element/action/ActionElement.kt b/project/runtime-bukkit/src/main/kotlin/cn/fd/ratziel/bukkit/element/action/ActionElement.kt index 65cb890a..83c3d7a3 100644 --- a/project/runtime-bukkit/src/main/kotlin/cn/fd/ratziel/bukkit/element/action/ActionElement.kt +++ b/project/runtime-bukkit/src/main/kotlin/cn/fd/ratziel/bukkit/element/action/ActionElement.kt @@ -7,7 +7,7 @@ import cn.fd.ratziel.core.element.api.ElementHandler import cn.fd.ratziel.core.serialization.toBasic import cn.fd.ratziel.script.ScriptBlockBuilder import cn.fd.ratziel.script.ScriptManager -import cn.fd.ratziel.script.impl.SimpleScriptEnvironment +import cn.fd.ratziel.script.impl.SimpleScriptEnv import taboolib.common.LifeCycle /** @@ -27,7 +27,7 @@ object ActionElement : ElementHandler { element.property.let { json -> val block = ScriptBlockBuilder.build(json.toBasic(), ScriptManager.defaultScriptLanguage.executor) println(block) - val result = block.evaluate(SimpleScriptEnvironment()) + val result = block.evaluate(SimpleScriptEnv()) println(result) } }