/*******************************************************************************
* 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.factory.gadgets;
import buildcraft.api.statements.ITriggerExternal;
import forestry.api.core.EnumHumidity;
import forestry.api.core.ForestryAPI;
import forestry.core.EnumErrorCode;
import forestry.core.config.Config;
import forestry.core.config.Defaults;
import forestry.core.fluids.TankManager;
import forestry.core.fluids.tanks.FilteredTank;
import forestry.core.gadgets.TileBase;
import forestry.core.interfaces.ILiquidTankContainer;
import forestry.core.network.GuiId;
import forestry.core.triggers.ForestryTrigger;
import forestry.core.utils.Fluids;
import forestry.core.utils.InventoryAdapter;
import forestry.core.utils.LiquidHelper;
import forestry.core.utils.StackUtils;
import forestry.core.utils.Utils;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ICrafting;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.biome.BiomeGenBase;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;
import java.util.LinkedList;
public class MachineRaintank extends TileBase implements ISidedInventory, ILiquidTankContainer {
/* CONSTANTS */
public static final short SLOT_RESOURCE = 0;
public static final short SLOT_PRODUCT = 1;
private static final FluidStack STACK_WATER = LiquidHelper.getLiquid(Defaults.LIQUID_WATER, Defaults.RAINTANK_AMOUNT_PER_UPDATE);
/* MEMBER */
public FilteredTank resourceTank;
private final TankManager tankManager;
private final InventoryAdapter inventory = new InventoryAdapter(3, "Items");
private boolean isValidBiome = true;
private int fillingTime;
private ItemStack usedEmpty;
public MachineRaintank() {
setHints(Config.hints.get("raintank"));
resourceTank = new FilteredTank(Defaults.RAINTANK_TANK_CAPACITY, FluidRegistry.WATER);
tankManager = new TankManager(resourceTank);
}
@Override
public void validate() {
// Raintanks in desert and snow biomes are useless
if (worldObj != null) {
BiomeGenBase biome = Utils.getBiomeAt(worldObj, xCoord, zCoord);
if (EnumHumidity.getFromValue(biome.rainfall) == EnumHumidity.ARID) {
setErrorState(EnumErrorCode.INVALIDBIOME);
isValidBiome = false;
}
}
super.validate();
}
@Override
public String getInventoryName() {
return getUnlocalizedName();
}
@Override
public void openGui(EntityPlayer player, TileBase tile) {
player.openGui(ForestryAPI.instance, GuiId.RaintankGUI.ordinal(), player.worldObj, xCoord, yCoord, zCoord);
}
@Override
public void writeToNBT(NBTTagCompound nbttagcompound) {
super.writeToNBT(nbttagcompound);
nbttagcompound.setBoolean("IsValidBiome", isValidBiome);
tankManager.writeTanksToNBT(nbttagcompound);
inventory.writeToNBT(nbttagcompound);
}
@Override
public void readFromNBT(NBTTagCompound nbttagcompound) {
super.readFromNBT(nbttagcompound);
isValidBiome = nbttagcompound.getBoolean("IsValidBiome");
tankManager.readTanksFromNBT(nbttagcompound);
inventory.readFromNBT(nbttagcompound);
}
@Override
public void updateServerSide() {
if (!isValidBiome)
setErrorState(EnumErrorCode.INVALIDBIOME);
else if (!worldObj.canBlockSeeTheSky(xCoord, yCoord + 1, zCoord))
setErrorState(EnumErrorCode.NOSKY);
else if (!worldObj.isRaining())
setErrorState(EnumErrorCode.NOTRAINING);
else {
resourceTank.fill(STACK_WATER, true);
setErrorState(EnumErrorCode.OK);
}
if (worldObj.getTotalWorldTime() % 16 != 0)
return;
if (!StackUtils.isIdenticalItem(usedEmpty, inventory.getStackInSlot(SLOT_RESOURCE))) {
fillingTime = 0;
usedEmpty = null;
}
if (usedEmpty == null)
usedEmpty = inventory.getStackInSlot(SLOT_RESOURCE);
if (!isFilling())
tryToStartFillling();
else {
fillingTime--;
if (fillingTime <= 0 && LiquidHelper.fillContainers(this, inventory, SLOT_RESOURCE, SLOT_PRODUCT, Fluids.WATER.get()))
fillingTime = 0;
}
}
public boolean isFilling() {
return fillingTime > 0;
}
private void tryToStartFillling() {
// Nothing to do if no empty cans are available
if (!LiquidHelper.fillContainers(this, inventory, SLOT_RESOURCE, SLOT_PRODUCT, Fluids.WATER.get(), false))
return;
fillingTime = Defaults.RAINTANK_FILLING_TIME;
}
public int getFillProgressScaled(int i) {
return (fillingTime * i) / Defaults.RAINTANK_FILLING_TIME;
}
/* IINVENTORY */
@Override
public int getSizeInventory() {
return inventory.getSizeInventory();
}
@Override
public ItemStack getStackInSlot(int i) {
return inventory.getStackInSlot(i);
}
@Override
public ItemStack decrStackSize(int i, int j) {
return inventory.decrStackSize(i, j);
}
@Override
public void setInventorySlotContents(int i, ItemStack itemstack) {
inventory.setInventorySlotContents(i, itemstack);
}
@Override
public ItemStack getStackInSlotOnClosing(int slot) {
return inventory.getStackInSlotOnClosing(slot);
}
@Override
public int getInventoryStackLimit() {
return inventory.getInventoryStackLimit();
}
@Override
public void openInventory() {
}
@Override
public void closeInventory() {
}
/**
* TODO: just a specialsource workaround
*/
@Override
public boolean isUseableByPlayer(EntityPlayer player) {
return super.isUseableByPlayer(player);
}
/**
* TODO: just a specialsource workaround
*/
@Override
public boolean hasCustomInventoryName() {
return super.hasCustomInventoryName();
}
/**
* TODO: just a specialsource workaround
*/
@Override
public boolean isItemValidForSlot(int slotIndex, ItemStack itemstack) {
return super.isItemValidForSlot(slotIndex, itemstack);
}
/**
* TODO: just a specialsource workaround
*/
@Override
public boolean canInsertItem(int i, ItemStack itemstack, int j) {
return super.canInsertItem(i, itemstack, j);
}
/**
* TODO: just a specialsource workaround
*/
@Override
public boolean canExtractItem(int i, ItemStack itemstack, int j) {
return super.canExtractItem(i, itemstack, j);
}
/**
* TODO: just a specialsource workaround
*/
@Override
public int[] getAccessibleSlotsFromSide(int side) {
return super.getAccessibleSlotsFromSide(side);
}
/* ISIDEDINVENTORY */
@Override
public InventoryAdapter getInternalInventory() {
return inventory;
}
@Override
protected boolean canTakeStackFromSide(int slotIndex, ItemStack itemstack, int side) {
if (!super.canTakeStackFromSide(slotIndex, itemstack, side))
return false;
return slotIndex == SLOT_PRODUCT;
}
@Override
protected boolean canPutStackFromSide(int slotIndex, ItemStack itemstack, int side) {
if (!super.canPutStackFromSide(slotIndex, itemstack, side))
return false;
if (slotIndex != SLOT_RESOURCE)
return false;
return LiquidHelper.getEmptyContainer(itemstack, STACK_WATER) != null;
}
/* SMP GUI */
public void getGUINetworkData(int i, int j) {
i -= tankManager.maxMessageId() + 1;
switch (i) {
case 0:
fillingTime = j;
break;
}
}
public void sendGUINetworkData(Container container, ICrafting iCrafting) {
int i = tankManager.maxMessageId() + 1;
iCrafting.sendProgressBarUpdate(container, i, fillingTime);
}
// / ILIQUIDCONTAINER IMPLEMENTATION
@Override
public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
return tankManager.fill(from, resource, doFill);
}
@Override
public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain) {
return tankManager.drain(from, resource, doDrain);
}
@Override
public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain) {
return tankManager.drain(from, maxDrain, doDrain);
}
@Override
public boolean canFill(ForgeDirection from, Fluid fluid) {
return tankManager.canFill(from, fluid);
}
@Override
public boolean canDrain(ForgeDirection from, Fluid fluid) {
return tankManager.canDrain(from, fluid);
}
@Override
public TankManager getTankManager() {
return tankManager;
}
public FluidTankInfo[] getTankInfo(ForgeDirection from) {
return tankManager.getTankInfo(from);
}
/* ITRIGGERPROVIDER */
@Override
public LinkedList<ITriggerExternal> getCustomTriggers() {
LinkedList<ITriggerExternal> res = new LinkedList<ITriggerExternal>();
res.add(ForestryTrigger.lowResource25);
res.add(ForestryTrigger.lowResource10);
return res;
}
}