package net.minecraft.world.level.block;

import com.google.common.collect.Maps;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.core.Direction;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.item.ItemStack;
import net.minecraft.stats.Stats;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.shapes.VoxelShape;
import java.util.Map;

public class FlowerPotBlock extends Block {
    private static final Map<Block, Block> POTTED_BY_CONTENT;
    protected static final VoxelShape SHAPE;
    private final Block content;
    
    public FlowerPotBlock(final Block bph, final Properties c) {
        super(c);
        this.content = bph;
        FlowerPotBlock.POTTED_BY_CONTENT.put(bph, this);
    }
    
    @Override
    public VoxelShape getShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        return FlowerPotBlock.SHAPE;
    }
    
    @Override
    public RenderShape getRenderShape(final BlockState byj) {
        return RenderShape.MODEL;
    }
    
    @Override
    public InteractionResult use(final BlockState byj, final Level bjw, final BlockPos fk, final Player ayj, final InteractionHand ajk, final BlockHitResult cvg) {
        final ItemStack ben8 = ayj.getItemInHand(ajk);
        final Item bei9 = ben8.getItem();
        final Block bph10 = (bei9 instanceof BlockItem) ? FlowerPotBlock.POTTED_BY_CONTENT.getOrDefault(((BlockItem)bei9).getBlock(), Blocks.AIR) : Blocks.AIR;
        final boolean boolean11 = bph10 == Blocks.AIR;
        final boolean boolean12 = this.content == Blocks.AIR;
        if (boolean11 != boolean12) {
            if (boolean12) {
                bjw.setBlock(fk, bph10.defaultBlockState(), 3);
                ayj.awardStat(Stats.POT_FLOWER);
                if (!ayj.abilities.instabuild) {
                    ben8.shrink(1);
                }
            }
            else {
                final ItemStack ben9 = new ItemStack(this.content);
                if (ben8.isEmpty()) {
                    ayj.setItemInHand(ajk, ben9);
                }
                else if (!ayj.addItem(ben9)) {
                    ayj.drop(ben9, false);
                }
                bjw.setBlock(fk, Blocks.FLOWER_POT.defaultBlockState(), 3);
            }
            return InteractionResult.SUCCESS;
        }
        return InteractionResult.CONSUME;
    }
    
    @Override
    public ItemStack getCloneItemStack(final BlockGetter bjg, final BlockPos fk, final BlockState byj) {
        if (this.content == Blocks.AIR) {
            return super.getCloneItemStack(bjg, fk, byj);
        }
        return new ItemStack(this.content);
    }
    
    @Override
    public BlockState updateShape(final BlockState byj1, final Direction fp, final BlockState byj3, final LevelAccessor bjx, final BlockPos fk5, final BlockPos fk6) {
        if (fp == Direction.DOWN && !byj1.canSurvive(bjx, fk5)) {
            return Blocks.AIR.defaultBlockState();
        }
        return super.updateShape(byj1, fp, byj3, bjx, fk5, fk6);
    }
    
    public Block getContent() {
        return this.content;
    }
    
    static {
        POTTED_BY_CONTENT = Maps.newHashMap();
        SHAPE = Block.box(5.0, 0.0, 5.0, 11.0, 6.0, 11.0);
    }
}
