BlockLantern.java 4.6 KB

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