diff --git a/eco-core/core-plugin/src/main/java/com/willfp/reforges/effects/Effect.java b/eco-core/core-plugin/src/main/java/com/willfp/reforges/effects/Effect.java index cdcd8d8..6634c44 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/reforges/effects/Effect.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/reforges/effects/Effect.java @@ -62,8 +62,15 @@ protected UUID getUUID(final int index, * @param player The player. * @param config The config. */ - public void handleEnabling(@NotNull final Player player, - @NotNull final JSONConfig config) { + public final void handleEnabling(@NotNull final Player player, + @NotNull final JSONConfig config) { + PlayerEffectStack.pushEffect(player, this); + + this.handleEnable(player, config); + } + + protected void handleEnable(@NotNull final Player player, + @NotNull final JSONConfig config) { // Override when needed. } @@ -72,7 +79,13 @@ public void handleEnabling(@NotNull final Player player, * * @param player The player. */ - public void handleDisabling(@NotNull final Player player) { + public final void handleDisabling(@NotNull final Player player) { + this.handleDisable(player); + + PlayerEffectStack.popEffect(player, this); + } + + protected void handleDisable(@NotNull final Player player) { // Override when needed. } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/reforges/effects/PlayerEffectStack.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/reforges/effects/PlayerEffectStack.kt new file mode 100644 index 0000000..62ddab2 --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/reforges/effects/PlayerEffectStack.kt @@ -0,0 +1,29 @@ +@file:JvmName("PlayerEffectStack") + +package com.willfp.reforges.effects + +import org.bukkit.entity.Player +import java.util.* + +private val effectStack = mutableMapOf>() + +fun Player.pushEffect(effect: Effect) { + val stack = effectStack[this.uniqueId] ?: mutableMapOf() + var amount = stack[effect] ?: 0 + amount++ + stack[effect] = amount + effectStack[this.uniqueId] = stack +} + +fun Player.popEffect(effect: Effect) { + val stack = effectStack[this.uniqueId] ?: mutableMapOf() + var amount = stack[effect] ?: 0 + amount-- + stack[effect] = if (amount < 0) 0 else amount + effectStack[this.uniqueId] = stack +} + +fun Player.getEffectAmount(effect: Effect): Int { + val stack = effectStack[this.uniqueId] ?: mutableMapOf() + return stack[effect] ?: 0 +} diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/reforges/effects/effects/EffectAttackSpeedMultiplier.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/reforges/effects/effects/EffectAttackSpeedMultiplier.kt index d100ec1..f66c255 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/reforges/effects/effects/EffectAttackSpeedMultiplier.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/reforges/effects/effects/EffectAttackSpeedMultiplier.kt @@ -2,17 +2,17 @@ package com.willfp.reforges.effects.effects import com.willfp.eco.core.config.interfaces.JSONConfig import com.willfp.reforges.effects.Effect +import com.willfp.reforges.effects.getEffectAmount import org.bukkit.attribute.Attribute import org.bukkit.attribute.AttributeModifier import org.bukkit.entity.Player class EffectAttackSpeedMultiplier : Effect("attack_speed_multiplier") { - override fun handleEnabling(player: Player, - config: JSONConfig) { + override fun handleEnable(player: Player, config: JSONConfig) { val attribute = player.getAttribute(Attribute.GENERIC_ATTACK_SPEED) ?: return attribute.addModifier( AttributeModifier( - this.getUUID(1), + this.getUUID(player.getEffectAmount(this)), this.id, config.getDouble("multiplier") - 1, AttributeModifier.Operation.MULTIPLY_SCALAR_1 @@ -20,11 +20,11 @@ class EffectAttackSpeedMultiplier : Effect("attack_speed_multiplier") { ) } - override fun handleDisabling(player: Player) { + override fun handleDisable(player: Player) { val attribute = player.getAttribute(Attribute.GENERIC_ATTACK_SPEED) ?: return attribute.removeModifier( AttributeModifier( - this.getUUID(1), + this.getUUID(player.getEffectAmount(this)), this.id, 0.0, AttributeModifier.Operation.MULTIPLY_SCALAR_1 diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/reforges/effects/effects/EffectKnockbackMultiplier.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/reforges/effects/effects/EffectKnockbackMultiplier.kt index 07da49d..0f67c7a 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/reforges/effects/effects/EffectKnockbackMultiplier.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/reforges/effects/effects/EffectKnockbackMultiplier.kt @@ -2,17 +2,20 @@ package com.willfp.reforges.effects.effects import com.willfp.eco.core.config.interfaces.JSONConfig import com.willfp.reforges.effects.Effect +import com.willfp.reforges.effects.getEffectAmount import org.bukkit.attribute.Attribute import org.bukkit.attribute.AttributeModifier import org.bukkit.entity.Player class EffectKnockbackMultiplier : Effect("knockback_multiplier") { - override fun handleEnabling(player: Player, - config: JSONConfig) { + override fun handleEnable( + player: Player, + config: JSONConfig + ) { val attribute = player.getAttribute(Attribute.GENERIC_ATTACK_KNOCKBACK) ?: return attribute.addModifier( AttributeModifier( - this.getUUID(1), + this.getUUID(player.getEffectAmount(this)), this.id, config.getDouble("multiplier") - 1, AttributeModifier.Operation.MULTIPLY_SCALAR_1 @@ -20,11 +23,11 @@ class EffectKnockbackMultiplier : Effect("knockback_multiplier") { ) } - override fun handleDisabling(player: Player) { + override fun handleDisable(player: Player) { val attribute = player.getAttribute(Attribute.GENERIC_ATTACK_KNOCKBACK) ?: return attribute.removeModifier( AttributeModifier( - this.getUUID(1), + this.getUUID(player.getEffectAmount(this)), this.id, 0.0, AttributeModifier.Operation.MULTIPLY_SCALAR_1 diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/reforges/effects/effects/EffectMovementSpeedMultiplier.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/reforges/effects/effects/EffectMovementSpeedMultiplier.kt index 5e6fb16..a64132f 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/reforges/effects/effects/EffectMovementSpeedMultiplier.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/reforges/effects/effects/EffectMovementSpeedMultiplier.kt @@ -2,17 +2,20 @@ package com.willfp.reforges.effects.effects import com.willfp.eco.core.config.interfaces.JSONConfig import com.willfp.reforges.effects.Effect +import com.willfp.reforges.effects.getEffectAmount import org.bukkit.attribute.Attribute import org.bukkit.attribute.AttributeModifier import org.bukkit.entity.Player class EffectMovementSpeedMultiplier : Effect("movement_speed_multiplier") { - override fun handleEnabling(player: Player, - config: JSONConfig) { + override fun handleEnable( + player: Player, + config: JSONConfig + ) { val attribute = player.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED) ?: return attribute.addModifier( AttributeModifier( - this.getUUID(1), + this.getUUID(player.getEffectAmount(this)), this.id, config.getDouble("multiplier") - 1, AttributeModifier.Operation.MULTIPLY_SCALAR_1 @@ -20,11 +23,11 @@ class EffectMovementSpeedMultiplier : Effect("movement_speed_multiplier") { ) } - override fun handleDisabling(player: Player) { + override fun handleDisable(player: Player) { val attribute = player.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED) ?: return attribute.removeModifier( AttributeModifier( - this.getUUID(1), + this.getUUID(player.getEffectAmount(this)), this.id, 0.0, AttributeModifier.Operation.MULTIPLY_SCALAR_1