9
0
mirror of https://github.com/Auxilor/Reforges.git synced 2025-12-24 17:39:32 +00:00

Added support for multiple of same effect being activated at the same time

This commit is contained in:
Auxilor
2021-10-04 11:08:55 +01:00
parent 9c06f7013f
commit 7ed6e59a44
5 changed files with 66 additions and 18 deletions

View File

@@ -62,8 +62,15 @@ public abstract class Effect implements Listener, Watcher {
* @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 abstract class Effect implements Listener, Watcher {
*
* @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.
}

View File

@@ -0,0 +1,29 @@
@file:JvmName("PlayerEffectStack")
package com.willfp.reforges.effects
import org.bukkit.entity.Player
import java.util.*
private val effectStack = mutableMapOf<UUID, MutableMap<Effect, Int>>()
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
}

View File

@@ -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

View File

@@ -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

View File

@@ -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