Close #1144 add IC2 compatibility back

This commit is contained in:
mezz committed 2016-04-28 19:52:12 -07:00
1 parent 04173d6bcf
commit 08d543b2ef
24 files changed
+1694 -7

No files matched your search

@@ -0,0 +1,81 @@
package forestry.farming.logic;
import java.util.Collection;
import java.util.List;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.Optional;
import forestry.core.utils.vect.Vect;
import forestry.plugins.compat.PluginIC2;
import ic2.api.crops.ICropTile;
public class CropBasicIC2Crop extends Crop {
private final TileEntity tileEntity;
public CropBasicIC2Crop(World world, TileEntity tileEntity, Vect position) {
super(world, position);
this.tileEntity = tileEntity;
}
@Override
protected boolean isCrop(Vect pos) {
return canHarvestCrop(this.tileEntity);
}
@Override
protected Collection<ItemStack> harvestBlock(Vect pos) {
return getCropDrops(this.tileEntity);
}
/**
* Check if there is an instance of ICropTile.
* @param tileEntity tile entity to be checked.
* @return true if there is an IC2 crop and false otherwise.
*/
@Optional.Method(modid = PluginIC2.modId)
public static boolean isIC2Crop(TileEntity tileEntity) {
if (tileEntity instanceof ICropTile) {
return true;
}
return false;
}
/**
* Check if an IC2 crop is ready to be harvested.
* @param tileEntity tile entity to be checked.
* @return true if crop size is optimal for harvest and false otherwise.
*/
@Optional.Method(modid = PluginIC2.modId)
public static boolean canHarvestCrop(TileEntity tileEntity) {
if (isIC2Crop(tileEntity)) {
ICropTile crop = (ICropTile) tileEntity;
if (crop.getCrop() == null) {
return false;
}
if (crop.getCurrentSize() == crop.getCrop().getOptimalHarvestSize(crop)) {
return true;
}
}
return false;
}
/**
* This function takes care of everything related to the harvesting of the
* crop meaning it will calculate the drops and also do setSizeAfterHarvest().
* @param tileEntity tile entity to be checked.
* @return list containing the drops.
*/
@Optional.Method(modid = PluginIC2.modId)
private static List<ItemStack> getCropDrops(TileEntity tileEntity) {
if (isIC2Crop(tileEntity)) {
ICropTile crop = (ICropTile) tileEntity;
return crop.performHarvest();
}
return null;
}
}
+40
View File
@@ -0,0 +1,40 @@
/*******************************************************************************
* 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 net.minecraft.block.Block;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import forestry.core.config.Constants;
import forestry.core.proxy.Proxies;
import forestry.core.utils.vect.Vect;
import forestry.plugins.compat.PluginIC2;
public class CropRubber extends CropBlock {
public CropRubber(World world, Block block, int meta, Vect position) {
super(world, block, meta, position);
}
@Override
protected Collection<ItemStack> harvestBlock(Vect pos) {
Collection<ItemStack> harvested = new ArrayList<>();
harvested.add(PluginIC2.resin.copy());
Proxies.common.addBlockDestroyEffects(world, pos, block.getStateFromMeta(0));
world.setBlockState(pos, block.getStateFromMeta(meta + 6), Constants.FLAG_BLOCK_SYNCH);
return harvested;
}
}
+190
View File
@@ -0,0 +1,190 @@
/*******************************************************************************
* 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.Set;
import java.util.Stack;
import net.minecraft.block.Block;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
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.core.utils.vect.Vect;
import forestry.core.utils.vect.VectUtil;
import forestry.plugins.compat.PluginIC2;
public class FarmLogicRubber extends FarmLogic {
private boolean inActive;
public FarmLogicRubber(IFarmHousing housing) {
super(housing);
if (PluginIC2.rubberWood == null || PluginIC2.resin == null) {
Log.warning("Failed to init a farm logic %s since IC2 was not found", getClass().getName());
inActive = true;
}
}
@Override
@SideOnly(Side.CLIENT)
public Item getItem() {
if (!inActive) {
return PluginIC2.resin.getItem();
} else {
return Items.gunpowder;
}
}
@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<ItemStack> collect() {
return null;
}
@Override
public boolean cultivate(BlockPos pos, FarmDirection direction, int extent) {
return false;
}
private final HashMap<Vect, Integer> lastExtents = new HashMap<>();
@Override
public Collection<ICrop> harvest(BlockPos pos, FarmDirection direction, int extent) {
if (inActive) {
return null;
}
Vect start = new Vect(pos);
if (!lastExtents.containsKey(start)) {
lastExtents.put(start, 0);
}
int lastExtent = lastExtents.get(start);
if (lastExtent > extent) {
lastExtent = 0;
}
Vect position = translateWithOffset(pos.add(0, 1, 0), direction, lastExtent);
Collection<ICrop> crops = getHarvestBlocks(position);
lastExtent++;
lastExtents.put(start, lastExtent);
return crops;
}
private Collection<ICrop> getHarvestBlocks(Vect position) {
Set<Vect> seen = new HashSet<>();
Stack<ICrop> crops = new Stack<>();
World world = getWorld();
// Determine what type we want to harvest.
Block block = VectUtil.getBlock(world, position);
if (!ItemStackUtil.equals(block, PluginIC2.rubberWood)) {
return crops;
}
int meta = VectUtil.getBlockMeta(world, position);
if (meta >= 2 && meta <= 5) {
crops.push(new CropRubber(getWorld(), block, meta, position));
}
ArrayList<Vect> candidates = processHarvestBlock(crops, seen, position);
ArrayList<Vect> temp = new ArrayList<>();
while (!candidates.isEmpty() && crops.size() < 100) {
for (Vect candidate : candidates) {
temp.addAll(processHarvestBlock(crops, seen, candidate));
}
candidates.clear();
candidates.addAll(temp);
temp.clear();
}
return crops;
}
private ArrayList<Vect> processHarvestBlock(Stack<ICrop> crops, Set<Vect> seen, Vect position) {
World world = getWorld();
ArrayList<Vect> candidates = new ArrayList<>();
// Get additional candidates to return
for (int j = 0; j < 2; j++) {
Vect candidate = new Vect(position.getX(), position.getY() + j, position.getZ());
if (candidate.equals(position)) {
continue;
}
// See whether the given position has already been processed
if (seen.contains(candidate)) {
continue;
}
Block block = VectUtil.getBlock(world, candidate);
if (ItemStackUtil.equals(block, PluginIC2.rubberWood)) {
int meta = VectUtil.getBlockMeta(world, candidate);
if (meta >= 2 && meta <= 5) {
crops.push(new CropRubber(world, block, meta, candidate));
}
candidates.add(candidate);
seen.add(candidate);
}
}
return candidates;
}
}
@@ -0,0 +1,82 @@
package forestry.farming.logic;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.Optional;
import forestry.api.farming.ICrop;
import forestry.api.farming.IFarmable;
import forestry.core.utils.vect.Vect;
import forestry.plugins.compat.PluginIC2;
import ic2.api.crops.ICropTile;
public class FarmableBasicIC2Crop implements IFarmable {
/**
* Perform some of the actions of the crop-matron.
*/
@Optional.Method(modid = PluginIC2.modId)
public static void babysitCrop(TileEntity tileEntity) {
if (CropBasicIC2Crop.isIC2Crop(tileEntity)) {
ICropTile crop = (ICropTile) tileEntity;
/*
This part might be unbalanced until a custom farm logic is added and makes use of weed-ex.
if (crop.getCrop() != null) {
if (crop.getCrop().isWeed(crop)) {
crop.reset();
}
}*/
if (crop.getStorageWater() <= 200) {
crop.setStorageWater(200);
}
if (crop.getStorageNutrient() <= 100) {
crop.setStorageNutrient(crop.getStorageNutrient() + 100);
}
}
}
@Override
public boolean isSaplingAt(World world, BlockPos pos) {
TileEntity crop = world.getTileEntity(pos);
if (CropBasicIC2Crop.isIC2Crop(crop)) {
babysitCrop(crop);
return true;
}
return false;
}
@Override
public ICrop getCropAt(World world, BlockPos pos) {
TileEntity crop = world.getTileEntity(pos);
if (crop == null) {
return null;
}
if (!CropBasicIC2Crop.isIC2Crop(crop)) {
return null;
}
if (!CropBasicIC2Crop.canHarvestCrop(crop)) {
return null;
}
return new CropBasicIC2Crop(world, crop, new Vect(pos));
}
@Override
public boolean isGermling(ItemStack itemstack) {
return false;
}
@Override
public boolean isWindfall(ItemStack itemstack) {
return false;
}
@Override
public boolean plantSaplingAt(EntityPlayer player, ItemStack germling, World world, BlockPos pos) {
return false;
}
}