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