Package net.neoforged.neoforge.fluids
Class FluidStack
java.lang.Object
net.neoforged.neoforge.fluids.FluidStack
- All Implemented Interfaces:
net.minecraft.core.component.DataComponentGetter
,net.minecraft.core.component.DataComponentHolder
,IDataComponentHolderExtension
,MutableDataComponentHolder
ItemStack
equivalent for fluids.
The main difference is that a fluid stack is always required to have an amount, while an item stack defaults to 1.
Another difference is that the component prototype of a fluid stack is currently always empty, while an item stack gets its component prototype from the item.
Most methods in this class are adapted from ItemStack
.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate int
static final com.mojang.serialization.Codec
<FluidStack> A standard codec for fluid stacks that does not accept empty stacks.private final net.minecraft.core.component.PatchedDataComponentMap
static final FluidStack
private final net.minecraft.world.level.material.Fluid
static final com.mojang.serialization.Codec
<net.minecraft.core.Holder<net.minecraft.world.level.material.Fluid>> private static final org.slf4j.Logger
static final com.mojang.serialization.Codec
<FluidStack> A standard codec for fluid stacks that accepts empty stacks, serializing them as{}
.static final net.minecraft.network.codec.StreamCodec
<net.minecraft.network.RegistryFriendlyByteBuf, FluidStack> A stream codec for fluid stacks that accepts empty stacks.static final net.minecraft.network.codec.StreamCodec
<net.minecraft.network.RegistryFriendlyByteBuf, FluidStack> A stream codec for fluid stacks that does not accept empty stacks. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
FluidStack
(@Nullable Void unused) FluidStack
(net.minecraft.core.Holder<net.minecraft.world.level.material.Fluid> fluid, int amount) FluidStack
(net.minecraft.core.Holder<net.minecraft.world.level.material.Fluid> fluid, int amount, net.minecraft.core.component.DataComponentPatch patch) FluidStack
(net.minecraft.world.level.material.Fluid fluid, int amount) private
FluidStack
(net.minecraft.world.level.material.Fluid fluid, int amount, net.minecraft.core.component.PatchedDataComponentMap components) -
Method Summary
Modifier and TypeMethodDescriptionvoid
applyComponents
(net.minecraft.core.component.DataComponentMap components) Applies a set of component changes to this stack.void
applyComponents
(net.minecraft.core.component.DataComponentPatch patch) Applies a set of component changes to this stack.copy()
Creates a copy of this fluid stack.Creates a copy of this stack with0
amount.copyWithAmount
(int amount) Creates a copy of this fluid stack with the given amount.static com.mojang.serialization.Codec
<FluidStack> fixedAmountCodec
(int amount) A standard codec for fluid stacks that always deserializes with a fixed amount, and does not accept empty stacks.int
Returns the amount of this stack.net.minecraft.core.component.PatchedDataComponentMap
net.minecraft.core.component.DataComponentPatch
Returns thedescription id
of this stack.net.minecraft.world.level.material.Fluid
getFluid()
Returns the fluid in this stack, orFluids.EMPTY
if this stack is empty.net.minecraft.core.Holder
<net.minecraft.world.level.material.Fluid> Returns the fluid type of this stack.net.minecraft.network.chat.Component
Returns the hover name of this stack.Stream
<net.minecraft.tags.TagKey<net.minecraft.world.level.material.Fluid>> getTags()
void
grow
(int addedAmount) Adds the given amount to this stack.static int
hashFluidAndComponents
(@Nullable FluidStack stack) Hashes the fluid and components of this stack, ignoring the amount.boolean
boolean
is
(net.minecraft.core.Holder<net.minecraft.world.level.material.Fluid> holder) boolean
is
(net.minecraft.core.HolderSet<net.minecraft.world.level.material.Fluid> holderSet) boolean
is
(net.minecraft.tags.TagKey<net.minecraft.world.level.material.Fluid> tag) boolean
is
(net.minecraft.world.level.material.Fluid fluid) boolean
Check if the fluid type of this stack is equal to the given fluid type.boolean
boolean
isEmpty()
Checks if this fluid stack is empty.static boolean
isSameFluid
(FluidStack first, FluidStack second) Checks if the two fluid stacks have the same fluid.static boolean
isSameFluidSameComponents
(FluidStack first, FluidStack second) Checks if the two fluid stacks have the same fluid and components.static com.mojang.serialization.MapCodec
<FluidStack> lenientOtionalFieldOf
(String fieldName) void
limitSize
(int amount) Limits the amount of this stack is at most the given amount.static boolean
matches
(FluidStack first, FluidStack second) Checks if the two fluid stacks are equal.static Optional
<FluidStack> parse
(net.minecraft.core.HolderLookup.Provider lookupProvider, net.minecraft.nbt.Tag tag) Tries to parse a fluid stack.static FluidStack
parseOptional
(net.minecraft.core.HolderLookup.Provider lookupProvider, net.minecraft.nbt.CompoundTag tag) Tries to parse a fluid stack, defaulting toEMPTY
on parsing failure.<T> T
remove
(net.minecraft.core.component.DataComponentType<? extends T> type) Removes a data component.net.minecraft.nbt.Tag
save
(net.minecraft.core.HolderLookup.Provider lookupProvider) Saves this stack to a new tag.net.minecraft.nbt.Tag
save
(net.minecraft.core.HolderLookup.Provider lookupProvider, net.minecraft.nbt.Tag prefix) Saves this stack to a tag, directly writing the keys into the passed tag.net.minecraft.nbt.Tag
saveOptional
(net.minecraft.core.HolderLookup.Provider lookupProvider) Saves this stack to a new tag.<T> T
set
(net.minecraft.core.component.DataComponentType<T> type, T component) Sets a data component.void
setAmount
(int amount) Sets the amount of this stack.void
shrink
(int removedAmount) Removes the given amount from this stack.split
(int amount) Splits off a stack of the given amount of this stack and reduces this stack by the amount.toString()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface net.minecraft.core.component.DataComponentGetter
get, getOrDefault, getTyped, has
Methods inherited from interface net.minecraft.core.component.DataComponentHolder
get, getAllOfType, getOrDefault, has
Methods inherited from interface net.neoforged.neoforge.common.extensions.IDataComponentHolderExtension
addToTooltip, addToTooltip
-
Field Details
-
FLUID_NON_EMPTY_CODEC
public static final com.mojang.serialization.Codec<net.minecraft.core.Holder<net.minecraft.world.level.material.Fluid>> FLUID_NON_EMPTY_CODEC -
CODEC
A standard codec for fluid stacks that does not accept empty stacks. -
OPTIONAL_CODEC
A standard codec for fluid stacks that accepts empty stacks, serializing them as{}
. -
OPTIONAL_STREAM_CODEC
public static final net.minecraft.network.codec.StreamCodec<net.minecraft.network.RegistryFriendlyByteBuf,FluidStack> OPTIONAL_STREAM_CODECA stream codec for fluid stacks that accepts empty stacks. -
STREAM_CODEC
public static final net.minecraft.network.codec.StreamCodec<net.minecraft.network.RegistryFriendlyByteBuf,FluidStack> STREAM_CODECA stream codec for fluid stacks that does not accept empty stacks. -
LOGGER
private static final org.slf4j.Logger LOGGER -
EMPTY
-
amount
private int amount -
fluid
private final net.minecraft.world.level.material.Fluid fluid -
components
private final net.minecraft.core.component.PatchedDataComponentMap components
-
-
Constructor Details
-
FluidStack
public FluidStack(net.minecraft.core.Holder<net.minecraft.world.level.material.Fluid> fluid, int amount, net.minecraft.core.component.DataComponentPatch patch) -
FluidStack
public FluidStack(net.minecraft.core.Holder<net.minecraft.world.level.material.Fluid> fluid, int amount) -
FluidStack
public FluidStack(net.minecraft.world.level.material.Fluid fluid, int amount) -
FluidStack
private FluidStack(net.minecraft.world.level.material.Fluid fluid, int amount, net.minecraft.core.component.PatchedDataComponentMap components) -
FluidStack
-
-
Method Details
-
fixedAmountCodec
A standard codec for fluid stacks that always deserializes with a fixed amount, and does not accept empty stacks.Fluid equivalent of
ItemStack.SINGLE_ITEM_CODEC
. -
getComponents
public net.minecraft.core.component.PatchedDataComponentMap getComponents()- Specified by:
getComponents
in interfacenet.minecraft.core.component.DataComponentHolder
-
getComponentsPatch
public net.minecraft.core.component.DataComponentPatch getComponentsPatch() -
isComponentsPatchEmpty
public boolean isComponentsPatchEmpty() -
parse
public static Optional<FluidStack> parse(net.minecraft.core.HolderLookup.Provider lookupProvider, net.minecraft.nbt.Tag tag) Tries to parse a fluid stack. Empty stacks cannot be parsed with this method. -
parseOptional
public static FluidStack parseOptional(net.minecraft.core.HolderLookup.Provider lookupProvider, net.minecraft.nbt.CompoundTag tag) Tries to parse a fluid stack, defaulting toEMPTY
on parsing failure. -
isEmpty
public boolean isEmpty()Checks if this fluid stack is empty. -
split
Splits off a stack of the given amount of this stack and reduces this stack by the amount. -
copyAndClear
Creates a copy of this stack with0
amount. -
getFluid
public net.minecraft.world.level.material.Fluid getFluid()Returns the fluid in this stack, orFluids.EMPTY
if this stack is empty. -
getFluidHolder
public net.minecraft.core.Holder<net.minecraft.world.level.material.Fluid> getFluidHolder() -
is
public boolean is(net.minecraft.tags.TagKey<net.minecraft.world.level.material.Fluid> tag) -
is
public boolean is(net.minecraft.world.level.material.Fluid fluid) -
is
public boolean is(Predicate<net.minecraft.core.Holder<net.minecraft.world.level.material.Fluid>> holderPredicate) -
is
public boolean is(net.minecraft.core.Holder<net.minecraft.world.level.material.Fluid> holder) -
is
public boolean is(net.minecraft.core.HolderSet<net.minecraft.world.level.material.Fluid> holderSet) -
getTags
-
save
public net.minecraft.nbt.Tag save(net.minecraft.core.HolderLookup.Provider lookupProvider, net.minecraft.nbt.Tag prefix) Saves this stack to a tag, directly writing the keys into the passed tag.- Throws:
IllegalStateException
- if this stack is empty
-
save
public net.minecraft.nbt.Tag save(net.minecraft.core.HolderLookup.Provider lookupProvider) Saves this stack to a new tag.- Throws:
IllegalStateException
- if this stack is empty
-
saveOptional
public net.minecraft.nbt.Tag saveOptional(net.minecraft.core.HolderLookup.Provider lookupProvider) Saves this stack to a new tag. Empty stacks are supported and will be saved as an empty tag. -
copy
Creates a copy of this fluid stack. -
copyWithAmount
Creates a copy of this fluid stack with the given amount. -
matches
Checks if the two fluid stacks are equal. This checks the fluid, amount, and components.- Returns:
true
if the two fluid stacks have equal fluid, amount, and components
-
isSameFluid
Checks if the two fluid stacks have the same fluid. Ignores amount and components.- Returns:
true
if the two fluid stacks have the same fluid
-
isSameFluidSameComponents
Checks if the two fluid stacks have the same fluid and components. Ignores amount.- Returns:
true
if the two fluid stacks have the same fluid and components
-
lenientOtionalFieldOf
-
hashFluidAndComponents
Hashes the fluid and components of this stack, ignoring the amount. -
getDescriptionId
Returns thedescription id
of this stack. -
toString
-
set
@Nullable public <T> T set(net.minecraft.core.component.DataComponentType<T> type, @Nullable T component) Sets a data component.- Specified by:
set
in interfaceMutableDataComponentHolder
-
remove
@Nullable public <T> T remove(net.minecraft.core.component.DataComponentType<? extends T> type) Removes a data component.- Specified by:
remove
in interfaceMutableDataComponentHolder
-
applyComponents
public void applyComponents(net.minecraft.core.component.DataComponentPatch patch) Applies a set of component changes to this stack.- Specified by:
applyComponents
in interfaceMutableDataComponentHolder
-
applyComponents
public void applyComponents(net.minecraft.core.component.DataComponentMap components) Applies a set of component changes to this stack.- Specified by:
applyComponents
in interfaceMutableDataComponentHolder
-
getHoverName
public net.minecraft.network.chat.Component getHoverName()Returns the hover name of this stack. -
getAmount
public int getAmount()Returns the amount of this stack. -
setAmount
public void setAmount(int amount) Sets the amount of this stack. -
limitSize
public void limitSize(int amount) Limits the amount of this stack is at most the given amount. -
grow
public void grow(int addedAmount) Adds the given amount to this stack. -
shrink
public void shrink(int removedAmount) Removes the given amount from this stack. -
getFluidType
Returns the fluid type of this stack. -
is
Check if the fluid type of this stack is equal to the given fluid type.
-