package net.minecraft.core.dispenser;

import java.util.Iterator;
import java.util.List;
import net.minecraft.world.entity.Shearable;
import java.util.function.Predicate;
import net.minecraft.world.entity.EntitySelector;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.entity.BeehiveBlockEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.sounds.SoundSource;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.level.block.BeehiveBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.tags.Tag;
import net.minecraft.tags.BlockTags;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.DispenserBlock;
import net.minecraft.core.Direction;
import net.minecraft.world.item.ItemStack;
import net.minecraft.core.BlockSource;

public class ShearsDispenseItemBehavior extends OptionalDispenseItemBehavior {
    @Override
    protected ItemStack execute(final BlockSource fv, final ItemStack bki) {
        final Level bqb4 = fv.getLevel();
        if (!bqb4.isClientSide()) {
            final BlockPos fu5 = fv.getPos().relative(fv.getBlockState().<Direction>getValue((Property<Direction>)DispenserBlock.FACING));
            this.setSuccess(tryShearBeehive((ServerLevel)bqb4, fu5) || tryShearLivingEntity((ServerLevel)bqb4, fu5));
            if (this.isSuccess() && bki.hurt(1, bqb4.getRandom(), null)) {
                bki.setCount(0);
            }
        }
        return bki;
    }
    
    private static boolean tryShearBeehive(final ServerLevel zd, final BlockPos fu) {
        final BlockState cfj3 = zd.getBlockState(fu);
        if (cfj3.is(BlockTags.BEEHIVES)) {
            final int integer4 = cfj3.<Integer>getValue((Property<Integer>)BeehiveBlock.HONEY_LEVEL);
            if (integer4 >= 5) {
                zd.playSound(null, fu, SoundEvents.BEEHIVE_SHEAR, SoundSource.BLOCKS, 1.0f, 1.0f);
                BeehiveBlock.dropHoneycomb(zd, fu);
                ((BeehiveBlock)cfj3.getBlock()).releaseBeesAndResetHoneyLevel(zd, cfj3, fu, null, BeehiveBlockEntity.BeeReleaseStatus.BEE_RELEASED);
                return true;
            }
        }
        return false;
    }
    
    private static boolean tryShearLivingEntity(final ServerLevel zd, final BlockPos fu) {
        final List<LivingEntity> list3 = zd.<LivingEntity>getEntitiesOfClass(LivingEntity.class, new AABB(fu), EntitySelector.NO_SPECTATORS);
        for (final LivingEntity aoy5 : list3) {
            if (aoy5 instanceof Shearable) {
                final Shearable apn6 = (Shearable)aoy5;
                if (apn6.readyForShearing()) {
                    apn6.shear(SoundSource.BLOCKS);
                    return true;
                }
                continue;
            }
        }
        return false;
    }
}
