Skip to content

Commit

Permalink
Fixed effects not supporting triggers when mutators would allow them
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed Jun 18, 2023
1 parent 8bfeda0 commit 4981700
Show file tree
Hide file tree
Showing 16 changed files with 139 additions and 10 deletions.
10 changes: 9 additions & 1 deletion core/src/main/kotlin/com/willfp/libreforge/effects/Effect.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import com.willfp.eco.core.map.defaultMap
import com.willfp.libreforge.Compilable
import com.willfp.libreforge.ProvidedHolder
import com.willfp.libreforge.applyHolder
import com.willfp.libreforge.mutators.MutatorList
import com.willfp.libreforge.mutators.emptyMutatorList
import com.willfp.libreforge.plugin
import com.willfp.libreforge.triggers.DispatchedTrigger
import com.willfp.libreforge.triggers.Trigger
Expand Down Expand Up @@ -63,7 +65,13 @@ abstract class Effect<T>(
* If the effect supports a certain [trigger].
*/
fun supportsTrigger(trigger: Trigger) =
Triggers.withParameters(parameters)(trigger)
Triggers.withParameters(parameters)(trigger, emptyMutatorList())

/**
* If the effect supports a certain [trigger] after [mutators] are applied to it.
*/
fun supportsTrigger(trigger: Trigger, mutators: MutatorList) =
Triggers.withParameters(parameters)(trigger, mutators)

/**
* Enable a permanent effect.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ object Effects : Registry<Effect<*>>() {
var isInvalid = false
for (element in chain) {
for (trigger in triggers) {
if (!element.effect.supportsTrigger(trigger)) {
if (!element.effect.supportsTrigger(trigger, mutators)) {
isInvalid = true
context.log(
ConfigViolation(
Expand Down
15 changes: 15 additions & 0 deletions core/src/main/kotlin/com/willfp/libreforge/mutators/Mutator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.willfp.eco.core.config.interfaces.Config
import com.willfp.libreforge.Compilable
import com.willfp.libreforge.effects.RunOrder
import com.willfp.libreforge.triggers.TriggerData
import com.willfp.libreforge.triggers.TriggerParameter

abstract class Mutator<T>(
override val id: String
Expand All @@ -13,6 +14,20 @@ abstract class Mutator<T>(
*/
open val runOrder = RunOrder.NORMAL

/**
* The parameter transformers.
*/
protected open val parameterTransformers: Set<TriggerParameterTransformer> = emptySet()

/**
* Transform the parameters.
*/
fun transform(parameters: Set<TriggerParameter>): Set<TriggerParameter> {
return parameterTransformers.fold(parameters) { acc, transformer ->
transformer.transform(acc)
}
}

/**
* Mutate the trigger data.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.willfp.libreforge.mutators
import com.willfp.eco.core.config.interfaces.Config
import com.willfp.libreforge.Compiled
import com.willfp.libreforge.triggers.TriggerData
import com.willfp.libreforge.triggers.TriggerParameter

/**
* A single mutator config block.
Expand All @@ -14,4 +15,7 @@ class MutatorBlock<T> internal constructor(
): Compiled<T> {
fun mutate(data: TriggerData) =
mutator.mutate(data, this)

fun transform(parameters: Set<TriggerParameter>) =
mutator.transform(parameters)
}
18 changes: 13 additions & 5 deletions core/src/main/kotlin/com/willfp/libreforge/mutators/MutatorList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.willfp.libreforge.mutators

import com.willfp.libreforge.DelegatedList
import com.willfp.libreforge.triggers.TriggerData
import com.willfp.libreforge.triggers.TriggerParameter

/**
* A list of mutators.
Expand All @@ -14,12 +15,19 @@ class MutatorList(
}
) {
fun mutate(data: TriggerData): TriggerData {
var current = data

for (block in this) {
current = block.mutate(current)
return this.fold(data) { currentData, block ->
block.mutate(currentData)
}
}

return current
fun transform(parameters: Set<TriggerParameter>): Set<TriggerParameter> {
return this.fold(parameters) { currentParameters, block ->
block.transform(currentParameters)
}
}
}

/**
* Creates an empty [MutatorList].
*/
fun emptyMutatorList() = MutatorList(emptyList())
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.willfp.libreforge.mutators

import com.willfp.libreforge.triggers.TriggerParameter

data class TriggerParameterTransformer(
val parameterIn: TriggerParameter,
val parameterOut: TriggerParameter
) {
fun transform(parameters: Set<TriggerParameter>): Set<TriggerParameter> {
return parameters.fold(mutableSetOf()) { acc, parameter ->
if (parameter == parameterIn) {
acc.apply {
add(parameterIn)
add(parameterOut)
}
} else {
acc.apply { add(parameter) }
}
}
}
}

class TriggerParameterBuilder {
private val set = mutableSetOf<TriggerParameterTransformer>()

infix fun TriggerParameter.becomes(other: TriggerParameter) {
set += TriggerParameterTransformer(this, other)
}

internal fun toSet(): Set<TriggerParameterTransformer> {
return set
}
}

fun parameterTransformers(block: TriggerParameterBuilder.() -> Unit): Set<TriggerParameterTransformer> =
TriggerParameterBuilder().apply(block).toSet()
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ package com.willfp.libreforge.mutators.impl
import com.willfp.eco.core.config.interfaces.Config
import com.willfp.libreforge.NoCompileData
import com.willfp.libreforge.mutators.Mutator
import com.willfp.libreforge.mutators.parameterTransformers
import com.willfp.libreforge.triggers.TriggerData
import com.willfp.libreforge.triggers.TriggerParameter

object MutatorBlockToLocation : Mutator<NoCompileData>("block_to_location") {
override val parameterTransformers = parameterTransformers {
TriggerParameter.LOCATION becomes TriggerParameter.BLOCK
}

override fun mutate(data: TriggerData, config: Config, compileData: NoCompileData): TriggerData {
return data.copy(
block = data.location?.block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@ package com.willfp.libreforge.mutators.impl
import com.willfp.eco.core.config.interfaces.Config
import com.willfp.libreforge.NoCompileData
import com.willfp.libreforge.mutators.Mutator
import com.willfp.libreforge.mutators.TriggerParameterTransformer
import com.willfp.libreforge.mutators.parameterTransformers
import com.willfp.libreforge.triggers.TriggerData
import com.willfp.libreforge.triggers.TriggerParameter

object MutatorLocationToBlock : Mutator<NoCompileData>("location_to_block") {
override val parameterTransformers = parameterTransformers {
TriggerParameter.BLOCK becomes TriggerParameter.LOCATION
}

override fun mutate(data: TriggerData, config: Config, compileData: NoCompileData): TriggerData {
return data.copy(
location = data.block?.location
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import com.willfp.eco.core.config.interfaces.Config
import com.willfp.libreforge.NoCompileData
import com.willfp.libreforge.arguments
import com.willfp.libreforge.mutators.Mutator
import com.willfp.libreforge.mutators.parameterTransformers
import com.willfp.libreforge.plugin
import com.willfp.libreforge.triggers.TriggerData
import com.willfp.libreforge.triggers.TriggerParameter
import org.bukkit.FluidCollisionMode

object MutatorLocationToCursor : Mutator<NoCompileData>("location_to_cursor") {
Expand All @@ -18,6 +20,11 @@ object MutatorLocationToCursor : Mutator<NoCompileData>("location_to_cursor") {
}
}

override val parameterTransformers = parameterTransformers {
TriggerParameter.PLAYER becomes TriggerParameter.LOCATION
TriggerParameter.VICTIM becomes TriggerParameter.LOCATION
}

override fun mutate(data: TriggerData, config: Config, compileData: NoCompileData): TriggerData {
val target = config.getString("target")
val startingEntity = config.getString("start")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@ package com.willfp.libreforge.mutators.impl
import com.willfp.eco.core.config.interfaces.Config
import com.willfp.libreforge.NoCompileData
import com.willfp.libreforge.mutators.Mutator
import com.willfp.libreforge.mutators.TriggerParameterTransformer
import com.willfp.libreforge.mutators.parameterTransformers
import com.willfp.libreforge.triggers.TriggerData
import com.willfp.libreforge.triggers.TriggerParameter

object MutatorLocationToPlayer : Mutator<NoCompileData>("location_to_player") {
override val parameterTransformers = parameterTransformers {
TriggerParameter.PLAYER becomes TriggerParameter.LOCATION
}

override fun mutate(data: TriggerData, config: Config, compileData: NoCompileData): TriggerData {
return data.copy(
location = data.player?.location
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ package com.willfp.libreforge.mutators.impl
import com.willfp.eco.core.config.interfaces.Config
import com.willfp.libreforge.NoCompileData
import com.willfp.libreforge.mutators.Mutator
import com.willfp.libreforge.mutators.parameterTransformers
import com.willfp.libreforge.triggers.TriggerData
import com.willfp.libreforge.triggers.TriggerParameter

object MutatorLocationToProjectile : Mutator<NoCompileData>("location_to_projectile") {
override val parameterTransformers = parameterTransformers {
TriggerParameter.PROJECTILE becomes TriggerParameter.LOCATION
}

override fun mutate(data: TriggerData, config: Config, compileData: NoCompileData): TriggerData {
return data.copy(
location = data.projectile?.location
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ package com.willfp.libreforge.mutators.impl
import com.willfp.eco.core.config.interfaces.Config
import com.willfp.libreforge.NoCompileData
import com.willfp.libreforge.mutators.Mutator
import com.willfp.libreforge.mutators.parameterTransformers
import com.willfp.libreforge.triggers.TriggerData
import com.willfp.libreforge.triggers.TriggerParameter

object MutatorLocationToVictim : Mutator<NoCompileData>("location_to_victim") {
override val parameterTransformers = parameterTransformers {
TriggerParameter.VICTIM becomes TriggerParameter.LOCATION
}

override fun mutate(data: TriggerData, config: Config, compileData: NoCompileData): TriggerData {
return data.copy(
location = data.victim?.location
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ package com.willfp.libreforge.mutators.impl
import com.willfp.eco.core.config.interfaces.Config
import com.willfp.libreforge.NoCompileData
import com.willfp.libreforge.mutators.Mutator
import com.willfp.libreforge.mutators.parameterTransformers
import com.willfp.libreforge.triggers.TriggerData
import com.willfp.libreforge.triggers.TriggerParameter

object MutatorPlayerAsVictim : Mutator<NoCompileData>("player_as_victim") {
override val parameterTransformers = parameterTransformers {
TriggerParameter.PLAYER becomes TriggerParameter.VICTIM
}

override fun mutate(data: TriggerData, config: Config, compileData: NoCompileData): TriggerData {
return data.copy(
victim = data.player
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ package com.willfp.libreforge.mutators.impl
import com.willfp.eco.core.config.interfaces.Config
import com.willfp.libreforge.NoCompileData
import com.willfp.libreforge.mutators.Mutator
import com.willfp.libreforge.mutators.parameterTransformers
import com.willfp.libreforge.triggers.TriggerData
import com.willfp.libreforge.triggers.TriggerParameter
import org.bukkit.entity.Player

object MutatorVictimAsPlayer : Mutator<NoCompileData>("victim_as_player") {
override val parameterTransformers = parameterTransformers {
TriggerParameter.VICTIM becomes TriggerParameter.PLAYER
}

override fun mutate(data: TriggerData, config: Config, compileData: NoCompileData): TriggerData {
return data.copy(
player = data.victim as? Player
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package com.willfp.libreforge.mutators.impl
import com.willfp.eco.core.config.interfaces.Config
import com.willfp.libreforge.NoCompileData
import com.willfp.libreforge.mutators.Mutator
import com.willfp.libreforge.mutators.parameterTransformers
import com.willfp.libreforge.triggers.TriggerData
import com.willfp.libreforge.triggers.TriggerParameter
import org.bukkit.entity.LivingEntity
import org.bukkit.entity.Tameable

Expand Down
11 changes: 8 additions & 3 deletions core/src/main/kotlin/com/willfp/libreforge/triggers/Triggers.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.willfp.libreforge.triggers

import com.willfp.eco.core.registry.Registry
import com.willfp.libreforge.mutators.MutatorList
import com.willfp.libreforge.triggers.impl.TriggerAltClick
import com.willfp.libreforge.triggers.impl.TriggerBite
import com.willfp.libreforge.triggers.impl.TriggerBlockItemDrop
Expand Down Expand Up @@ -113,9 +114,13 @@ object Triggers : Registry<Trigger>() {
/**
* Get a predicate requiring certain trigger parameters.
*/
fun withParameters(parameters: Set<TriggerParameter>): (Trigger) -> Boolean {
return {
it.parameters.flatMap { param -> param.inheritsFrom.toList().plusElement(param) }.containsAll(parameters)
fun withParameters(parameters: Set<TriggerParameter>): (Trigger, MutatorList) -> Boolean {
return { trigger, mutators ->
trigger.parameters
.flatMap { param -> param.inheritsFrom.toSet().plusElement(param) }
.toSet()
.let { mutators.transform(it) }
.containsAll(parameters)
}
}

Expand Down

0 comments on commit 4981700

Please sign in to comment.