package net.minecraft.world.item;

import net.minecraft.world.entity.EntitySelector;
import net.minecraft.world.phys.AABB;
import java.util.Iterator;
import java.util.List;
import net.minecraft.world.phys.Vec3;
import net.minecraft.stats.Stat;
import net.minecraft.stats.Stats;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.entity.vehicle.Boat;
import net.minecraft.world.entity.Entity;
import java.util.function.Predicate;

public class BoatItem extends Item {
    private static final Predicate<Entity> ENTITY_PREDICATE;
    private final Boat.Type type;
    
    public BoatItem(final Boat.Type b, final Properties a) {
        super(a);
        this.type = b;
    }
    
    @Override
    public InteractionResultHolder<ItemStack> use(final Level bqb, final Player bec, final InteractionHand anf) {
        final ItemStack bki5 = bec.getItemInHand(anf);
        final HitResult dej6 = Item.getPlayerPOVHitResult(bqb, bec, ClipContext.Fluid.ANY);
        if (dej6.getType() == HitResult.Type.MISS) {
            return InteractionResultHolder.<ItemStack>pass(bki5);
        }
        final Vec3 dem7 = bec.getViewVector(1.0f);
        final double double8 = 5.0;
        final List<Entity> list10 = bqb.getEntities(bec, bec.getBoundingBox().expandTowards(dem7.scale(5.0)).inflate(1.0), BoatItem.ENTITY_PREDICATE);
        if (!list10.isEmpty()) {
            final Vec3 dem8 = bec.getEyePosition(1.0f);
            for (final Entity aom13 : list10) {
                final AABB deg14 = aom13.getBoundingBox().inflate(aom13.getPickRadius());
                if (deg14.contains(dem8)) {
                    return InteractionResultHolder.<ItemStack>pass(bki5);
                }
            }
        }
        if (dej6.getType() != HitResult.Type.BLOCK) {
            return InteractionResultHolder.<ItemStack>pass(bki5);
        }
        final Boat bft11 = new Boat(bqb, dej6.getLocation().x, dej6.getLocation().y, dej6.getLocation().z);
        bft11.setType(this.type);
        bft11.yRot = bec.yRot;
        if (!bqb.noCollision(bft11, bft11.getBoundingBox().inflate(-0.1))) {
            return InteractionResultHolder.<ItemStack>fail(bki5);
        }
        if (!bqb.isClientSide) {
            bqb.addFreshEntity(bft11);
            if (!bec.abilities.instabuild) {
                bki5.shrink(1);
            }
        }
        bec.awardStat(Stats.ITEM_USED.get(this));
        return InteractionResultHolder.<ItemStack>sidedSuccess(bki5, bqb.isClientSide());
    }
    
    static {
        ENTITY_PREDICATE = EntitySelector.NO_SPECTATORS.and(Entity::isPickable);
    }
}
