package pw.hamzantal.bloodmoon.listeners

import hazae41.minecraft.kutils.bukkit.schedule
import org.bukkit.entity.Arrow
import org.bukkit.entity.Skeleton
import org.bukkit.event.entity.ProjectileLaunchEvent
import pw.hamzantal.bloodmoon.BMData
import pw.hamzantal.bloodmoon.BloodMoon

val recent = mutableListOf<Skeleton>()

fun onSkellyShoot(e: ProjectileLaunchEvent) {
    val sh = e.entity.shooter
    if (!BMData.running || BMData.transformed.any { (d, _) -> d.entity == sh }) return
    if (sh !is Skeleton || sh in recent) return

    var count = 5
    var taskID = 0

    recent += sh as Skeleton
    var pl = BloodMoon.pl
    taskID = pl.server.scheduler.scheduleSyncRepeatingTask(pl, {
        sh.launchProjectile(Arrow::class.java).apply arrow@{
            pl.schedule(delay = 2) {
                this@arrow.remove()
            }
        }
        if (--count == 0) {
            recent -= sh as Skeleton
            pl.server.scheduler.cancelTask(taskID)
        }
    }, 7, 7)
}