Advertisement
Guest User

Untitled

a guest
Jun 18th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.99 KB | None | 0 0
  1. package com.rbs.slurpiesdongles.blocks;
  2.  
  3. import com.rbs.slurpiesdongles.SlurpiesDongles;
  4. import com.rbs.slurpiesdongles.item.ItemRegistry;
  5. import net.minecraft.block.BlockCrops;
  6. import net.minecraft.block.IGrowable;
  7. import net.minecraftforge.common.IPlantable;
  8. import net.minecraft.block.Block;
  9. import net.minecraft.block.properties.PropertyInteger;
  10. import net.minecraft.block.state.BlockStateContainer;
  11. import net.minecraft.block.state.IBlockState;
  12. import net.minecraft.creativetab.CreativeTabs;
  13. import net.minecraft.entity.item.EntityItem;
  14. import net.minecraft.entity.player.EntityPlayer;
  15. import net.minecraft.init.Blocks;
  16. import net.minecraft.item.Item;
  17. import net.minecraft.item.ItemStack;
  18. import net.minecraft.util.EnumFacing;
  19. import net.minecraft.util.EnumHand;
  20. import net.minecraft.util.math.AxisAlignedBB;
  21. import net.minecraft.util.math.BlockPos;
  22. import net.minecraft.util.math.MathHelper;
  23. import net.minecraft.world.IBlockAccess;
  24. import net.minecraft.world.World;
  25. import net.minecraftforge.common.EnumPlantType;
  26. import net.minecraftforge.fml.common.FMLLog;
  27. /**
  28. * Created by RedBu on 6/17/2016.
  29. */
  30.  
  31.  
  32. import java.util.List;
  33. import java.util.Random;
  34.  
  35. public class BlockRbsCrop extends BlockCrops implements IGrowable, IPlantable {
  36.  
  37. public static final PropertyInteger AGE = PropertyInteger.create("age", 0, 3);
  38.  
  39. public static final AxisAlignedBB[] CROPS_AABB = new AxisAlignedBB[]{new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 0.125D, 1.0D), new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 0.25D, 1.0D), new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 0.375D, 1.0D), new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 0.5D, 1.0D), new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 0.625D, 1.0D), new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 0.75D, 1.0D), new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 0.875D, 1.0D), new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 1.0D, 1.0D)};
  40.  
  41. public final String registerName;
  42.  
  43. public BlockRbsCrop(String registerName)
  44. {super();
  45.  
  46. this.registerName = registerName;
  47. this.setDefaultState(blockState.getBaseState().withProperty(getAge(), 0));
  48. this.setCreativeTab(SlurpiesDongles.SD);
  49. }
  50.  
  51. @Override
  52. public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) {
  53. // CROPS_AABB is based on an age range from 0 to 7. Times two should fix that issue.
  54. return CROPS_AABB[state.getValue(getAge()) * 2];
  55. }
  56.  
  57. public boolean isSuitableSoilBlock(Block soilBlock) {
  58. return soilBlock == Blocks.FARMLAND;
  59. }
  60.  
  61. protected PropertyInteger getAge() {
  62. return AGE;
  63. }
  64.  
  65. public int getHarvestReadyAge() {
  66. return 3;
  67. }
  68.  
  69. public boolean isHarvestReady(IBlockState state) {
  70. return state.getValue(getAge()) >= getHarvestReadyAge();
  71. }
  72.  
  73.  
  74. protected Item getSeeds() {
  75. final Item seeds = ItemRegistry.seedsMap.get(this);
  76.  
  77. if (seeds == null) {
  78. FMLLog.bigWarning("No seeds have been set up.");
  79. return new Item();
  80. }
  81.  
  82. return seeds;
  83. }
  84.  
  85. @Override
  86. public ItemStack getItem(World worldIn, BlockPos pos, IBlockState state) {
  87. return new ItemStack(getSeeds());
  88. }
  89.  
  90. @Override
  91. public boolean canGrow(World worldIn, BlockPos pos, IBlockState state, boolean isClient) {
  92. return !isHarvestReady(state);
  93. }
  94.  
  95. protected Item getHarvestedItem() {
  96. final Item harvestedItem = ItemRegistry.harvestedItemMap.get(this);
  97. if (harvestedItem == null) {
  98. FMLLog.bigWarning("No harvested item has been set up.");
  99. return new Item();
  100. }
  101.  
  102. return harvestedItem;
  103. }
  104.  
  105. @Override
  106. public IBlockState getStateFromMeta(int meta) {
  107. return getDefaultState().withProperty(getAge(), meta);
  108. }
  109.  
  110. @Override
  111. public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand) {
  112. this.checkAndDropBlock(worldIn, pos, state);
  113.  
  114. if (worldIn.getLightFromNeighbors(pos.up()) >= 9) {
  115. int i = this.getMetaFromState(state);
  116.  
  117. if (i < this.getHarvestReadyAge()) {
  118. float f = getGrowthChance(this, worldIn, pos);
  119.  
  120. if (rand.nextInt((int) (25.0F / f) + 1) == 0) {
  121. worldIn.setBlockState(pos, this.getStateFromMeta(i + 1), 2);
  122. }
  123. }
  124. }
  125. }
  126.  
  127. @Override
  128. public Item getItemDropped(IBlockState state, Random rand, int fortune) {
  129. if (!isHarvestReady(state)) {
  130. return getSeeds();
  131. } else {
  132. return getHarvestedItem();
  133. }
  134. }
  135.  
  136. public int getMetaFromState(IBlockState state) {
  137. return state.getValue(getAge());
  138. }
  139.  
  140. @Override
  141. public boolean canPlaceBlockAt(World world, BlockPos pos) {
  142. Block soilBlock = world.getBlockState(pos.down()).getBlock();
  143.  
  144. return this.isSuitableSoilBlock(soilBlock);
  145. }
  146.  
  147. @Override
  148. public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) {
  149. if (isHarvestReady(state)) {
  150. if (worldIn.isRemote) {
  151. return true;
  152. }
  153.  
  154. final ItemStack savedStack = new ItemStack(getHarvestedItem());
  155.  
  156. worldIn.setBlockState(pos, state.withProperty(AGE, 0), 3);
  157. final EntityItem entityItem = new EntityItem(worldIn, playerIn.posX, playerIn.posY - 1D, playerIn.posZ, savedStack);
  158. worldIn.spawnEntityInWorld(entityItem);
  159. entityItem.onCollideWithPlayer(playerIn);
  160. return true;
  161. }
  162. return false;
  163. }
  164.  
  165. @SuppressWarnings({"unchecked", "rawtypes"})
  166. @Override
  167. public void getSubBlocks(Item itemIn, CreativeTabs tab, List list) {
  168. list.add(new ItemStack(itemIn, 1, 0));
  169. list.add(new ItemStack(itemIn, 1, 3));
  170. }
  171.  
  172.  
  173. @Override
  174. public EnumPlantType getPlantType(IBlockAccess world, BlockPos pos) {
  175. return EnumPlantType.Crop;
  176. }
  177.  
  178. @Override
  179. protected BlockStateContainer createBlockState() {
  180. return new BlockStateContainer(this, AGE);
  181. }
  182.  
  183. protected int getRandomInt(World world) {
  184. return MathHelper.getRandomIntegerInRange(world.rand, 1, 3);
  185. }
  186.  
  187. @Override
  188. public void grow(World worldIn, BlockPos pos, IBlockState state) {
  189. int newGrowth = getMetaFromState(state) + getRandomInt(worldIn);
  190. int maxGrowth = getHarvestReadyAge();
  191.  
  192. if (newGrowth > maxGrowth) {
  193. newGrowth = maxGrowth;
  194. }
  195.  
  196. worldIn.setBlockState(pos, getStateFromMeta(newGrowth), 2);
  197. }
  198.  
  199. @Override
  200. public void grow(World worldIn, Random rand, BlockPos pos, IBlockState state) {
  201. grow(worldIn, pos, state);
  202. }
  203.  
  204. @Override
  205. public int hashCode() {
  206. return registerName.hashCode();
  207. }
  208.  
  209. @Override
  210. public boolean equals(Object obj) {
  211. return (obj instanceof BlockRbsCrop && registerName.equals(((BlockRbsCrop) obj).registerName));
  212. }
  213.  
  214. @Override
  215. public List<ItemStack> getDrops(IBlockAccess world, BlockPos pos, IBlockState state, int fortune) {
  216.  
  217. List<ItemStack> ret = new java.util.ArrayList<ItemStack>();
  218.  
  219. Random rand = world instanceof World ? ((World) world).rand : new Random();
  220.  
  221. int age = getMetaFromState(state);
  222.  
  223. int count = quantityDropped(state, fortune, rand);
  224. for (int i = 0; i < count; i++) {
  225. Item item = this.getItemDropped(state, rand, fortune);
  226. if (item != null) {
  227. ret.add(new ItemStack(item, 1, this.damageDropped(state)));
  228. }
  229. }
  230.  
  231. if (age >= getHarvestReadyAge()) {
  232. for (int i = 0; i < 3 + fortune; ++i) {
  233. if (rand.nextInt(2 * getHarvestReadyAge()) <= age) {
  234. ret.add(new ItemStack(this.getSeed(), 1, 0));
  235. }
  236. }
  237. }
  238.  
  239. return ret;
  240. }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement