Guest User

MysticalCropBlock

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