package net.minecraft.world.item;

import net.minecraft.world.level.block.state.AbstractStateHolder;
import java.util.Iterator;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.NetherPortalBlock;
import net.minecraft.core.Direction;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.FireBlock;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.sounds.SoundSource;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.InteractionResult;

public class FlintAndSteelItem extends Item {
    public FlintAndSteelItem(final Properties a) {
        super(a);
    }
    
    @Override
    public InteractionResult useOn(final UseOnContext bfz) {
        final Player ayj2 = bfz.getPlayer();
        final LevelAccessor bjx4 = bfz.getLevel();
        final BlockPos fk5 = bfz.getClickedPos();
        final BlockState byj6 = bjx4.getBlockState(fk5);
        if (canLightCampFire(byj6)) {
            bjx4.playSound(ayj2, fk5, SoundEvents.FLINTANDSTEEL_USE, SoundSource.BLOCKS, 1.0f, FlintAndSteelItem.random.nextFloat() * 0.4f + 0.8f);
            bjx4.setBlock(fk5, ((AbstractStateHolder<O, BlockState>)byj6).<Comparable, Boolean>setValue((Property<Comparable>)BlockStateProperties.LIT, true), 11);
            if (ayj2 != null) {
                bfz.getItemInHand().<Player>hurtAndBreak(1, ayj2, ayj -> ayj.broadcastBreakEvent(bfz.getHand()));
            }
            return InteractionResult.SUCCESS;
        }
        final BlockPos fk6 = fk5.relative(bfz.getClickedFace());
        if (canUse(bjx4.getBlockState(fk6), bjx4, fk6)) {
            bjx4.playSound(ayj2, fk6, SoundEvents.FLINTANDSTEEL_USE, SoundSource.BLOCKS, 1.0f, FlintAndSteelItem.random.nextFloat() * 0.4f + 0.8f);
            final BlockState byj7 = ((FireBlock)Blocks.FIRE).getStateForPlacement(bjx4, fk6);
            bjx4.setBlock(fk6, byj7, 11);
            final ItemStack ben9 = bfz.getItemInHand();
            if (ayj2 instanceof ServerPlayer) {
                CriteriaTriggers.PLACED_BLOCK.trigger((ServerPlayer)ayj2, fk6, ben9);
                ben9.<Player>hurtAndBreak(1, ayj2, ayj -> ayj.broadcastBreakEvent(bfz.getHand()));
            }
            return InteractionResult.SUCCESS;
        }
        return InteractionResult.FAIL;
    }
    
    public static boolean canLightCampFire(final BlockState byj) {
        return byj.getBlock() == Blocks.CAMPFIRE && !byj.<Boolean>getValue((Property<Boolean>)BlockStateProperties.WATERLOGGED) && !byj.<Boolean>getValue((Property<Boolean>)BlockStateProperties.LIT);
    }
    
    public static boolean canUse(final BlockState byj, final LevelAccessor bjx, final BlockPos fk) {
        final BlockState byj2 = ((FireBlock)Blocks.FIRE).getStateForPlacement(bjx, fk);
        boolean boolean5 = false;
        for (final Direction fp7 : Direction.Plane.HORIZONTAL) {
            if (bjx.getBlockState(fk.relative(fp7)).getBlock() == Blocks.OBSIDIAN && ((NetherPortalBlock)Blocks.NETHER_PORTAL).isPortal(bjx, fk) != null) {
                boolean5 = true;
            }
        }
        return byj.isAir() && (byj2.canSurvive(bjx, fk) || boolean5);
    }
}
