Skip to content

Commit

Permalink
Reworked Hunger, Regen, and Xp multipliers to not be triggered
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed Nov 25, 2021
1 parent 8218a13 commit 03151ad
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 42 deletions.
4 changes: 1 addition & 3 deletions eco-api/src/main/kotlin/com/willfp/libreforge/LibReforge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.willfp.libreforge.effects

import java.util.UUID

data class MultiplierModifier(val uuid: UUID, val multiplier: Double)
Original file line number Diff line number Diff line change
@@ -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<UUID, MutableList<MultiplierModifier>>()

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()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<UUID, MutableList<MultiplierModifier>>()

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<ConfigViolation> {
Expand All @@ -34,4 +77,4 @@ class EffectRegenMultiplier : Effect(

return violations
}
}
}
Original file line number Diff line number Diff line change
@@ -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<UUID, MutableList<MultiplierModifier>>()

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<ConfigViolation> {
Expand All @@ -36,4 +72,4 @@ class EffectXpMultiplier : Effect(

return violations
}
}
}

0 comments on commit 03151ad

Please sign in to comment.