Advertisement
jayhillx

ModSapling

Aug 19th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. package com.meepshadow.mysticsbiomes.init;
  2.  
  3. import net.minecraft.block.Block;
  4. import net.minecraft.block.BlockState;
  5. import net.minecraft.block.BushBlock;
  6. import net.minecraft.block.IGrowable;
  7. import net.minecraft.block.trees.Tree;
  8. import net.minecraft.state.IntegerProperty;
  9. import net.minecraft.state.StateContainer;
  10. import net.minecraft.state.properties.BlockStateProperties;
  11. import net.minecraft.util.math.BlockPos;
  12. import net.minecraft.util.math.shapes.ISelectionContext;
  13. import net.minecraft.util.math.shapes.VoxelShape;
  14. import net.minecraft.world.IBlockReader;
  15. import net.minecraft.world.World;
  16. import net.minecraft.world.server.ServerWorld;
  17. import net.minecraftforge.event.ForgeEventFactory;
  18.  
  19. import java.util.Random;
  20. import java.util.function.Supplier;
  21.  
  22. public class ModSapling extends BushBlock implements IGrowable {
  23.  
  24. public static final IntegerProperty STAGE = BlockStateProperties.STAGE_0_1;
  25. protected static final VoxelShape SHAPE = Block.makeCuboidShape(2.0D, 0.0D, 2.0D, 14.0D, 12.0D, 14.0D);
  26. private final Supplier<Tree> tree;
  27.  
  28. public ModSapling(Supplier<Tree> treeIn, Properties properties) {
  29. super (properties);
  30. this.tree = treeIn;
  31. }
  32.  
  33. @Override
  34. public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
  35. return SHAPE;
  36. }
  37.  
  38. @SuppressWarnings("deprecation")
  39. @Override
  40. public void tick(BlockState state, ServerWorld worldIn, BlockPos pos, Random rand) {
  41. super.tick(state, worldIn, pos, rand);
  42. if(!worldIn.isAreaLoaded(pos, 1)) {
  43. return;
  44. }
  45. if(worldIn.getLight(pos.up()) >= 9 && rand.nextInt(7) == 0) {
  46. this.grow(worldIn, rand, pos, state);
  47. }
  48. }
  49.  
  50. public void grow(ServerWorld serverWorld, BlockPos pos, BlockState state, Random rand) {
  51. if(state.get(STAGE) == 0) {
  52. serverWorld.setBlockState(pos, state.cycle(STAGE), 4);
  53. } else {
  54. if (!ForgeEventFactory.saplingGrowTree(serverWorld, rand, pos)) return;
  55. this.tree.get().place(serverWorld, serverWorld.getChunkProvider().getChunkGenerator(), pos, state, rand);
  56. }
  57. }
  58.  
  59. @Override
  60. public void grow(ServerWorld worldIn, Random rand, BlockPos pos, BlockState state) {
  61. this.grow(worldIn, rand, pos, state);
  62. }
  63.  
  64. @Override
  65. public boolean canGrow(IBlockReader worldIn, BlockPos pos, BlockState state, boolean isClient) {
  66. return true;
  67. }
  68.  
  69. @Override
  70. public boolean canUseBonemeal(World worldIn, Random rand, BlockPos pos, BlockState state) {
  71. return (double)worldIn.rand.nextFloat() < 0.45D;
  72. }
  73.  
  74. @Override
  75. protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
  76. builder.add(STAGE);
  77. }
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement