Files
src
pom.xml
Code
8 447
src/main/kotlin/com/kodari/paladinboss/boss/PaladinBossManager.kt
package com.kodari.paladinboss.boss
import com.kodari.paladinboss.PaladinBossPlugin
import org.bukkit.Location
import org.bukkit.Material
import org.bukkit.attribute.Attribute
import org.bukkit.boss.BarColor
import org.bukkit.boss.BarStyle
import org.bukkit.enchantments.Enchantment
import org.bukkit.entity.EntityType
import org.bukkit.entity.LivingEntity
import org.bukkit.entity.Player
import org.bukkit.entity.Zombie
import org.bukkit.inventory.EquipmentSlot
import org.bukkit.inventory.ItemStack
import org.bukkit.potion.PotionEffect
import org.bukkit.potion.PotionEffectType
import org.bukkit.util.Vector
import java.util.UUID
import kotlin.random.Random
class PaladinBossManager(private val plugin: PaladinBossPlugin) {
private val activeBosses = mutableMapOf<UUID, ActivePaladinBoss>()
fun isPaladinBoss(entityId: UUID): Boolean = activeBosses.containsKey(entityId)
fun getActiveBoss(entityId: UUID): ActivePaladinBoss? = activeBosses[entityId]
fun spawnBoss(location: Location): LivingEntity {
val config = plugin.config
val maxHealth = config.getDouble("boss.max-health", 200.0)
val bossName = config.getString("boss.name", "&6&lPaladin Champion")!!
val zombie = location.world!!.spawn(location, Zombie::class.java) { entity ->
entity.customName(net.kyori.adventure.text.Component.text(
org.bukkit.ChatColor.translateAlternateColorCodes('&', bossName)
))
entity.isCustomNameVisible = true
entity.getAttribute(Attribute.MAX_HEALTH)?.baseValue = maxHealth
entity.health = maxHealth
entity.getAttribute(Attribute.MOVEMENT_SPEED)?.baseValue = 0.28
entity.getAttribute(Attribute.ATTACK_DAMAGE)?.baseValue = 8.0
entity.isPersistent = true
entity.removeWhenFarAway = false
}
equipPaladin(zombie)
val bossBar = plugin.server.createBossBar(
org.bukkit.ChatColor.translateAlternateColorCodes('&', bossName),
BarColor.WHITE,
BarStyle.SOLID
)
bossBar.progress = 1.0
val active = ActivePaladinBoss(zombie, bossBar)
activeBosses[zombie.uniqueId] = active
val task = plugin.server.scheduler.runTaskTimer(plugin, Runnable {
runAbilityCycle(active)
}, 100L, config.getLong("boss.ability-interval-ticks", 100L))
active.abilityTask = task
return zombie
}
private fun equipPaladin(zombie: Zombie) {
val equipment = zombie.equipment ?: return
val helmet = ItemStack(Material.GOLDEN_HELMET)
val chestplate = ItemStack(Material.GOLDEN_CHESTPLATE)
val leggings = ItemStack(Material.GOLDEN_LEGGINGS)
val boots = ItemStack(Material.GOLDEN_BOOTS)
val sword = ItemStack(Material.GOLDEN_SWORD)
sword.addUnsafeEnchantment(Enchantment.SHARPNESS, 3)
equipment.setItem(EquipmentSlot.HEAD, helmet)
equipment.setItem(EquipmentSlot.CHEST, chestplate)
equipment.setItem(EquipmentSlot.LEGS, leggings)
equipment.setItem(EquipmentSlot.FEET, boots)
equipment.setItem(EquipmentSlot.HAND, sword)
equipment.setDropChance(EquipmentSlot.HEAD, 0f)
equipment.setDropChance(EquipmentSlot.CHEST, 0f)
equipment.setDropChance(EquipmentSlot.LEGS, 0f)
equipment.setDropChance(EquipmentSlot.FEET, 0f)
equipment.setDropChance(EquipmentSlot.HAND, 0f)
}
fun addPlayerToBossBar(entityId: UUID, player: Player) {
activeBosses[entityId]?.bossBar?.addPlayer(player)
}
fun updateBossBar(entityId: UUID) {
val active = activeBosses[entityId] ?: return
val entity = active.entity
val maxHealth = entity.getAttribute(Attribute.MAX_HEALTH)?.value ?: 1.0
active.bossBar.progress = (entity.health / maxHealth).coerceIn(0.0, 1.0)
}
private fun runAbilityCycle(active: ActivePaladinBoss) {
val entity = active.entity
if (!entity.isValid || entity.isDead) {
removeBoss(entity.uniqueId)
return
}
val nearbyPlayers = entity.location.getNearbyPlayers(
plugin.config.getDouble("boss.smite-radius", 6.0)
).toList()
if (nearbyPlayers.isEmpty()) return
when (Random.nextInt(3)) {
0 -> holySmite(entity, nearbyPlayers)
1 -> healingLight(entity)
2 -> chargeTarget(entity, nearbyPlayers.random())
}
if (Random.nextInt(4) == 0) {
summonMinions(entity)
}
}
private fun holySmite(boss: LivingEntity, targets: List<Player>) {
val damage = plugin.config.getDouble("boss.smite-damage", 6.0)
for (target in targets) {
target.world.strikeLightningEffect(target.location)
target.damage(damage, boss)
}
}
private fun healingLight(boss: LivingEntity) {
val healAmount = plugin.config.getDouble("boss.heal-amount", 15.0)
val maxHealth = boss.getAttribute(Attribute.MAX_HEALTH)?.value ?: boss.health
boss.health = (boss.health + healAmount).coerceAtMost(maxHealth)
boss.world.spawnParticle(org.bukkit.Particle.HEART, boss.location.add(0.0, 2.0, 0.0), 10)
}
private fun chargeTarget(boss: LivingEntity, target: Player) {
val multiplier = plugin.config.getDouble("boss.charge-speed-multiplier", 2.5)
val direction = target.location.toVector().subtract(boss.location.toVector()).normalize()
boss.velocity = direction.multiply(multiplier) as Vector
boss.addPotionEffect(PotionEffect(PotionEffectType.SPEED, 40, 1))
}
private fun summonMinions(boss: LivingEntity) {
val amount = plugin.config.getInt("boss.minions-to-summon", 3)
repeat(amount) {
val loc = boss.location.clone().add(
Random.nextDouble(-3.0, 3.0), 0.0, Random.nextDouble(-3.0, 3.0)
)
boss.world.spawnEntity(loc, EntityType.VINDICATOR)
}
}
fun removeBoss(entityId: UUID) {
val active = activeBosses.remove(entityId) ?: return
active.abilityTask?.cancel()
active.bossBar.removeAll()
if (active.entity.isValid) {
active.entity.remove()
}
}
fun removeAllBosses() {
activeBosses.keys.toList().forEach { removeBoss(it) }
}
}