Files
ForestryMC/src/main/java/forestry/farming/logic/FarmLogicHomogeneous.java
T

124 lines
3.5 KiB
Java
Raw Normal View History

2014-05-02 21:31:20 +02:00
/*******************************************************************************
2014-08-07 08:29:25 +02:00
* 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
2014-08-07 02:56:58 -07:00
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
2014-08-07 08:29:25 +02:00
* Various Contributors including, but not limited to:
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
2014-05-02 21:31:20 +02:00
******************************************************************************/
package forestry.farming.logic;
import java.util.ArrayList;
2015-02-08 13:29:28 -08:00
import net.minecraft.block.Block;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
2014-05-02 21:31:20 +02:00
import forestry.api.farming.IFarmHousing;
import forestry.api.farming.IFarmable;
import forestry.core.utils.BlockUtil;
2014-05-02 21:31:20 +02:00
import forestry.core.utils.StackUtils;
import forestry.core.vect.Vect;
import forestry.core.vect.VectUtil;
2015-02-08 13:29:28 -08:00
import forestry.farming.gadgets.StructureLogicFarm;
public abstract class FarmLogicHomogeneous extends FarmLogic {
2014-05-02 21:31:20 +02:00
protected final ItemStack[] resource;
2015-02-08 13:29:28 -08:00
protected final ItemStack soilBlock;
2014-05-02 21:31:20 +02:00
protected final IFarmable[] germlings;
ArrayList<ItemStack> produce = new ArrayList<ItemStack>();
2015-02-08 13:29:28 -08:00
public FarmLogicHomogeneous(IFarmHousing housing, ItemStack[] resource, ItemStack soilBlock, IFarmable[] germlings) {
2014-05-02 21:31:20 +02:00
super(housing);
this.resource = resource;
2015-02-08 13:29:28 -08:00
this.soilBlock = soilBlock;
2014-05-02 21:31:20 +02:00
this.germlings = germlings;
}
2015-02-08 13:29:28 -08:00
public boolean isAcceptedSoil(ItemStack itemStack) {
return StackUtils.isIdenticalItem(soilBlock, itemStack);
}
2014-05-02 21:31:20 +02:00
@Override
public boolean isAcceptedResource(ItemStack itemstack) {
return resource[0].isItemEqual(itemstack);
}
@Override
public boolean isAcceptedGermling(ItemStack itemstack) {
for (IFarmable germling : germlings) {
if (germling.isGermling(itemstack)) {
2014-05-02 21:31:20 +02:00
return true;
}
}
2014-05-02 21:31:20 +02:00
return false;
}
public boolean isWindfall(ItemStack itemstack) {
for (IFarmable germling : germlings) {
if (germling.isWindfall(itemstack)) {
2014-05-02 21:31:20 +02:00
return true;
}
}
2014-05-02 21:31:20 +02:00
return false;
}
@Override
public boolean cultivate(int x, int y, int z, ForgeDirection direction, int extent) {
if (maintainSoil(x, y, z, direction, extent)) {
2014-05-02 21:31:20 +02:00
return true;
}
2014-05-02 21:31:20 +02:00
if (maintainGermlings(x, y + 1, z, direction, extent)) {
2014-05-02 21:31:20 +02:00
return true;
}
2014-05-02 21:31:20 +02:00
return false;
}
private boolean maintainSoil(int x, int yGround, int z, ForgeDirection direction, int extent) {
if (!housing.hasResources(resource)) {
2014-05-02 21:31:20 +02:00
return false;
}
2014-05-02 21:31:20 +02:00
World world = getWorld();
for (int i = 0; i < extent; i++) {
2014-05-02 21:31:20 +02:00
Vect position = translateWithOffset(x, yGround, z, direction, i);
2015-02-08 13:29:28 -08:00
Block soil = VectUtil.getBlock(world, position);
2014-05-02 21:31:20 +02:00
2015-02-08 13:29:28 -08:00
if (StructureLogicFarm.bricks.contains(soil)) {
break;
}
ItemStack soilStack = VectUtil.getAsItemStack(world, position);
if (isAcceptedSoil(soilStack) || !canBreakSoil(world, position)) {
continue;
}
2015-02-08 13:29:28 -08:00
Vect platformPosition = position.add(0, -1, 0);
Block platformBlock = VectUtil.getBlock(world, platformPosition);
if (!StructureLogicFarm.bricks.contains(platformBlock)) {
break;
}
produce.addAll(BlockUtil.getBlockDrops(world, position));
2015-02-08 13:29:28 -08:00
setBlock(position, StackUtils.getBlock(soilBlock), soilBlock.getItemDamage());
2014-05-02 21:31:20 +02:00
housing.removeResources(resource);
return true;
}
return false;
}
protected abstract boolean maintainGermlings(int x, int ySaplings, int z, ForgeDirection direction, int extent);
}