Skip to content

Commit

Permalink
Added support for multiple of same effect being activated at the same…
Browse files Browse the repository at this point in the history
… time
  • Loading branch information
WillFP committed Oct 4, 2021
1 parent 9c06f70 commit 7ed6e59
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}

Expand All @@ -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.
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@ 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
)
)
}

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,32 @@ 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
)
)
}

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,32 @@ 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
)
)
}

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
Expand Down

0 comments on commit 7ed6e59

Please sign in to comment.