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 bjw, final Player ayj, final InteractionHand ajk) {
        final ItemStack ben5 = ayj.getItemInHand(ajk);
        final HitResult cvi6 = Item.getPlayerPOVHitResult(bjw, ayj, ClipContext.Fluid.ANY);
        if (cvi6.getType() == HitResult.Type.MISS) {
            return InteractionResultHolder.<ItemStack>pass(ben5);
        }
        final Vec3 cvl7 = ayj.getViewVector(1.0f);
        final double double8 = 5.0;
        final List<Entity> list10 = bjw.getEntities(ayj, ayj.getBoundingBox().expandTowards(cvl7.scale(5.0)).inflate(1.0), BoatItem.ENTITY_PREDICATE);
        if (!list10.isEmpty()) {
            final Vec3 cvl8 = ayj.getEyePosition(1.0f);
            for (final Entity akq13 : list10) {
                final AABB cvf14 = akq13.getBoundingBox().inflate(akq13.getPickRadius());
                if (cvf14.contains(cvl8)) {
                    return InteractionResultHolder.<ItemStack>pass(ben5);
                }
            }
        }
        if (cvi6.getType() != HitResult.Type.BLOCK) {
            return InteractionResultHolder.<ItemStack>pass(ben5);
        }
        final Boat azz11 = new Boat(bjw, cvi6.getLocation().x, cvi6.getLocation().y, cvi6.getLocation().z);
        azz11.setType(this.type);
        azz11.yRot = ayj.yRot;
        if (!bjw.noCollision(azz11, azz11.getBoundingBox().inflate(-0.1))) {
            return InteractionResultHolder.<ItemStack>fail(ben5);
        }
        if (!bjw.isClientSide) {
            bjw.addFreshEntity(azz11);
            if (!ayj.abilities.instabuild) {
                ben5.shrink(1);
            }
        }
        ayj.awardStat(Stats.ITEM_USED.get(this));
        return InteractionResultHolder.<ItemStack>success(ben5);
    }
    
    static {
        ENTITY_PREDICATE = EntitySelector.NO_SPECTATORS.and(Entity::isPickable);
    }
}
