BlockLantern.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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)
  32. .setLightLevel(state -> 14).sound(SoundType.METAL));
  33. this.setRegistryName(name);
  34. this.setDefaultState(this.stateContainer.getBaseState().with(FACING, Direction.UP));
  35. }
  36. @Override
  37. public VoxelShape getShape(BlockState state, IBlockReader w, BlockPos pos, ISelectionContext context) {
  38. switch(state.get(FACING)) {
  39. case UP:
  40. return STANDING_AABB;
  41. case DOWN:
  42. return HANGING_AABB;
  43. case EAST:
  44. return TORCH_EAST_AABB;
  45. case WEST:
  46. return TORCH_WEST_AABB;
  47. case SOUTH:
  48. return TORCH_SOUTH_AABB;
  49. case NORTH:
  50. return TORCH_NORTH_AABB;
  51. }
  52. return TORCH_NORTH_AABB;
  53. }
  54. @OnlyIn(Dist.CLIENT)
  55. @Override
  56. public void animateTick(BlockState state, World w, BlockPos pos, Random rand) {
  57. Direction face = state.get(FACING);
  58. double x = pos.getX() + 0.5;
  59. double y = pos.getY() + 0.7;
  60. double z = pos.getZ() + 0.5;
  61. switch(face) {
  62. case EAST:
  63. case NORTH:
  64. case WEST:
  65. case SOUTH:
  66. Direction oface = face.getOpposite();
  67. x += 0.1875 * oface.getXOffset();
  68. z += 0.1875 * oface.getZOffset();
  69. break;
  70. case DOWN:
  71. break;
  72. case UP:
  73. y -= 0.125d;
  74. break;
  75. }
  76. w.addParticle(ParticleTypes.SMOKE, x, y, z, 0.0, 0.0, 0.0);
  77. w.addParticle(ParticleTypes.FLAME, x, y, z, 0.0, 0.0, 0.0);
  78. }
  79. @Override
  80. protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
  81. builder.add(FACING);
  82. }
  83. @Override
  84. public boolean isValidPosition(BlockState state, IWorldReader w, BlockPos pos) {
  85. Direction direction = state.get(FACING);
  86. if(direction == Direction.UP) {
  87. return hasEnoughSolidSide(w, pos.down(), Direction.UP);
  88. }
  89. BlockPos blockpos = pos.offset(direction.getOpposite());
  90. BlockState blockstate = w.getBlockState(blockpos);
  91. return blockstate.isSolidSide(w, blockpos, direction);
  92. }
  93. @Override
  94. public BlockState getStateForPlacement(BlockItemUseContext context) {
  95. return this.getDefaultState().with(FACING, context.getFace());
  96. }
  97. @Override
  98. public BlockState updatePostPlacement(BlockState state, Direction facing, BlockState facingState, IWorld w, BlockPos currentPos, BlockPos facingPos) {
  99. return state.get(FACING).getOpposite() == facing && !state.isValidPosition(w, currentPos) ? Blocks.AIR.getDefaultState() : state;
  100. }
  101. }