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

236 lines
6.8 KiB
Java
Raw Normal View History

/*
* Minecraft Forge
2019-02-10 17:57:03 -05:00
* Copyright (c) 2016-2019.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
2013-05-23 01:01:19 -04:00
package net.minecraftforge.fluids;
import net.minecraft.item.ItemStack;
2019-05-23 16:02:15 -07:00
import net.minecraft.nbt.CompoundNBT;
import net.minecraftforge.common.util.Constants;
2017-06-19 15:02:18 -07:00
import net.minecraftforge.registries.IRegistryDelegate;
2013-05-23 01:01:19 -04:00
import javax.annotation.Nullable;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
2013-05-23 01:01:19 -04:00
/**
* ItemStack substitute for Fluids.
*
2013-05-23 01:01:19 -04: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-05-23 01:01:19 -04:00
*/
public class FluidStack
{
private static final Logger LOGGER = LogManager.getLogger();
2013-05-23 01:01:19 -04:00
public int amount;
2019-05-23 16:02:15 -07:00
public CompoundNBT tag;
2017-06-19 15:02:18 -07:00
private IRegistryDelegate<Fluid> fluidDelegate;
2013-05-23 01:01:19 -04:00
public FluidStack(Fluid fluid, int amount)
{
if (fluid == null)
{
LOGGER.fatal("Null fluid supplied to fluidstack. Did you try and create a stack for an unregistered fluid?");
throw new IllegalArgumentException("Cannot create a fluidstack from a null fluid");
}/* TODO fluids
else if (!FluidRegistry.isFluidRegistered(fluid))
{
LOGGER.fatal("Failed attempt to create a FluidStack for an unregistered Fluid {} (type {})", fluid.getName(), fluid.getClass().getName());
throw new IllegalArgumentException("Cannot create a fluidstack from an unregistered fluid");
} */
this.fluidDelegate = null; // TODO fluids FluidRegistry.makeDelegate(fluid);
2013-05-23 01:01:19 -04:00
this.amount = amount;
}
2019-05-23 16:02:15 -07:00
public FluidStack(Fluid fluid, int amount, CompoundNBT nbt)
{
2015-03-25 01:27:15 -04:00
this(fluid, amount);
2013-05-23 01:01:19 -04:00
if (nbt != null)
{
2019-05-23 16:02:15 -07:00
tag = (CompoundNBT) nbt.copy();
2013-05-23 01:01:19 -04:00
}
}
public FluidStack(FluidStack stack, int amount)
{
this(stack.getFluid(), amount, stack.tag);
2015-03-25 01:27:15 -04:00
}
2013-05-23 01:01:19 -04:00
/**
* This provides a safe method for retrieving a FluidStack - if the Fluid is invalid, the stack
* will return as null.
*/
@Nullable
2019-05-23 16:02:15 -07:00
public static FluidStack loadFluidStackFromNBT(CompoundNBT nbt)
{
if (nbt == null)
{
return null;
}
2018-09-22 03:40:22 -07:00
if (!nbt.contains("FluidName", Constants.NBT.TAG_STRING))
{
return null;
}
String fluidName = nbt.getString("FluidName");
// TODO fluids if (FluidRegistry.getFluid(fluidName) == null)
{
2013-05-23 01:01:19 -04:00
return null;
}/*
2015-03-25 01:27:15 -04:00
FluidStack stack = new FluidStack(FluidRegistry.getFluid(fluidName), nbt.getInteger("Amount"));
2013-05-23 01:01:19 -04:00
if (nbt.hasKey("Tag"))
{
2013-05-23 01:01:19 -04:00
stack.tag = nbt.getCompoundTag("Tag");
}
return stack;*/
2013-05-23 01:01:19 -04:00
}
2019-05-23 16:02:15 -07:00
public CompoundNBT writeToNBT(CompoundNBT nbt)
{
// TODO fluids bt.setString("FluidName", FluidRegistry.getFluidName(getFluid()));
2019-05-23 16:02:15 -07:00
nbt.putInt("Amount", amount);
2013-05-23 01:01:19 -04:00
if (tag != null)
{
2019-05-23 16:02:15 -07:00
nbt.func_218657_a("Tag", tag);
2013-05-23 01:01:19 -04:00
}
return nbt;
}
public final Fluid getFluid()
{
return fluidDelegate.get();
2013-05-23 01:01:19 -04:00
}
public String getLocalizedName()
{
return this.getFluid().getLocalizedName(this);
}
public String getUnlocalizedName()
{
return this.getFluid().getUnlocalizedName(this);
}
2013-05-23 01:01:19 -04:00
/**
* @return A copy of this FluidStack
*/
public FluidStack copy()
{
return new FluidStack(getFluid(), amount, tag);
2013-05-23 01:01:19 -04:00
}
/**
* Determines if the FluidIDs and NBT Tags are equal. This does not check amounts.
*
2013-05-23 01:01:19 -04:00
* @param other
* The FluidStack for comparison
* @return true if the Fluids (IDs and NBT Tags) are the same
*/
public boolean isFluidEqual(@Nullable FluidStack other)
{
return other != null && getFluid() == other.getFluid() && isFluidStackTagEqual(other);
2013-05-23 01:01:19 -04:00
}
private boolean isFluidStackTagEqual(FluidStack other)
{
2013-05-23 01:01:19 -04: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.
*/
public static boolean areFluidStackTagsEqual(@Nullable FluidStack stack1, @Nullable FluidStack stack2)
{
2013-05-23 01:01:19 -04: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-05-23 01:01:19 -04:00
* @param other
* @return true if this FluidStack contains the other FluidStack (same fluid and >= amount)
*/
public boolean containsFluid(@Nullable FluidStack other)
{
2013-05-23 01:01:19 -04:00
return isFluidEqual(other) && amount >= other.amount;
}
/**
* Determines if the FluidIDs, Amounts, and NBT Tags are all equal.
*
2013-05-23 01:01:19 -04:00
* @param other
* - the FluidStack for comparison
* @return true if the two FluidStacks are exactly the same
*/
public boolean isFluidStackIdentical(FluidStack other)
{
2013-05-23 01:01:19 -04: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-05-23 01:01:19 -04:00
* @param other
* The ItemStack for comparison
* @return true if the Fluids (IDs and NBT Tags) are the same
*/
public boolean isFluidEqual(ItemStack other)
{
if (other == null)
{
2013-05-23 01:01:19 -04:00
return false;
}
return FluidUtil.getFluidContained(other).map(this::isFluidEqual).orElse(false);
2013-05-23 01:01:19 -04:00
}
@Override
public final int hashCode()
{
int code = 1;
code = 31*code + getFluid().hashCode();
code = 31*code + amount;
if (tag != null)
code = 31*code + tag.hashCode();
return code;
2013-05-23 01:01:19 -04:00
}
/**
* Default equality comparison for a FluidStack. Same functionality as isFluidEqual().
*
2013-05-23 01:01:19 -04:00
* This is included for use in data structures.
*/
2013-05-23 01:01:19 -04:00
@Override
public final boolean equals(Object o)
{
if (!(o instanceof FluidStack))
{
2013-05-23 01:01:19 -04:00
return false;
}
2013-05-23 01:01:19 -04:00
return isFluidEqual((FluidStack) o);
}
}