diff --git a/eco-api/src/main/kotlin/com/willfp/libreforge/effects/Effects.kt b/eco-api/src/main/kotlin/com/willfp/libreforge/effects/Effects.kt index 25c42e792..2c49cde8f 100644 --- a/eco-api/src/main/kotlin/com/willfp/libreforge/effects/Effects.kt +++ b/eco-api/src/main/kotlin/com/willfp/libreforge/effects/Effects.kt @@ -26,6 +26,8 @@ object Effects { val SPAWN_MOBS: Effect = EffectSpawnMobs() val HUNGER_MULTIPLIER: Effect = EffectHungerMultiplier() val REGEN_MULTIPLIER: Effect = EffectRegenMultiplier() + val PERMANENT_POTION_EFFECT: Effect = EffectPermanentPotionEffect() + val POTION_EFFECT: Effect = EffectPotionEffect() /** * Get effect matching id. diff --git a/eco-api/src/main/kotlin/com/willfp/libreforge/effects/effects/EffectPermanentPotionEffect.kt b/eco-api/src/main/kotlin/com/willfp/libreforge/effects/effects/EffectPermanentPotionEffect.kt new file mode 100644 index 000000000..3e63f8e0b --- /dev/null +++ b/eco-api/src/main/kotlin/com/willfp/libreforge/effects/effects/EffectPermanentPotionEffect.kt @@ -0,0 +1,74 @@ +package com.willfp.libreforge.effects.effects + +import com.willfp.eco.core.config.interfaces.JSONConfig +import com.willfp.libreforge.ConfigViolation +import com.willfp.libreforge.LibReforge +import com.willfp.libreforge.effects.Effect +import com.willfp.libreforge.effects.getEffectAmount +import org.bukkit.entity.Player +import org.bukkit.potion.PotionEffect +import org.bukkit.potion.PotionEffectType +import java.util.* + +@Suppress("UNCHECKED_CAST") +class EffectPermanentPotionEffect : Effect("permanent_potion_effect") { + private val metaKey = "${LibReforge.plugin.name}_${this.id}" + + override fun handleEnable(player: Player, config: JSONConfig) { + val effectType = PotionEffectType.getByName(config.getString("effect")) + ?: PotionEffectType.INCREASE_DAMAGE + + val effect = PotionEffect( + effectType, + 1_500_000_000, + config.getInt("level") - 1, + false, + false, + true + ) + + player.addPotionEffect(effect) + + val meta = player.getMetadata(metaKey).firstOrNull()?.value() + as? MutableMap ?: mutableMapOf() + + meta[this.getUUID(player.getEffectAmount(this))] = effectType + } + + override fun handleDisable(player: Player) { + val meta = player.getMetadata(metaKey).firstOrNull()?.value() + as? MutableMap ?: mutableMapOf() + + val toRemove = meta[this.getUUID(player.getEffectAmount(this))] ?: return + + val active = player.getPotionEffect(toRemove) ?: return + + if (active.duration < 1_000_000_000) { + return + } + + player.removePotionEffect(toRemove) + } + + override fun validateConfig(config: JSONConfig): List { + val violations = mutableListOf() + + config.getStringOrNull("effect") + ?: violations.add( + ConfigViolation( + "effect", + "You must specify the potion effect!" + ) + ) + + config.getIntOrNull("level") + ?: violations.add( + ConfigViolation( + "level", + "You must specify the effect level!" + ) + ) + + return violations + } +} \ No newline at end of file diff --git a/eco-api/src/main/kotlin/com/willfp/libreforge/effects/effects/EffectPotionEffect.kt b/eco-api/src/main/kotlin/com/willfp/libreforge/effects/effects/EffectPotionEffect.kt new file mode 100644 index 000000000..cb8775437 --- /dev/null +++ b/eco-api/src/main/kotlin/com/willfp/libreforge/effects/effects/EffectPotionEffect.kt @@ -0,0 +1,76 @@ +package com.willfp.libreforge.effects.effects + +import com.willfp.eco.core.config.interfaces.JSONConfig +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 org.bukkit.potion.PotionEffect +import org.bukkit.potion.PotionEffectType + +class EffectPotionEffect : Effect( + "potion_effect", + applicableTriggers = Triggers.withParameters( + TriggerParameter.PLAYER, + TriggerParameter.VICTIM + ) +) { + override fun handle(data: TriggerData, config: JSONConfig) { + val toApply = if (config.getBool("apply_to_player")) { + data.player ?: return + } else { + data.victim ?: return + } + + toApply.addPotionEffect( + PotionEffect( + PotionEffectType.getByName(config.getString("effect")) + ?: PotionEffectType.INCREASE_DAMAGE, + 1_500_000_000, + config.getInt("level") - 1, + false, + true, + true + ) + ) + } + + override fun validateConfig(config: JSONConfig): List { + val violations = mutableListOf() + + config.getStringOrNull("effect") + ?: violations.add( + ConfigViolation( + "effect", + "You must specify the potion effect!" + ) + ) + + config.getIntOrNull("level") + ?: violations.add( + ConfigViolation( + "level", + "You must specify the effect level!" + ) + ) + + config.getIntOrNull("duration") + ?: violations.add( + ConfigViolation( + "duration", + "You must specify the duration!" + ) + ) + + config.getBoolOrNull("apply_to_player") + ?: violations.add( + ConfigViolation( + "apply_to_player", + "You must specify whether the player or victim gets the effect!" + ) + ) + + return violations + } +} \ No newline at end of file