package net.minecraft.world.item;

import net.minecraft.world.level.block.state.AbstractStateHolder;
import java.util.Map;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.network.chat.Component;
import java.util.List;
import net.minecraft.core.NonNullList;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.level.block.state.properties.Property;
import java.util.Iterator;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.nbt.CompoundTag;
import javax.annotation.Nullable;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.sounds.SoundSource;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.level.block.Block;

public class BlockItem extends Item {
    @Deprecated
    private final Block block;
    
    public BlockItem(final Block bph, final Properties a) {
        super(a);
        this.block = bph;
    }
    
    @Override
    public InteractionResult useOn(final UseOnContext bfz) {
        final InteractionResult ajl3 = this.place(new BlockPlaceContext(bfz));
        if (ajl3 != InteractionResult.SUCCESS && this.isEdible()) {
            return this.use(bfz.level, bfz.player, bfz.hand).getResult();
        }
        return ajl3;
    }
    
    public InteractionResult place(final BlockPlaceContext bcq) {
        if (!bcq.canPlace()) {
            return InteractionResult.FAIL;
        }
        final BlockPlaceContext bcq2 = this.updatePlacementContext(bcq);
        if (bcq2 == null) {
            return InteractionResult.FAIL;
        }
        final BlockState byj4 = this.getPlacementState(bcq2);
        if (byj4 == null) {
            return InteractionResult.FAIL;
        }
        if (!this.placeBlock(bcq2, byj4)) {
            return InteractionResult.FAIL;
        }
        final BlockPos fk5 = bcq2.getClickedPos();
        final Level bjw6 = bcq2.getLevel();
        final Player ayj7 = bcq2.getPlayer();
        final ItemStack ben8 = bcq2.getItemInHand();
        BlockState byj5 = bjw6.getBlockState(fk5);
        final Block bph10 = byj5.getBlock();
        if (bph10 == byj4.getBlock()) {
            byj5 = this.updateBlockStateFromTag(fk5, bjw6, ben8, byj5);
            this.updateCustomBlockEntityTag(fk5, bjw6, ayj7, ben8, byj5);
            bph10.setPlacedBy(bjw6, fk5, byj5, ayj7, ben8);
            if (ayj7 instanceof ServerPlayer) {
                CriteriaTriggers.PLACED_BLOCK.trigger((ServerPlayer)ayj7, fk5, ben8);
            }
        }
        final SoundType bum11 = byj5.getSoundType();
        bjw6.playSound(ayj7, fk5, this.getPlaceSound(byj5), SoundSource.BLOCKS, (bum11.getVolume() + 1.0f) / 2.0f, bum11.getPitch() * 0.8f);
        ben8.shrink(1);
        return InteractionResult.SUCCESS;
    }
    
    protected SoundEvent getPlaceSound(final BlockState byj) {
        return byj.getSoundType().getPlaceSound();
    }
    
    @Nullable
    public BlockPlaceContext updatePlacementContext(final BlockPlaceContext bcq) {
        return bcq;
    }
    
    protected boolean updateCustomBlockEntityTag(final BlockPos fk, final Level bjw, @Nullable final Player ayj, final ItemStack ben, final BlockState byj) {
        return updateCustomBlockEntityTag(bjw, ayj, fk, ben);
    }
    
    @Nullable
    protected BlockState getPlacementState(final BlockPlaceContext bcq) {
        final BlockState byj3 = this.getBlock().getStateForPlacement(bcq);
        return (byj3 != null && this.canPlace(bcq, byj3)) ? byj3 : null;
    }
    
    private BlockState updateBlockStateFromTag(final BlockPos fk, final Level bjw, final ItemStack ben, final BlockState byj) {
        BlockState byj2 = byj;
        final CompoundTag jt7 = ben.getTag();
        if (jt7 != null) {
            final CompoundTag jt8 = jt7.getCompound("BlockStateTag");
            final StateDefinition<Block, BlockState> byk9 = byj2.getBlock().getStateDefinition();
            for (final String string11 : jt8.getAllKeys()) {
                final Property<?> bzm12 = byk9.getProperty(string11);
                if (bzm12 != null) {
                    final String string12 = jt8.get(string11).getAsString();
                    byj2 = BlockItem.updateState(byj2, bzm12, string12);
                }
            }
        }
        if (byj2 != byj) {
            bjw.setBlock(fk, byj2, 2);
        }
        return byj2;
    }
    
    private static <T extends Comparable<T>> BlockState updateState(final BlockState byj, final Property<T> bzm, final String string) {
        return bzm.getValue(string).<BlockState>map(comparable -> (BlockState)((AbstractStateHolder<Object, Object>)byj).<T, Comparable>setValue(bzm, comparable)).orElse(byj);
    }
    
    protected boolean canPlace(final BlockPlaceContext bcq, final BlockState byj) {
        final Player ayj4 = bcq.getPlayer();
        final CollisionContext cvq5 = (ayj4 == null) ? CollisionContext.empty() : CollisionContext.of(ayj4);
        return (!this.mustSurvive() || byj.canSurvive(bcq.getLevel(), bcq.getClickedPos())) && bcq.getLevel().isUnobstructed(byj, bcq.getClickedPos(), cvq5);
    }
    
    protected boolean mustSurvive() {
        return true;
    }
    
    protected boolean placeBlock(final BlockPlaceContext bcq, final BlockState byj) {
        return bcq.getLevel().setBlock(bcq.getClickedPos(), byj, 11);
    }
    
    public static boolean updateCustomBlockEntityTag(final Level bjw, @Nullable final Player ayj, final BlockPos fk, final ItemStack ben) {
        final MinecraftServer minecraftServer5 = bjw.getServer();
        if (minecraftServer5 == null) {
            return false;
        }
        final CompoundTag jt6 = ben.getTagElement("BlockEntityTag");
        if (jt6 != null) {
            final BlockEntity bwl7 = bjw.getBlockEntity(fk);
            if (bwl7 != null) {
                if (!bjw.isClientSide && bwl7.onlyOpCanSetNbt() && (ayj == null || !ayj.canUseGameMasterBlocks())) {
                    return false;
                }
                final CompoundTag jt7 = bwl7.save(new CompoundTag());
                final CompoundTag jt8 = jt7.copy();
                jt7.merge(jt6);
                jt7.putInt("x", fk.getX());
                jt7.putInt("y", fk.getY());
                jt7.putInt("z", fk.getZ());
                if (!jt7.equals(jt8)) {
                    bwl7.load(jt7);
                    bwl7.setChanged();
                    return true;
                }
            }
        }
        return false;
    }
    
    @Override
    public String getDescriptionId() {
        return this.getBlock().getDescriptionId();
    }
    
    @Override
    public void fillItemCategory(final CreativeModeTab bdd, final NonNullList<ItemStack> fy) {
        if (this.allowdedIn(bdd)) {
            this.getBlock().fillItemCategory(bdd, fy);
        }
    }
    
    @Override
    public void appendHoverText(final ItemStack ben, @Nullable final Level bjw, final List<Component> list, final TooltipFlag bfw) {
        super.appendHoverText(ben, bjw, list, bfw);
        this.getBlock().appendHoverText(ben, bjw, list, bfw);
    }
    
    public Block getBlock() {
        return this.block;
    }
    
    public void registerBlocks(final Map<Block, Item> map, final Item bei) {
        map.put(this.getBlock(), bei);
    }
}
