package net.mcreator.customoregen.procedures;

import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.core.Holder;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ExperienceOrb;
import net.minecraft.world.entity.Entity;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.core.BlockPos;

public class OreexperienceProcedure {
	public static void execute(LevelAccessor world, double x, double y, double z, Entity entity) {
		if (entity == null)
			return;
		// 1.21: Enchantment API changed - use getAllEnchantments with registry lookup
		boolean hasSilkTouch = false;
		if (entity instanceof LivingEntity _livEnt) {
			ItemStack mainHandItem = _livEnt.getMainHandItem();
			var enchantmentRegistry = world.registryAccess().lookupOrThrow(Registries.ENCHANTMENT);
			for (Holder<Enchantment> enchantment : mainHandItem.getAllEnchantments(enchantmentRegistry).keySet()) {
				if (enchantment.is(Enchantments.SILK_TOUCH)) {
					hasSilkTouch = mainHandItem.getAllEnchantments(enchantmentRegistry).getLevel(enchantment) > 0;
					break;
				}
			}
		}

		if (!hasSilkTouch) {
			// XP is now handled centrally in ConfigurableOreDropsProcedure to match Vanilla 1.21 values
			// if (world instanceof ServerLevel _level)
			// 	_level.addFreshEntity(new ExperienceOrb(_level, x, y, z, 2));
		}
	}
}
