minsto

Cannabisplant

Jul 30th, 2021
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.99 KB | None | 0 0
  1. package com.Ivorius.psychedelicraft.Crops;
  2.  
  3. import com.Ivorius.psychedelicraft.Items.ItemsInit;
  4. import net.minecraft.block.*;
  5. import net.minecraft.fluid.FluidState;
  6. import net.minecraft.item.Item;
  7. import net.minecraft.item.ItemStack;
  8. import net.minecraft.state.IntegerProperty;
  9. import net.minecraft.state.StateContainer;
  10. import net.minecraft.tags.FluidTags;
  11. import net.minecraft.util.Direction;
  12. import net.minecraft.util.IItemProvider;
  13. import net.minecraft.util.math.BlockPos;
  14. import net.minecraft.util.math.MathHelper;
  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.minecraft.world.server.ServerWorld;
  22.  
  23. import javax.swing.text.html.BlockView;
  24. import java.util.Random;
  25.  
  26. import static net.minecraft.state.properties.BlockStateProperties.AGE_7;
  27.  
  28. public class Cannabisplant extends BushBlock implements IGrowable{
  29.  
  30. public static final IntegerProperty AGE = AGE_7;
  31. private static final VoxelShape[] SHAPE_BY_AGE = new VoxelShape[]{Block.box(0.0D, 0.0D, 0.0D, 16.0D, 2.0D, 16.0D), Block.box(0.0D, 0.0D, 0.0D, 16.0D, 4.0D, 16.0D), Block.box(0.0D, 0.0D, 0.0D, 16.0D, 6.0D, 16.0D), Block.box(0.0D, 0.0D, 0.0D, 16.0D, 8.0D, 16.0D), Block.box(0.0D, 0.0D, 0.0D, 16.0D, 10.0D, 16.0D), Block.box(0.0D, 0.0D, 0.0D, 16.0D, 12.0D, 16.0D), Block.box(0.0D, 0.0D, 0.0D, 16.0D, 14.0D, 16.0D), Block.box(0.0D, 0.0D, 0.0D, 16.0D, 16.0D, 16.0D)};
  32.  
  33. public Cannabisplant(Properties builder) {
  34. super(builder);
  35. this.registerDefaultState(this.stateDefinition.any().setValue(AGE, Integer.valueOf(0)));
  36. }
  37.  
  38.  
  39. @Override
  40. public ItemStack getCloneItemStack(IBlockReader p_185473_1_, BlockPos p_185473_2_, BlockState p_185473_3_) {
  41. return new ItemStack(ItemsInit.cannabisseeds.get());
  42. }
  43. //@Override
  44. //protected IItemProvider getBaseSeedId() {
  45. // return ItemsInit.cannabisseeds.get().getItem();
  46. // }
  47.  
  48. @Override
  49. public VoxelShape getShape(BlockState p_220053_1_, IBlockReader p_220053_2_, BlockPos p_220053_3_, ISelectionContext p_220053_4_) {
  50. return SHAPE_BY_AGE[p_220053_1_.getValue(this.getAgeProperty())];
  51. }
  52. @Override
  53. public BlockState updateShape(BlockState p_196271_1_, Direction p_196271_2_, BlockState p_196271_3_, IWorld p_196271_4_, BlockPos p_196271_5_, BlockPos p_196271_6_) {
  54. if (!p_196271_1_.canSurvive(p_196271_4_, p_196271_5_)) {
  55. p_196271_4_.getBlockTicks().scheduleTick(p_196271_5_, this, 1);
  56. }
  57.  
  58. return super.updateShape(p_196271_1_, p_196271_2_, p_196271_3_, p_196271_4_, p_196271_5_, p_196271_6_);
  59. }
  60.  
  61.  
  62. @Deprecated
  63. public void tick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
  64. if (!state.isViewBlocking(world, pos)) {
  65. world.destroyBlock(pos, true);
  66. } else if (world.isEmptyBlock(pos.above())) {
  67. int i = 1;
  68. for (; world.getBlockState(pos.below(i)).getBlock() == this; ++i);
  69. if (i < 14) {
  70. int j = state.getValue(AGE);
  71. if (j == 7) {
  72.  
  73. world.setBlock(pos, state.setValue(AGE, 7), 4);
  74. } else {
  75. world.setBlock(pos, state.setValue(AGE, j + 1), 4);
  76. }
  77. }
  78. }
  79. }
  80. @Override
  81. public BlockState getPlant(IBlockReader world, BlockPos pos) {
  82. return defaultBlockState();
  83. }
  84.  
  85.  
  86. @Override
  87. protected void createBlockStateDefinition(StateContainer.Builder<Block, BlockState> p_206840_1_) {
  88. p_206840_1_.add(AGE);
  89. }
  90. @Override
  91. public boolean canSurvive(BlockState p_196260_1_, IWorldReader p_196260_2_, BlockPos p_196260_3_) {
  92. BlockState soil = p_196260_2_.getBlockState(p_196260_3_.below());
  93. if (soil.canSustainPlant(p_196260_2_, p_196260_3_.below(), Direction.UP, this)) return true;
  94. BlockState blockstate = p_196260_2_.getBlockState(p_196260_3_.below());
  95. if (blockstate.getBlock() == this) {
  96. return true;
  97. } else {
  98. if (blockstate.is(Blocks.DIRT) || blockstate.is(Blocks.COARSE_DIRT) || blockstate.is(Blocks.PODZOL)) {
  99. BlockPos blockpos = p_196260_3_.below();
  100.  
  101. for(Direction direction : Direction.Plane.HORIZONTAL) {
  102. BlockState blockstate1 = p_196260_2_.getBlockState(blockpos.relative(direction));
  103. FluidState fluidstate = p_196260_2_.getFluidState(blockpos.relative(direction));
  104. if (fluidstate.is(FluidTags.WATER) || blockstate1.is(Blocks.FROSTED_ICE)|| blockstate.is(Blocks.DIRT)|| blockstate.is(Blocks.PODZOL)) {
  105. return true;
  106. }
  107. }
  108. }
  109.  
  110. return false;
  111. }
  112. }
  113.  
  114.  
  115.  
  116. public void randomTick(BlockState p_225542_1_, ServerWorld p_225542_2_, BlockPos p_225542_3_, Random p_225542_4_) {
  117. BlockPos blockpos = p_225542_3_.above();
  118. if (p_225542_2_.isEmptyBlock(blockpos)) {
  119. int i;
  120. for(i = 1; p_225542_2_.getBlockState(p_225542_3_.below(i)).is(this); ++i) {
  121. }
  122.  
  123. if (i < 8) {
  124. int j = p_225542_1_.getValue(AGE);
  125. if(net.minecraftforge.common.ForgeHooks.onCropsGrowPre(p_225542_2_, blockpos, p_225542_1_, true)) {
  126. if (j == 7) {
  127. p_225542_2_.setBlockAndUpdate(blockpos, this.defaultBlockState());
  128. BlockState blockstate = p_225542_1_.setValue(AGE, Integer.valueOf(7));
  129. p_225542_2_.setBlock(p_225542_3_, blockstate, 7);
  130. blockstate.neighborChanged(p_225542_2_, blockpos, this, p_225542_3_, false);
  131. } else {
  132. p_225542_2_.setBlock(p_225542_3_, p_225542_1_.setValue(AGE, Integer.valueOf(j + 1)), 7);
  133. }
  134. net.minecraftforge.common.ForgeHooks.onCropsGrowPost(p_225542_2_, p_225542_3_, p_225542_1_);
  135. }
  136. }
  137. }
  138. }
  139.  
  140.  
  141. @Override
  142. public boolean isValidBonemealTarget(IBlockReader p_176473_1_, BlockPos p_176473_2_, BlockState p_176473_3_, boolean p_176473_4_) {
  143. return !this.isMaxAge(p_176473_3_);
  144. }
  145. public boolean isMaxAge(BlockState p_185525_1_) {
  146. return p_185525_1_.getValue(this.getAgeProperty()) >= this.getMaxAge();
  147. }
  148. @Override
  149. public boolean isBonemealSuccess(World p_180670_1_, Random p_180670_2_, BlockPos p_180670_3_, BlockState p_180670_4_) {
  150. return true;
  151. }
  152.  
  153. @Override
  154. public void performBonemeal(ServerWorld p_225535_1_, Random p_225535_2_, BlockPos p_225535_3_, BlockState p_225535_4_) {
  155. this.growCrops(p_225535_1_, p_225535_3_, p_225535_4_);
  156. }
  157.  
  158.  
  159. protected int getAge(BlockState p_185527_1_) {
  160. return p_185527_1_.getValue(this.getAgeProperty());
  161. }
  162. public IntegerProperty getAgeProperty() {
  163. return AGE;
  164. }
  165.  
  166. public int getMaxAge() {
  167. return 7;
  168. }
  169. public void growCrops(World p_176487_1_, BlockPos p_176487_2_, BlockState p_176487_3_) {
  170. int i = this.getAge(p_176487_3_) + this.getBonemealAgeIncrease(p_176487_1_);
  171. int j = this.getMaxAge();
  172. if (i > j) {
  173. i = j;
  174. }
  175.  
  176. p_176487_1_.setBlock(p_176487_2_, this.getStateForAge(i), 2);
  177. }
  178. public BlockState getStateForAge(int p_185528_1_) {
  179. return this.defaultBlockState().setValue(this.getAgeProperty(), Integer.valueOf(p_185528_1_));
  180. }
  181.  
  182. protected int getBonemealAgeIncrease(World p_185529_1_) {
  183. return MathHelper.nextInt(p_185529_1_.random, 2, 5);
  184. }
  185.  
  186. protected static float getGrowthSpeed(Block p_180672_0_, IBlockReader p_180672_1_, BlockPos p_180672_2_) {
  187. float f = 1.0F;
  188. BlockPos blockpos = p_180672_2_.below();
  189.  
  190. for (int i = -1; i <= 1; ++i) {
  191. for (int j = -1; j <= 1; ++j) {
  192. float f1 = 0.0F;
  193. BlockState blockstate = p_180672_1_.getBlockState(blockpos.offset(i, 0, j));
  194. if (blockstate.canSustainPlant(p_180672_1_, blockpos.offset(i, 0, j), net.minecraft.util.Direction.UP, (net.minecraftforge.common.IPlantable) p_180672_0_)) {
  195. f1 = 1.0F;
  196. if (blockstate.isFertile(p_180672_1_, p_180672_2_.offset(i, 0, j))) {
  197. f1 = 3.0F;
  198. }
  199. }
  200.  
  201. if (i != 0 || j != 0) {
  202. f1 /= 4.0F;
  203. }
  204.  
  205. f += f1;
  206. }
  207. }
  208. BlockPos blockpos1 = p_180672_2_.north();
  209. BlockPos blockpos2 = p_180672_2_.south();
  210. BlockPos blockpos3 = p_180672_2_.west();
  211. BlockPos blockpos4 = p_180672_2_.east();
  212. boolean flag = p_180672_0_ == p_180672_1_.getBlockState(blockpos3).getBlock() || p_180672_0_ == p_180672_1_.getBlockState(blockpos4).getBlock();
  213. boolean flag1 = p_180672_0_ == p_180672_1_.getBlockState(blockpos1).getBlock() || p_180672_0_ == p_180672_1_.getBlockState(blockpos2).getBlock();
  214. if (flag && flag1) {
  215. f /= 2.0F;
  216. } else {
  217. boolean flag2 = p_180672_0_ == p_180672_1_.getBlockState(blockpos3.north()).getBlock() || p_180672_0_ == p_180672_1_.getBlockState(blockpos4.north()).getBlock() || p_180672_0_ == p_180672_1_.getBlockState(blockpos4.south()).getBlock() || p_180672_0_ == p_180672_1_.getBlockState(blockpos3.south()).getBlock();
  218. if (flag2) {
  219. f /= 2.0F;
  220. }
  221. }
  222.  
  223. return f;
  224. }
  225.  
  226.  
  227.  
  228.  
  229. private Item seed;
  230. public void setSeed(Item seed) {
  231. this.seed = seed;
  232. }
  233.  
  234.  
  235. protected IItemProvider getSeedsItem() {
  236. return ItemsInit.cannabisseeds.get().getItem();
  237. }
  238. protected boolean canPlantOnTop(BlockState floor, BlockView world, BlockPos pos) {
  239. return floor.is(Blocks.DIRT) || floor.is(Blocks.FARMLAND);
  240. }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment