Skip to content

Commit

Permalink
ContextIntention-specific autofillers
Browse files Browse the repository at this point in the history
  • Loading branch information
NichtStudioCode committed Nov 23, 2024
1 parent 5f62cf0 commit 9dd788c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
6 changes: 5 additions & 1 deletion nova/src/main/kotlin/xyz/xenondevs/nova/context/Context.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ class Context<I : ContextIntention> private constructor(
// try to resolve value through autofillers
val autofillers = paramType.autofillers
var value: V? = null
autofiller@ for ((requiredParamTypes, fillerFunction) in autofillers) {
autofiller@ for ((autofillerIntention, requiredParamTypes, fillerFunction) in autofillers) {
// skip autofiller if it is not intended for this context
if (autofillerIntention != null && autofillerIntention != intention)
continue

// load params required by autofiller
val requiredParamValues = arrayOfNulls<Any>(requiredParamTypes.size)
for ((i, requiredParamType) in requiredParamTypes.withIndex()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ internal class Requirement<V : Any>(
)

internal class Autofiller<V : Any>(
val intention: ContextIntention? = null,
lazyParamTypes: List<Lazy<ContextParamType<*>>>,
val filler: Function<V?>
) {

val params: List<ContextParamType<*>> by lazy { lazyParamTypes.map { it.value } }

operator fun component1(): List<ContextParamType<*>> = params
operator fun component2(): Function<V?> = filler
operator fun component1(): ContextIntention? = intention
operator fun component2(): List<ContextParamType<*>> = params
operator fun component3(): Function<V?> = filler

}

Expand Down Expand Up @@ -144,7 +146,47 @@ class ContextParamTypeBuilder<V : Any> internal constructor(private val id: Reso
vararg lazyParamTypes: () -> ContextParamType<*>
): ContextParamTypeBuilder<V> {
val paramTypes = lazyParamTypes.map(::lazy)
autofillers += Autofiller(paramTypes, fillValue)
autofillers += Autofiller(null, paramTypes, fillValue)
return this
}

fun <A : Any> autofilledBy(
inIntention: ContextIntention,
lazyParamType: () -> ContextParamType<A>,
fillValue: (A) -> V?
) = autofilledBy(fillValue, lazyParamType)

fun <A : Any, B : Any> autofilledBy(
inIntention: ContextIntention,
lazyParamTypeA: () -> ContextParamType<A>,
lazyParamTypeB: () -> ContextParamType<B>,
fillValue: (A, B) -> V?
) = autofilledBy(fillValue, lazyParamTypeA, lazyParamTypeB)

fun <A : Any, B : Any, C : Any> autofilledBy(
inIntention: ContextIntention,
lazyParamTypeA: () -> ContextParamType<A>,
lazyParamTypeB: () -> ContextParamType<B>,
lazyParamTypeC: () -> ContextParamType<C>,
fillValue: (A, B, C) -> V?
) = autofilledBy(fillValue, lazyParamTypeA, lazyParamTypeB, lazyParamTypeC)

fun <A : Any, B : Any, C : Any, D : Any> autofilledBy(
inIntention: ContextIntention,
lazyParamTypeA: () -> ContextParamType<A>,
lazyParamTypeB: () -> ContextParamType<B>,
lazyParamTypeC: () -> ContextParamType<C>,
lazyParamTypeD: () -> ContextParamType<D>,
fillValue: (A, B, C, D) -> V?
) = autofilledBy(fillValue, lazyParamTypeA, lazyParamTypeB, lazyParamTypeC, lazyParamTypeD)

private fun autofilledBy(
inIntention: ContextIntention,
fillValue: Function<V?>,
vararg lazyParamTypes: () -> ContextParamType<*>
): ContextParamTypeBuilder<V> {
val paramTypes = lazyParamTypes.map(::lazy)
autofillers += Autofiller(inIntention, paramTypes, fillValue)
return this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,18 @@ object DefaultContextParamTypes {
* - [BlockPlace]
*
* Autofilled by:
* - [BLOCK_ITEM_STACK] if data is present
* - [BLOCK_ITEM_STACK] in [BlockPlace] if data is present
* - [TILE_ENTITY_NOVA] in [BlockBreak] and [BlockInteract]
*
* Autofills: none
*/
val TILE_ENTITY_DATA_NOVA: ContextParamType<Compound> =
ContextParamType.builder<Compound>("tile_entity_data_nova")
.optionalIn(BlockPlace)
.copiedBy(Compound::copy)
.autofilledBy(::BLOCK_ITEM_STACK) { itemStack ->
.autofilledBy(BlockBreak, ::TILE_ENTITY_NOVA) { tileEntity -> tileEntity.data }
.autofilledBy(BlockInteract, ::TILE_ENTITY_NOVA) { tileEntity -> tileEntity.data }
.autofilledBy(BlockPlace, ::BLOCK_ITEM_STACK) { itemStack ->
itemStack.novaCompound
?.get<Compound>(TileEntity.TILE_ENTITY_DATA_KEY)
?.let { persistentData -> Compound().also { it["persistent"] = persistentData } }
Expand Down

0 comments on commit 9dd788c

Please sign in to comment.