package me.km.blocks;

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.util.DamageSource;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.shapes.ISelectionContext;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.IWorld;
import net.minecraft.world.IWorldReader;
import net.minecraft.world.World;

public class BlockSpikes extends Block {
    private static final VoxelShape SHAPE = Block.makeCuboidShape(0.0, 0.0, 0.0, 16.0, 1.0, 16.0);

    public BlockSpikes(String name, Properties properties) {
        super(properties.notSolid());
        super.setRegistryName(name);
    }

    @Override
    public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
        return SHAPE;
    }

    @Override
    public boolean isValidPosition(BlockState state, IWorldReader w, BlockPos pos) {
        return hasSolidSideOnTop(w, pos.down()); // from RedstoneDiodeBlock
    }

    @Override
    public void onEntityCollision(BlockState state, World w, BlockPos pos, Entity ent) {
        if(!ent.isCrouching() && ent instanceof LivingEntity) {
            ent.attackEntityFrom(DamageSource.CACTUS, 1.0F);
        }
    }

    @Override
    public boolean isNormalCube(BlockState state, IBlockReader worldIn, BlockPos pos) {
        return false;
    }

    @Override
    public BlockState updatePostPlacement(BlockState stateIn, Direction facing, BlockState facingState, IWorld w, BlockPos currentPos, BlockPos facingPos) {
        return !stateIn.isValidPosition(w, currentPos) ? Blocks.AIR.getDefaultState()
                : super.updatePostPlacement(stateIn, facing, facingState, w, currentPos, facingPos);
    }
}
