package pers.neige.neigeitems.listener

import org.bukkit.event.EventPriority
import org.bukkit.event.player.PlayerInteractEntityEvent
import org.bukkit.inventory.EquipmentSlot
import pers.neige.neigeitems.annotation.Listener
import pers.neige.neigeitems.item.ItemDurability
import pers.neige.neigeitems.libs.bot.inker.bukkit.nbt.NbtCompound
import pers.neige.neigeitems.utils.ItemUtils.isNiItem

object PlayerInteractEntityListener {
    @JvmStatic
    @Listener(eventPriority = EventPriority.LOWEST, ignoreCancelled = true)
    private fun listener(event: PlayerInteractEntityEvent) {
        val player = event.player
        // 对于已损坏物品取消事件
        val itemStack = when (event.hand) {
            EquipmentSlot.HAND -> player.inventory.itemInMainHand
            else -> player.inventory.itemInOffHand
        }
        // 获取NI物品信息(不是NI物品就停止操作)
        val itemInfo = itemStack.isNiItem() ?: return
        // NI物品数据
        val neigeItems: NbtCompound = itemInfo.neigeItems

        // 检测已损坏物品
        ItemDurability.durabilityChecker(player, neigeItems, event)
        if (event.isCancelled) return
    }
}