Files
MinecraftForge/src/main/java/net/minecraftforge/fluids/ItemFluidContainer.java
T

156 lines
4.2 KiB
Java
Raw Normal View History

2013-05-23 01:01:19 -04: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.
*/
public class ItemFluidContainer extends Item implements IFluidContainerItem
{
2013-05-23 01:01:19 -04:00
protected int capacity;
public ItemFluidContainer(int itemID)
{
super();
2013-05-23 01:01:19 -04:00
}
public ItemFluidContainer(int itemID, int capacity)
{
super();
2013-05-23 01:01:19 -04:00
this.capacity = capacity;
}
public ItemFluidContainer setCapacity(int capacity)
{
2013-05-23 01:01:19 -04:00
this.capacity = capacity;
return this;
}
/* IFluidContainerItem */
2013-05-23 01:01:19 -04:00
@Override
public FluidStack getFluid(ItemStack container)
{
if (!container.hasTagCompound() || !container.getTagCompound().hasKey("Fluid"))
{
2013-05-23 01:01:19 -04:00
return null;
}
return FluidStack.loadFluidStackFromNBT(container.getTagCompound().getCompoundTag("Fluid"));
2013-05-23 01:01:19 -04:00
}
@Override
public int getCapacity(ItemStack container)
{
2013-05-23 01:01:19 -04:00
return capacity;
}
@Override
public int fill(ItemStack container, FluidStack resource, boolean doFill)
{
if (resource == null)
{
2013-05-23 01:01:19 -04:00
return 0;
}
if (!doFill)
{
if (!container.hasTagCompound() || !container.getTagCompound().hasKey("Fluid"))
{
2013-05-23 01:01:19 -04:00
return Math.min(capacity, resource.amount);
}
FluidStack stack = FluidStack.loadFluidStackFromNBT(container.getTagCompound().getCompoundTag("Fluid"));
2013-05-23 01:01:19 -04:00
if (stack == null)
{
2013-05-23 01:01:19 -04:00
return Math.min(capacity, resource.amount);
}
if (!stack.isFluidEqual(resource))
{
2013-05-23 01:01:19 -04:00
return 0;
}
2013-05-23 01:01:19 -04:00
return Math.min(capacity - stack.amount, resource.amount);
}
if (!container.hasTagCompound())
{
container.setTagCompound(new NBTTagCompound());
2013-05-23 01:01:19 -04:00
}
if (!container.getTagCompound().hasKey("Fluid"))
{
2013-05-23 01:01:19 -04:00
NBTTagCompound fluidTag = resource.writeToNBT(new NBTTagCompound());
if (capacity < resource.amount)
{
2013-05-23 01:01:19 -04:00
fluidTag.setInteger("Amount", capacity);
container.getTagCompound().setTag("Fluid", fluidTag);
2013-05-23 01:01:19 -04:00
return capacity;
}
container.getTagCompound().setTag("Fluid", fluidTag);
2013-05-23 01:01:19 -04:00
return resource.amount;
}
NBTTagCompound fluidTag = container.getTagCompound().getCompoundTag("Fluid");
2013-05-23 01:01:19 -04:00
FluidStack stack = FluidStack.loadFluidStackFromNBT(fluidTag);
if (!stack.isFluidEqual(resource))
{
2013-05-23 01:01:19 -04:00
return 0;
}
2013-08-26 12:21:31 -05:00
int filled = capacity - stack.amount;
if (resource.amount < filled)
{
2013-05-23 01:01:19 -04:00
stack.amount += resource.amount;
filled = resource.amount;
}
else
{
2013-05-23 01:01:19 -04:00
stack.amount = capacity;
}
container.getTagCompound().setTag("Fluid", stack.writeToNBT(fluidTag));
2013-05-23 01:01:19 -04:00
return filled;
}
@Override
public FluidStack drain(ItemStack container, int maxDrain, boolean doDrain)
{
if (!container.hasTagCompound() || !container.getTagCompound().hasKey("Fluid"))
{
2013-05-23 01:01:19 -04:00
return null;
}
FluidStack stack = FluidStack.loadFluidStackFromNBT(container.getTagCompound().getCompoundTag("Fluid"));
if (stack == null)
{
2013-05-23 01:01:19 -04:00
return null;
}
int currentAmount = stack.amount;
stack.amount = Math.min(stack.amount, maxDrain);
if (doDrain)
{
if (currentAmount == stack.amount)
{
container.getTagCompound().removeTag("Fluid");
2013-05-23 01:01:19 -04:00
if (container.getTagCompound().hasNoTags())
{
container.setTagCompound(null);
2013-05-23 01:01:19 -04:00
}
return stack;
}
NBTTagCompound fluidTag = container.getTagCompound().getCompoundTag("Fluid");
fluidTag.setInteger("Amount", currentAmount - stack.amount);
container.getTagCompound().setTag("Fluid", fluidTag);
2013-05-23 01:01:19 -04:00
}
return stack;
}
}