package ru.dbotthepony.mc.otm.entity

import net.minecraft.world.entity.EquipmentSlot
import net.minecraft.world.entity.monster.WitherSkeleton
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.Items
import net.neoforged.bus.api.SubscribeEvent
import net.neoforged.neoforge.event.entity.EntityJoinLevelEvent
import ru.dbotthepony.mc.otm.config.ServerConfig
import ru.dbotthepony.mc.otm.core.otmRandom
import ru.dbotthepony.mc.otm.registry.game.MItems


object WitheredSkeletonSpawnHandler {
	@SubscribeEvent
	fun onEntityJoin(event: EntityJoinLevelEvent) {
		val entity = event.entity

		if (entity is WitherSkeleton) {
			val giveHelmet = event.level.otmRandom.nextFloat() < ServerConfig.WITHER_SKELETON_HELMET_CHANCE
			val giveSword = event.level.otmRandom.nextFloat() < ServerConfig.WITHER_SKELETON_SWORD_CHANCE

			if (giveHelmet) {
				if (!entity.hasItemInSlot(EquipmentSlot.HEAD))
					entity.setItemSlot(EquipmentSlot.HEAD, ItemStack(Items.NETHERITE_HELMET))
			}

			if (giveSword || giveHelmet) {
				if (!entity.hasItemInSlot(EquipmentSlot.MAINHAND) || entity.getItemBySlot(EquipmentSlot.MAINHAND).item == Items.STONE_SWORD)
					entity.setItemSlot(EquipmentSlot.MAINHAND, ItemStack(MItems.WITHERED_STEEL_SWORD))
			}
		}
	}
}
