2013-05-23 05:01:19 +00:00
|
|
|
|
|
|
|
package net.minecraftforge.fluids;
|
|
|
|
|
2013-07-27 22:53:23 +00:00
|
|
|
import java.util.Locale;
|
|
|
|
|
2013-05-23 05:01:19 +00:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ItemStack substitute for Fluids.
|
2013-07-27 22:53:23 +00:00
|
|
|
*
|
2013-05-23 05:01:19 +00:00
|
|
|
* NOTE: Equality is based on the Fluid, not the amount. Use
|
|
|
|
* {@link #isFluidStackIdentical(FluidStack)} to determine if FluidID, Amount and NBT Tag are all
|
|
|
|
* equal.
|
2013-07-27 22:53:23 +00:00
|
|
|
*
|
2013-05-23 05:01:19 +00:00
|
|
|
* @author King Lemming, SirSengir (LiquidStack)
|
2013-07-27 22:53:23 +00:00
|
|
|
*
|
2013-05-23 05:01:19 +00:00
|
|
|
*/
|
2013-07-17 04:40:49 +00:00
|
|
|
public class FluidStack
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
public int fluidID;
|
|
|
|
public int amount;
|
|
|
|
public NBTTagCompound tag;
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
public FluidStack(Fluid fluid, int amount)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
this.fluidID = fluid.getID();
|
|
|
|
this.amount = amount;
|
|
|
|
}
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
public FluidStack(int fluidID, int amount)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
this.fluidID = fluidID;
|
|
|
|
this.amount = amount;
|
|
|
|
}
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
public FluidStack(int fluidID, int amount, NBTTagCompound nbt)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
this(fluidID, amount);
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
if (nbt != null)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
tag = (NBTTagCompound) nbt.copy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
public FluidStack(FluidStack stack, int amount)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
this(stack.fluidID, amount, stack.tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This provides a safe method for retrieving a FluidStack - if the Fluid is invalid, the stack
|
|
|
|
* will return as null.
|
|
|
|
*/
|
2013-07-17 04:40:49 +00:00
|
|
|
public static FluidStack loadFluidStackFromNBT(NBTTagCompound nbt)
|
|
|
|
{
|
2013-07-27 22:53:23 +00:00
|
|
|
if (nbt == null)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
String fluidName = nbt.getString("FluidName");
|
|
|
|
if (fluidName == null)
|
|
|
|
{
|
|
|
|
fluidName = nbt.hasKey("LiquidName") ? nbt.getString("LiquidName").toLowerCase(Locale.ENGLISH) : null;
|
|
|
|
}
|
|
|
|
if (fluidName ==null || FluidRegistry.getFluid(fluidName) == null)
|
2013-07-17 04:40:49 +00:00
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return null;
|
|
|
|
}
|
2013-07-27 22:53:23 +00:00
|
|
|
FluidStack stack = new FluidStack(FluidRegistry.getFluidID(fluidName), nbt.getInteger("Amount"));
|
2013-05-23 05:01:19 +00:00
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
if (nbt.hasKey("Tag"))
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
stack.tag = nbt.getCompoundTag("Tag");
|
|
|
|
}
|
2013-07-27 22:53:23 +00:00
|
|
|
else if (nbt.hasKey("extra"))
|
|
|
|
{
|
|
|
|
stack.tag = nbt.getCompoundTag("extra");
|
|
|
|
}
|
2013-05-23 05:01:19 +00:00
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
public NBTTagCompound writeToNBT(NBTTagCompound nbt)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
nbt.setString("FluidName", FluidRegistry.getFluidName(fluidID));
|
|
|
|
nbt.setInteger("Amount", amount);
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
if (tag != null)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
nbt.setTag("Tag", tag);
|
|
|
|
}
|
|
|
|
return nbt;
|
|
|
|
}
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
public final Fluid getFluid()
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return FluidRegistry.getFluid(fluidID);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return A copy of this FluidStack
|
|
|
|
*/
|
2013-07-17 04:40:49 +00:00
|
|
|
public FluidStack copy()
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return new FluidStack(fluidID, amount, tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if the FluidIDs and NBT Tags are equal. This does not check amounts.
|
2013-07-27 22:53:23 +00:00
|
|
|
*
|
2013-05-23 05:01:19 +00:00
|
|
|
* @param other
|
|
|
|
* The FluidStack for comparison
|
|
|
|
* @return true if the Fluids (IDs and NBT Tags) are the same
|
|
|
|
*/
|
2013-07-17 04:40:49 +00:00
|
|
|
public boolean isFluidEqual(FluidStack other)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return other != null && fluidID == other.fluidID && isFluidStackTagEqual(other);
|
|
|
|
}
|
|
|
|
|
2013-07-17 04:40:49 +00:00
|
|
|
private boolean isFluidStackTagEqual(FluidStack other)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return tag == null ? other.tag == null : other.tag == null ? false : tag.equals(other.tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if the NBT Tags are equal. Useful if the FluidIDs are known to be equal.
|
|
|
|
*/
|
2013-07-17 04:40:49 +00:00
|
|
|
public static boolean areFluidStackTagsEqual(FluidStack stack1, FluidStack stack2)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return stack1 == null && stack2 == null ? true : stack1 == null || stack2 == null ? false : stack1.isFluidStackTagEqual(stack2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if the Fluids are equal and this stack is larger.
|
2013-07-27 22:53:23 +00:00
|
|
|
*
|
2013-05-23 05:01:19 +00:00
|
|
|
* @param other
|
|
|
|
* @return true if this FluidStack contains the other FluidStack (same fluid and >= amount)
|
|
|
|
*/
|
2013-07-17 04:40:49 +00:00
|
|
|
public boolean containsFluid(FluidStack other)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return isFluidEqual(other) && amount >= other.amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if the FluidIDs, Amounts, and NBT Tags are all equal.
|
2013-07-27 22:53:23 +00:00
|
|
|
*
|
2013-05-23 05:01:19 +00:00
|
|
|
* @param other
|
|
|
|
* - the FluidStack for comparison
|
|
|
|
* @return true if the two FluidStacks are exactly the same
|
|
|
|
*/
|
2013-07-17 04:40:49 +00:00
|
|
|
public boolean isFluidStackIdentical(FluidStack other)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return isFluidEqual(other) && amount == other.amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if the FluidIDs and NBT Tags are equal compared to a registered container
|
|
|
|
* ItemStack. This does not check amounts.
|
2013-07-27 22:53:23 +00:00
|
|
|
*
|
2013-05-23 05:01:19 +00:00
|
|
|
* @param other
|
|
|
|
* The ItemStack for comparison
|
|
|
|
* @return true if the Fluids (IDs and NBT Tags) are the same
|
|
|
|
*/
|
2013-07-17 04:40:49 +00:00
|
|
|
public boolean isFluidEqual(ItemStack other)
|
|
|
|
{
|
|
|
|
if (other == null)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-07-17 04:40:49 +00:00
|
|
|
|
|
|
|
if (other.getItem() instanceof IFluidContainerItem)
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return isFluidEqual(((IFluidContainerItem) other.getItem()).getFluid(other));
|
|
|
|
}
|
2013-07-17 04:40:49 +00:00
|
|
|
|
2013-05-23 05:01:19 +00:00
|
|
|
return isFluidEqual(FluidContainerRegistry.getFluidForFilledItem(other));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2013-07-17 04:40:49 +00:00
|
|
|
public final int hashCode()
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return fluidID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default equality comparison for a FluidStack. Same functionality as isFluidEqual().
|
2013-07-27 22:53:23 +00:00
|
|
|
*
|
2013-05-23 05:01:19 +00:00
|
|
|
* This is included for use in data structures.
|
|
|
|
*/
|
|
|
|
@Override
|
2013-07-17 04:40:49 +00:00
|
|
|
public final boolean equals(Object o)
|
|
|
|
{
|
|
|
|
if (!(o instanceof FluidStack))
|
|
|
|
{
|
2013-05-23 05:01:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-07-17 04:40:49 +00:00
|
|
|
|
2013-05-23 05:01:19 +00:00
|
|
|
return isFluidEqual((FluidStack) o);
|
|
|
|
}
|
|
|
|
}
|