Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed Jun 19, 2023
2 parents d4537c8 + 4de48df commit ada1760
Show file tree
Hide file tree
Showing 24 changed files with 261 additions and 33 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
@@ -1,6 +1,7 @@
package com.willfp.libreforge.effects

import com.willfp.eco.core.config.interfaces.Config
import com.willfp.libreforge.BlankHolder.effects
import com.willfp.libreforge.ProvidedHolder
import com.willfp.libreforge.conditions.ConditionList
import com.willfp.libreforge.effects.arguments.EffectArgumentList
Expand All @@ -19,7 +20,7 @@ class EffectBlock internal constructor(
override val uuid: UUID,
override val config: Config,
val effects: Chain,
val triggers: Collection<Trigger>,
val triggers: Set<Trigger>,
override val arguments: EffectArgumentList,
override val conditions: ConditionList,
override val mutators: MutatorList,
Expand All @@ -45,13 +46,14 @@ class EffectBlock internal constructor(
) = effects.forEach { it.disable(player, holder, isReload = isReload) }

fun tryTrigger(trigger: DispatchedTrigger) {
if (trigger.trigger !in triggers) {
return
if (canBeTriggeredBy(trigger.trigger)) {
trigger(trigger)
}

trigger(trigger)
}

fun canBeTriggeredBy(trigger: Trigger) =
trigger in triggers

override fun doTrigger(trigger: DispatchedTrigger) =
effects.trigger(trigger)

Expand Down
8 changes: 6 additions & 2 deletions core/src/main/kotlin/com/willfp/libreforge/effects/Effects.kt
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ import com.willfp.libreforge.effects.impl.EffectSpawnParticle
import com.willfp.libreforge.effects.impl.EffectSpawnPotionCloud
import com.willfp.libreforge.effects.impl.EffectStrikeLightning
import com.willfp.libreforge.effects.impl.EffectStripAI
import com.willfp.libreforge.effects.impl.EffectSwarm
import com.willfp.libreforge.effects.impl.EffectTakeMoney
import com.willfp.libreforge.effects.impl.EffectTargetPlayer
import com.willfp.libreforge.effects.impl.EffectTeleport
import com.willfp.libreforge.effects.impl.EffectTeleportTo
import com.willfp.libreforge.effects.impl.EffectTeleportToGround
Expand Down Expand Up @@ -185,7 +187,7 @@ object Effects : Registry<Effect<*>>() {
val filters = Filters.compile(config.getSubsection("filters"), context.with("filters"))
val triggers = config.getStrings("triggers").mapNotNull {
Triggers[it]
}
}.toSet()

val effectConfigs = if (config.has("id")) {
listOf(config)
Expand Down Expand Up @@ -225,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 Expand Up @@ -485,5 +487,7 @@ object Effects : Registry<Effect<*>>() {
register(EffectSetItemPoints)
register(EffectSetCustomModelData)
register(EffectDropWeightedRandomItem)
register(EffectSwarm)
register(EffectTargetPlayer)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,6 @@ abstract class ElementLike : ConfigurableElement {

val data = mutators.mutate(trigger.data)

// Inject placeholders everywhere after mutation
trigger.generatePlaceholders(data)
listOf(arguments, conditions, mutators, filters)
.flatten()
.map { it.config }
.plusElement(config)
.forEach { it.addInjectablePlaceholder(trigger.placeholders) }

// Antigrief check here - not very clean, but it works.
if (data.player != null && data.victim != null && data.victim != data.player) {
if (!config.getBool("disable_antigrief_check")) {
Expand All @@ -90,6 +82,14 @@ abstract class ElementLike : ConfigurableElement {
}
}

// Inject placeholders everywhere after mutation
trigger.generatePlaceholders(data)
listOf(arguments, conditions, mutators, filters)
.flatten()
.map { it.config }
.plusElement(config)
.forEach { it.addInjectablePlaceholder(trigger.placeholders) }

// Filter
val filterResult = if (config.getBool("filters_before_mutation")) {
filters.isMet(trigger.data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ object EffectBonusHealth : AttributeEffect(
}

override fun getValue(config: Config, player: Player) =
config.getIntFromExpression("health", player).toDouble()
config.getDoubleFromExpression("health", player)

override fun constrainAttribute(player: Player, value: Double) {
if (player.health > value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package com.willfp.libreforge.effects.impl

import com.willfp.libreforge.effects.templates.MultiplierEffect
import com.willfp.libreforge.effects.templates.MultiMultiplierEffect
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.entity.EntityRegainHealthEvent
import org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason

object EffectRegenMultiplier : MultiMultiplierEffect<RegainReason>("regen_multiplier") {
override val key: String = "reason"

override fun getElement(key: String): RegainReason? =
runCatching { enumValueOf<RegainReason>("reason") }.getOrNull()

override fun getAllElements(): Collection<RegainReason> =
RegainReason.values().toList()

object EffectRegenMultiplier : MultiplierEffect("regen_multiplier") {
@EventHandler(ignoreCancelled = true)
fun handle(event: EntityRegainHealthEvent) {
val player = event.entity
Expand All @@ -14,6 +23,6 @@ object EffectRegenMultiplier : MultiplierEffect("regen_multiplier") {
return
}

event.amount *= getMultiplier(player)
event.amount *= getMultiplier(player, event.regainReason)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.willfp.libreforge.effects.impl

import com.willfp.eco.core.config.interfaces.Config
import com.willfp.eco.core.entities.Entities
import com.willfp.eco.core.entities.TestableEntity
import com.willfp.eco.core.lookup.matches
import com.willfp.libreforge.ConfigArguments
import com.willfp.libreforge.NoCompileData
import com.willfp.libreforge.ViolationContext
import com.willfp.libreforge.arguments
import com.willfp.libreforge.effects.Effect
import com.willfp.libreforge.getDoubleFromExpression
import com.willfp.libreforge.triggers.TriggerData
import com.willfp.libreforge.triggers.TriggerParameter
import org.bukkit.entity.Monster
import org.checkerframework.checker.units.qual.t

object EffectSwarm : Effect<List<TestableEntity>?>("swarm") {
override val parameters = setOf(
TriggerParameter.VICTIM
)

override val arguments = arguments {
require("radius", "You must specify the maximum distance to swarm the victim from!")
}

override fun onTrigger(config: Config, data: TriggerData, compileData: List<TestableEntity>?): Boolean {
val victim = data.victim ?: return false

val radius = config.getDoubleFromExpression("radius", data)

victim.getNearbyEntities(radius, radius, radius)
.filterIsInstance<Monster>()
.filter { compileData?.any { t -> t.matches(it) } ?: true }
.forEach { it.target = victim }

return true
}

override fun makeCompileData(config: Config, context: ViolationContext): List<TestableEntity>? {
return config.getStringsOrNull("entities")
?.mapNotNull { Entities.lookup(it) }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.willfp.libreforge.effects.impl

import com.willfp.eco.core.config.interfaces.Config
import com.willfp.libreforge.NoCompileData
import com.willfp.libreforge.effects.Effect
import com.willfp.libreforge.triggers.TriggerData
import com.willfp.libreforge.triggers.TriggerParameter
import org.bukkit.entity.Monster

object EffectTargetPlayer : Effect<NoCompileData>("target_player") {
override val parameters = setOf(
TriggerParameter.PLAYER,
TriggerParameter.VICTIM
)

override fun onTrigger(config: Config, data: TriggerData, compileData: NoCompileData): Boolean {
val player = data.player ?: return false
val victim = data.victim as? Monster ?: return false

victim.target = player

return true
}
}
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
Loading

0 comments on commit ada1760

Please sign in to comment.