Skip to content

Commit

Permalink
Let data driven could interaction to kotlin script.
Browse files Browse the repository at this point in the history
Add unnamed event context used to attach present contexts.
Load script before all data-driven data.
  • Loading branch information
cao-awa committed Nov 28, 2024
1 parent a6ec046 commit cd2e298
Show file tree
Hide file tree
Showing 12 changed files with 547 additions and 33 deletions.
5 changes: 4 additions & 1 deletion sample/datapacks/tests/data/awa/item/conium.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions sample/datapacks/tests/data/awa/script/interaction.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export(
"awa:test_glint",
false,
ITEM_USED_ON_BLOCK,
ITEM_USAGE_CONTEXT
) { item, context ->
return@export (context.player?.isCreative ?: false)
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,35 +66,28 @@ class ConiumRecipeManager(private val registries: WrapperLookup) : ServerRecipeM
)
)

private fun <X : SingleStackRecipe> cookingIngredientGetter(expectedType: RecipeType<X>): SoleIngredientGetter {
return SoleIngredientGetter {
if (it.type == expectedType && it is SingleStackRecipe) {
Optional.of(it.ingredient())
} else {
Optional.empty()
}
private fun <X : SingleStackRecipe> cookingIngredientGetter(expectedType: RecipeType<X>): SoleIngredientGetter = SoleIngredientGetter {
if (it.type == expectedType && it is SingleStackRecipe) {
Optional.of(it.ingredient())
} else {
Optional.empty()
}
}

fun filterIngredients(features: FeatureSet, ingredients: MutableList<Ingredient>): List<Ingredient> {
ingredients.removeIf { ingredient -> !isEnabled(features, ingredient) }
return ingredients
fun filterIngredients(features: FeatureSet, ingredients: MutableList<Ingredient>): List<Ingredient> = ingredients.apply {
removeIf { ingredient: Ingredient -> !isEnabled(features, ingredient) }
}

private fun isEnabled(features: FeatureSet, ingredient: Ingredient): Boolean {
return ingredient.matchingItems.stream().allMatch { it.value().isEnabled(features) }
}

open class PropertySetBuilder(val propertySetKey: RegistryKey<RecipePropertySet>, private val ingredientGetter: SoleIngredientGetter) : Consumer<Recipe<*>> {
private val ingredients: MutableList<Ingredient> = ArrayList()
private val ingredients: MutableList<Ingredient> = 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))
}
}

Expand All @@ -108,8 +101,8 @@ class ConiumRecipeManager(private val registries: WrapperLookup) : ServerRecipeM
val sortedMap: SortedMap<Identifier, Recipe<*>> = TreeMap()
load(resourceManager, RegistryKeys.getPath(RegistryKeys.RECIPE), sortedMap)
val list: MutableList<RecipeEntry<*>> = ArrayList(sortedMap.size)
sortedMap.forEach { (id, recipe) ->
val registryKey = RegistryKey.of(RegistryKeys.RECIPE, id)
sortedMap.forEach { (id: Identifier, recipe: Recipe<*>) ->
val registryKey: RegistryKey<Recipe<*>> = RegistryKey.of(RegistryKeys.RECIPE, id)
val recipeEntry: RecipeEntry<*> = RecipeEntry(registryKey, recipe)
list.add(recipeEntry)
}
Expand Down Expand Up @@ -232,15 +225,18 @@ class ConiumRecipeManager(private val registries: WrapperLookup) : ServerRecipeM

override fun <I : RecipeInput, T : Recipe<I>> getFirstMatch(
type: RecipeType<T>, input: I, world: World, recipe: RegistryKey<Recipe<*>>?
): Optional<RecipeEntry<T>> {
val recipeEntry: RecipeEntry<T>? = if (recipe != null) this.get(type, recipe) else null
return this.getFirstMatch(type, input, world, recipeEntry)
}
): Optional<RecipeEntry<T>> = getFirstMatch(
type,
input,
world,
if (recipe != null) get(type, recipe) else null
)


override fun <I : RecipeInput, T : Recipe<I>> getFirstMatch(
type: RecipeType<T>, input: I, world: World, recipe: RecipeEntry<T>?
): Optional<RecipeEntry<T>> {
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)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -72,6 +74,13 @@ class ConiumScriptManager : SinglePreparationResourceReloader<MutableMap<Identif
*/
private val exportedScript: MutableMap<String, ScriptExport> = 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<String, NamedInteractionScript<*>> = CollectionFactor.hashMap()

/**
* Prepares the intermediate object.
*
Expand All @@ -86,10 +95,25 @@ class ConiumScriptManager : SinglePreparationResourceReloader<MutableMap<Identif
*
* @since 1.0.0
*/
override fun prepare(manager: ResourceManager, profiler: Profiler): MutableMap<Identifier, Resource> =
CollectionFactor.hashMap<Identifier, Resource>().also {
load(manager, it)
}
override fun prepare(manager: ResourceManager, profiler: Profiler): MutableMap<Identifier, Resource> = CollectionFactor.hashMap<Identifier, Resource>().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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class ConiumEventContext<P : ParameterSelective?>(

private val args: MutableMap<DynamicArgType<*>, Any?> = CollectionFactor.hashMap()
private val attaches: MutableList<ConiumEventContext<*>> = CollectionFactor.arrayList()
private val attachesDynamic: MutableList<P> = CollectionFactor.arrayList()
private var lastIdentity: Any? = null

private var targetedIdentity: ParameterSelective1<Boolean, Any> = ParameterSelective1 { true }

Expand Down Expand Up @@ -82,6 +84,16 @@ class ConiumEventContext<P : ParameterSelective?>(
return this
}

fun attachDynamic(dynamicArgs: P): ConiumEventContext<P> {
this.attachesDynamic.add(dynamicArgs)
return this
}

fun attachDynamic(dynamicArgs: MutableList<P>): ConiumEventContext<P> {
this.attachesDynamic.addAll(dynamicArgs);
return this
}

fun attach(context: ConiumEventContext<*>): ConiumEventContext<P> {
this.attaches.add(context)
return this
Expand All @@ -100,6 +112,8 @@ class ConiumEventContext<P : ParameterSelective?>(
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()) {
Expand All @@ -118,17 +132,27 @@ class ConiumEventContext<P : ParameterSelective?>(
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()) {
attach.inherit(this)
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<P> {
this.lastIdentity = context.lastIdentity
return resetArgs(context.args)
}
}
Loading

0 comments on commit cd2e298

Please sign in to comment.