BlockSpikes.java 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package me.km.blocks;
  2. import net.minecraft.block.Block;
  3. import net.minecraft.block.BlockState;
  4. import net.minecraft.block.Blocks;
  5. import net.minecraft.entity.Entity;
  6. import net.minecraft.entity.LivingEntity;
  7. import net.minecraft.util.DamageSource;
  8. import net.minecraft.util.Direction;
  9. import net.minecraft.util.math.BlockPos;
  10. import net.minecraft.util.math.shapes.ISelectionContext;
  11. import net.minecraft.util.math.shapes.VoxelShape;
  12. import net.minecraft.world.IBlockReader;
  13. import net.minecraft.world.IWorld;
  14. import net.minecraft.world.IWorldReader;
  15. import net.minecraft.world.World;
  16. public class BlockSpikes extends Block {
  17. private static final VoxelShape SHAPE = Block.makeCuboidShape(0.0, 0.0, 0.0, 16.0, 1.0, 16.0);
  18. public BlockSpikes(String name, Properties properties) {
  19. super(properties.notSolid());
  20. super.setRegistryName(name);
  21. }
  22. @Override
  23. public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
  24. return SHAPE;
  25. }
  26. @Override
  27. public boolean isValidPosition(BlockState state, IWorldReader w, BlockPos pos) {
  28. return hasSolidSideOnTop(w, pos.down()); // from RedstoneDiodeBlock
  29. }
  30. @Override
  31. public void onEntityCollision(BlockState state, World w, BlockPos pos, Entity ent) {
  32. if(!ent.isCrouching() && ent instanceof LivingEntity) {
  33. ent.attackEntityFrom(DamageSource.CACTUS, 1.0F);
  34. }
  35. }
  36. @Override
  37. public BlockState updatePostPlacement(BlockState stateIn, Direction facing, BlockState facingState, IWorld w, BlockPos currentPos, BlockPos facingPos) {
  38. return !stateIn.isValidPosition(w, currentPos) ? Blocks.AIR.getDefaultState()
  39. : super.updatePostPlacement(stateIn, facing, facingState, w, currentPos, facingPos);
  40. }
  41. }