From 8bfeda0092e2c91a24c259cc5971b185faa0241c Mon Sep 17 00:00:00 2001 From: Auxilor Date: Sun, 18 Jun 2023 20:28:04 +0200 Subject: [PATCH] Optimised trigger dispatching, fixed bug with dynamically registered triggers --- .../com/willfp/libreforge/effects/EffectBlock.kt | 12 +++++++----- .../com/willfp/libreforge/effects/Effects.kt | 2 +- .../com/willfp/libreforge/effects/ElementLike.kt | 16 ++++++++-------- .../com/willfp/libreforge/triggers/Trigger.kt | 9 +++++++++ .../com/willfp/libreforge/triggers/Triggers.kt | 2 +- 5 files changed, 26 insertions(+), 15 deletions(-) diff --git a/core/src/main/kotlin/com/willfp/libreforge/effects/EffectBlock.kt b/core/src/main/kotlin/com/willfp/libreforge/effects/EffectBlock.kt index e877166e3..140917c1f 100644 --- a/core/src/main/kotlin/com/willfp/libreforge/effects/EffectBlock.kt +++ b/core/src/main/kotlin/com/willfp/libreforge/effects/EffectBlock.kt @@ -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 @@ -19,7 +20,7 @@ class EffectBlock internal constructor( override val uuid: UUID, override val config: Config, val effects: Chain, - val triggers: Collection, + val triggers: Set, override val arguments: EffectArgumentList, override val conditions: ConditionList, override val mutators: MutatorList, @@ -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) diff --git a/core/src/main/kotlin/com/willfp/libreforge/effects/Effects.kt b/core/src/main/kotlin/com/willfp/libreforge/effects/Effects.kt index 333928410..c9f8b826a 100644 --- a/core/src/main/kotlin/com/willfp/libreforge/effects/Effects.kt +++ b/core/src/main/kotlin/com/willfp/libreforge/effects/Effects.kt @@ -187,7 +187,7 @@ object Effects : Registry>() { 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) diff --git a/core/src/main/kotlin/com/willfp/libreforge/effects/ElementLike.kt b/core/src/main/kotlin/com/willfp/libreforge/effects/ElementLike.kt index 1cfb1d50b..073364cf0 100644 --- a/core/src/main/kotlin/com/willfp/libreforge/effects/ElementLike.kt +++ b/core/src/main/kotlin/com/willfp/libreforge/effects/ElementLike.kt @@ -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")) { @@ -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) diff --git a/core/src/main/kotlin/com/willfp/libreforge/triggers/Trigger.kt b/core/src/main/kotlin/com/willfp/libreforge/triggers/Trigger.kt index 3d322ec5c..64f62815a 100644 --- a/core/src/main/kotlin/com/willfp/libreforge/triggers/Trigger.kt +++ b/core/src/main/kotlin/com/willfp/libreforge/triggers/Trigger.kt @@ -1,6 +1,9 @@ package com.willfp.libreforge.triggers +import com.sun.tools.javac.jvm.ByteCodes.ret import com.willfp.eco.core.registry.KRegistrable +import com.willfp.libreforge.BlankHolder.effects +import com.willfp.libreforge.EmptyProvidedHolder.holder import com.willfp.libreforge.ProvidedHolder import com.willfp.libreforge.generatePlaceholders import com.willfp.libreforge.getProvidedActiveEffects @@ -41,6 +44,7 @@ abstract class Trigger( forceHolders: Collection? = null ) { val dispatch = plugin.dispatchedTriggerFactory.create(player, this, data) ?: return + dispatch.generatePlaceholders(data) val dispatchEvent = TriggerDispatchEvent(player, dispatch) @@ -52,6 +56,11 @@ abstract class Trigger( val effects = forceHolders?.getProvidedActiveEffects(player) ?: player.providedActiveEffects for ((holder, blocks) in effects) { + // Avoid generating placeholders for nothing + if (blocks.none { it.canBeTriggeredBy(this) }) { + continue + } + val withHolder = data.copy(holder = holder) val dispatchWithHolder = DispatchedTrigger(player, this, withHolder).inheritPlaceholders(dispatch) diff --git a/core/src/main/kotlin/com/willfp/libreforge/triggers/Triggers.kt b/core/src/main/kotlin/com/willfp/libreforge/triggers/Triggers.kt index 719eefd46..05de644a1 100644 --- a/core/src/main/kotlin/com/willfp/libreforge/triggers/Triggers.kt +++ b/core/src/main/kotlin/com/willfp/libreforge/triggers/Triggers.kt @@ -121,7 +121,7 @@ object Triggers : Registry() { init { register(TriggerGroupCustom) - register(TriggerGroupStatic) + //register(TriggerGroupStatic) register(TriggerAltClick) register(TriggerBite) register(TriggerBlockItemDrop)