diff --git a/src/main/java/forestry/farming/logic/FarmLogic.java b/src/main/java/forestry/farming/logic/FarmLogic.java index d6eafb11b..3136a8b52 100755 --- a/src/main/java/forestry/farming/logic/FarmLogic.java +++ b/src/main/java/forestry/farming/logic/FarmLogic.java @@ -130,4 +130,10 @@ public abstract class FarmLogic implements IFarmLogic { return farmLogic.isAcceptedGermling(contained) || farmLogic.isAcceptedWindfall(contained); } } + + // for debugging + @Override + public String toString() { + return getName(); + } } diff --git a/src/main/java/forestry/farming/logic/FarmLogicArboreal.java b/src/main/java/forestry/farming/logic/FarmLogicArboreal.java index 5d2fbd9ce..2d3db80bf 100755 --- a/src/main/java/forestry/farming/logic/FarmLogicArboreal.java +++ b/src/main/java/forestry/farming/logic/FarmLogicArboreal.java @@ -10,12 +10,10 @@ ******************************************************************************/ package forestry.farming.logic; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Set; import java.util.Stack; @@ -38,8 +36,6 @@ import forestry.api.farming.IFarmable; import forestry.core.PluginCore; public class FarmLogicArboreal extends FarmLogicHomogeneous { - private static final int BRANCH_RANGE = 20; - public FarmLogicArboreal(ItemStack resource, IBlockState ground, Collection germlings) { super(resource, ground, germlings); } @@ -91,83 +87,59 @@ public class FarmLogicArboreal extends FarmLogicHomogeneous { } BlockPos position = translateWithOffset(pos.up(), direction, lastExtent); - Collection crops = getHarvestBlocks(world, position); + Collection crops = harvestBlocks(world, position); lastExtent++; lastExtentsHarvest.put(pos, lastExtent); return crops; } - private Collection getHarvestBlocks(World world, BlockPos position) { + private Collection harvestBlocks(World world, BlockPos position) { + // Determine what type we want to harvest. + IFarmable farmable = getFarmableForBlock(world, position, farmables); + if (farmable == null) { + return Collections.emptyList(); + } + + // get all crops of the same type that are connected to the first one + Stack candidates = new Stack<>(); + candidates.add(position); + Set seen = new HashSet<>(); Stack crops = new Stack<>(); - IBlockState blockState = world.getBlockState(position); - // Determine what type we want to harvest. - IFarmable germling = null; - for (IFarmable germl : farmables) { - ICrop crop = germl.getCropAt(world, position, blockState); - if (crop != null) { - crops.push(crop); - seen.add(position); - germling = germl; - break; - } - } - - if (germling == null) { - return crops; - } - - List candidates = processHarvestBlock(world, germling, crops, seen, position, position); - List temp = new ArrayList<>(); - while (!candidates.isEmpty()) { - for (BlockPos candidate : candidates) { - temp.addAll(processHarvestBlock(world, germling, crops, seen, position, candidate)); - } - candidates.clear(); - candidates.addAll(temp); - temp.clear(); + while (!candidates.empty()) { + BlockPos candidate = candidates.pop(); + processHarvestBlock(world, candidate, farmable, crops, candidates, seen); } return crops; } - private static List processHarvestBlock(World world, IFarmable germling, Stack crops, Set seen, BlockPos start, BlockPos position) { - List candidates = new ArrayList<>(); + private static IFarmable getFarmableForBlock(World world, BlockPos position, Collection farmables) { + IBlockState blockState = world.getBlockState(position); + for (IFarmable farmable : farmables) { + ICrop crop = farmable.getCropAt(world, position, blockState); + if (crop != null) { + return farmable; + } + } + return null; + } - // Get additional candidates to return - for (int x = -1; x < 2; x++) { - for (int y = -1; y < 2; y++) { - for (int z = -1; z < 2; z++) { - BlockPos candidate = position.add(x, y, z); - if (candidate.equals(position)) { - continue; - } - if (Math.abs(candidate.getX() - start.getX()) > BRANCH_RANGE) { - continue; - } - if (Math.abs(candidate.getZ() - start.getZ()) > BRANCH_RANGE) { - continue; - } + private static void processHarvestBlock(World world, BlockPos position, IFarmable farmable, Stack crops, Stack candidates, Set seen) { + for (BlockPos candidate : BlockPos.getAllInBox(position.add(-1, -1, -1), position.add(1, 1, 1))) { + if (!seen.contains(candidate)) { + seen.add(candidate); - // See whether the given position has already been processed - if (seen.contains(candidate)) { - continue; - } - - IBlockState blockState = world.getBlockState(position); - ICrop crop = germling.getCropAt(world, candidate, blockState); - if (crop != null) { - crops.push(crop); - candidates.add(candidate); - seen.add(candidate); - } + IBlockState blockState = world.getBlockState(candidate); + ICrop crop = farmable.getCropAt(world, candidate, blockState); + if (crop != null) { + crops.push(crop); + candidates.push(candidate); } } } - - return candidates; } @Override diff --git a/src/main/java/forestry/farming/logic/FarmableHandPlanted.java b/src/main/java/forestry/farming/logic/FarmableHandPlanted.java deleted file mode 100644 index bb021ba4a..000000000 --- a/src/main/java/forestry/farming/logic/FarmableHandPlanted.java +++ /dev/null @@ -1,45 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2011-2014 SirSengir. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the GNU Lesser Public License v3 - * which accompanies this distribution, and is available at - * http://www.gnu.org/licenses/lgpl-3.0.txt - * - * Various Contributors including, but not limited to: - * SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges - ******************************************************************************/ -package forestry.farming.logic; - -import net.minecraft.block.state.IBlockState; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.item.ItemStack; -import net.minecraft.util.EnumActionResult; -import net.minecraft.util.EnumFacing; -import net.minecraft.util.EnumHand; -import net.minecraft.util.math.BlockPos; -import net.minecraft.world.World; - -public class FarmableHandPlanted extends FarmableBase { - private final ItemStack[] windfall; - - public FarmableHandPlanted(ItemStack seed, IBlockState plantedState, IBlockState matureState, boolean replant, ItemStack... windfall) { - super(seed, plantedState, matureState, replant); - this.windfall = windfall; - } - - @Override - public boolean plantSaplingAt(EntityPlayer player, ItemStack germling, World world, BlockPos pos) { - return germling.copy().onItemUse(player, world, pos.down(), EnumHand.MAIN_HAND, EnumFacing.UP, 0, 0, 0) == EnumActionResult.SUCCESS; - } - - @Override - public boolean isWindfall(ItemStack itemstack) { - for (ItemStack drop : windfall) { - if (drop.isItemEqual(itemstack)) { - return true; - } - } - return false; - } - -} diff --git a/src/main/java/forestry/farming/multiblock/FarmController.java b/src/main/java/forestry/farming/multiblock/FarmController.java index bdebb424a..b412be36c 100644 --- a/src/main/java/forestry/farming/multiblock/FarmController.java +++ b/src/main/java/forestry/farming/multiblock/FarmController.java @@ -10,9 +10,12 @@ ******************************************************************************/ package forestry.farming.multiblock; +import com.google.common.base.Objects; + import javax.annotation.Nonnull; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.EnumMap; @@ -473,8 +476,9 @@ public class FarmController extends RectangularMultiblockControllerBase implemen // Cultivation and collection FarmWorkStatus farmWorkStatus = new FarmWorkStatus(); - for (FarmDirection farmSide : FarmDirection.values()) { - + List farmDirections = Arrays.asList(FarmDirection.values()); + Collections.shuffle(farmDirections, worldObj.rand); + for (FarmDirection farmSide : farmDirections) { IFarmLogic logic = getFarmLogic(farmSide); if (logic == null) { continue; @@ -856,4 +860,10 @@ public class FarmController extends RectangularMultiblockControllerBase implemen public ICircuitSocketType getSocketType() { return CircuitSocketType.FARM; } + + // for debugging + @Override + public String toString() { + return Objects.toStringHelper(this).add("stage", stage).add("logic", farmLogics.toString()).toString(); + } }