2013-05-23 05:01:19 +00:00
|
|
|
package net.minecraftforge.fluids;
|
|
|
|
|
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference implementation of {@link IFluidContainerItem}. Use/extend this or implement your own.
|
|
|
|
*
|
|
|
|
* @author King Lemming
|
|
|
|
*
|
|
|
|
*/
|
2013-07-17 04:40:49 +00:00
|
|
|
public class ItemFluidContainer extends Item implements IFluidContainerItem
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
protected int capacity;
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
public ItemFluidContainer(int itemID)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
super(itemID);
|
|
|
|
}
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
public ItemFluidContainer(int itemID, int capacity)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
super(itemID);
|
|
|
|
this.capacity = capacity;
|
|
|
|
}
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
public ItemFluidContainer setCapacity(int capacity)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
this.capacity = capacity;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* IFluidContainerItem */
|
|
|
|
@Override
|
2013-07-17 04:40:49 +00:00
|
|
|
public FluidStack getFluid(ItemStack container)
|
|
|
|
{
|
|
|
|
if (container.stackTagCompound == null || !container.stackTagCompound.hasKey("Fluid"))
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return FluidStack.loadFluidStackFromNBT(container.stackTagCompound.getCompoundTag("Fluid"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-07-17 04:40:49 +00:00
|
|
|
public int getCapacity(ItemStack container)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-07-17 04:40:49 +00:00
|
|
|
public int fill(ItemStack container, FluidStack resource, boolean doFill)
|
|
|
|
{
|
|
|
|
if (resource == null)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2013-07-17 04:40:49 +00:00
|
|
|
|
|
|
|
if (!doFill)
|
|
|
|
{
|
|
|
|
if (container.stackTagCompound == null || !container.stackTagCompound.hasKey("Fluid"))
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return Math.min(capacity, resource.amount);
|
|
|
|
}
|
2013-07-17 04:40:49 +00:00
|
|
|
|
2013-05-23 05:01:19 +00:00
|
|
|
FluidStack stack = FluidStack.loadFluidStackFromNBT(container.stackTagCompound.getCompoundTag("Fluid"));
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
if (stack == null)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return Math.min(capacity, resource.amount);
|
|
|
|
}
|
2013-07-17 04:40:49 +00:00
|
|
|
|
|
|
|
if (!stack.isFluidEqual(resource))
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2013-07-17 04:40:49 +00:00
|
|
|
|
2013-05-23 05:01:19 +00:00
|
|
|
return Math.min(capacity - stack.amount, resource.amount);
|
|
|
|
}
|
2013-07-17 04:40:49 +00:00
|
|
|
|
|
|
|
if (container.stackTagCompound == null)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
container.stackTagCompound = new NBTTagCompound();
|
|
|
|
}
|
2013-07-17 04:40:49 +00:00
|
|
|
|
|
|
|
if (!container.stackTagCompound.hasKey("Fluid"))
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
NBTTagCompound fluidTag = resource.writeToNBT(new NBTTagCompound());
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
if (capacity < resource.amount)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
fluidTag.setInteger("Amount", capacity);
|
|
|
|
container.stackTagCompound.setTag("Fluid", fluidTag);
|
|
|
|
return capacity;
|
|
|
|
}
|
2013-07-17 04:40:49 +00:00
|
|
|
|
2013-05-23 05:01:19 +00:00
|
|
|
container.stackTagCompound.setTag("Fluid", fluidTag);
|
|
|
|
return resource.amount;
|
|
|
|
}
|
2013-07-17 04:40:49 +00:00
|
|
|
|
2013-05-23 05:01:19 +00:00
|
|
|
NBTTagCompound fluidTag = container.stackTagCompound.getCompoundTag("Fluid");
|
|
|
|
FluidStack stack = FluidStack.loadFluidStackFromNBT(fluidTag);
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
if (!stack.isFluidEqual(resource))
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-26 17:21:31 +00:00
|
|
|
int filled = capacity - stack.amount;
|
2013-07-17 04:40:49 +00:00
|
|
|
if (resource.amount < filled)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
stack.amount += resource.amount;
|
|
|
|
filled = resource.amount;
|
2013-07-17 04:40:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
stack.amount = capacity;
|
|
|
|
}
|
2013-07-17 04:40:49 +00:00
|
|
|
|
2013-05-23 05:01:19 +00:00
|
|
|
container.stackTagCompound.setTag("Fluid", stack.writeToNBT(fluidTag));
|
|
|
|
return filled;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-07-17 04:40:49 +00:00
|
|
|
public FluidStack drain(ItemStack container, int maxDrain, boolean doDrain)
|
|
|
|
{
|
|
|
|
if (container.stackTagCompound == null || !container.stackTagCompound.hasKey("Fluid"))
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
FluidStack stack = FluidStack.loadFluidStackFromNBT(container.stackTagCompound.getCompoundTag("Fluid"));
|
|
|
|
if (stack == null)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
stack.amount = Math.min(stack.amount, maxDrain);
|
|
|
|
if (doDrain)
|
|
|
|
{
|
|
|
|
if (maxDrain >= capacity)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
container.stackTagCompound.removeTag("Fluid");
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
if (container.stackTagCompound.hasNoTags())
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
container.stackTagCompound = null;
|
|
|
|
}
|
|
|
|
return stack;
|
|
|
|
}
|
2013-07-17 04:40:49 +00:00
|
|
|
|
2013-05-23 05:01:19 +00:00
|
|
|
NBTTagCompound fluidTag = container.stackTagCompound.getCompoundTag("Fluid");
|
|
|
|
fluidTag.setInteger("Amount", fluidTag.getInteger("Amount") - maxDrain);
|
|
|
|
container.stackTagCompound.setTag("Fluid", fluidTag);
|
|
|
|
}
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
}
|