BlockLantern.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package me.km.blocks;
  2. import java.util.Random;
  3. import net.minecraft.block.Block;
  4. import net.minecraft.block.BlockState;
  5. import net.minecraft.block.Blocks;
  6. import net.minecraft.block.DirectionalBlock;
  7. import net.minecraft.block.SoundType;
  8. import net.minecraft.block.material.Material;
  9. import net.minecraft.item.BlockItemUseContext;
  10. import net.minecraft.particles.ParticleTypes;
  11. import net.minecraft.state.StateContainer;
  12. import net.minecraft.util.Direction;
  13. import net.minecraft.util.math.BlockPos;
  14. import net.minecraft.util.math.shapes.ISelectionContext;
  15. import net.minecraft.util.math.shapes.VoxelShape;
  16. import net.minecraft.world.IBlockReader;
  17. import net.minecraft.world.IWorld;
  18. import net.minecraft.world.IWorldReader;
  19. import net.minecraft.world.World;
  20. import net.minecraftforge.api.distmarker.Dist;
  21. import net.minecraftforge.api.distmarker.OnlyIn;
  22. public class BlockLantern extends DirectionalBlock {
  23. protected static final VoxelShape HANGING_AABB = Block.makeCuboidShape(3.0, 2.0, 3.0, 13.0, 16.0, 13.0);
  24. protected static final VoxelShape STANDING_AABB = Block.makeCuboidShape(3.0, 0.0, 3.0, 13.0, 12.0, 13.0);
  25. protected static final VoxelShape TORCH_NORTH_AABB = Block.makeCuboidShape(3.0, 0.0, 6.0, 13.0, 16.0, 16.0);
  26. protected static final VoxelShape TORCH_SOUTH_AABB = Block.makeCuboidShape(3.0, 0.0, 0.0, 13.0, 16.0, 10.0);
  27. protected static final VoxelShape TORCH_WEST_AABB = Block.makeCuboidShape(6.0, 0.0, 3.0, 16.0, 16.0, 13.0);
  28. protected static final VoxelShape TORCH_EAST_AABB = Block.makeCuboidShape(0, 0.0, 3.0, 10.0, 16.0, 13.0);
  29. protected String name;
  30. public BlockLantern(String name) {
  31. super(Properties.create(Material.MISCELLANEOUS).doesNotBlockMovement().hardnessAndResistance(0.0f).lightValue(14).sound(SoundType.METAL));
  32. this.setRegistryName(name);
  33. this.setDefaultState(this.stateContainer.getBaseState().with(FACING, Direction.UP));
  34. }
  35. @Override
  36. public VoxelShape getShape(BlockState state, IBlockReader w, BlockPos pos, ISelectionContext context) {
  37. switch(state.get(FACING)) {
  38. case UP:
  39. return STANDING_AABB;
  40. case DOWN:
  41. return HANGING_AABB;
  42. case EAST:
  43. return TORCH_EAST_AABB;
  44. case WEST:
  45. return TORCH_WEST_AABB;
  46. case SOUTH:
  47. return TORCH_SOUTH_AABB;
  48. case NORTH:
  49. return TORCH_NORTH_AABB;
  50. }
  51. return TORCH_NORTH_AABB;
  52. }
  53. @OnlyIn(Dist.CLIENT)
  54. @Override
  55. public void animateTick(BlockState state, World w, BlockPos pos, Random rand) {
  56. Direction face = state.get(FACING);
  57. double x = pos.getX() + 0.5;
  58. double y = pos.getY() + 0.7;
  59. double z = pos.getZ() + 0.5;
  60. switch(face) {
  61. case EAST:
  62. case NORTH:
  63. case WEST:
  64. case SOUTH:
  65. Direction oface = face.getOpposite();
  66. x += 0.1875 * oface.getXOffset();
  67. z += 0.1875 * oface.getZOffset();
  68. break;
  69. case DOWN:
  70. break;
  71. case UP:
  72. y -= 0.125d;
  73. break;
  74. }
  75. w.addParticle(ParticleTypes.SMOKE, x, y, z, 0.0, 0.0, 0.0);
  76. w.addParticle(ParticleTypes.FLAME, x, y, z, 0.0, 0.0, 0.0);
  77. }
  78. @Override
  79. public boolean isNormalCube(BlockState state, IBlockReader worldIn, BlockPos pos) {
  80. return false;
  81. }
  82. @Override
  83. protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
  84. builder.add(FACING);
  85. }
  86. @Override
  87. public boolean isValidPosition(BlockState state, IWorldReader w, BlockPos pos) {
  88. Direction direction = state.get(FACING);
  89. if(direction == Direction.UP) {
  90. return hasEnoughSolidSide(w, pos.down(), Direction.UP);
  91. }
  92. BlockPos blockpos = pos.offset(direction.getOpposite());
  93. BlockState blockstate = w.getBlockState(blockpos);
  94. return Block.hasSolidSide(blockstate, w, blockpos, direction);
  95. }
  96. @Override
  97. public BlockState getStateForPlacement(BlockItemUseContext context) {
  98. return this.getDefaultState().with(FACING, context.getFace());
  99. }
  100. @Override
  101. public BlockState updatePostPlacement(BlockState state, Direction facing, BlockState facingState, IWorld w, BlockPos currentPos, BlockPos facingPos) {
  102. return state.get(FACING).getOpposite() == facing && !state.isValidPosition(w, currentPos) ? Blocks.AIR.getDefaultState() : state;
  103. }
  104. }