From 03151ad4ca3f2693717069ccdd1d1e5118323da4 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Thu, 25 Nov 2021 16:15:32 +0000 Subject: [PATCH] Reworked Hunger, Regen, and Xp multipliers to not be triggered --- .../com/willfp/libreforge/LibReforge.kt | 4 +- .../libreforge/effects/MultiplierModifier.kt | 5 ++ .../effects/effects/EffectHungerMultiplier.kt | 65 +++++++++++++----- .../effects/effects/EffectRegenMultiplier.kt | 67 +++++++++++++++---- .../effects/effects/EffectXpMultiplier.kt | 60 +++++++++++++---- 5 files changed, 159 insertions(+), 42 deletions(-) create mode 100644 eco-api/src/main/kotlin/com/willfp/libreforge/effects/MultiplierModifier.kt diff --git a/eco-api/src/main/kotlin/com/willfp/libreforge/LibReforge.kt b/eco-api/src/main/kotlin/com/willfp/libreforge/LibReforge.kt index b01c2db49..332c6be02 100644 --- a/eco-api/src/main/kotlin/com/willfp/libreforge/LibReforge.kt +++ b/eco-api/src/main/kotlin/com/willfp/libreforge/LibReforge.kt @@ -54,9 +54,7 @@ object LibReforge { plugin.eventManager.registerListener(condition) } for (effect in Effects.values()) { - if (effect is Listener) { - plugin.eventManager.registerListener(effect) - } + plugin.eventManager.registerListener(effect) } for (trigger in Triggers.values()) { plugin.eventManager.registerListener(trigger) diff --git a/eco-api/src/main/kotlin/com/willfp/libreforge/effects/MultiplierModifier.kt b/eco-api/src/main/kotlin/com/willfp/libreforge/effects/MultiplierModifier.kt new file mode 100644 index 000000000..993ca3c2f --- /dev/null +++ b/eco-api/src/main/kotlin/com/willfp/libreforge/effects/MultiplierModifier.kt @@ -0,0 +1,5 @@ +package com.willfp.libreforge.effects + +import java.util.UUID + +data class MultiplierModifier(val uuid: UUID, val multiplier: Double) \ No newline at end of file diff --git a/eco-api/src/main/kotlin/com/willfp/libreforge/effects/effects/EffectHungerMultiplier.kt b/eco-api/src/main/kotlin/com/willfp/libreforge/effects/effects/EffectHungerMultiplier.kt index 2c8f4b892..0e7009b3a 100644 --- a/eco-api/src/main/kotlin/com/willfp/libreforge/effects/effects/EffectHungerMultiplier.kt +++ b/eco-api/src/main/kotlin/com/willfp/libreforge/effects/effects/EffectHungerMultiplier.kt @@ -1,33 +1,68 @@ package com.willfp.libreforge.effects.effects import com.willfp.eco.core.config.interfaces.JSONConfig +import com.willfp.eco.core.integrations.mcmmo.McmmoManager import com.willfp.eco.util.NumberUtils import com.willfp.libreforge.ConfigViolation import com.willfp.libreforge.effects.Effect -import com.willfp.libreforge.triggers.TriggerData -import com.willfp.libreforge.triggers.TriggerParameter -import com.willfp.libreforge.triggers.Triggers +import com.willfp.libreforge.effects.MultiplierModifier +import com.willfp.libreforge.effects.getEffectAmount import com.willfp.libreforge.triggers.wrappers.WrappedHungerEvent +import org.bukkit.entity.Player +import org.bukkit.event.EventHandler +import org.bukkit.event.entity.FoodLevelChangeEvent +import java.util.UUID import kotlin.math.ceil -class EffectHungerMultiplier : Effect( - "hunger_multiplier", - supportsFilters = false, - applicableTriggers = Triggers.withParameters( - TriggerParameter.EVENT - ) -) { - override fun handle(data: TriggerData, config: JSONConfig) { - val event = data.event as? WrappedHungerEvent ?: return +class EffectHungerMultiplier : Effect("hunger_multiplier") { + private val modifiers = mutableMapOf>() - val multiplier = config.getDouble("multiplier") + override fun handleEnable(player: Player, config: JSONConfig) { + val registeredModifiers = modifiers[player.uniqueId] ?: mutableListOf() + val uuid = this.getUUID(player.getEffectAmount(this)) + registeredModifiers.removeIf { it.uuid == uuid } + registeredModifiers.add( + MultiplierModifier( + uuid, + config.getDouble("multiplier") + ) + ) + modifiers[player.uniqueId] = registeredModifiers + } + + override fun handleDisable(player: Player) { + val registeredModifiers = modifiers[player.uniqueId] ?: mutableListOf() + val uuid = this.getUUID(player.getEffectAmount(this)) + registeredModifiers.removeIf { it.uuid == uuid } + modifiers[player.uniqueId] = registeredModifiers + } + + @EventHandler(ignoreCancelled = true) + fun handle(event: FoodLevelChangeEvent) { + if (McmmoManager.isFake(event)) { + return + } + + val player = event.entity + + if (player !is Player) { + return + } + + var multiplier = 1.0 + + for (modifier in (modifiers[player.uniqueId] ?: emptyList())) { + multiplier *= modifier.multiplier + } + + val wrapped = WrappedHungerEvent(event) if (multiplier < 1) { if (NumberUtils.randFloat(0.0, 1.0) > multiplier) { - event.isCancelled = true + wrapped.isCancelled = true } } else { - event.amount = ceil(event.amount * multiplier).toInt() + wrapped.amount = ceil(wrapped.amount * multiplier).toInt() } } diff --git a/eco-api/src/main/kotlin/com/willfp/libreforge/effects/effects/EffectRegenMultiplier.kt b/eco-api/src/main/kotlin/com/willfp/libreforge/effects/effects/EffectRegenMultiplier.kt index 89e901334..f9297eb85 100644 --- a/eco-api/src/main/kotlin/com/willfp/libreforge/effects/effects/EffectRegenMultiplier.kt +++ b/eco-api/src/main/kotlin/com/willfp/libreforge/effects/effects/EffectRegenMultiplier.kt @@ -1,24 +1,67 @@ package com.willfp.libreforge.effects.effects import com.willfp.eco.core.config.interfaces.JSONConfig +import com.willfp.eco.core.integrations.mcmmo.McmmoManager +import com.willfp.eco.util.NumberUtils import com.willfp.libreforge.ConfigViolation import com.willfp.libreforge.effects.Effect +import com.willfp.libreforge.effects.MultiplierModifier +import com.willfp.libreforge.effects.getEffectAmount import com.willfp.libreforge.triggers.TriggerData import com.willfp.libreforge.triggers.TriggerParameter import com.willfp.libreforge.triggers.Triggers +import com.willfp.libreforge.triggers.wrappers.WrappedHungerEvent import com.willfp.libreforge.triggers.wrappers.WrappedRegenEvent +import org.bukkit.entity.Player +import org.bukkit.event.EventHandler +import org.bukkit.event.entity.EntityRegainHealthEvent +import org.bukkit.event.entity.FoodLevelChangeEvent +import java.util.UUID +import kotlin.math.ceil -class EffectRegenMultiplier : Effect( - "regen_multiplier", - supportsFilters = false, - applicableTriggers = Triggers.withParameters( - TriggerParameter.EVENT - ) -) { - override fun handle(data: TriggerData, config: JSONConfig) { - val event = data.event as? WrappedRegenEvent ?: return - - event.amount *= config.getDouble("multiplier") +class EffectRegenMultiplier : Effect("regen_multiplier") { + private val modifiers = mutableMapOf>() + + override fun handleEnable(player: Player, config: JSONConfig) { + val registeredModifiers = modifiers[player.uniqueId] ?: mutableListOf() + val uuid = this.getUUID(player.getEffectAmount(this)) + registeredModifiers.removeIf { it.uuid == uuid } + registeredModifiers.add( + MultiplierModifier( + uuid, + config.getDouble("multiplier") + ) + ) + modifiers[player.uniqueId] = registeredModifiers + } + + override fun handleDisable(player: Player) { + val registeredModifiers = modifiers[player.uniqueId] ?: mutableListOf() + val uuid = this.getUUID(player.getEffectAmount(this)) + registeredModifiers.removeIf { it.uuid == uuid } + modifiers[player.uniqueId] = registeredModifiers + } + + @EventHandler(ignoreCancelled = true) + fun handle(event: EntityRegainHealthEvent) { + if (McmmoManager.isFake(event)) { + return + } + + val player = event.entity + + if (player !is Player) { + return + } + + var multiplier = 1.0 + + for (modifier in (modifiers[player.uniqueId] ?: emptyList())) { + multiplier *= modifier.multiplier + } + + val wrapped = WrappedRegenEvent(event) + wrapped.amount *= multiplier } override fun validateConfig(config: JSONConfig): List { @@ -34,4 +77,4 @@ class EffectRegenMultiplier : Effect( return violations } -} \ No newline at end of file +} diff --git a/eco-api/src/main/kotlin/com/willfp/libreforge/effects/effects/EffectXpMultiplier.kt b/eco-api/src/main/kotlin/com/willfp/libreforge/effects/effects/EffectXpMultiplier.kt index 18ce59c46..0eb63eb44 100644 --- a/eco-api/src/main/kotlin/com/willfp/libreforge/effects/effects/EffectXpMultiplier.kt +++ b/eco-api/src/main/kotlin/com/willfp/libreforge/effects/effects/EffectXpMultiplier.kt @@ -1,26 +1,62 @@ package com.willfp.libreforge.effects.effects import com.willfp.eco.core.config.interfaces.JSONConfig +import com.willfp.eco.core.events.NaturalExpGainEvent +import com.willfp.eco.core.integrations.mcmmo.McmmoManager import com.willfp.libreforge.ConfigViolation import com.willfp.libreforge.effects.Effect +import com.willfp.libreforge.effects.MultiplierModifier +import com.willfp.libreforge.effects.getEffectAmount import com.willfp.libreforge.triggers.TriggerData import com.willfp.libreforge.triggers.TriggerParameter import com.willfp.libreforge.triggers.Triggers import com.willfp.libreforge.triggers.wrappers.WrappedRegenEvent import com.willfp.libreforge.triggers.wrappers.WrappedXpEvent +import org.bukkit.entity.Player +import org.bukkit.event.EventHandler +import org.bukkit.event.entity.EntityRegainHealthEvent +import java.util.UUID import kotlin.math.ceil -class EffectXpMultiplier : Effect( - "xp_multiplier", - supportsFilters = false, - applicableTriggers = Triggers.withParameters( - TriggerParameter.EVENT - ) -) { - override fun handle(data: TriggerData, config: JSONConfig) { - val event = data.event as? WrappedXpEvent ?: return - - event.amount = ceil(event.amount * config.getDouble("multiplier")).toInt() +class EffectXpMultiplier : Effect("xp_multiplier") { + private val modifiers = mutableMapOf>() + + override fun handleEnable(player: Player, config: JSONConfig) { + val registeredModifiers = modifiers[player.uniqueId] ?: mutableListOf() + val uuid = this.getUUID(player.getEffectAmount(this)) + registeredModifiers.removeIf { it.uuid == uuid } + registeredModifiers.add( + MultiplierModifier( + uuid, + config.getDouble("multiplier") + ) + ) + modifiers[player.uniqueId] = registeredModifiers + } + + override fun handleDisable(player: Player) { + val registeredModifiers = modifiers[player.uniqueId] ?: mutableListOf() + val uuid = this.getUUID(player.getEffectAmount(this)) + registeredModifiers.removeIf { it.uuid == uuid } + modifiers[player.uniqueId] = registeredModifiers + } + + @EventHandler(ignoreCancelled = true) + fun handle(event: NaturalExpGainEvent) { + if (McmmoManager.isFake(event)) { + return + } + + val player = event.expChangeEvent.player + + var multiplier = 1.0 + + for (modifier in (modifiers[player.uniqueId] ?: emptyList())) { + multiplier *= modifier.multiplier + } + + val wrapped = WrappedXpEvent(event.expChangeEvent) + wrapped.amount = ceil(wrapped.amount * multiplier).toInt() } override fun validateConfig(config: JSONConfig): List { @@ -36,4 +72,4 @@ class EffectXpMultiplier : Effect( return violations } -} \ No newline at end of file +}