2016-06-22 23:49:47 -04:00
|
|
|
/*
|
|
|
|
|
* Minecraft Forge
|
2019-02-10 17:57:03 -05:00
|
|
|
* Copyright (c) 2016-2019.
|
2016-06-22 23:49:47 -04:00
|
|
|
*
|
|
|
|
|
* 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;
|
|
|
|
|
|
2019-07-28 21:35:30 +02:00
|
|
|
import net.minecraft.fluid.Fluid;
|
2019-08-29 03:01:11 +02:00
|
|
|
import net.minecraft.fluid.Fluids;
|
2013-05-23 01:01:19 -04:00
|
|
|
import net.minecraft.item.ItemStack;
|
2019-05-23 16:02:15 -07:00
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
2019-08-29 03:01:11 +02:00
|
|
|
import net.minecraft.network.PacketBuffer;
|
|
|
|
|
import net.minecraft.util.ResourceLocation;
|
2019-07-28 21:35:30 +02:00
|
|
|
import net.minecraft.util.text.ITextComponent;
|
2017-01-11 15:17:56 -08:00
|
|
|
import net.minecraftforge.common.util.Constants;
|
2019-08-29 03:01:11 +02:00
|
|
|
import net.minecraftforge.registries.ForgeRegistries;
|
2017-06-19 15:02:18 -07:00
|
|
|
import net.minecraftforge.registries.IRegistryDelegate;
|
2013-05-23 01:01:19 -04:00
|
|
|
|
2019-08-29 03:01:11 +02:00
|
|
|
import javax.annotation.Nonnull;
|
2017-01-11 15:17:56 -08:00
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
|
2018-09-18 00:04:02 -04:00
|
|
|
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-07-28 00:53:23 +02:00
|
|
|
*
|
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-07-28 00:53:23 +02:00
|
|
|
*
|
2013-05-23 01:01:19 -04:00
|
|
|
*/
|
2013-07-16 21:40:49 -07:00
|
|
|
public class FluidStack
|
|
|
|
|
{
|
2018-09-18 00:04:02 -04:00
|
|
|
private static final Logger LOGGER = LogManager.getLogger();
|
|
|
|
|
|
2019-08-29 03:01:11 +02:00
|
|
|
public static final FluidStack EMPTY = new FluidStack(Fluids.EMPTY, 0);
|
|
|
|
|
|
|
|
|
|
private boolean isEmpty;
|
|
|
|
|
private int amount;
|
|
|
|
|
private CompoundNBT tag;
|
2017-06-19 15:02:18 -07:00
|
|
|
private IRegistryDelegate<Fluid> fluidDelegate;
|
2013-05-23 01:01:19 -04:00
|
|
|
|
2013-07-16 21:40:49 -07:00
|
|
|
public FluidStack(Fluid fluid, int amount)
|
|
|
|
|
{
|
2015-04-11 17:38:15 -04:00
|
|
|
if (fluid == null)
|
|
|
|
|
{
|
2018-09-18 00:04:02 -04:00
|
|
|
LOGGER.fatal("Null fluid supplied to fluidstack. Did you try and create a stack for an unregistered fluid?");
|
2015-04-11 17:38:15 -04:00
|
|
|
throw new IllegalArgumentException("Cannot create a fluidstack from a null fluid");
|
2019-08-29 03:01:11 +02:00
|
|
|
}
|
|
|
|
|
else if (ForgeRegistries.FLUIDS.getKey(fluid) == null)
|
2015-04-05 22:37:40 -04:00
|
|
|
{
|
2019-08-29 03:01:11 +02:00
|
|
|
LOGGER.fatal("Failed attempt to create a FluidStack for an unregistered Fluid {} (type {})", fluid.getRegistryName(), fluid.getClass().getName());
|
2015-04-05 22:37:40 -04:00
|
|
|
throw new IllegalArgumentException("Cannot create a fluidstack from an unregistered fluid");
|
2019-08-29 03:01:11 +02:00
|
|
|
}
|
|
|
|
|
this.fluidDelegate = fluid.delegate;
|
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)
|
2013-07-16 21:40:49 -07:00
|
|
|
{
|
2015-03-25 01:27:15 -04:00
|
|
|
this(fluid, amount);
|
2013-05-23 01:01:19 -04:00
|
|
|
|
2013-07-16 21:40:49 -07:00
|
|
|
if (nbt != null)
|
|
|
|
|
{
|
2019-08-29 03:01:11 +02:00
|
|
|
tag = nbt.copy();
|
2013-05-23 01:01:19 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-16 21:40:49 -07:00
|
|
|
public FluidStack(FluidStack stack, int amount)
|
|
|
|
|
{
|
2015-04-20 09:06:23 -04:00
|
|
|
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.
|
2014-01-29 18:34:23 -08:00
|
|
|
*/
|
2019-05-23 16:02:15 -07:00
|
|
|
public static FluidStack loadFluidStackFromNBT(CompoundNBT nbt)
|
2013-07-16 21:40:49 -07:00
|
|
|
{
|
2013-07-28 00:53:23 +02:00
|
|
|
if (nbt == null)
|
|
|
|
|
{
|
2019-08-29 03:01:11 +02:00
|
|
|
return EMPTY;
|
2013-07-28 00:53:23 +02:00
|
|
|
}
|
2018-09-22 03:40:22 -07:00
|
|
|
if (!nbt.contains("FluidName", Constants.NBT.TAG_STRING))
|
2017-01-11 15:17:56 -08:00
|
|
|
{
|
2019-08-29 03:01:11 +02:00
|
|
|
return EMPTY;
|
2017-01-11 15:17:56 -08:00
|
|
|
}
|
2013-07-28 01:06:16 +02:00
|
|
|
|
2019-08-29 03:01:11 +02:00
|
|
|
ResourceLocation fluidName = new ResourceLocation(nbt.getString("FluidName"));
|
|
|
|
|
Fluid fluid = ForgeRegistries.FLUIDS.getValue(fluidName);
|
|
|
|
|
if (fluid == null)
|
2013-07-16 21:40:49 -07:00
|
|
|
{
|
2019-08-29 03:01:11 +02:00
|
|
|
return EMPTY;
|
|
|
|
|
}
|
|
|
|
|
FluidStack stack = new FluidStack(fluid, nbt.getInt("Amount"));
|
2013-05-23 01:01:19 -04:00
|
|
|
|
2019-08-29 03:01:11 +02:00
|
|
|
if (nbt.contains("Tag", Constants.NBT.TAG_COMPOUND))
|
2013-07-16 21:40:49 -07:00
|
|
|
{
|
2019-08-29 03:01:11 +02:00
|
|
|
stack.tag = nbt.getCompound("Tag");
|
2013-05-23 01:01:19 -04:00
|
|
|
}
|
2019-08-29 03:01:11 +02:00
|
|
|
return stack;
|
2013-05-23 01:01:19 -04:00
|
|
|
}
|
|
|
|
|
|
2019-05-23 16:02:15 -07:00
|
|
|
public CompoundNBT writeToNBT(CompoundNBT nbt)
|
2013-07-16 21:40:49 -07:00
|
|
|
{
|
2019-08-29 03:01:11 +02:00
|
|
|
nbt.putString("FluidName", getFluid().getRegistryName().toString());
|
2019-05-23 16:02:15 -07:00
|
|
|
nbt.putInt("Amount", amount);
|
2013-05-23 01:01:19 -04:00
|
|
|
|
2013-07-16 21:40:49 -07:00
|
|
|
if (tag != null)
|
|
|
|
|
{
|
2019-06-08 10:45:28 -04:00
|
|
|
nbt.put("Tag", tag);
|
2013-05-23 01:01:19 -04:00
|
|
|
}
|
|
|
|
|
return nbt;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-29 03:01:11 +02:00
|
|
|
public void writeToPacket(PacketBuffer buf)
|
|
|
|
|
{
|
|
|
|
|
buf.writeRegistryId(getFluid());
|
|
|
|
|
buf.writeVarInt(getAmount());
|
|
|
|
|
buf.writeCompoundTag(tag);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static FluidStack readFromPacket(PacketBuffer buf)
|
|
|
|
|
{
|
|
|
|
|
Fluid fluid = buf.readRegistryId();
|
|
|
|
|
int amount = buf.readVarInt();
|
|
|
|
|
CompoundNBT tag = buf.readCompoundTag();
|
|
|
|
|
if (fluid == Fluids.EMPTY) return EMPTY;
|
|
|
|
|
return new FluidStack(fluid, amount, tag);
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-16 21:40:49 -07:00
|
|
|
public final Fluid getFluid()
|
2019-08-29 03:01:11 +02:00
|
|
|
{
|
|
|
|
|
return isEmpty ? Fluids.EMPTY : fluidDelegate.get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public final Fluid getRawFluid()
|
2013-07-16 21:40:49 -07:00
|
|
|
{
|
2015-04-20 09:06:23 -04:00
|
|
|
return fluidDelegate.get();
|
2013-05-23 01:01:19 -04:00
|
|
|
}
|
|
|
|
|
|
2019-08-29 03:01:11 +02:00
|
|
|
public boolean isEmpty() {
|
|
|
|
|
return isEmpty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void updateEmpty() {
|
|
|
|
|
isEmpty = getRawFluid() == Fluids.EMPTY || amount == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getAmount()
|
|
|
|
|
{
|
|
|
|
|
return isEmpty ? 0 : amount ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setAmount(int amount)
|
|
|
|
|
{
|
|
|
|
|
if (getRawFluid() == Fluids.EMPTY) throw new IllegalStateException("Can't modify the empty stack.");
|
|
|
|
|
if (amount < 0) throw new IllegalStateException("The amount can't be negative.");
|
|
|
|
|
this.amount = amount;
|
|
|
|
|
updateEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void grow(int amount) {
|
|
|
|
|
setAmount(this.amount + amount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void shrink(int amount) {
|
|
|
|
|
setAmount(this.amount - amount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean hasTag()
|
|
|
|
|
{
|
|
|
|
|
return tag != null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CompoundNBT getTag()
|
|
|
|
|
{
|
|
|
|
|
return tag;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setTag(CompoundNBT tag)
|
|
|
|
|
{
|
|
|
|
|
if (getRawFluid() == Fluids.EMPTY) throw new IllegalStateException("Can't modify the empty stack.");
|
|
|
|
|
this.tag = tag;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CompoundNBT getOrCreateTag()
|
|
|
|
|
{
|
|
|
|
|
if (tag == null)
|
|
|
|
|
setTag(new CompoundNBT());
|
|
|
|
|
return tag;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CompoundNBT getChildTag(String childName)
|
|
|
|
|
{
|
|
|
|
|
if (tag == null)
|
|
|
|
|
return null;
|
|
|
|
|
return tag.getCompound(childName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CompoundNBT getOrCreateChildTag(String childName)
|
|
|
|
|
{
|
|
|
|
|
getOrCreateTag();
|
|
|
|
|
CompoundNBT child = tag.getCompound(childName);
|
|
|
|
|
if (child == null)
|
|
|
|
|
tag.put(childName, child = new CompoundNBT());
|
|
|
|
|
return child;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void removeChildTag(String childName)
|
|
|
|
|
{
|
|
|
|
|
if (tag != null)
|
|
|
|
|
tag.remove(childName);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 21:35:30 +02:00
|
|
|
public ITextComponent getDisplayName()
|
2014-07-10 15:25:53 +03:00
|
|
|
{
|
2019-07-28 21:35:30 +02:00
|
|
|
return this.getFluid().getAttributes().getDisplayName(this);
|
2014-07-10 15:25:53 +03:00
|
|
|
}
|
|
|
|
|
|
2019-07-28 21:35:30 +02:00
|
|
|
public String getTranslationKey()
|
2014-07-10 15:25:53 +03:00
|
|
|
{
|
2019-07-28 21:35:30 +02:00
|
|
|
return this.getFluid().getAttributes().getTranslationKey(this);
|
2014-07-10 15:25:53 +03:00
|
|
|
}
|
|
|
|
|
|
2013-05-23 01:01:19 -04:00
|
|
|
/**
|
|
|
|
|
* @return A copy of this FluidStack
|
2014-01-29 18:34:23 -08:00
|
|
|
*/
|
2013-07-16 21:40:49 -07:00
|
|
|
public FluidStack copy()
|
|
|
|
|
{
|
2015-04-20 09:06:23 -04:00
|
|
|
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-07-28 00:53:23 +02:00
|
|
|
*
|
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
|
2014-01-29 18:34:23 -08:00
|
|
|
*/
|
2019-08-29 03:01:11 +02:00
|
|
|
public boolean isFluidEqual(@Nonnull FluidStack other)
|
2013-07-16 21:40:49 -07:00
|
|
|
{
|
2019-08-29 03:01:11 +02:00
|
|
|
return getFluid() == other.getFluid() && isFluidStackTagEqual(other);
|
2013-05-23 01:01:19 -04:00
|
|
|
}
|
|
|
|
|
|
2013-07-16 21:40:49 -07:00
|
|
|
private boolean isFluidStackTagEqual(FluidStack other)
|
|
|
|
|
{
|
2019-07-28 21:35:30 +02:00
|
|
|
return tag == null ? other.tag == null : other.tag != null && tag.equals(other.tag);
|
2013-05-23 01:01:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determines if the NBT Tags are equal. Useful if the FluidIDs are known to be equal.
|
2014-01-29 18:34:23 -08:00
|
|
|
*/
|
2019-08-29 03:01:11 +02:00
|
|
|
public static boolean areFluidStackTagsEqual(@Nonnull FluidStack stack1, @Nonnull FluidStack stack2)
|
2013-07-16 21:40:49 -07:00
|
|
|
{
|
2019-08-29 03:01:11 +02:00
|
|
|
return stack1.isFluidStackTagEqual(stack2);
|
2013-05-23 01:01:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determines if the Fluids are equal and this stack is larger.
|
2013-07-28 00:53:23 +02:00
|
|
|
*
|
2013-05-23 01:01:19 -04:00
|
|
|
* @param other
|
|
|
|
|
* @return true if this FluidStack contains the other FluidStack (same fluid and >= amount)
|
2014-01-29 18:34:23 -08:00
|
|
|
*/
|
2019-08-29 03:01:11 +02:00
|
|
|
public boolean containsFluid(@Nonnull FluidStack other)
|
2013-07-16 21:40:49 -07:00
|
|
|
{
|
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-07-28 00:53:23 +02:00
|
|
|
*
|
2013-05-23 01:01:19 -04:00
|
|
|
* @param other
|
|
|
|
|
* - the FluidStack for comparison
|
|
|
|
|
* @return true if the two FluidStacks are exactly the same
|
2014-01-29 18:34:23 -08:00
|
|
|
*/
|
2013-07-16 21:40:49 -07:00
|
|
|
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-07-28 00:53:23 +02:00
|
|
|
*
|
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
|
2014-01-29 18:34:23 -08:00
|
|
|
*/
|
2013-07-16 21:40:49 -07:00
|
|
|
public boolean isFluidEqual(ItemStack other)
|
|
|
|
|
{
|
|
|
|
|
if (other == null)
|
|
|
|
|
{
|
2013-05-23 01:01:19 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
2013-07-16 21:40:49 -07:00
|
|
|
|
2018-09-18 00:04:02 -04:00
|
|
|
return FluidUtil.getFluidContained(other).map(this::isFluidEqual).orElse(false);
|
2013-05-23 01:01:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2013-07-16 21:40:49 -07:00
|
|
|
public final int hashCode()
|
|
|
|
|
{
|
2016-03-06 16:54:47 -08:00
|
|
|
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-07-28 00:53:23 +02:00
|
|
|
*
|
2013-05-23 01:01:19 -04:00
|
|
|
* This is included for use in data structures.
|
2014-01-29 18:34:23 -08:00
|
|
|
*/
|
2013-05-23 01:01:19 -04:00
|
|
|
@Override
|
2013-07-16 21:40:49 -07:00
|
|
|
public final boolean equals(Object o)
|
|
|
|
|
{
|
|
|
|
|
if (!(o instanceof FluidStack))
|
|
|
|
|
{
|
2013-05-23 01:01:19 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return isFluidEqual((FluidStack) o);
|
|
|
|
|
}
|
|
|
|
|
}
|