From cd2e29806872dc4568496f32e516332f57d6a55c Mon Sep 17 00:00:00 2001 From: cao-awa Date: Thu, 28 Nov 2024 19:49:36 +0800 Subject: [PATCH] Let data driven could interaction to kotlin script. Add unnamed event context used to attach present contexts. Load script before all data-driven data. --- .../datapacks/tests/data/awa/item/conium.json | 5 +- .../tests/data/awa/script/interaction.kts | 8 + .../datapack/recipe/ConiumRecipeManager.kt | 44 +- .../datapack/script/ConiumScriptManager.kt | 32 +- .../event/context/ConiumEventContext.kt | 24 + .../context/ConiumEventContextBuilder.kt | 419 ++++++++++++++++++ .../template/glint/ConiumGlintTemplate.kt | 30 +- .../item/ConiumItemStackKotlinExtents.kt | 6 + .../mixin/datapack/DataPackContentsMixin.java | 2 +- .../conium/parameter/DynamicArgsLifecycle.kt | 3 +- .../interaction/NamedInteractionScript.kt | 6 + .../assets/conium/scripts/conium.commons.kts | 1 + 12 files changed, 547 insertions(+), 33 deletions(-) create mode 100644 sample/datapacks/tests/data/awa/script/interaction.kts create mode 100644 src/main/java/com/github/cao/awa/conium/kotlin/extent/item/ConiumItemStackKotlinExtents.kt create mode 100644 src/main/java/com/github/cao/awa/conium/script/interaction/NamedInteractionScript.kt diff --git a/sample/datapacks/tests/data/awa/item/conium.json b/sample/datapacks/tests/data/awa/item/conium.json index f4f7957..26f4dca 100644 --- a/sample/datapacks/tests/data/awa/item/conium.json +++ b/sample/datapacks/tests/data/awa/item/conium.json @@ -39,7 +39,10 @@ "rarity": "epic", "use_action": "eat", "fuel": 60, - "glint": true, + "glint": { + "script": "awa:test_glint", + "default": true + }, "armor": { "slot": "chest", "defense": 10, diff --git a/sample/datapacks/tests/data/awa/script/interaction.kts b/sample/datapacks/tests/data/awa/script/interaction.kts new file mode 100644 index 0000000..a0fe343 --- /dev/null +++ b/sample/datapacks/tests/data/awa/script/interaction.kts @@ -0,0 +1,8 @@ +export( + "awa:test_glint", + false, + ITEM_USED_ON_BLOCK, + ITEM_USAGE_CONTEXT +) { item, context -> + return@export (context.player?.isCreative ?: false) +} \ No newline at end of file diff --git a/src/main/java/com/github/cao/awa/conium/datapack/recipe/ConiumRecipeManager.kt b/src/main/java/com/github/cao/awa/conium/datapack/recipe/ConiumRecipeManager.kt index ac2ec79..8612f40 100644 --- a/src/main/java/com/github/cao/awa/conium/datapack/recipe/ConiumRecipeManager.kt +++ b/src/main/java/com/github/cao/awa/conium/datapack/recipe/ConiumRecipeManager.kt @@ -66,19 +66,16 @@ class ConiumRecipeManager(private val registries: WrapperLookup) : ServerRecipeM ) ) - private fun cookingIngredientGetter(expectedType: RecipeType): SoleIngredientGetter { - return SoleIngredientGetter { - if (it.type == expectedType && it is SingleStackRecipe) { - Optional.of(it.ingredient()) - } else { - Optional.empty() - } + private fun cookingIngredientGetter(expectedType: RecipeType): SoleIngredientGetter = SoleIngredientGetter { + if (it.type == expectedType && it is SingleStackRecipe) { + Optional.of(it.ingredient()) + } else { + Optional.empty() } } - fun filterIngredients(features: FeatureSet, ingredients: MutableList): List { - ingredients.removeIf { ingredient -> !isEnabled(features, ingredient) } - return ingredients + fun filterIngredients(features: FeatureSet, ingredients: MutableList): List = ingredients.apply { + removeIf { ingredient: Ingredient -> !isEnabled(features, ingredient) } } private fun isEnabled(features: FeatureSet, ingredient: Ingredient): Boolean { @@ -86,15 +83,11 @@ class ConiumRecipeManager(private val registries: WrapperLookup) : ServerRecipeM } open class PropertySetBuilder(val propertySetKey: RegistryKey, private val ingredientGetter: SoleIngredientGetter) : Consumer> { - private val ingredients: MutableList = ArrayList() + private val ingredients: MutableList = CollectionFactor.arrayList() - override fun accept(recipe: Recipe<*>) { - this.ingredientGetter.apply(recipe).ifPresent(this.ingredients::add) - } + override fun accept(recipe: Recipe<*>) = this.ingredientGetter.apply(recipe).ifPresent(this.ingredients::add) - fun build(enabledFeatures: FeatureSet): RecipePropertySet { - return RecipePropertySetAccessor.of(filterIngredients(enabledFeatures, this.ingredients)) - } + fun build(enabledFeatures: FeatureSet): RecipePropertySet = RecipePropertySetAccessor.of(filterIngredients(enabledFeatures, this.ingredients)) } } @@ -108,8 +101,8 @@ class ConiumRecipeManager(private val registries: WrapperLookup) : ServerRecipeM val sortedMap: SortedMap> = TreeMap() load(resourceManager, RegistryKeys.getPath(RegistryKeys.RECIPE), sortedMap) val list: MutableList> = ArrayList(sortedMap.size) - sortedMap.forEach { (id, recipe) -> - val registryKey = RegistryKey.of(RegistryKeys.RECIPE, id) + sortedMap.forEach { (id: Identifier, recipe: Recipe<*>) -> + val registryKey: RegistryKey> = RegistryKey.of(RegistryKeys.RECIPE, id) val recipeEntry: RecipeEntry<*> = RecipeEntry(registryKey, recipe) list.add(recipeEntry) } @@ -232,15 +225,18 @@ class ConiumRecipeManager(private val registries: WrapperLookup) : ServerRecipeM override fun > getFirstMatch( type: RecipeType, input: I, world: World, recipe: RegistryKey>? - ): Optional> { - val recipeEntry: RecipeEntry? = if (recipe != null) this.get(type, recipe) else null - return this.getFirstMatch(type, input, world, recipeEntry) - } + ): Optional> = getFirstMatch( + type, + input, + world, + if (recipe != null) get(type, recipe) else null + ) + override fun > getFirstMatch( type: RecipeType, input: I, world: World, recipe: RecipeEntry? ): Optional> { - return if (recipe != null && recipe.value()!!.matches(input, world)) Optional.of(recipe) else this.getFirstMatch(type, input, world) + return if (recipe != null && recipe.value()!!.matches(input, world)) Optional.of(recipe) else getFirstMatch(type, input, world) } /** diff --git a/src/main/java/com/github/cao/awa/conium/datapack/script/ConiumScriptManager.kt b/src/main/java/com/github/cao/awa/conium/datapack/script/ConiumScriptManager.kt index 5ea3822..4c4a627 100644 --- a/src/main/java/com/github/cao/awa/conium/datapack/script/ConiumScriptManager.kt +++ b/src/main/java/com/github/cao/awa/conium/datapack/script/ConiumScriptManager.kt @@ -2,9 +2,11 @@ package com.github.cao.awa.conium.datapack.script import com.github.cao.awa.conium.Conium import com.github.cao.awa.conium.event.ConiumEvent +import com.github.cao.awa.conium.event.context.ConiumEventContext import com.github.cao.awa.conium.registry.ConiumRegistryKeys import com.github.cao.awa.conium.script.ScriptExport import com.github.cao.awa.conium.script.eval.ScriptEval +import com.github.cao.awa.conium.script.interaction.NamedInteractionScript import com.github.cao.awa.conium.script.kts.ConiumScript import com.github.cao.awa.language.translator.builtin.typescript.antlr.TypescriptLexer import com.github.cao.awa.language.translator.builtin.typescript.antlr.TypescriptParser @@ -72,6 +74,13 @@ class ConiumScriptManager : SinglePreparationResourceReloader = CollectionFactor.hashMap() + /** + * The 'exportedInteraction' is conditions or actions that referenced in data-driven. + * + * The data-driven framework will acquire and invoke the ParameterSelective to dynamically produce runtime variables. + */ + private val exportedInteraction: MutableMap> = CollectionFactor.hashMap() + /** * Prepares the intermediate object. * @@ -86,10 +95,25 @@ class ConiumScriptManager : SinglePreparationResourceReloader = - CollectionFactor.hashMap().also { - load(manager, it) - } + override fun prepare(manager: ResourceManager, profiler: Profiler): MutableMap = CollectionFactor.hashMap().also { + load(manager, it) + } + + fun export(name: String, context: ConiumEventContext<*>, result: (Any) -> Any) { + this.exportedInteraction[name] = NamedInteractionScript( + name, + context, + result + ) + } + + fun acquire(name: String): ConiumEventContext<*> { + return this.exportedInteraction[name]!!.context + } + + fun acquireResult(name: String): ((Any) -> Any?) { + return this.exportedInteraction[name]!!.result + } /** * Prepares the intermediate object. diff --git a/src/main/java/com/github/cao/awa/conium/event/context/ConiumEventContext.kt b/src/main/java/com/github/cao/awa/conium/event/context/ConiumEventContext.kt index e2d0472..29a0a08 100644 --- a/src/main/java/com/github/cao/awa/conium/event/context/ConiumEventContext.kt +++ b/src/main/java/com/github/cao/awa/conium/event/context/ConiumEventContext.kt @@ -34,6 +34,8 @@ class ConiumEventContext

( private val args: MutableMap, Any?> = CollectionFactor.hashMap() private val attaches: MutableList> = CollectionFactor.arrayList() + private val attachesDynamic: MutableList

= CollectionFactor.arrayList() + private var lastIdentity: Any? = null private var targetedIdentity: ParameterSelective1 = ParameterSelective1 { true } @@ -82,6 +84,16 @@ class ConiumEventContext

( return this } + fun attachDynamic(dynamicArgs: P): ConiumEventContext

{ + this.attachesDynamic.add(dynamicArgs) + return this + } + + fun attachDynamic(dynamicArgs: MutableList

): ConiumEventContext

{ + this.attachesDynamic.addAll(dynamicArgs); + return this + } + fun attach(context: ConiumEventContext<*>): ConiumEventContext

{ this.attaches.add(context) return this @@ -100,6 +112,8 @@ class ConiumEventContext

( return true } + this.lastIdentity = identity + var success: Boolean = this.presageTrigger == null || this.dynamicArgs.arising(identity, this.args, this.presageTrigger!!) for (attach: ConiumEventContext<*> in this.attaches) { if (attach.hasPresaging()) { @@ -118,6 +132,8 @@ class ConiumEventContext

( return true } + this.lastIdentity = identity + var success: Boolean = this.ariseTrigger == null || this.dynamicArgs.arising(identity, this.args, this.ariseTrigger!!) for (attach: ConiumEventContext<*> in this.attaches) { if (attach.hasArising()) { @@ -125,10 +141,18 @@ class ConiumEventContext

( success = attach.arising(identity) && success } } + for (attachDynamic: P in this.attachesDynamic) { + this.dynamicArgs.arising( + identity, + this.args, + attachDynamic + ) + } return success } fun inherit(context: ConiumEventContext<*>): ConiumEventContext

{ + this.lastIdentity = context.lastIdentity return resetArgs(context.args) } } diff --git a/src/main/java/com/github/cao/awa/conium/event/context/ConiumEventContextBuilder.kt b/src/main/java/com/github/cao/awa/conium/event/context/ConiumEventContextBuilder.kt index 27d360e..f958988 100644 --- a/src/main/java/com/github/cao/awa/conium/event/context/ConiumEventContextBuilder.kt +++ b/src/main/java/com/github/cao/awa/conium/event/context/ConiumEventContextBuilder.kt @@ -2,6 +2,7 @@ package com.github.cao.awa.conium.event.context +import com.github.cao.awa.conium.Conium import com.github.cao.awa.conium.event.ConiumEvent import com.github.cao.awa.conium.event.type.ConiumEventType import com.github.cao.awa.conium.parameter.* @@ -795,4 +796,422 @@ object ConiumEventContextBuilder { return context } + + @JvmStatic + @SuppressWarnings("UNCHECKED_CAST") + fun export( + name: String, + defaultResult: R, + eventType: ConiumEventType, + arising: ParameterSelective1 + ): ConiumEventContext> { + val context = ConiumEventContext( + DynamicArgsBuilder.requires(true).lifecycle(DynamicArgsLifecycle.FOREVER) + ) + + ConiumEvent.forever(eventType, context) + + var result: R = defaultResult + + Conium.scriptManager!!.export(name, context) { result } + + context.arise { i: Any -> + result = (i as? I)?.let { + arising(i) + } ?: defaultResult + true + } + + return context + } + + + @JvmStatic + @SuppressWarnings("UNCHECKED_CAST") + fun export( + name: String, + defaultResult: R, + eventType: ConiumEventType, + arg1: DynamicArgType, + arising: ParameterSelective2 + ): ConiumEventContext> { + val context = ConiumEventContext( + DynamicArgsBuilder.requires(arg1, true).lifecycle(DynamicArgsLifecycle.FOREVER) + ) + + ConiumEvent.forever(eventType, context) + + var result: R = defaultResult + + Conium.scriptManager!!.export(name, context) { result } + + context.arise { i: Any, p1: P1 -> + result = (i as? I)?.let { + arising(i, p1) + } ?: defaultResult + true + } + + return context + } + + @JvmStatic + @SuppressWarnings("UNCHECKED_CAST") + fun export( + name: String, + defaultResult: R, + eventType: ConiumEventType, + arg1: DynamicArgType, + arg2: DynamicArgType, + arising: ParameterSelective3 + ): ConiumEventContext> { + val context = ConiumEventContext( + DynamicArgsBuilder.requires(arg1, arg2, true).lifecycle(DynamicArgsLifecycle.FOREVER) + ) + + ConiumEvent.forever(eventType, context) + + var result: R = defaultResult + + Conium.scriptManager!!.export(name, context) { result } + + context.arise { i: Any, p1: P1, p2: P2 -> + result = (i as? I)?.let { + arising(i, p1, p2) + } ?: defaultResult + true + } + + return context + } + + @JvmStatic + @SuppressWarnings("UNCHECKED_CAST") + fun export( + name: String, + defaultResult: R, + eventType: ConiumEventType, + arg1: DynamicArgType, + arg2: DynamicArgType, + arg3: DynamicArgType, + arising: ParameterSelective4 + ): ConiumEventContext> { + val context = ConiumEventContext( + DynamicArgsBuilder.requires(arg1, arg2, arg3, true).lifecycle(DynamicArgsLifecycle.FOREVER) + ) + + ConiumEvent.forever(eventType, context) + + var result: R = defaultResult + + Conium.scriptManager!!.export(name, context) { result } + + context.arise { i: Any, p1: P1, p2: P2, p3: P3 -> + result = (i as? I)?.let { + arising(i, p1, p2, p3) + } ?: defaultResult + true + } + + return context + } + + @JvmStatic + @SuppressWarnings("UNCHECKED_CAST") + fun export( + name: String, + defaultResult: R, + eventType: ConiumEventType, + arg1: DynamicArgType, + arg2: DynamicArgType, + arg3: DynamicArgType, + arg4: DynamicArgType, + arising: ParameterSelective5 + ): ConiumEventContext> { + val context = ConiumEventContext( + DynamicArgsBuilder.requires(arg1, arg2, arg3, arg4, true).lifecycle(DynamicArgsLifecycle.FOREVER) + ) + + ConiumEvent.forever(eventType, context) + + var result: R = defaultResult + + Conium.scriptManager!!.export(name, context) { result } + + context.arise { i: Any, p1: P1, p2: P2, p3: P3, p4: P4 -> + result = (i as? I)?.let { + arising(i, p1, p2, p3, p4) + } ?: defaultResult + true + } + + return context + } + + @JvmStatic + @SuppressWarnings("UNCHECKED_CAST") + fun export( + name: String, + defaultResult: R, + eventType: ConiumEventType, + arg1: DynamicArgType, + arg2: DynamicArgType, + arg3: DynamicArgType, + arg4: DynamicArgType, + arg5: DynamicArgType, + arising: ParameterSelective6 + ): ConiumEventContext> { + val context = ConiumEventContext( + DynamicArgsBuilder.requires(arg1, arg2, arg3, arg4, arg5, true).lifecycle(DynamicArgsLifecycle.FOREVER) + ) + + ConiumEvent.forever(eventType, context) + + var result: R = defaultResult + + Conium.scriptManager!!.export(name, context) { result } + + context.arise { i: Any, p1: P1, p2: P2, p3: P3, p4: P4, p5: P5 -> + result = (i as? I)?.let { + arising(i, p1, p2, p3, p4, p5) + } ?: defaultResult + true + } + + return context + } + + @JvmStatic + @SuppressWarnings("UNCHECKED_CAST") + fun export( + name: String, + defaultResult: R, + eventType: ConiumEventType, + arg1: DynamicArgType, + arg2: DynamicArgType, + arg3: DynamicArgType, + arg4: DynamicArgType, + arg5: DynamicArgType, + arg6: DynamicArgType, + arising: ParameterSelective7 + ): ConiumEventContext> { + val context = ConiumEventContext( + DynamicArgsBuilder.requires(arg1, arg2, arg3, arg4, arg5, arg6, true).lifecycle(DynamicArgsLifecycle.FOREVER) + ) + + ConiumEvent.forever(eventType, context) + + var result: R = defaultResult + + Conium.scriptManager!!.export(name, context) { result } + + context.arise { i: Any, p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6 -> + result = (i as? I)?.let { + arising(i, p1, p2, p3, p4, p5, p6) + } ?: defaultResult + true + } + + return context + } + + @JvmStatic + @SuppressWarnings("UNCHECKED_CAST") + fun export( + name: String, + defaultResult: R, + eventType: ConiumEventType, + arg1: DynamicArgType, + arg2: DynamicArgType, + arg3: DynamicArgType, + arg4: DynamicArgType, + arg5: DynamicArgType, + arg6: DynamicArgType, + arg7: DynamicArgType, + arising: ParameterSelective8 + ): ConiumEventContext> { + val context = ConiumEventContext( + DynamicArgsBuilder.requires(arg1, arg2, arg3, arg4, arg5, arg6, arg7, true).lifecycle(DynamicArgsLifecycle.FOREVER) + ) + + ConiumEvent.forever(eventType, context) + + var result: R = defaultResult + + Conium.scriptManager!!.export(name, context) { result } + + context.arise { i: Any, p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7 -> + result = (i as? I)?.let { + arising(i, p1, p2, p3, p4, p5, p6, p7) + } ?: defaultResult + true + } + + return context + } + + @JvmStatic + @SuppressWarnings("UNCHECKED_CAST") + fun unnamed( + arising: ParameterSelective1 + ): ConiumEventContext> { + val context = ConiumEventContext( + DynamicArgsBuilder.requires(true).lifecycle(DynamicArgsLifecycle.UNNAMED) + ) + + context.arise { i: Any -> + i.let(arising::arise) + true + } + + return context + } + + + @JvmStatic + @SuppressWarnings("UNCHECKED_CAST") + fun unnamed( + arg1: DynamicArgType, + arising: ParameterSelective2 + ): ConiumEventContext> { + val context = ConiumEventContext( + DynamicArgsBuilder.requires(arg1, true).lifecycle(DynamicArgsLifecycle.UNNAMED) + ) + + context.arise { i: Any, p1: P1 -> + arising(i, p1) + true + } + + return context + } + + @JvmStatic + @SuppressWarnings("UNCHECKED_CAST") + fun unnamed( + arg1: DynamicArgType, + arg2: DynamicArgType, + arising: ParameterSelective3 + ): ConiumEventContext> { + val context = ConiumEventContext( + DynamicArgsBuilder.requires(arg1, arg2, true).lifecycle(DynamicArgsLifecycle.UNNAMED) + ) + + context.arise { i: Any, p1: P1, p2: P2 -> + arising(i, p1, p2) + true + } + + return context + } + + @JvmStatic + @SuppressWarnings("UNCHECKED_CAST") + fun unnamed( + arg1: DynamicArgType, + arg2: DynamicArgType, + arg3: DynamicArgType, + arising: ParameterSelective4 + ): ConiumEventContext> { + val context = ConiumEventContext( + DynamicArgsBuilder.requires(arg1, arg2, arg3, true).lifecycle(DynamicArgsLifecycle.UNNAMED) + ) + + context.arise { i: Any, p1: P1, p2: P2, p3: P3 -> + arising(i, p1, p2, p3) + true + } + + return context + } + + @JvmStatic + @SuppressWarnings("UNCHECKED_CAST") + fun unnamed( + arg1: DynamicArgType, + arg2: DynamicArgType, + arg3: DynamicArgType, + arg4: DynamicArgType, + arising: ParameterSelective5 + ): ConiumEventContext> { + val context = ConiumEventContext( + DynamicArgsBuilder.requires(arg1, arg2, arg3, arg4, true).lifecycle(DynamicArgsLifecycle.UNNAMED) + ) + + context.arise { i: Any, p1: P1, p2: P2, p3: P3, p4: P4 -> + arising(i, p1, p2, p3, p4) + true + } + + return context + } + + @JvmStatic + @SuppressWarnings("UNCHECKED_CAST") + fun unnamed( + arg1: DynamicArgType, + arg2: DynamicArgType, + arg3: DynamicArgType, + arg4: DynamicArgType, + arg5: DynamicArgType, + arising: ParameterSelective6 + ): ConiumEventContext> { + val context = ConiumEventContext( + DynamicArgsBuilder.requires(arg1, arg2, arg3, arg4, arg5, true).lifecycle(DynamicArgsLifecycle.UNNAMED) + ) + + context.arise { i: Any, p1: P1, p2: P2, p3: P3, p4: P4, p5: P5 -> + arising(i, p1, p2, p3, p4, p5) + true + } + + return context + } + + @JvmStatic + @SuppressWarnings("UNCHECKED_CAST") + fun unnamed( + arg1: DynamicArgType, + arg2: DynamicArgType, + arg3: DynamicArgType, + arg4: DynamicArgType, + arg5: DynamicArgType, + arg6: DynamicArgType, + arising: ParameterSelective7 + ): ConiumEventContext> { + val context = ConiumEventContext( + DynamicArgsBuilder.requires(arg1, arg2, arg3, arg4, arg5, arg6, true).lifecycle(DynamicArgsLifecycle.UNNAMED) + ) + + context.arise { i: Any, p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6 -> + arising(i, p1, p2, p3, p4, p5, p6) + true + } + + return context + } + + @JvmStatic + @SuppressWarnings("UNCHECKED_CAST") + fun unnamed( + arg1: DynamicArgType, + arg2: DynamicArgType, + arg3: DynamicArgType, + arg4: DynamicArgType, + arg5: DynamicArgType, + arg6: DynamicArgType, + arg7: DynamicArgType, + arising: ParameterSelective8 + ): ConiumEventContext> { + val context = ConiumEventContext( + DynamicArgsBuilder.requires(arg1, arg2, arg3, arg4, arg5, arg6, arg7, true).lifecycle(DynamicArgsLifecycle.UNNAMED) + ) + + context.arise { i: Any, p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7 -> + arising(i, p1, p2, p3, p4, p5, p6, p7) + true + } + + return context + } } diff --git a/src/main/java/com/github/cao/awa/conium/item/template/glint/ConiumGlintTemplate.kt b/src/main/java/com/github/cao/awa/conium/item/template/glint/ConiumGlintTemplate.kt index c6607bb..1923f57 100644 --- a/src/main/java/com/github/cao/awa/conium/item/template/glint/ConiumGlintTemplate.kt +++ b/src/main/java/com/github/cao/awa/conium/item/template/glint/ConiumGlintTemplate.kt @@ -1,16 +1,42 @@ package com.github.cao.awa.conium.item.template.glint +import com.github.cao.awa.conium.Conium +import com.github.cao.awa.conium.event.context.ConiumEventContext +import com.github.cao.awa.conium.event.context.ConiumEventContextBuilder +import com.github.cao.awa.conium.event.type.ConiumEventArgTypes import com.github.cao.awa.conium.item.template.ConiumItemTemplate +import com.github.cao.awa.conium.kotlin.extent.item.mergedComponents +import com.github.cao.awa.conium.kotlin.extent.json.ifJsonObject +import com.github.cao.awa.conium.parameter.ParameterSelective2 import com.github.cao.awa.conium.template.ConiumTemplates.Item.GLINT import com.google.gson.JsonElement +import com.google.gson.JsonObject import net.minecraft.component.DataComponentTypes import net.minecraft.item.Item +import net.minecraft.item.ItemStack import net.minecraft.registry.RegistryWrapper.WrapperLookup -class ConiumGlintTemplate(private val glint: Boolean) : ConiumItemTemplate(name = GLINT) { +class ConiumGlintTemplate(private val glint: Boolean, private val script: (ItemStack) -> Unit = { }) : ConiumItemTemplate(name = GLINT) { companion object { @JvmStatic - fun create(element: JsonElement, registryLookup: WrapperLookup): ConiumGlintTemplate = ConiumGlintTemplate(element.asBoolean) + fun create(element: JsonElement, registryLookup: WrapperLookup): ConiumGlintTemplate = element.ifJsonObject({ json: JsonObject -> + val glint: Boolean = json["default"].asBoolean + + val changer: (Any) -> Any? = Conium.scriptManager!!.acquireResult(json["script"].asString) + + val dynamicGlint: ConiumEventContext> = ConiumEventContextBuilder.unnamed( + ConiumEventArgTypes.ITEM_STACK + ) { identity: Any, stack: ItemStack -> + println("Changing item stack: $stack") + stack.mergedComponents[DataComponentTypes.ENCHANTMENT_GLINT_OVERRIDE] = changer(identity) as Boolean + } + + Conium.scriptManager!!.acquire(json["script"].asString).attach(dynamicGlint) + + ConiumGlintTemplate(glint) + }) { + ConiumGlintTemplate(it.asBoolean) + }!! } override fun settings(settings: Item.Settings) { diff --git a/src/main/java/com/github/cao/awa/conium/kotlin/extent/item/ConiumItemStackKotlinExtents.kt b/src/main/java/com/github/cao/awa/conium/kotlin/extent/item/ConiumItemStackKotlinExtents.kt new file mode 100644 index 0000000..85ce720 --- /dev/null +++ b/src/main/java/com/github/cao/awa/conium/kotlin/extent/item/ConiumItemStackKotlinExtents.kt @@ -0,0 +1,6 @@ +package com.github.cao.awa.conium.kotlin.extent.item + +import net.minecraft.component.MergedComponentMap +import net.minecraft.item.ItemStack + +val ItemStack.mergedComponents: MergedComponentMap get() = this.components as MergedComponentMap \ No newline at end of file diff --git a/src/main/java/com/github/cao/awa/conium/mixin/datapack/DataPackContentsMixin.java b/src/main/java/com/github/cao/awa/conium/mixin/datapack/DataPackContentsMixin.java index 49d03a0..e3e3f26 100644 --- a/src/main/java/com/github/cao/awa/conium/mixin/datapack/DataPackContentsMixin.java +++ b/src/main/java/com/github/cao/awa/conium/mixin/datapack/DataPackContentsMixin.java @@ -118,11 +118,11 @@ public ServerRecipeManager delegateRecipes(RegistryWrapper.WrapperLookup registr ) public void contents(CallbackInfoReturnable> cir) { List reloaderList = CollectionFactor.arrayList(cir.getReturnValue()); + reloaderList.add(this.scriptManager); reloaderList.add(this.itemPropertyInjectManager); reloaderList.add(this.coniumItemManager); reloaderList.add(this.coniumBlockManager); reloaderList.add(this.coniumEntityManager); - reloaderList.add(this.scriptManager); cir.setReturnValue(reloaderList); } } diff --git a/src/main/java/com/github/cao/awa/conium/parameter/DynamicArgsLifecycle.kt b/src/main/java/com/github/cao/awa/conium/parameter/DynamicArgsLifecycle.kt index 99998c2..78ca033 100644 --- a/src/main/java/com/github/cao/awa/conium/parameter/DynamicArgsLifecycle.kt +++ b/src/main/java/com/github/cao/awa/conium/parameter/DynamicArgsLifecycle.kt @@ -3,5 +3,6 @@ package com.github.cao.awa.conium.parameter enum class DynamicArgsLifecycle { ONCE, TRANSFORM, - FOREVER; + FOREVER, + UNNAMED; } diff --git a/src/main/java/com/github/cao/awa/conium/script/interaction/NamedInteractionScript.kt b/src/main/java/com/github/cao/awa/conium/script/interaction/NamedInteractionScript.kt new file mode 100644 index 0000000..4cec4c3 --- /dev/null +++ b/src/main/java/com/github/cao/awa/conium/script/interaction/NamedInteractionScript.kt @@ -0,0 +1,6 @@ +package com.github.cao.awa.conium.script.interaction + +import com.github.cao.awa.conium.event.context.ConiumEventContext + +@JvmRecord +data class NamedInteractionScript(val name: String, val context: ConiumEventContext<*>, val result: (Any) -> R) \ No newline at end of file diff --git a/src/main/resources/assets/conium/scripts/conium.commons.kts b/src/main/resources/assets/conium/scripts/conium.commons.kts index d389eed..2ff8004 100644 --- a/src/main/resources/assets/conium/scripts/conium.commons.kts +++ b/src/main/resources/assets/conium/scripts/conium.commons.kts @@ -1,6 +1,7 @@ import com.github.cao.awa.conium.bedrock.event.context.BedrockEventContext import com.github.cao.awa.conium.event.context.ConiumEventContextBuilder.request import com.github.cao.awa.conium.event.context.ConiumEventContextBuilder.preRequest +import com.github.cao.awa.conium.event.context.ConiumEventContextBuilder.export import com.github.cao.awa.conium.event.context.ConiumEventContext import com.github.cao.awa.conium.event.context.ConiumEventContextBuilder