package net.minecraft.world.item;

import net.minecraft.world.level.block.state.StateHolder;
import net.minecraft.stats.Stat;
import net.minecraft.stats.Stats;
import net.minecraft.sounds.SoundSource;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.projectile.EyeOfEnder;
import net.minecraft.world.level.levelgen.feature.StructureFeature;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.phys.BlockHitResult;
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.block.state.pattern.BlockPattern;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.EndPortalFrameBlock;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.InteractionResult;

public class EnderEyeItem extends Item {
    public EnderEyeItem(final Properties a) {
        super(a);
    }
    
    @Override
    public InteractionResult useOn(final UseOnContext blv) {
        final Level bqb3 = blv.getLevel();
        final BlockPos fu4 = blv.getClickedPos();
        final BlockState cfj5 = bqb3.getBlockState(fu4);
        if (!cfj5.is(Blocks.END_PORTAL_FRAME) || cfj5.<Boolean>getValue((Property<Boolean>)EndPortalFrameBlock.HAS_EYE)) {
            return InteractionResult.PASS;
        }
        if (bqb3.isClientSide) {
            return InteractionResult.SUCCESS;
        }
        final BlockState cfj6 = ((StateHolder<O, BlockState>)cfj5).<Comparable, Boolean>setValue((Property<Comparable>)EndPortalFrameBlock.HAS_EYE, true);
        Block.pushEntitiesUp(cfj5, cfj6, bqb3, fu4);
        bqb3.setBlock(fu4, cfj6, 2);
        bqb3.updateNeighbourForOutputSignal(fu4, Blocks.END_PORTAL_FRAME);
        blv.getItemInHand().shrink(1);
        bqb3.levelEvent(1503, fu4, 0);
        final BlockPattern.BlockPatternMatch b7 = EndPortalFrameBlock.getOrCreatePortalShape().find(bqb3, fu4);
        if (b7 != null) {
            final BlockPos fu5 = b7.getFrontTopLeft().offset(-3, 0, -3);
            for (int integer9 = 0; integer9 < 3; ++integer9) {
                for (int integer10 = 0; integer10 < 3; ++integer10) {
                    bqb3.setBlock(fu5.offset(integer9, 0, integer10), Blocks.END_PORTAL.defaultBlockState(), 2);
                }
            }
            bqb3.globalLevelEvent(1038, fu5.offset(1, 0, 1), 0);
        }
        return InteractionResult.CONSUME;
    }
    
    @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.NONE);
        if (dej6.getType() == HitResult.Type.BLOCK && bqb.getBlockState(((BlockHitResult)dej6).getBlockPos()).is(Blocks.END_PORTAL_FRAME)) {
            return InteractionResultHolder.<ItemStack>pass(bki5);
        }
        bec.startUsingItem(anf);
        if (bqb instanceof ServerLevel) {
            final BlockPos fu7 = ((ServerLevel)bqb).getChunkSource().getGenerator().findNearestMapFeature((ServerLevel)bqb, StructureFeature.STRONGHOLD, bec.blockPosition(), 100, false);
            if (fu7 != null) {
                final EyeOfEnder bel8 = new EyeOfEnder(bqb, bec.getX(), bec.getY(0.5), bec.getZ());
                bel8.setItem(bki5);
                bel8.signalTo(fu7);
                bqb.addFreshEntity(bel8);
                if (bec instanceof ServerPlayer) {
                    CriteriaTriggers.USED_ENDER_EYE.trigger((ServerPlayer)bec, fu7);
                }
                bqb.playSound(null, bec.getX(), bec.getY(), bec.getZ(), SoundEvents.ENDER_EYE_LAUNCH, SoundSource.NEUTRAL, 0.5f, 0.4f / (EnderEyeItem.random.nextFloat() * 0.4f + 0.8f));
                bqb.levelEvent(null, 1003, bec.blockPosition(), 0);
                if (!bec.abilities.instabuild) {
                    bki5.shrink(1);
                }
                bec.awardStat(Stats.ITEM_USED.get(this));
                bec.swing(anf, true);
                return InteractionResultHolder.<ItemStack>success(bki5);
            }
        }
        return InteractionResultHolder.<ItemStack>consume(bki5);
    }
}
