From addb36c6f71af1293a0ce17d6ae1bf5c3ebba203 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Wed, 24 May 2023 14:45:13 +0100 Subject: [PATCH] Unused triggers will no longer be dispatched --- .../libreforge/triggers/DispatchedTriggerFactory.kt | 4 ++++ .../main/kotlin/com/willfp/libreforge/triggers/Trigger.kt | 6 ++++++ .../kotlin/com/willfp/libreforge/triggers/Triggers.kt | 8 ++++++++ 3 files changed, 18 insertions(+) diff --git a/core/src/main/kotlin/com/willfp/libreforge/triggers/DispatchedTriggerFactory.kt b/core/src/main/kotlin/com/willfp/libreforge/triggers/DispatchedTriggerFactory.kt index 6b32b0dc6..36e6acac8 100644 --- a/core/src/main/kotlin/com/willfp/libreforge/triggers/DispatchedTriggerFactory.kt +++ b/core/src/main/kotlin/com/willfp/libreforge/triggers/DispatchedTriggerFactory.kt @@ -17,6 +17,10 @@ class DispatchedTriggerFactory( private val playerTriggers = listMap() fun create(player: Player, trigger: Trigger, data: TriggerData): DispatchedTrigger? { + if (!trigger.isEnabled) { + return null + } + val hash = (trigger.hashCode() shl 5) xor data.hashCode() if (hash in playerTriggers[player.uniqueId]) { return null 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 7c1eeccde..935163fe7 100644 --- a/core/src/main/kotlin/com/willfp/libreforge/triggers/Trigger.kt +++ b/core/src/main/kotlin/com/willfp/libreforge/triggers/Trigger.kt @@ -19,6 +19,12 @@ abstract class Trigger( */ abstract val parameters: Set + /** + * Whether this trigger is enabled. + */ + var isEnabled: Boolean = false + internal set + /** * Dispatch the trigger. */ 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 64116c2df..c7aaf53bc 100644 --- a/core/src/main/kotlin/com/willfp/libreforge/triggers/Triggers.kt +++ b/core/src/main/kotlin/com/willfp/libreforge/triggers/Triggers.kt @@ -84,8 +84,16 @@ object Triggers : Registry() { /** * Get a trigger by [id]. + * + * This will enable the trigger. */ override fun get(id: String): Trigger? { + return doGet(id)?.apply { + isEnabled = true + } + } + + private fun doGet(id: String): Trigger? { for (group in groupRegistry.values()) { if (id.startsWith("${group.prefix}_")) { return group.create(id.removePrefix("${group.prefix}_"))