From 82d228ea7deab03b387ff66c4862329632a9911a Mon Sep 17 00:00:00 2001 From: Auxilor Date: Thu, 4 May 2023 18:26:01 +0100 Subject: [PATCH] Refactor on trigger placeholders, fixed placeholders with mutators --- .../willfp/libreforge/effects/ElementLike.kt | 10 +++++--- .../libreforge/triggers/DispatchedTrigger.kt | 24 +++++++++++-------- .../com/willfp/libreforge/triggers/Trigger.kt | 2 +- .../placeholders/TriggerPlaceholder.kt | 6 ++--- .../impl/TriggerPlaceholderDistance.kt | 8 +++---- .../impl/TriggerPlaceholderHits.kt | 8 +++---- .../impl/TriggerPlaceholderLocation.kt | 6 ++--- .../impl/TriggerPlaceholderText.kt | 8 ++++--- .../impl/TriggerPlaceholderValue.kt | 6 ++--- .../impl/TriggerPlaceholderVictim.kt | 6 ++--- 10 files changed, 47 insertions(+), 37 deletions(-) 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 e1c748481..186c02405 100644 --- a/core/src/main/kotlin/com/willfp/libreforge/effects/ElementLike.kt +++ b/core/src/main/kotlin/com/willfp/libreforge/effects/ElementLike.kt @@ -71,15 +71,19 @@ abstract class ElementLike { .let { if (!supportsDelay) 0 else it } .toLong() - // Extremely janky code, but it's cleaner than repeating myself 5 times I think. + // Initial injection into mutators + mutators.map { it.config }.forEach { it.addInjectablePlaceholder(trigger.placeholders) } + + 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) } - val data = mutators.mutate(trigger.data) - // 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")) { diff --git a/core/src/main/kotlin/com/willfp/libreforge/triggers/DispatchedTrigger.kt b/core/src/main/kotlin/com/willfp/libreforge/triggers/DispatchedTrigger.kt index 3f3844c3b..fa5346a03 100644 --- a/core/src/main/kotlin/com/willfp/libreforge/triggers/DispatchedTrigger.kt +++ b/core/src/main/kotlin/com/willfp/libreforge/triggers/DispatchedTrigger.kt @@ -15,12 +15,6 @@ data class DispatchedTrigger( val placeholders: List get() = _placeholders.flatMap { it.placeholders } - init { - for (placeholder in TriggerPlaceholders) { - _placeholders += placeholder.createPlaceholders(this) - } - } - fun addPlaceholder(placeholder: NamedValue) { _placeholders += placeholder } @@ -29,10 +23,20 @@ data class DispatchedTrigger( _placeholders += placeholder } - companion object { - fun DispatchedTrigger.inheritPlaceholders(other: DispatchedTrigger): DispatchedTrigger { - _placeholders += other._placeholders - return this + fun inheritPlaceholders(other: DispatchedTrigger): DispatchedTrigger { + _placeholders += other._placeholders + return this + } + + /** + * Generate placeholders for some [data]. + * + * This is called automatically when the trigger is dispatched, + * and again after mutation with the updated [data]. + */ + internal fun generatePlaceholders(data: TriggerData = this.data) { + for (placeholder in TriggerPlaceholders) { + _placeholders += placeholder.createPlaceholders(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 e16ed7e90..f2214a8b2 100644 --- a/core/src/main/kotlin/com/willfp/libreforge/triggers/Trigger.kt +++ b/core/src/main/kotlin/com/willfp/libreforge/triggers/Trigger.kt @@ -6,7 +6,6 @@ import com.willfp.libreforge.generatePlaceholders import com.willfp.libreforge.getProvidedActiveEffects import com.willfp.libreforge.plugin import com.willfp.libreforge.providedActiveEffects -import com.willfp.libreforge.triggers.DispatchedTrigger.Companion.inheritPlaceholders import com.willfp.libreforge.triggers.event.TriggerDispatchEvent import org.bukkit.Bukkit import org.bukkit.entity.Player @@ -29,6 +28,7 @@ abstract class Trigger( forceHolders: Collection? = null ) { val dispatch = DispatchedTrigger(player, this, data) + dispatch.generatePlaceholders(data) val dispatchEvent = TriggerDispatchEvent(player, dispatch) Bukkit.getPluginManager().callEvent(dispatchEvent) diff --git a/core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/TriggerPlaceholder.kt b/core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/TriggerPlaceholder.kt index f203dcc08..6f4677dca 100644 --- a/core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/TriggerPlaceholder.kt +++ b/core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/TriggerPlaceholder.kt @@ -3,16 +3,16 @@ package com.willfp.libreforge.triggers.placeholders import com.willfp.eco.core.registry.KRegistrable import com.willfp.libreforge.NamedValue import com.willfp.libreforge.plugin -import com.willfp.libreforge.triggers.DispatchedTrigger +import com.willfp.libreforge.triggers.TriggerData import org.bukkit.event.Listener abstract class TriggerPlaceholder( override val id: String ) : Listener, KRegistrable { /** - * Create placeholders for a [trigger]. + * Create placeholders for some [data]. */ - abstract fun createPlaceholders(trigger: DispatchedTrigger): Collection + abstract fun createPlaceholders(data: TriggerData): Collection final override fun onRegister() { plugin.runWhenEnabled { diff --git a/core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/impl/TriggerPlaceholderDistance.kt b/core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/impl/TriggerPlaceholderDistance.kt index 3c1520126..7a96e3bf2 100644 --- a/core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/impl/TriggerPlaceholderDistance.kt +++ b/core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/impl/TriggerPlaceholderDistance.kt @@ -1,13 +1,13 @@ package com.willfp.libreforge.triggers.placeholders.impl import com.willfp.libreforge.NamedValue -import com.willfp.libreforge.triggers.DispatchedTrigger +import com.willfp.libreforge.triggers.TriggerData import com.willfp.libreforge.triggers.placeholders.TriggerPlaceholder object TriggerPlaceholderDistance : TriggerPlaceholder("distance") { - override fun createPlaceholders(trigger: DispatchedTrigger): Collection { - val victim = trigger.data.victim ?: return emptyList() - val player = trigger.data.player ?: return emptyList() + override fun createPlaceholders(data: TriggerData): Collection { + val victim = data.victim ?: return emptyList() + val player = data.player ?: return emptyList() return listOf( NamedValue( diff --git a/core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/impl/TriggerPlaceholderHits.kt b/core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/impl/TriggerPlaceholderHits.kt index 828bfd703..5c6e36dbd 100644 --- a/core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/impl/TriggerPlaceholderHits.kt +++ b/core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/impl/TriggerPlaceholderHits.kt @@ -2,7 +2,7 @@ package com.willfp.libreforge.triggers.placeholders.impl import com.willfp.libreforge.NamedValue import com.willfp.libreforge.plugin -import com.willfp.libreforge.triggers.DispatchedTrigger +import com.willfp.libreforge.triggers.TriggerData import com.willfp.libreforge.triggers.event.TriggerDispatchEvent import com.willfp.libreforge.triggers.impl.TriggerBowAttack import com.willfp.libreforge.triggers.impl.TriggerMeleeAttack @@ -17,9 +17,9 @@ import java.util.UUID object TriggerPlaceholderHits : TriggerPlaceholder("distance") { private const val HITS_META_KEY = "libreforge_tracked_hits" - override fun createPlaceholders(trigger: DispatchedTrigger): Collection { - val victim = trigger.data.victim ?: return emptyList() - val player = trigger.data.player ?: return emptyList() + override fun createPlaceholders(data: TriggerData): Collection { + val victim = data.victim ?: return emptyList() + val player = data.player ?: return emptyList() return listOf( NamedValue( diff --git a/core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/impl/TriggerPlaceholderLocation.kt b/core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/impl/TriggerPlaceholderLocation.kt index 0a172051a..b50f62ad9 100644 --- a/core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/impl/TriggerPlaceholderLocation.kt +++ b/core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/impl/TriggerPlaceholderLocation.kt @@ -1,12 +1,12 @@ package com.willfp.libreforge.triggers.placeholders.impl import com.willfp.libreforge.NamedValue -import com.willfp.libreforge.triggers.DispatchedTrigger +import com.willfp.libreforge.triggers.TriggerData import com.willfp.libreforge.triggers.placeholders.TriggerPlaceholder object TriggerPlaceholderLocation : TriggerPlaceholder("location") { - override fun createPlaceholders(trigger: DispatchedTrigger): Collection { - val location = trigger.data.location ?: return emptyList() + override fun createPlaceholders(data: TriggerData): Collection { + val location = data.location ?: return emptyList() return listOf( NamedValue( diff --git a/core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/impl/TriggerPlaceholderText.kt b/core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/impl/TriggerPlaceholderText.kt index 3c4a16488..3b2df5653 100644 --- a/core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/impl/TriggerPlaceholderText.kt +++ b/core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/impl/TriggerPlaceholderText.kt @@ -1,15 +1,17 @@ package com.willfp.libreforge.triggers.placeholders.impl import com.willfp.libreforge.NamedValue -import com.willfp.libreforge.triggers.DispatchedTrigger +import com.willfp.libreforge.triggers.TriggerData import com.willfp.libreforge.triggers.placeholders.TriggerPlaceholder object TriggerPlaceholderText : TriggerPlaceholder("text") { - override fun createPlaceholders(trigger: DispatchedTrigger): Collection { + override fun createPlaceholders(data: TriggerData): Collection { + val text = data.text ?: return emptyList() + return listOf( NamedValue( listOf("text", "string", "message"), - trigger.data.text ?: "" + text ) ) } diff --git a/core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/impl/TriggerPlaceholderValue.kt b/core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/impl/TriggerPlaceholderValue.kt index ea2f6b036..ba0a49cb4 100644 --- a/core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/impl/TriggerPlaceholderValue.kt +++ b/core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/impl/TriggerPlaceholderValue.kt @@ -1,15 +1,15 @@ package com.willfp.libreforge.triggers.placeholders.impl import com.willfp.libreforge.NamedValue -import com.willfp.libreforge.triggers.DispatchedTrigger +import com.willfp.libreforge.triggers.TriggerData import com.willfp.libreforge.triggers.placeholders.TriggerPlaceholder object TriggerPlaceholderValue : TriggerPlaceholder("trigger_value") { - override fun createPlaceholders(trigger: DispatchedTrigger): Collection { + override fun createPlaceholders(data: TriggerData): Collection { return listOf( NamedValue( listOf("trigger_value", "triggervalue", "trigger", "value", "tv", "v", "t"), - trigger.data.value + data.value ) ) } diff --git a/core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/impl/TriggerPlaceholderVictim.kt b/core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/impl/TriggerPlaceholderVictim.kt index e06dbaeaa..716b3fcff 100644 --- a/core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/impl/TriggerPlaceholderVictim.kt +++ b/core/src/main/kotlin/com/willfp/libreforge/triggers/placeholders/impl/TriggerPlaceholderVictim.kt @@ -1,13 +1,13 @@ package com.willfp.libreforge.triggers.placeholders.impl import com.willfp.libreforge.NamedValue -import com.willfp.libreforge.triggers.DispatchedTrigger +import com.willfp.libreforge.triggers.TriggerData import com.willfp.libreforge.triggers.placeholders.TriggerPlaceholder import org.bukkit.attribute.Attribute object TriggerPlaceholderVictim : TriggerPlaceholder("victim") { - override fun createPlaceholders(trigger: DispatchedTrigger): Collection { - val victim = trigger.data.victim ?: return emptyList() + override fun createPlaceholders(data: TriggerData): Collection { + val victim = data.victim ?: return emptyList() return listOf( NamedValue(