Guest User

Untitled

a guest
Aug 17th, 2015
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.78 KB | None | 0 0
  1.  
  2. public class BlockCorn extends Block implements IPlantable{
  3.     public static final PropertyInteger AGE = PropertyInteger.create("age", 0, 11);
  4.    
  5.     public BlockCorn()
  6.     {
  7.         super(Material.plants);
  8.         setDefaultState(this.blockState.getBaseState().withProperty(AGE, Integer.valueOf(0)));
  9.         float f = 0.375F;
  10.         setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, 1.0F, 0.5F + f);
  11.         setTickRandomly(true);
  12.         setHardness(0.3F);  
  13.     }
  14.    
  15.     @Override
  16.     public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
  17.     {
  18.         int j = ((Integer)state.getValue(AGE)).intValue();
  19.        
  20.         if(j != 4 && j != 7 && j != 9 && j!= 10 && j!= 11){ //states  are on the bottom,have blocks above, or corn is done, do not grow them
  21.             if (worldIn.getBlockState(pos.down()).getBlock() == this || this.checkForDrop(worldIn, pos, state))
  22.             {
  23.                 if (worldIn.isAirBlock(pos.up()))
  24.                 {
  25.                     if (worldIn.getLightFromNeighbors(pos.up()) >= 9)
  26.                     {
  27.                         if (rand.nextInt((int)(25.0F / .5) + 1) == 0)
  28.                         {
  29.                            
  30.                                 //Then Corn can grow
  31.                                 if(j == 0 || j == 1 || j == 2){
  32.                                     worldIn.setBlockState(pos, this.getStateFromMeta(j+1));
  33.                                 }
  34.                                 else
  35.                                 if(j == 3){
  36.                                     worldIn.setBlockState(pos, this.getStateFromMeta(4));
  37.                                     worldIn.setBlockState(pos.up(), this.getStateFromMeta(5));
  38.                                 }
  39.                                 if(j == 5){
  40.                                     worldIn.setBlockState(pos, this.getStateFromMeta(6));
  41.                                 }
  42.                                 if(j == 6){
  43.                                     worldIn.setBlockState(pos, this.getStateFromMeta(7));
  44.                                     worldIn.setBlockState(pos.up(), this.getStateFromMeta(8));
  45.                                 }
  46.                                 if(j == 9){
  47.                                     worldIn.setBlockState(pos.up(), this.getStateFromMeta(11));
  48.                                     worldIn.setBlockState(pos, this.getStateFromMeta(10));
  49.                                     worldIn.setBlockState(pos.down(), this.getStateFromMeta(9));
  50.                                 }
  51.                         }
  52.                     }
  53.                 }
  54.             }
  55.         }
  56.     }
  57.                    
  58.                    
  59.                    
  60.                    
  61.                    
  62.                    
  63.                /*    
  64.                     if (j == 15)
  65.                     {
  66.                         worldIn.setBlockState(pos.up(), this.getDefaultState());
  67.                         worldIn.setBlockState(pos, state.withProperty(AGE, Integer.valueOf(0)), 4);
  68.                     }
  69.                     else
  70.                     {
  71.                         worldIn.setBlockState(pos, state.withProperty(AGE, Integer.valueOf(j + 1)), 4);
  72.                     }*/
  73.  
  74.    
  75.     @Override
  76.     public void onNeighborBlockChange(World world, BlockPos pos, IBlockState state, Block neighborBlock)
  77.     {
  78.         this.checkForDrop(world, pos, state);
  79.     }
  80.  
  81.     protected final boolean checkForDrop(World world, BlockPos pos, IBlockState state)
  82.     {
  83.         if (this.canBlockStay(world, pos))
  84.         {
  85.             return true;
  86.         }
  87.         else
  88.         {
  89.             int s = ((Integer)state.getValue(AGE)).intValue();
  90.             if(s == 9 || s == 10 || s == 11 ){ //if corn is ripe
  91.                
  92.             }
  93.            
  94.             ItemStack out;
  95.            
  96.             if(world.rand.nextFloat() >= .40){
  97.                 out = new ItemStack(GameRegistry.findItem(Corn.MODID, "corncob"),2);
  98.             }
  99.             out = new ItemStack(GameRegistry.findItem(Corn.MODID, "corncob"));
  100.  
  101.             super.spawnAsEntity(world,pos,out);
  102.             world.setBlockToAir(pos);
  103.             return false;
  104.         }
  105.     }
  106.    
  107.  
  108.     public boolean canBlockStay(World world, BlockPos pos)
  109.     {
  110.         if (world.getBlockState(pos.down()).getBlock() == this)
  111.         {
  112.             return true;
  113.         }
  114.         else{
  115.             return this.canPlaceBlockAt(world, pos);
  116.         }
  117.     }
  118.    
  119.     @Override
  120.     public boolean canPlaceBlockAt(World world, BlockPos pos)
  121.     {
  122.         Block block = world.getBlockState(pos.down()).getBlock();
  123.        
  124.         if (block.canSustainPlant(world, pos, EnumFacing.UP, this)){
  125.             return true;
  126.         }
  127.         else if (block != Blocks.farmland )
  128.         {
  129.             return false;
  130.         }
  131.        
  132.         return false;
  133.     }
  134.    
  135.     @Override
  136.     public Item getItemDropped(IBlockState state, Random rand, int fortune)
  137.     {
  138.         int s = ((Integer)state.getValue(AGE)).intValue();
  139.         if(s == 9 || s == 10 || s == 11 ){ //if corn is ripe
  140.             return GameRegistry.findItem(Corn.MODID, "corncob");
  141.         }
  142.         return GameRegistry.findItem(Corn.MODID, "kernels");
  143.        
  144.     }
  145.    
  146.     @Override
  147.     public boolean isOpaqueCube()
  148.     {
  149.         return false;
  150.     }
  151.    
  152.     @Override
  153.     public boolean isFullCube()
  154.     {
  155.         return false;
  156.     }
  157.    
  158.     @SideOnly(Side.CLIENT)@Override
  159.     public Item getItem(World worldIn, BlockPos pos)
  160.     {
  161.         int s = ((Integer)worldIn.getBlockState(pos).getValue(AGE)).intValue();
  162.         if(s == 9 || s == 10 || s == 11 ){ //if corn is ripe
  163.             return GameRegistry.findItem(Corn.MODID, "corncob");
  164.         }
  165.         return GameRegistry.findItem(Corn.MODID, "kernels");
  166.     }
  167.    
  168.     @Override
  169.     public List<ItemStack> getDrops(IBlockAccess world,BlockPos pos, IBlockState state, int fortune){
  170.         List<ItemStack> out = new ArrayList<ItemStack>();
  171.         if(((World)world).rand.nextFloat() >= .50){
  172.             out.add(new ItemStack(GameRegistry.findItem(Corn.MODID, "corncob"),2));
  173.             return out;
  174.         }
  175.         out.add(new ItemStack(GameRegistry.findItem(Corn.MODID, "corncob")));
  176.         return out;
  177.     }
  178.  
  179.     @SideOnly(Side.CLIENT)@Override
  180.     public int colorMultiplier(IBlockAccess worldIn, BlockPos pos, int renderPass)
  181.     {
  182.         return worldIn.getBiomeGenForCoords(pos).getGrassColorAtPos(pos);
  183.     }
  184.    
  185.     @Override
  186.     public int getMetaFromState(IBlockState state)
  187.     {
  188.         return ((Integer)state.getValue(AGE)).intValue();
  189.     }
  190.    
  191.     @Override
  192.     public IBlockState getStateFromMeta(int meta)
  193.     {
  194.         return this.getDefaultState().withProperty(AGE, Integer.valueOf(meta));
  195.     }
  196.    
  197.     @Override
  198.     protected BlockState createBlockState()
  199.     {
  200.         return new BlockState(this, new IProperty[] {AGE});
  201.     }
  202.  
  203.  
  204.     @Override
  205.     public EnumPlantType getPlantType(IBlockAccess world, BlockPos pos) {
  206.         return EnumPlantType.Crop;
  207.     }
  208.    
  209.     protected Item getSeed()
  210.     {
  211.         return GameRegistry.findItem(Corn.MODID, "kernels");
  212.     }
  213.  
  214.     @Override
  215.     public IBlockState getPlant(IBlockAccess world, BlockPos pos) {
  216.         // TODO Auto-generated method stub
  217.         IBlockState state = world.getBlockState(pos);
  218.         if (state.getBlock() != this) return getDefaultState();
  219.         return state;
  220.        
  221.         //return this.blockState.getBaseState().withProperty(AGE, Integer.valueOf(0));
  222.     }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment