BlockLantern.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. {
  24. protected static final VoxelShape HANGING_AABB = Block.makeCuboidShape(3.0, 2.0, 3.0, 13.0, 16.0, 13.0);
  25. protected static final VoxelShape STANDING_AABB = Block.makeCuboidShape(3.0, 0.0, 3.0, 13.0, 12.0, 13.0);
  26. protected static final VoxelShape TORCH_NORTH_AABB = Block.makeCuboidShape(3.0, 0.0, 6.0, 13.0, 16.0, 16.0);
  27. protected static final VoxelShape TORCH_SOUTH_AABB = Block.makeCuboidShape(3.0, 0.0, 0.0, 13.0, 16.0, 10.0);
  28. protected static final VoxelShape TORCH_WEST_AABB = Block.makeCuboidShape(6.0, 0.0, 3.0, 16.0, 16.0, 13.0);
  29. protected static final VoxelShape TORCH_EAST_AABB = Block.makeCuboidShape(0, 0.0, 3.0, 10.0, 16.0, 13.0);
  30. protected String name;
  31. public BlockLantern(String name)
  32. {
  33. super(Properties.create(Material.MISCELLANEOUS).doesNotBlockMovement().hardnessAndResistance(0.0f).lightValue(14).sound(SoundType.METAL));
  34. this.setRegistryName(name);
  35. this.setDefaultState(this.stateContainer.getBaseState().with(FACING, Direction.UP));
  36. }
  37. @Override
  38. public VoxelShape getShape(BlockState state, IBlockReader w, BlockPos pos, ISelectionContext context)
  39. {
  40. switch(state.get(FACING))
  41. {
  42. case UP:
  43. return STANDING_AABB;
  44. case DOWN:
  45. return HANGING_AABB;
  46. case EAST:
  47. return TORCH_EAST_AABB;
  48. case WEST:
  49. return TORCH_WEST_AABB;
  50. case SOUTH:
  51. return TORCH_SOUTH_AABB;
  52. case NORTH:
  53. return TORCH_NORTH_AABB;
  54. }
  55. return TORCH_NORTH_AABB;
  56. }
  57. @OnlyIn(Dist.CLIENT)
  58. @Override
  59. public void animateTick(BlockState state, World w, BlockPos pos, Random rand)
  60. {
  61. Direction face = state.get(FACING);
  62. double x = pos.getX() + 0.5;
  63. double y = pos.getY() + 0.7;
  64. double z = pos.getZ() + 0.5;
  65. switch(face)
  66. {
  67. case EAST:
  68. case NORTH:
  69. case WEST:
  70. case SOUTH:
  71. Direction oface = face.getOpposite();
  72. x += 0.1875 * oface.getXOffset();
  73. z += 0.1875 * oface.getZOffset();
  74. break;
  75. case DOWN:
  76. break;
  77. case UP:
  78. y -= 0.125d;
  79. break;
  80. }
  81. w.addParticle(ParticleTypes.SMOKE, x, y, z, 0.0, 0.0, 0.0);
  82. w.addParticle(ParticleTypes.FLAME, x, y, z, 0.0, 0.0, 0.0);
  83. }
  84. @Override
  85. public boolean isNormalCube(BlockState state, IBlockReader worldIn, BlockPos pos)
  86. {
  87. return false;
  88. }
  89. @Override
  90. protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder)
  91. {
  92. builder.add(FACING);
  93. }
  94. @Override
  95. public boolean isValidPosition(BlockState state, IWorldReader w, BlockPos pos)
  96. {
  97. Direction direction = state.get(FACING);
  98. if(direction == Direction.UP)
  99. {
  100. return hasEnoughSolidSide(w, pos.down(), Direction.UP);
  101. }
  102. BlockPos blockpos = pos.offset(direction.getOpposite());
  103. BlockState blockstate = w.getBlockState(blockpos);
  104. return Block.hasSolidSide(blockstate, w, blockpos, direction);
  105. }
  106. @Override
  107. public BlockState getStateForPlacement(BlockItemUseContext context)
  108. {
  109. return this.getDefaultState().with(FACING, context.getFace());
  110. }
  111. @Override
  112. public BlockState updatePostPlacement(BlockState state, Direction facing, BlockState facingState, IWorld w, BlockPos currentPos, BlockPos facingPos)
  113. {
  114. return state.get(FACING).getOpposite() == facing && !state.isValidPosition(w, currentPos) ? Blocks.AIR.getDefaultState() : state;
  115. }
  116. }