package crazypants.enderio.machine.farm.farmers;

import java.util.List;

import net.minecraft.block.Block;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.util.ForgeDirection;
import crazypants.enderio.config.Config;
import crazypants.enderio.machine.farm.TileFarmStation;
import crazypants.util.BlockCoord;

public class NaturaBerryFarmer extends PickableFarmer {

  public NaturaBerryFarmer(Block plantedBlock, int plantedBlockMeta, int grownBlockMeta, ItemStack seeds) {
    super(plantedBlock, plantedBlockMeta, grownBlockMeta, seeds);
  }

  @Override
  public IHarvestResult harvestBlock(TileFarmStation farm, BlockCoord bc, Block block, int meta) {
    
    if(block != getPlantedBlock()) {
      return null;
    }
    if(!farm.hasHoe()) {
      farm.setNotification(TileFarmStation.NOTIFICATION_NO_HOE);
      return null;
    }
    
    IHarvestResult res = new HarvestResult();    
    
    BlockCoord checkBlock = bc;
    for(int i=0; i < 5 && farm.hasHoe(); i++) {
      meta = farm.getBlockMeta(checkBlock);
      IHarvestResult blockRes = super.harvestBlock(farm, checkBlock, block, meta);
    
      if(blockRes != null) {       
        res.getHarvestedBlocks().add(checkBlock);
        List<EntityItem> addToDrops = blockRes.getDrops();
        for(EntityItem stack : addToDrops) {
          res.getDrops().add(stack);
        }
      }
      checkBlock = checkBlock.getLocation(ForgeDirection.UP);
    }
    
    if(res.getHarvestedBlocks().isEmpty()) {
      return null;
    }    
    
    return res;
  }

  @Override
  public boolean canHarvest(TileFarmStation farm, BlockCoord bc, Block block, int meta) {
    if(!Config.farmEssenceBerriesEnabled && "tile.ore.berries.two".equals(block.getUnlocalizedName()) && meta == grownBlockMeta) {
      return false;
    }
    
    BlockCoord checkBlock = bc;

    meta = farm.getBlockMeta(checkBlock);
    block = checkBlock.getBlock(farm.getWorldObj());
    return super.canHarvest(farm, checkBlock, block, meta);
  }
}
@@ -717,20 +717,16 @@
 meta) %7B%0A
-    
 %0A    if(
@@ -891,28 +891,24 @@
 null;%0A    %7D%0A
-    
 %0A    IHarves
@@ -941,25 +941,17 @@
 esult();
-    %0A    
+%0A
 %0A    Blo
@@ -986,15 +986,18 @@
  for
+ 
 (int i
-=
+ = 
 0; i
@@ -1066,24 +1066,225 @@
 heckBlock);%0A
+      block = farm.getBlock(checkBlock);%0A%0A      if(super.canHarvest(farm, bc, block, meta)) %7B // redundant check because our canHarvest checks all 5 blocks so a bush may be invalid in the stack of 5%0A  
       IHarve
@@ -1354,21 +1354,19 @@
  meta);%0A
-  
+%0A
   
-%0A
       if
@@ -1389,24 +1389,19 @@
 l) %7B
-  
+%0A
      
-%0A
      
-   
 res.
@@ -1434,24 +1434,26 @@
 heckBlock);%0A
+  
         List
@@ -1499,16 +1499,18 @@
 rops();%0A
+  
         
@@ -1512,16 +1512,17 @@
      for
+ 
 (EntityI
@@ -1553,24 +1553,26 @@
 %7B%0A          
+  
 res.getDrops
@@ -1582,24 +1582,36 @@
 add(stack);%0A
+          %7D%0A
         %7D%0A  
@@ -1612,24 +1612,25 @@
   %7D%0A      %7D%0A
+%0A
       checkB
@@ -1681,28 +1681,24 @@
 .UP);%0A    %7D%0A
-    
 %0A    if(res.
@@ -1755,25 +1755,17 @@
 l;%0A    %7D
-    %0A    
+%0A
 %0A    ret
@@ -2037,20 +2037,16 @@
 ;%0A    %7D%0A
-    
 %0A    Blo
@@ -2071,16 +2071,52 @@
  = bc;%0A%0A
+    for (int i = 0; i %3C 5; i++) %7B%0A  
     meta
@@ -2145,24 +2145,26 @@
 heckBlock);%0A
+  
     block = 
@@ -2208,23 +2208,21 @@
 ));%0A    
-return 
+  if(
 super.ca
@@ -2260,15 +2260,133 @@
 k, meta)
+) %7B%0A        return true;%0A      %7D%0A      checkBlock = checkBlock.getLocation(ForgeDirection.UP);%0A    %7D%0A%0A    return false
 ;%0A  %7D%0A%7D

Fix wrong behavior of berry bush harvester