package net.minecraft.world.item;

import net.minecraft.world.level.block.state.StateHolder;
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 bvr, final Properties a) {
        super(a);
        this.block = bvr;
    }
    
    @Override
    public InteractionResult useOn(final UseOnContext blv) {
        final InteractionResult ang3 = this.place(new BlockPlaceContext(blv));
        if (!ang3.consumesAction() && this.isEdible()) {
            return this.use(blv.level, blv.player, blv.hand).getResult();
        }
        return ang3;
    }
    
    public InteractionResult place(final BlockPlaceContext bin) {
        if (!bin.canPlace()) {
            return InteractionResult.FAIL;
        }
        final BlockPlaceContext bin2 = this.updatePlacementContext(bin);
        if (bin2 == null) {
            return InteractionResult.FAIL;
        }
        final BlockState cfj4 = this.getPlacementState(bin2);
        if (cfj4 == null) {
            return InteractionResult.FAIL;
        }
        if (!this.placeBlock(bin2, cfj4)) {
            return InteractionResult.FAIL;
        }
        final BlockPos fu5 = bin2.getClickedPos();
        final Level bqb6 = bin2.getLevel();
        final Player bec7 = bin2.getPlayer();
        final ItemStack bki8 = bin2.getItemInHand();
        BlockState cfj5 = bqb6.getBlockState(fu5);
        final Block bvr10 = cfj5.getBlock();
        if (bvr10 == cfj4.getBlock()) {
            cfj5 = this.updateBlockStateFromTag(fu5, bqb6, bki8, cfj5);
            this.updateCustomBlockEntityTag(fu5, bqb6, bec7, bki8, cfj5);
            bvr10.setPlacedBy(bqb6, fu5, cfj5, bec7, bki8);
            if (bec7 instanceof ServerPlayer) {
                CriteriaTriggers.PLACED_BLOCK.trigger((ServerPlayer)bec7, fu5, bki8);
            }
        }
        final SoundType cbh11 = cfj5.getSoundType();
        bqb6.playSound(bec7, fu5, this.getPlaceSound(cfj5), SoundSource.BLOCKS, (cbh11.getVolume() + 1.0f) / 2.0f, cbh11.getPitch() * 0.8f);
        if (bec7 == null || !bec7.abilities.instabuild) {
            bki8.shrink(1);
        }
        return InteractionResult.sidedSuccess(bqb6.isClientSide);
    }
    
    protected SoundEvent getPlaceSound(final BlockState cfj) {
        return cfj.getSoundType().getPlaceSound();
    }
    
    @Nullable
    public BlockPlaceContext updatePlacementContext(final BlockPlaceContext bin) {
        return bin;
    }
    
    protected boolean updateCustomBlockEntityTag(final BlockPos fu, final Level bqb, @Nullable final Player bec, final ItemStack bki, final BlockState cfj) {
        return updateCustomBlockEntityTag(bqb, bec, fu, bki);
    }
    
    @Nullable
    protected BlockState getPlacementState(final BlockPlaceContext bin) {
        final BlockState cfj3 = this.getBlock().getStateForPlacement(bin);
        return (cfj3 != null && this.canPlace(bin, cfj3)) ? cfj3 : null;
    }
    
    private BlockState updateBlockStateFromTag(final BlockPos fu, final Level bqb, final ItemStack bki, final BlockState cfj) {
        BlockState cfj2 = cfj;
        final CompoundTag le7 = bki.getTag();
        if (le7 != null) {
            final CompoundTag le8 = le7.getCompound("BlockStateTag");
            final StateDefinition<Block, BlockState> cfk9 = cfj2.getBlock().getStateDefinition();
            for (final String string11 : le8.getAllKeys()) {
                final Property<?> cgl12 = cfk9.getProperty(string11);
                if (cgl12 != null) {
                    final String string12 = le8.get(string11).getAsString();
                    cfj2 = BlockItem.updateState(cfj2, cgl12, string12);
                }
            }
        }
        if (cfj2 != cfj) {
            bqb.setBlock(fu, cfj2, 2);
        }
        return cfj2;
    }
    
    private static <T extends Comparable<T>> BlockState updateState(final BlockState cfj, final Property<T> cgl, final String string) {
        return cgl.getValue(string).<BlockState>map(comparable -> (BlockState)((StateHolder<Object, Object>)cfj).<T, Comparable>setValue(cgl, comparable)).orElse(cfj);
    }
    
    protected boolean canPlace(final BlockPlaceContext bin, final BlockState cfj) {
        final Player bec4 = bin.getPlayer();
        final CollisionContext der5 = (bec4 == null) ? CollisionContext.empty() : CollisionContext.of(bec4);
        return (!this.mustSurvive() || cfj.canSurvive(bin.getLevel(), bin.getClickedPos())) && bin.getLevel().isUnobstructed(cfj, bin.getClickedPos(), der5);
    }
    
    protected boolean mustSurvive() {
        return true;
    }
    
    protected boolean placeBlock(final BlockPlaceContext bin, final BlockState cfj) {
        return bin.getLevel().setBlock(bin.getClickedPos(), cfj, 11);
    }
    
    public static boolean updateCustomBlockEntityTag(final Level bqb, @Nullable final Player bec, final BlockPos fu, final ItemStack bki) {
        final MinecraftServer minecraftServer5 = bqb.getServer();
        if (minecraftServer5 == null) {
            return false;
        }
        final CompoundTag le6 = bki.getTagElement("BlockEntityTag");
        if (le6 != null) {
            final BlockEntity cdl7 = bqb.getBlockEntity(fu);
            if (cdl7 != null) {
                if (!bqb.isClientSide && cdl7.onlyOpCanSetNbt() && (bec == null || !bec.canUseGameMasterBlocks())) {
                    return false;
                }
                final CompoundTag le7 = cdl7.save(new CompoundTag());
                final CompoundTag le8 = le7.copy();
                le7.merge(le6);
                le7.putInt("x", fu.getX());
                le7.putInt("y", fu.getY());
                le7.putInt("z", fu.getZ());
                if (!le7.equals(le8)) {
                    cdl7.load(bqb.getBlockState(fu), le7);
                    cdl7.setChanged();
                    return true;
                }
            }
        }
        return false;
    }
    
    @Override
    public String getDescriptionId() {
        return this.getBlock().getDescriptionId();
    }
    
    @Override
    public void fillItemCategory(final CreativeModeTab biy, final NonNullList<ItemStack> gi) {
        if (this.allowdedIn(biy)) {
            this.getBlock().fillItemCategory(biy, gi);
        }
    }
    
    @Override
    public void appendHoverText(final ItemStack bki, @Nullable final Level bqb, final List<Component> list, final TooltipFlag bls) {
        super.appendHoverText(bki, bqb, list, bls);
        this.getBlock().appendHoverText(bki, bqb, list, bls);
    }
    
    public Block getBlock() {
        return this.block;
    }
    
    public void registerBlocks(final Map<Block, Item> map, final Item bke) {
        map.put(this.getBlock(), bke);
    }
}
