package net.minecraft.world.level.block;

import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.LivingEntity;
import javax.annotation.Nullable;
import net.minecraft.network.chat.Component;
import net.minecraft.world.SimpleMenuProvider;
import net.minecraft.world.inventory.EnchantmentMenu;
import net.minecraft.world.inventory.ContainerLevelAccess;
import net.minecraft.world.Nameable;
import net.minecraft.world.MenuProvider;
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.block.entity.EnchantmentTableBlockEntity;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleTypes;
import java.util.Random;
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;

public class EnchantmentTableBlock extends BaseEntityBlock {
    protected static final VoxelShape SHAPE;
    
    protected EnchantmentTableBlock(final Properties c) {
        super(c);
    }
    
    @Override
    public boolean useShapeForLightOcclusion(final BlockState byj) {
        return true;
    }
    
    @Override
    public VoxelShape getShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        return EnchantmentTableBlock.SHAPE;
    }
    
    @Override
    public void animateTick(final BlockState byj, final Level bjw, final BlockPos fk, final Random random) {
        super.animateTick(byj, bjw, fk, random);
        for (int integer6 = -2; integer6 <= 2; ++integer6) {
            for (int integer7 = -2; integer7 <= 2; ++integer7) {
                if (integer6 > -2 && integer6 < 2 && integer7 == -1) {
                    integer7 = 2;
                }
                if (random.nextInt(16) == 0) {
                    for (int integer8 = 0; integer8 <= 1; ++integer8) {
                        final BlockPos fk2 = fk.offset(integer6, integer8, integer7);
                        if (bjw.getBlockState(fk2).getBlock() == Blocks.BOOKSHELF) {
                            if (!bjw.isEmptyBlock(fk.offset(integer6 / 2, 0, integer7 / 2))) {
                                break;
                            }
                            bjw.addParticle(ParticleTypes.ENCHANT, fk.getX() + 0.5, fk.getY() + 2.0, fk.getZ() + 0.5, integer6 + random.nextFloat() - 0.5, integer8 - random.nextFloat() - 1.0f, integer7 + random.nextFloat() - 0.5);
                        }
                    }
                }
            }
        }
    }
    
    @Override
    public RenderShape getRenderShape(final BlockState byj) {
        return RenderShape.MODEL;
    }
    
    @Override
    public BlockEntity newBlockEntity(final BlockGetter bjg) {
        return new EnchantmentTableBlockEntity();
    }
    
    @Override
    public InteractionResult use(final BlockState byj, final Level bjw, final BlockPos fk, final Player ayj, final InteractionHand ajk, final BlockHitResult cvg) {
        if (bjw.isClientSide) {
            return InteractionResult.SUCCESS;
        }
        ayj.openMenu(byj.getMenuProvider(bjw, fk));
        return InteractionResult.SUCCESS;
    }
    
    @Nullable
    @Override
    public MenuProvider getMenuProvider(final BlockState byj, final Level bjw, final BlockPos fk) {
        final BlockEntity bwl5 = bjw.getBlockEntity(fk);
        if (bwl5 instanceof EnchantmentTableBlockEntity) {
            final Component lf6 = ((Nameable)bwl5).getDisplayName();
            return new SimpleMenuProvider((integer, ayi, ayj) -> new EnchantmentMenu(integer, ayi, ContainerLevelAccess.create(bjw, fk)), lf6);
        }
        return null;
    }
    
    @Override
    public void setPlacedBy(final Level bjw, final BlockPos fk, final BlockState byj, final LivingEntity akz, final ItemStack ben) {
        if (ben.hasCustomHoverName()) {
            final BlockEntity bwl7 = bjw.getBlockEntity(fk);
            if (bwl7 instanceof EnchantmentTableBlockEntity) {
                ((EnchantmentTableBlockEntity)bwl7).setCustomName(ben.getHoverName());
            }
        }
    }
    
    @Override
    public boolean isPathfindable(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final PathComputationType cqr) {
        return false;
    }
    
    static {
        SHAPE = Block.box(0.0, 0.0, 0.0, 16.0, 12.0, 16.0);
    }
}
