Added logic to allow fluid-accepting machines to use IFluidContainerItem fluids

This commit is contained in:
Aidan C. Brady 2014-07-10 16:24:55 -04:00
parent 95b6ac8be1
commit 1f9782d1c7
8 changed files with 292 additions and 138 deletions

View file

@ -787,7 +787,7 @@ public class ItemBlockMachine extends ItemBlock implements IEnergizedItem, ISpec
if(fluidStack == null || fluidStack.amount == 0 || fluidStack.fluidID == 0)
{
itemStack.stackTagCompound.setTag("fluidTank", null);
itemStack.stackTagCompound.removeTag("fluidTank");
}
else {
itemStack.stackTagCompound.setTag("fluidTank", fluidStack.writeToNBT(new NBTTagCompound()));

View file

@ -21,6 +21,7 @@ import mekanism.common.block.BlockMachine.MachineType;
import mekanism.common.network.PacketTileEntity.TileEntityMessage;
import mekanism.common.recipe.RecipeHandler;
import mekanism.common.util.ChargeUtils;
import mekanism.common.util.FluidContainerUtils;
import mekanism.common.util.InventoryUtils;
import mekanism.common.util.MekanismUtils;
import mekanism.common.util.PipeUtils;
@ -36,6 +37,7 @@ import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidContainerItem;
import net.minecraftforge.fluids.IFluidHandler;
public class TileEntityChemicalWasher extends TileEntityElectricBlock implements IActiveState, IGasHandler, ITubeConnection, IRedstoneControl, IHasSound, IFluidHandler
@ -169,52 +171,71 @@ public class TileEntityChemicalWasher extends TileEntityElectricBlock implements
private void manageBuckets()
{
if(FluidContainerRegistry.isFilledContainer(inventory[0]))
if(inventory[0] != null)
{
FluidStack itemFluid = FluidContainerRegistry.getFluidForFilledItem(inventory[0]);
if((fluidTank.getFluid() == null && itemFluid.amount <= MAX_FLUID) || fluidTank.getFluid().amount+itemFluid.amount <= MAX_FLUID)
if(inventory[0].getItem() instanceof IFluidContainerItem)
{
if(itemFluid.getFluid() != FluidRegistry.WATER || (fluidTank.getFluid() != null && !fluidTank.getFluid().isFluidEqual(itemFluid)))
fluidTank.fill(FluidContainerUtils.extractFluid(fluidTank, inventory[0], FluidRegistry.WATER), true);
if(((IFluidContainerItem)inventory[0].getItem()).getFluid(inventory[0]) == null || fluidTank.getFluidAmount() == fluidTank.getCapacity())
{
return;
}
ItemStack containerItem = inventory[0].getItem().getContainerItem(inventory[0]);
boolean filled = false;
if(containerItem != null)
{
if(inventory[1] == null || (inventory[1].isItemEqual(containerItem) && inventory[1].stackSize+1 <= containerItem.getMaxStackSize()))
if(inventory[1] == null)
{
inventory[1] = inventory[0].copy();
inventory[0] = null;
if(inventory[1] == null)
markDirty();
}
}
}
else if(FluidContainerRegistry.isFilledContainer(inventory[0]))
{
FluidStack itemFluid = FluidContainerRegistry.getFluidForFilledItem(inventory[0]);
if((fluidTank.getFluid() == null && itemFluid.amount <= MAX_FLUID) || fluidTank.getFluid().amount+itemFluid.amount <= MAX_FLUID)
{
if(itemFluid.getFluid() != FluidRegistry.WATER || (fluidTank.getFluid() != null && !fluidTank.getFluid().isFluidEqual(itemFluid)))
{
return;
}
ItemStack containerItem = inventory[0].getItem().getContainerItem(inventory[0]);
boolean filled = false;
if(containerItem != null)
{
if(inventory[1] == null || (inventory[1].isItemEqual(containerItem) && inventory[1].stackSize+1 <= containerItem.getMaxStackSize()))
{
inventory[1] = containerItem;
inventory[0] = null;
if(inventory[1] == null)
{
inventory[1] = containerItem;
}
else {
inventory[1].stackSize++;
}
filled = true;
}
else {
inventory[1].stackSize++;
}
else {
inventory[0].stackSize--;
if(inventory[0].stackSize == 0)
{
inventory[0] = null;
}
filled = true;
}
}
else {
inventory[0].stackSize--;
if(inventory[0].stackSize == 0)
if(filled)
{
inventory[0] = null;
fluidTank.fill(itemFluid, true);
markDirty();
}
filled = true;
}
if(filled)
{
fluidTank.fill(itemFluid, true);
}
}
}

View file

@ -17,6 +17,7 @@ import mekanism.common.ISustainedTank;
import mekanism.common.Mekanism;
import mekanism.common.network.PacketTileEntity.TileEntityMessage;
import mekanism.common.util.ChargeUtils;
import mekanism.common.util.FluidContainerUtils;
import mekanism.common.util.MekanismUtils;
import mekanism.common.util.PipeUtils;
import net.minecraft.entity.player.EntityPlayer;
@ -32,6 +33,7 @@ import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidContainerItem;
import net.minecraftforge.fluids.IFluidHandler;
public class TileEntityElectricPump extends TileEntityElectricBlock implements IFluidHandler, ISustainedTank, IConfigurable
@ -62,7 +64,24 @@ public class TileEntityElectricPump extends TileEntityElectricBlock implements I
{
if(fluidTank.getFluid() != null && fluidTank.getFluid().amount >= FluidContainerRegistry.BUCKET_VOLUME)
{
if(FluidContainerRegistry.isEmptyContainer(inventory[0]))
if(inventory[0].getItem() instanceof IFluidContainerItem)
{
int prev = fluidTank.getFluidAmount();
fluidTank.drain(FluidContainerUtils.insertFluid(fluidTank, inventory[0]), true);
if(prev == fluidTank.getFluidAmount() || fluidTank.getFluidAmount() == 0)
{
if(inventory[1] == null)
{
inventory[1] = inventory[0].copy();
inventory[0] = null;
markDirty();
}
}
}
else if(FluidContainerRegistry.isEmptyContainer(inventory[0]))
{
ItemStack tempStack = FluidContainerRegistry.fillFluidContainer(fluidTank.getFluid(), inventory[0]);

View file

@ -21,6 +21,7 @@ import mekanism.common.network.PacketTileEntity.TileEntityMessage;
import mekanism.common.recipe.RecipeHandler;
import mekanism.common.recipe.RecipeHandler.Recipe;
import mekanism.common.util.ChargeUtils;
import mekanism.common.util.FluidContainerUtils;
import mekanism.common.util.InventoryUtils;
import mekanism.common.util.MekanismUtils;
import net.minecraft.item.ItemStack;
@ -33,10 +34,10 @@ import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidContainerItem;
import net.minecraftforge.fluids.IFluidHandler;
import cpw.mods.fml.common.Optional.Interface;
import cpw.mods.fml.common.Optional.Method;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.peripheral.IPeripheral;
@ -86,23 +87,29 @@ public class TileEntityElectrolyticSeparator extends TileEntityElectricBlock imp
{
if(RecipeHandler.Recipe.ELECTROLYTIC_SEPARATOR.containsRecipe(inventory[0]))
{
FluidStack fluid = FluidContainerRegistry.getFluidForFilledItem(inventory[0]);
if(fluid != null && fluidTank.getFluid() == null || fluid.isFluidEqual(fluidTank.getFluid()) && fluidTank.getFluid().amount+fluid.amount <= fluidTank.getCapacity())
if(inventory[0].getItem() instanceof IFluidContainerItem)
{
fluidTank.fill(fluid, true);
if(inventory[0].getItem().hasContainerItem())
fluidTank.fill(FluidContainerUtils.extractFluid(fluidTank, inventory[0]), true);
}
else {
FluidStack fluid = FluidContainerRegistry.getFluidForFilledItem(inventory[0]);
if(fluid != null && fluidTank.getFluid() == null || fluid.isFluidEqual(fluidTank.getFluid()) && fluidTank.getFluid().amount+fluid.amount <= fluidTank.getCapacity())
{
inventory[0] = inventory[0].getItem().getContainerItem(inventory[0]);
}
else {
inventory[0].stackSize--;
}
if(inventory[0].stackSize == 0)
{
inventory[0] = null;
fluidTank.fill(fluid, true);
if(inventory[0].getItem().hasContainerItem(inventory[0]))
{
inventory[0] = inventory[0].getItem().getContainerItem(inventory[0]);
}
else {
inventory[0].stackSize--;
}
if(inventory[0].stackSize == 0)
{
inventory[0] = null;
}
}
}
}

View file

@ -14,6 +14,7 @@ import mekanism.common.ISustainedTank;
import mekanism.common.Mekanism;
import mekanism.common.block.BlockMachine.MachineType;
import mekanism.common.util.ChargeUtils;
import mekanism.common.util.FluidContainerUtils;
import mekanism.common.util.MekanismUtils;
import mekanism.common.util.PipeUtils;
import net.minecraft.entity.player.EntityPlayer;
@ -25,10 +26,10 @@ import net.minecraftforge.common.util.Constants.NBT;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidContainerItem;
import net.minecraftforge.fluids.IFluidHandler;
public class TileEntityFluidicPlenisher extends TileEntityElectricBlock implements IConfigurable, IFluidHandler, ISustainedTank
@ -56,52 +57,71 @@ public class TileEntityFluidicPlenisher extends TileEntityElectricBlock implemen
{
ChargeUtils.discharge(2, this);
if(FluidContainerRegistry.isFilledContainer(inventory[0]))
if(inventory[0] != null)
{
FluidStack itemFluid = FluidContainerRegistry.getFluidForFilledItem(inventory[0]);
if((fluidTank.getFluid() == null && itemFluid.amount <= fluidTank.getCapacity()) || fluidTank.getFluid().amount+itemFluid.amount <= fluidTank.getCapacity())
if(inventory[0].getItem() instanceof IFluidContainerItem)
{
if(fluidTank.getFluid() != null && !fluidTank.getFluid().isFluidEqual(itemFluid))
fluidTank.fill(FluidContainerUtils.extractFluid(fluidTank, inventory[0]), true);
if(((IFluidContainerItem)inventory[0].getItem()).getFluid(inventory[0]) == null || fluidTank.getFluidAmount() == fluidTank.getCapacity())
{
return;
}
ItemStack containerItem = inventory[0].getItem().getContainerItem(inventory[0]);
boolean filled = false;
if(containerItem != null)
{
if(inventory[1] == null || (inventory[1].isItemEqual(containerItem) && inventory[1].stackSize+1 <= containerItem.getMaxStackSize()))
if(inventory[1] == null)
{
inventory[1] = inventory[0].copy();
inventory[0] = null;
if(inventory[1] == null)
markDirty();
}
}
}
else if(FluidContainerRegistry.isFilledContainer(inventory[0]))
{
FluidStack itemFluid = FluidContainerRegistry.getFluidForFilledItem(inventory[0]);
if((fluidTank.getFluid() == null && itemFluid.amount <= fluidTank.getCapacity()) || fluidTank.getFluid().amount+itemFluid.amount <= fluidTank.getCapacity())
{
if(fluidTank.getFluid() != null && !fluidTank.getFluid().isFluidEqual(itemFluid))
{
return;
}
ItemStack containerItem = inventory[0].getItem().getContainerItem(inventory[0]);
boolean filled = false;
if(containerItem != null)
{
if(inventory[1] == null || (inventory[1].isItemEqual(containerItem) && inventory[1].stackSize+1 <= containerItem.getMaxStackSize()))
{
inventory[1] = containerItem;
inventory[0] = null;
if(inventory[1] == null)
{
inventory[1] = containerItem;
}
else {
inventory[1].stackSize++;
}
filled = true;
}
else {
inventory[1].stackSize++;
}
else {
inventory[0].stackSize--;
if(inventory[0].stackSize == 0)
{
inventory[0] = null;
}
filled = true;
}
}
else {
inventory[0].stackSize--;
if(inventory[0].stackSize == 0)
if(filled)
{
inventory[0] = null;
fluidTank.fill(itemFluid, true);
markDirty();
}
filled = true;
}
if(filled)
{
fluidTank.fill(itemFluid, true);
}
}
}

View file

@ -19,6 +19,7 @@ import mekanism.common.Mekanism;
import mekanism.common.block.BlockMachine.MachineType;
import mekanism.common.network.PacketTileEntity.TileEntityMessage;
import mekanism.common.util.ChargeUtils;
import mekanism.common.util.FluidContainerUtils;
import mekanism.common.util.MekanismUtils;
import mekanism.common.util.PipeUtils;
import net.minecraft.entity.player.EntityPlayer;
@ -32,6 +33,7 @@ import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidContainerItem;
import net.minecraftforge.fluids.IFluidHandler;
public class TileEntityRotaryCondensentrator extends TileEntityElectricBlock implements IActiveState, ISustainedTank, IFluidHandler, IGasHandler, ITubeConnection, IRedstoneControl
@ -106,7 +108,24 @@ public class TileEntityRotaryCondensentrator extends TileEntityElectricBlock imp
if(inventory[2] != null)
{
if(FluidContainerRegistry.isEmptyContainer(inventory[2]))
if(inventory[2].getItem() instanceof IFluidContainerItem)
{
int prev = fluidTank.getFluidAmount();
fluidTank.drain(FluidContainerUtils.insertFluid(fluidTank, inventory[2]), true);
if(prev == fluidTank.getFluidAmount() || fluidTank.getFluidAmount() == 0)
{
if(inventory[3] == null)
{
inventory[3] = inventory[2].copy();
inventory[2] = null;
markDirty();
}
}
}
else if(FluidContainerRegistry.isEmptyContainer(inventory[2]))
{
if(fluidTank.getFluid() != null && fluidTank.getFluid().amount >= FluidContainerRegistry.BUCKET_VOLUME)
{
@ -130,6 +149,8 @@ public class TileEntityRotaryCondensentrator extends TileEntityElectricBlock imp
else {
inventory[3].stackSize++;
}
markDirty();
fluidTank.drain(FluidContainerRegistry.getFluidForFilledItem(filled).amount, true);
}
@ -174,52 +195,71 @@ public class TileEntityRotaryCondensentrator extends TileEntityElectricBlock imp
}
}
if(FluidContainerRegistry.isFilledContainer(inventory[2]))
if(inventory[2] != null)
{
FluidStack itemFluid = FluidContainerRegistry.getFluidForFilledItem(inventory[2]);
if((fluidTank.getFluid() == null && itemFluid.amount <= MAX_FLUID) || fluidTank.getFluid().amount+itemFluid.amount <= MAX_FLUID)
if(inventory[2].getItem() instanceof IFluidContainerItem)
{
if(fluidTank.getFluid() != null && !fluidTank.getFluid().isFluidEqual(itemFluid))
fluidTank.fill(FluidContainerUtils.extractFluid(fluidTank, inventory[2]), true);
if(((IFluidContainerItem)inventory[2].getItem()).getFluid(inventory[2]) == null || fluidTank.getFluidAmount() == fluidTank.getCapacity())
{
return;
}
ItemStack containerItem = inventory[2].getItem().getContainerItem(inventory[2]);
boolean filled = false;
if(containerItem != null)
{
if(inventory[3] == null || (inventory[3].isItemEqual(containerItem) && inventory[3].stackSize+1 <= containerItem.getMaxStackSize()))
if(inventory[3] == null)
{
inventory[3] = inventory[2].copy();
inventory[2] = null;
if(inventory[3] == null)
markDirty();
}
}
}
else if(FluidContainerRegistry.isFilledContainer(inventory[2]))
{
FluidStack itemFluid = FluidContainerRegistry.getFluidForFilledItem(inventory[2]);
if((fluidTank.getFluid() == null && itemFluid.amount <= MAX_FLUID) || fluidTank.getFluid().amount+itemFluid.amount <= MAX_FLUID)
{
if(fluidTank.getFluid() != null && !fluidTank.getFluid().isFluidEqual(itemFluid))
{
return;
}
ItemStack containerItem = inventory[2].getItem().getContainerItem(inventory[2]);
boolean filled = false;
if(containerItem != null)
{
if(inventory[3] == null || (inventory[3].isItemEqual(containerItem) && inventory[3].stackSize+1 <= containerItem.getMaxStackSize()))
{
inventory[3] = containerItem;
inventory[2] = null;
if(inventory[3] == null)
{
inventory[3] = containerItem;
}
else {
inventory[3].stackSize++;
}
filled = true;
}
else {
inventory[3].stackSize++;
}
else {
inventory[2].stackSize--;
if(inventory[2].stackSize == 0)
{
inventory[2] = null;
}
filled = true;
}
}
else {
inventory[2].stackSize--;
if(inventory[2].stackSize == 0)
if(filled)
{
inventory[2] = null;
fluidTank.fill(itemFluid, true);
markDirty();
}
filled = true;
}
if(filled)
{
fluidTank.fill(itemFluid, true);
}
}
}

View file

@ -0,0 +1,39 @@
package mekanism.common.util;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.IFluidContainerItem;
public final class FluidContainerUtils
{
public static FluidStack extractFluid(FluidTank tileTank, ItemStack container)
{
return extractFluid(tileTank, container, null);
}
public static FluidStack extractFluid(FluidTank tileTank, ItemStack container, Fluid fluid)
{
IFluidContainerItem item = (IFluidContainerItem)container.getItem();
if(fluid != null && item.getFluid(container) != null && item.getFluid(container).getFluid() != fluid)
{
return null;
}
return item.drain(container, tileTank.getCapacity()-tileTank.getFluidAmount(), true);
}
public static int insertFluid(FluidTank tileTank, ItemStack container)
{
IFluidContainerItem item = (IFluidContainerItem)container.getItem();
if(tileTank.getFluid() == null)
{
return 0;
}
return item.fill(container, tileTank.getFluid(), true);
}
}

View file

@ -6,6 +6,7 @@ import java.util.ArrayList;
import mekanism.common.Mekanism;
import mekanism.common.util.ChargeUtils;
import mekanism.common.util.FluidContainerUtils;
import mekanism.common.util.MekanismUtils;
import mekanism.generators.common.MekanismGenerators;
import net.minecraft.init.Blocks;
@ -20,9 +21,9 @@ import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidContainerItem;
import net.minecraftforge.fluids.IFluidHandler;
import cpw.mods.fml.common.Optional.Method;
import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.peripheral.IComputerAccess;
@ -49,24 +50,31 @@ public class TileEntityHeatGenerator extends TileEntityGenerator implements IFlu
if(inventory[0] != null)
{
FluidStack fluid = FluidContainerRegistry.getFluidForFilledItem(inventory[0]);
if(fluid != null && fluid.fluidID == FluidRegistry.LAVA.getID())
if(inventory[0].getItem() instanceof IFluidContainerItem)
{
if(lavaTank.getFluid() == null || lavaTank.getFluid().amount+fluid.amount <= lavaTank.getCapacity())
lavaTank.fill(FluidContainerUtils.extractFluid(lavaTank, inventory[0], FluidRegistry.LAVA), true);
}
else if(fluid != null)
{
if(fluid != null && fluid.fluidID == FluidRegistry.LAVA.getID())
{
lavaTank.fill(fluid, true);
if(inventory[0].getItem().getContainerItem(inventory[0]) != null)
if(lavaTank.getFluid() == null || lavaTank.getFluid().amount+fluid.amount <= lavaTank.getCapacity())
{
inventory[0] = inventory[0].getItem().getContainerItem(inventory[0]);
}
else {
inventory[0].stackSize--;
}
if(inventory[0].stackSize == 0)
{
inventory[0] = null;
lavaTank.fill(fluid, true);
if(inventory[0].getItem().getContainerItem(inventory[0]) != null)
{
inventory[0] = inventory[0].getItem().getContainerItem(inventory[0]);
}
else {
inventory[0].stackSize--;
}
if(inventory[0].stackSize == 0)
{
inventory[0] = null;
}
}
}
}