/******************************************************************************* * 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 java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.Stack; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; import net.minecraft.item.ItemStack; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import forestry.api.farming.FarmDirection; import forestry.api.farming.ICrop; import forestry.api.farming.IFarmHousing; import forestry.core.utils.ItemStackUtil; import forestry.core.utils.Log; import forestry.plugins.compat.PluginIC2; public class FarmLogicRubber extends FarmLogic { private boolean active = true; public FarmLogicRubber() { if (PluginIC2.rubberWood == null || PluginIC2.resin == null) { Log.warning("Failed to init a farm logic %s since IC2 was not found", getClass().getName()); active = false; } } @Override public ItemStack getIconItemStack() { return PluginIC2.resin; } @Override public String getName() { return "Rubber Plantation"; } @Override public int getFertilizerConsumption() { return 40; } @Override public int getWaterConsumption(float hydrationModifier) { return (int) (5 * hydrationModifier); } @Override public boolean isAcceptedResource(ItemStack itemstack) { return false; } @Override public boolean isAcceptedGermling(ItemStack itemstack) { return false; } @Override public boolean isAcceptedWindfall(ItemStack stack) { return false; } @Override public Collection collect(World world, IFarmHousing farmHousing) { return null; } @Override public boolean cultivate(World world, IFarmHousing farmHousing, BlockPos pos, FarmDirection direction, int extent) { return false; } private final Map lastExtents = new HashMap<>(); @Override public Collection harvest(World world, BlockPos pos, FarmDirection direction, int extent) { if (!active) { return null; } if (!lastExtents.containsKey(pos)) { lastExtents.put(pos, 0); } int lastExtent = lastExtents.get(pos); if (lastExtent > extent) { lastExtent = 0; } BlockPos position = translateWithOffset(pos.up(), direction, lastExtent); Collection crops = getHarvestBlocks(world, position); lastExtent++; lastExtents.put(pos, lastExtent); return crops; } private Collection getHarvestBlocks(World world, BlockPos position) { Set seen = new HashSet<>(); Stack crops = new Stack<>(); // Determine what type we want to harvest. IBlockState blockState = world.getBlockState(position); Block block = blockState.getBlock(); if (!ItemStackUtil.equals(block, PluginIC2.rubberWood)) { return crops; } int meta = block.getMetaFromState(blockState); if (meta >= 7 && meta <= 10) { crops.push(new CropDestroy(world, blockState, position, null)); } List candidates = processHarvestBlock(world, crops, seen, position); List temp = new ArrayList<>(); while (!candidates.isEmpty() && crops.size() < 100) { for (BlockPos candidate : candidates) { temp.addAll(processHarvestBlock(world, crops, seen, candidate)); } candidates.clear(); candidates.addAll(temp); temp.clear(); } return crops; } private List processHarvestBlock(World world, Stack crops, Set seen, BlockPos position) { List candidates = new ArrayList<>(); // Get additional candidates to return for (int j = 0; j < 2; j++) { BlockPos candidate = position.add(0, j, 0); if (candidate.equals(position)) { continue; } // See whether the given position has already been processed if (seen.contains(candidate)) { continue; } IBlockState blockState = world.getBlockState(candidate); Block block = blockState.getBlock(); if (ItemStackUtil.equals(block, PluginIC2.rubberWood)) { int meta = block.getMetaFromState(blockState); if (meta >= 7 && meta <= 10) { crops.push(new CropRubber(world, blockState, candidate)); } candidates.add(candidate); seen.add(candidate); } } return candidates; } }