diff --git a/src/main/java/forestry/farming/logic/CropBasicIC2Crop.java b/src/main/java/forestry/farming/logic/CropBasicIC2Crop.java new file mode 100644 index 000000000..ca0d5014e --- /dev/null +++ b/src/main/java/forestry/farming/logic/CropBasicIC2Crop.java @@ -0,0 +1,28 @@ +package forestry.farming.logic; + +import forestry.core.utils.vect.Vect; +import forestry.plugins.compat.PluginIC2; +import net.minecraft.item.ItemStack; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; + +import java.util.Collection; + +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 PluginIC2.instance.canHarvestCrop(this.tileEntity); + } + + @Override + protected Collection harvestBlock(Vect pos) { + return PluginIC2.instance.getCropDrops(this.tileEntity); + } +} diff --git a/src/main/java/forestry/farming/logic/FarmableBasicIC2Crop.java b/src/main/java/forestry/farming/logic/FarmableBasicIC2Crop.java new file mode 100644 index 000000000..7f867df9f --- /dev/null +++ b/src/main/java/forestry/farming/logic/FarmableBasicIC2Crop.java @@ -0,0 +1,52 @@ +package forestry.farming.logic; + +import forestry.api.farming.ICrop; +import forestry.api.farming.IFarmable; +import forestry.core.utils.vect.Vect; +import forestry.plugins.compat.PluginIC2; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; + +public class FarmableBasicIC2Crop implements IFarmable { + @Override + public boolean isSaplingAt(World world, int x, int y, int z) { + TileEntity crop = world.getTileEntity(x, y, z); + if(PluginIC2.instance.isIC2Crop(crop)) { + PluginIC2.instance.babysitCrop(crop); + return true; + } + return false; + } + + @Override + public ICrop getCropAt(World world, int x, int y, int z) { + TileEntity crop = world.getTileEntity(x, y, z); + if (crop == null) { + return null; + } + if (!PluginIC2.instance.isIC2Crop(crop)) { + return null; + } + if (!PluginIC2.instance.canHarvestCrop(crop)) { + return null; + } + return new CropBasicIC2Crop(world, crop, new Vect(x, y, z)); + } + + @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, int x, int y, int z) { + return false; + } +} diff --git a/src/main/java/forestry/plugins/compat/PluginIC2.java b/src/main/java/forestry/plugins/compat/PluginIC2.java index 2a0cc6351..a9f6f89da 100644 --- a/src/main/java/forestry/plugins/compat/PluginIC2.java +++ b/src/main/java/forestry/plugins/compat/PluginIC2.java @@ -12,11 +12,19 @@ package forestry.plugins.compat; import com.google.common.collect.ImmutableMap; +import java.util.ArrayList; +import java.util.Arrays; import java.util.EnumSet; +import cpw.mods.fml.common.registry.GameRegistry; +import forestry.api.farming.Farmables; +import forestry.farming.logic.FarmableBasicIC2Crop; +import ic2.api.crops.ICropTile; +import net.minecraft.block.Block; import net.minecraft.init.Blocks; import net.minecraft.item.ItemStack; +import net.minecraft.tileentity.TileEntity; import net.minecraftforge.fluids.FluidRegistry; import net.minecraftforge.fluids.FluidStack; @@ -300,6 +308,11 @@ public class PluginIC2 extends ForestryPlugin { ChipsetManager.solderManager.addRecipe(layoutManual, PluginCore.items.tubes.get(EnumElectronTube.RUBBER, 1), Circuit.farmRubberManual); } + Block ic2Crop = GameRegistry.findBlock("IC2", "blockCrop"); + if (PluginManager.Module.FARMING.isEnabled() && ic2Crop != null) { + Farmables.farmables.get("farmOrchard").add(new FarmableBasicIC2Crop()); + } + BlockRegistryEnergy energyBlocks = PluginEnergy.blocks; if (energyBlocks != null) { RecipeUtil.addRecipe(energyBlocks.engine.get(BlockEngineType.GENERATOR), @@ -320,4 +333,78 @@ public class PluginIC2 extends ForestryPlugin { 'V', Blocks.piston); } } + + /** + * 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 = "IC2") + public boolean isIC2Crop(TileEntity tileEntity) { + if (tileEntity != null && 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 = "IC2") + public boolean canHarvestCrop(TileEntity tileEntity) { + if (isIC2Crop(tileEntity)) { + ICropTile crop = (ICropTile)tileEntity; + if (crop.getCrop() == null) { + return false; + } + if (crop.getSize() == crop.getCrop().getOptimalHavestSize(crop)) { + return true; + } + } + return false; + } + + /** + * Perform some of the actions of the crop-matron. + * @param tileEntity + */ + @Optional.Method(modid = "IC2") + public void babysitCrop(TileEntity tileEntity) { + if (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.getHydrationStorage() <= 200) { + crop.setHydrationStorage(200); + } + if (crop.getNutrientStorage() <= 100) { + crop.setNutrientStorage(crop.getNutrientStorage() + 100); + } + } + } + + /** + * 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 arraylist containing the drops. + */ + @Optional.Method(modid = "IC2") + public ArrayList getCropDrops(TileEntity tileEntity) { + if (isIC2Crop(tileEntity)) { + ICropTile crop = (ICropTile)tileEntity; + ItemStack[] cropDrops = crop.harvest_automated(true); + if (cropDrops != null) { + return new ArrayList<>(Arrays.asList(cropDrops)); + } + } + return null; + } }