Skip to content

Commit

Permalink
Added Potion Effect effects
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed Nov 21, 2021
1 parent 71d7e11 commit f9580c4
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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<UUID, PotionEffectType> ?: mutableMapOf()

meta[this.getUUID(player.getEffectAmount(this))] = effectType
}

override fun handleDisable(player: Player) {
val meta = player.getMetadata(metaKey).firstOrNull()?.value()
as? MutableMap<UUID, PotionEffectType> ?: 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<ConfigViolation> {
val violations = mutableListOf<ConfigViolation>()

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
}
}
Original file line number Diff line number Diff line change
@@ -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<ConfigViolation> {
val violations = mutableListOf<ConfigViolation>()

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

0 comments on commit f9580c4

Please sign in to comment.