package powercrystals.minefactoryreloaded.tile.machine;
import java.util.List;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.liquids.ILiquidTank;
import net.minecraftforge.liquids.LiquidContainerRegistry;
import net.minecraftforge.liquids.LiquidStack;
import net.minecraftforge.liquids.LiquidTank;
import powercrystals.minefactoryreloaded.MFRRegistry;
import powercrystals.minefactoryreloaded.api.IFactoryRanchable;
import powercrystals.minefactoryreloaded.core.HarvestAreaManager;
import powercrystals.minefactoryreloaded.core.IHarvestAreaContainer;
import powercrystals.minefactoryreloaded.core.ITankContainerBucketable;
import powercrystals.minefactoryreloaded.core.MFRLiquidMover;
import powercrystals.minefactoryreloaded.gui.client.GuiFactoryInventory;
import powercrystals.minefactoryreloaded.gui.client.GuiFactoryPowered;
import powercrystals.minefactoryreloaded.gui.container.ContainerFactoryPowered;
import powercrystals.minefactoryreloaded.setup.Machine;
import powercrystals.minefactoryreloaded.tile.base.TileEntityFactoryPowered;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class TileEntityRancher extends TileEntityFactoryPowered implements ITankContainerBucketable, IHarvestAreaContainer
{
private HarvestAreaManager _areaManager;
private LiquidTank _tank;
public TileEntityRancher()
{
super(Machine.Rancher);
_areaManager = new HarvestAreaManager(this, 2, 2, 1);
_tank = new LiquidTank(4 * LiquidContainerRegistry.BUCKET_VOLUME);
}
@Override
public String getGuiBackground()
{
return "rancher.png";
}
@Override
@SideOnly(Side.CLIENT)
public GuiFactoryInventory getGui(InventoryPlayer inventoryPlayer)
{
return new GuiFactoryPowered(getContainer(inventoryPlayer), this);
}
@Override
public ContainerFactoryPowered getContainer(InventoryPlayer inventoryPlayer)
{
return new ContainerFactoryPowered(this, inventoryPlayer);
}
@Override
protected boolean shouldPumpLiquid()
{
return true;
}
@Override
public ILiquidTank getTank()
{
return _tank;
}
@Override
public int getEnergyStoredMax()
{
return 32000;
}
@Override
public int getWorkMax()
{
return 1;
}
@Override
public int getIdleTicksMax()
{
return 200;
}
@Override
public HarvestAreaManager getHAM()
{
return _areaManager;
}
@Override
public boolean activateMachine()
{
MFRLiquidMover.pumpLiquid(_tank, this);
boolean didDrop = false;
List<?> entities = worldObj.getEntitiesWithinAABB(EntityLiving.class, _areaManager.getHarvestArea().toAxisAlignedBB());
for(Object o : entities)
{
EntityLiving e = (EntityLiving)o;
if(MFRRegistry.getRanchables().containsKey(e.getClass()))
{
IFactoryRanchable r = MFRRegistry.getRanchables().get(e.getClass());
List<ItemStack> drops = r.ranch(worldObj, e, this);
if(drops != null)
{
for(ItemStack s : drops)
{
if(LiquidContainerRegistry.isLiquid(s))
{
_tank.fill(new LiquidStack(s.itemID, LiquidContainerRegistry.BUCKET_VOLUME, s.getItemDamage()), true);
didDrop = true;
continue;
}
doDrop(s);
didDrop = true;
}
if(didDrop)
{
setIdleTicks(20);
return true;
}
}
}
}
setIdleTicks(getIdleTicksMax());
return false;
}
@Override
public int getSizeInventory()
{
return 9;
}
@Override
public int fill(ForgeDirection from, LiquidStack resource, boolean doFill)
{
return 0;
}
@Override
public int fill(int tankIndex, LiquidStack resource, boolean doFill)
{
return 0;
}
@Override
public boolean allowBucketDrain()
{
return true;
}
@Override
public LiquidStack drain(ForgeDirection from, int maxDrain, boolean doDrain)
{
return null;
}
@Override
public LiquidStack drain(int tankIndex, int maxDrain, boolean doDrain)
{
return null;
}
@Override
public ILiquidTank[] getTanks(ForgeDirection direction)
{
return new ILiquidTank[] { _tank };
}
@Override
public ILiquidTank getTank(ForgeDirection direction, LiquidStack type)
{
return _tank;
}
@Override
public boolean canRotate()
{
return true;
}
}