package net.minecraft.world.item;

import net.minecraft.world.level.block.state.AbstractStateHolder;
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.projectile.EyeOfEnder;
import net.minecraft.world.entity.Entity;
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 bfz) {
        final Level bjw3 = bfz.getLevel();
        final BlockPos fk4 = bfz.getClickedPos();
        final BlockState byj5 = bjw3.getBlockState(fk4);
        if (byj5.getBlock() != Blocks.END_PORTAL_FRAME || byj5.<Boolean>getValue((Property<Boolean>)EndPortalFrameBlock.HAS_EYE)) {
            return InteractionResult.PASS;
        }
        if (bjw3.isClientSide) {
            return InteractionResult.SUCCESS;
        }
        final BlockState byj6 = ((AbstractStateHolder<O, BlockState>)byj5).<Comparable, Boolean>setValue((Property<Comparable>)EndPortalFrameBlock.HAS_EYE, true);
        Block.pushEntitiesUp(byj5, byj6, bjw3, fk4);
        bjw3.setBlock(fk4, byj6, 2);
        bjw3.updateNeighbourForOutputSignal(fk4, Blocks.END_PORTAL_FRAME);
        bfz.getItemInHand().shrink(1);
        bjw3.levelEvent(1503, fk4, 0);
        final BlockPattern.BlockPatternMatch b7 = EndPortalFrameBlock.getOrCreatePortalShape().find(bjw3, fk4);
        if (b7 != null) {
            final BlockPos fk5 = b7.getFrontTopLeft().offset(-3, 0, -3);
            for (int integer9 = 0; integer9 < 3; ++integer9) {
                for (int integer10 = 0; integer10 < 3; ++integer10) {
                    bjw3.setBlock(fk5.offset(integer9, 0, integer10), Blocks.END_PORTAL.defaultBlockState(), 2);
                }
            }
            bjw3.globalLevelEvent(1038, fk5.offset(1, 0, 1), 0);
        }
        return InteractionResult.SUCCESS;
    }
    
    @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.NONE);
        if (cvi6.getType() == HitResult.Type.BLOCK && bjw.getBlockState(((BlockHitResult)cvi6).getBlockPos()).getBlock() == Blocks.END_PORTAL_FRAME) {
            return InteractionResultHolder.<ItemStack>pass(ben5);
        }
        ayj.startUsingItem(ajk);
        if (bjw instanceof ServerLevel) {
            final BlockPos fk7 = ((ServerLevel)bjw).getChunkSource().getGenerator().findNearestMapFeature(bjw, "Stronghold", new BlockPos(ayj), 100, false);
            if (fk7 != null) {
                final EyeOfEnder ays8 = new EyeOfEnder(bjw, ayj.getX(), ayj.getY(0.5), ayj.getZ());
                ays8.setItem(ben5);
                ays8.signalTo(fk7);
                bjw.addFreshEntity(ays8);
                if (ayj instanceof ServerPlayer) {
                    CriteriaTriggers.USED_ENDER_EYE.trigger((ServerPlayer)ayj, fk7);
                }
                bjw.playSound(null, ayj.getX(), ayj.getY(), ayj.getZ(), SoundEvents.ENDER_EYE_LAUNCH, SoundSource.NEUTRAL, 0.5f, 0.4f / (EnderEyeItem.random.nextFloat() * 0.4f + 0.8f));
                bjw.levelEvent(null, 1003, new BlockPos(ayj), 0);
                if (!ayj.abilities.instabuild) {
                    ben5.shrink(1);
                }
                ayj.awardStat(Stats.ITEM_USED.get(this));
                ayj.swing(ajk, true);
                return InteractionResultHolder.<ItemStack>success(ben5);
            }
        }
        return InteractionResultHolder.<ItemStack>consume(ben5);
    }
}
