Advertisement
AOCAWOL

Stairs

Apr 6th, 2020
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.62 KB | None | 0 0
  1. extends Block implements IWaterLoggable {
  2. public static final DirectionProperty FACING = HorizontalBlock.HORIZONTAL_FACING;
  3. public static final EnumProperty<Half> HALF = BlockStateProperties.HALF;
  4. public static final EnumProperty<StairsShape> SHAPE = BlockStateProperties.STAIRS_SHAPE;
  5. public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
  6. public static final VoxelShape AABB_SLAB_TOP = BYGSlabBlock.TOP_SHAPE;
  7. public static final VoxelShape AABB_SLAB_BOTTOM = BYGSlabBlock.BOTTOM_SHAPE;
  8. public static final VoxelShape NWD_CORNER = Block.makeCuboidShape(0.0D, 0.0D, 0.0D, 8.0D, 8.0D, 8.0D);
  9. public static final VoxelShape SWD_CORNER = Block.makeCuboidShape(0.0D, 0.0D, 8.0D, 8.0D, 8.0D, 16.0D);
  10. public static final VoxelShape NWU_CORNER = Block.makeCuboidShape(0.0D, 8.0D, 0.0D, 8.0D, 16.0D, 8.0D);
  11. public static final VoxelShape SWU_CORNER = Block.makeCuboidShape(0.0D, 8.0D, 8.0D, 8.0D, 16.0D, 16.0D);
  12. public static final VoxelShape NED_CORNER = Block.makeCuboidShape(8.0D, 0.0D, 0.0D, 16.0D, 8.0D, 8.0D);
  13. public static final VoxelShape SED_CORNER = Block.makeCuboidShape(8.0D, 0.0D, 8.0D, 16.0D, 8.0D, 16.0D);
  14. public static final VoxelShape NEU_CORNER = Block.makeCuboidShape(8.0D, 8.0D, 0.0D, 16.0D, 16.0D, 8.0D);
  15. public static final VoxelShape SEU_CORNER = Block.makeCuboidShape(8.0D, 8.0D, 8.0D, 16.0D, 16.0D, 16.0D);
  16. public static final VoxelShape[] SLAB_TOP_SHAPES = makeShapes(AABB_SLAB_TOP, NWD_CORNER, NED_CORNER, SWD_CORNER, SED_CORNER);
  17. public static final VoxelShape[] SLAB_BOTTOM_SHAPES = makeShapes(AABB_SLAB_BOTTOM, NWU_CORNER, NEU_CORNER, SWU_CORNER, SEU_CORNER);
  18. public static final int[] field_196522_K = new int[]{12, 5, 3, 10, 14, 13, 7, 11, 13, 7, 11, 14, 8, 4, 1, 2, 4, 1, 2, 8};
  19. public static java.util.function.Supplier<BlockState> stateSupplier;
  20. public Block modelBlock;
  21. public BlockState modelState;
  22.  
  23. public BYGStairBlock(java.util.function.Supplier<BlockState> state, Block.Properties properties) {
  24. super(properties);
  25. this.setDefaultState(this.stateContainer.getBaseState().with(FACING, Direction.NORTH).with(HALF, Half.BOTTOM).with(SHAPE, StairsShape.STRAIGHT).with(WATERLOGGED, Boolean.valueOf(false)));
  26. this.modelBlock = Blocks.AIR;
  27. this.modelState = Blocks.AIR.getDefaultState();
  28. stateSupplier = state;
  29. }
  30.  
  31. private static VoxelShape[] makeShapes(VoxelShape slabShape, VoxelShape nwCorner, VoxelShape neCorner, VoxelShape swCorner, VoxelShape seCorner) {
  32. return IntStream.range(0, 16).mapToObj((p_199780_5_) -> combineShapes(p_199780_5_, slabShape, nwCorner, neCorner, swCorner, seCorner)).toArray((p_199778_0_) -> new VoxelShape[p_199778_0_]);
  33. }
  34.  
  35. private static VoxelShape combineShapes(int bitfield, VoxelShape slabShape, VoxelShape nwCorner, VoxelShape neCorner, VoxelShape swCorner, VoxelShape seCorner) {
  36. VoxelShape voxelshape = slabShape;
  37. if ((bitfield & 1) != 0) {
  38. voxelshape = VoxelShapes.or(slabShape, nwCorner);
  39. }
  40.  
  41. if ((bitfield & 2) != 0) {
  42. voxelshape = VoxelShapes.or(voxelshape, neCorner);
  43. }
  44.  
  45. if ((bitfield & 4) != 0) {
  46. voxelshape = VoxelShapes.or(voxelshape, swCorner);
  47. }
  48.  
  49. if ((bitfield & 8) != 0) {
  50. voxelshape = VoxelShapes.or(voxelshape, seCorner);
  51. }
  52.  
  53. return voxelshape;
  54. }
  55.  
  56. private static StairsShape getShapeProperty(BlockState state, IBlockReader worldIn, BlockPos pos) {
  57. Direction direction = state.get(FACING);
  58. BlockState blockstate = worldIn.getBlockState(pos.offset(direction));
  59. if (isBlockStairs(blockstate) && state.get(HALF) == blockstate.get(HALF)) {
  60. Direction direction1 = blockstate.get(FACING);
  61. if (direction1.getAxis() != state.get(FACING).getAxis() && isDifferentStairs(state, worldIn, pos, direction1.getOpposite())) {
  62. if (direction1 == direction.rotateYCCW()) {
  63. return StairsShape.OUTER_LEFT;
  64. }
  65.  
  66. return StairsShape.OUTER_RIGHT;
  67. }
  68. }
  69.  
  70. BlockState blockstate1 = worldIn.getBlockState(pos.offset(direction.getOpposite()));
  71. if (isBlockStairs(blockstate1) && state.get(HALF) == blockstate1.get(HALF)) {
  72. Direction direction2 = blockstate1.get(FACING);
  73. if (direction2.getAxis() != state.get(FACING).getAxis() && isDifferentStairs(state, worldIn, pos, direction2)) {
  74. if (direction2 == direction.rotateYCCW()) {
  75. return StairsShape.INNER_LEFT;
  76. }
  77.  
  78. return StairsShape.INNER_RIGHT;
  79. }
  80. }
  81.  
  82. return StairsShape.STRAIGHT;
  83. }
  84.  
  85. private static boolean isDifferentStairs(BlockState state, IBlockReader worldIn, BlockPos pos, Direction face) {
  86. BlockState blockstate = worldIn.getBlockState(pos.offset(face));
  87. return !isBlockStairs(blockstate) || blockstate.get(FACING) != state.get(FACING) || blockstate.get(HALF) != state.get(HALF);
  88. }
  89.  
  90. public static boolean isBlockStairs(BlockState state) {
  91. return state.getBlock() instanceof StairsBlock;
  92. }
  93.  
  94. public boolean isTransparent(BlockState state) {
  95. return true;
  96. }
  97.  
  98. public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
  99. return (state.get(HALF) == Half.TOP ? SLAB_TOP_SHAPES : SLAB_BOTTOM_SHAPES)[field_196522_K[this.func_196511_x(state)]];
  100. }
  101.  
  102. private int func_196511_x(BlockState state) {
  103. return state.get(SHAPE).ordinal() * 4 + state.get(FACING).getHorizontalIndex();
  104. }
  105.  
  106. @OnlyIn(Dist.CLIENT)
  107. public void animateTick(BlockState stateIn, World worldIn, BlockPos pos, Random rand) {
  108. this.modelBlock.animateTick(stateIn, worldIn, pos, rand);
  109. }
  110.  
  111. public void onBlockClicked(BlockState state, World worldIn, BlockPos pos, PlayerEntity player) {
  112. this.modelState.onBlockClicked(worldIn, pos, player);
  113. }
  114.  
  115. public void onPlayerDestroy(IWorld worldIn, BlockPos pos, BlockState state) {
  116. this.modelBlock.onPlayerDestroy(worldIn, pos, state);
  117. }
  118.  
  119. /**
  120. * Returns how much this block can resist explosions from the passed in entity.
  121. */
  122. public float getExplosionResistance() {
  123. return this.modelBlock.getExplosionResistance();
  124. }
  125.  
  126. public int tickRate(IWorldReader worldIn) {
  127. return this.modelBlock.tickRate(worldIn);
  128. }
  129.  
  130. public void onBlockAdded(BlockState state, World worldIn, BlockPos pos, BlockState oldState, boolean isMoving) {
  131. if (state.getBlock() != state.getBlock()) {
  132. this.modelState.neighborChanged(worldIn, pos, Blocks.AIR, pos, false);
  133. this.modelBlock.onBlockAdded(this.modelState, worldIn, pos, oldState, false);
  134. }
  135. }
  136.  
  137. public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving) {
  138. if (state.getBlock() != newState.getBlock()) {
  139. this.modelState.onReplaced(worldIn, pos, newState, isMoving);
  140. }
  141. }
  142.  
  143. public void onEntityWalk(World worldIn, BlockPos pos, Entity entityIn) {
  144. this.modelBlock.onEntityWalk(worldIn, pos, entityIn);
  145. }
  146.  
  147. public void tick(BlockState state, ServerWorld worldIn, BlockPos pos, Random rand) {
  148. this.modelBlock.tick(state, worldIn, pos, rand);
  149. }
  150.  
  151. public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
  152. return this.modelState.onBlockActivated(worldIn, player, handIn, hit);
  153. }
  154.  
  155. public void onExplosionDestroy(World worldIn, BlockPos pos, Explosion explosionIn) {
  156. this.modelBlock.onExplosionDestroy(worldIn, pos, explosionIn);
  157. }
  158.  
  159. public BlockState getStateForPlacement(BlockItemUseContext context) {
  160. Direction direction = context.getFace();
  161. BlockPos blockpos = context.getPos();
  162. IFluidState ifluidstate = context.getWorld().getFluidState(blockpos);
  163. BlockState blockstate = this.getDefaultState().with(FACING, context.getPlacementHorizontalFacing()).with(HALF, direction != Direction.DOWN && (direction == Direction.UP || !(context.getHitVec().y - (double) blockpos.getY() > 0.5D)) ? Half.BOTTOM : Half.TOP).with(WATERLOGGED, Boolean.valueOf(ifluidstate.getFluid() == Fluids.WATER));
  164. return blockstate.with(SHAPE, getShapeProperty(blockstate, context.getWorld(), blockpos));
  165. }
  166.  
  167. public BlockState updatePostPlacement(BlockState stateIn, Direction facing, BlockState facingState, IWorld worldIn, BlockPos currentPos, BlockPos facingPos) {
  168. if (stateIn.get(WATERLOGGED)) {
  169. worldIn.getPendingFluidTicks().scheduleTick(currentPos, Fluids.WATER, Fluids.WATER.getTickRate(worldIn));
  170. }
  171.  
  172. return facing.getAxis().isHorizontal() ? stateIn.with(SHAPE, getShapeProperty(stateIn, worldIn, currentPos)) : super.updatePostPlacement(stateIn, facing, facingState, worldIn, currentPos, facingPos);
  173. }
  174.  
  175. public BlockState rotate(BlockState state, Rotation rot) {
  176. return state.with(FACING, rot.rotate(state.get(FACING)));
  177. }
  178.  
  179. public BlockState mirror(BlockState state, Mirror mirrorIn) {
  180. Direction direction = state.get(FACING);
  181. StairsShape stairsshape = state.get(SHAPE);
  182. switch (mirrorIn) {
  183. case LEFT_RIGHT:
  184. if (direction.getAxis() == Direction.Axis.Z) {
  185. switch (stairsshape) {
  186. case INNER_LEFT:
  187. return state.rotate(Rotation.CLOCKWISE_180).with(SHAPE, StairsShape.INNER_RIGHT);
  188. case INNER_RIGHT:
  189. return state.rotate(Rotation.CLOCKWISE_180).with(SHAPE, StairsShape.INNER_LEFT);
  190. case OUTER_LEFT:
  191. return state.rotate(Rotation.CLOCKWISE_180).with(SHAPE, StairsShape.OUTER_RIGHT);
  192. case OUTER_RIGHT:
  193. return state.rotate(Rotation.CLOCKWISE_180).with(SHAPE, StairsShape.OUTER_LEFT);
  194. default:
  195. return state.rotate(Rotation.CLOCKWISE_180);
  196. }
  197. }
  198. break;
  199. case FRONT_BACK:
  200. if (direction.getAxis() == Direction.Axis.X) {
  201. switch (stairsshape) {
  202. case INNER_LEFT:
  203. return state.rotate(Rotation.CLOCKWISE_180).with(SHAPE, StairsShape.INNER_LEFT);
  204. case INNER_RIGHT:
  205. return state.rotate(Rotation.CLOCKWISE_180).with(SHAPE, StairsShape.INNER_RIGHT);
  206. case OUTER_LEFT:
  207. return state.rotate(Rotation.CLOCKWISE_180).with(SHAPE, StairsShape.OUTER_RIGHT);
  208. case OUTER_RIGHT:
  209. return state.rotate(Rotation.CLOCKWISE_180).with(SHAPE, StairsShape.OUTER_LEFT);
  210. case STRAIGHT:
  211. return state.rotate(Rotation.CLOCKWISE_180);
  212. }
  213. }
  214. }
  215.  
  216. return super.mirror(state, mirrorIn);
  217. }
  218.  
  219. protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
  220. builder.add(FACING, HALF, SHAPE, WATERLOGGED);
  221. }
  222.  
  223. public IFluidState getFluidState(BlockState state) {
  224. return state.get(WATERLOGGED) ? Fluids.WATER.getStillFluidState(false) : super.getFluidState(state);
  225. }
  226.  
  227. public boolean allowsMovement(BlockState state, IBlockReader worldIn, BlockPos pos, PathType type) {
  228. return false;
  229. }
  230.  
  231. private Block getModelBlock() {
  232. return getModelState().getBlock();
  233. }
  234.  
  235. private BlockState getModelState() {
  236. return stateSupplier.get();
  237. }
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement