Class FluidProperty<T>
java.lang.Object
alexiil.mc.lib.attributes.fluid.volume.FluidProperty<T>
public abstract class FluidProperty<T>
extends java.lang.Object
Some data that can be added to
FluidVolume
s.
They have the following requirements:
- Property values should be immutable. This allows multiple
FluidVolume
s to share the same object, and to allowdefaultValue
to exist as a public final field. If you want to store a mutable value as a property in a fluid volume then you should copy it before modifying it, as every client will expect it to be unchanged. - Property values should be independent to the amount of fluid in a volume. This allows
FluidVolume
s to leave properties alone until they are changed from their default values. - Property values that are
equal
(or are identical with ==) don't need to be merged. - Property values don't need any special handling to split them - the value will be used by both the volumes.
- All values that a property can take can be merged. (This is done to keep the singular requirement that fluid volumes can always be merged if they have the same key).
FluidVolume
if you can rather than add properties to one,
especially if the value you are storing is a primitive.
All of the simplifications allow optimising the storing array to store null instead of a value, and possibly use null for the entire array if every property is set to it's default value.
-
Field Summary
Fields Modifier and Type Field Description T
defaultValue
net.minecraft.util.Identifier
id
Used for reading and writing this property.FluidTemperature.ContinuousFluidTemperature
temperature
The temperature scale, if this fluid property provides one, or implementsFluidTemperature.ContinuousFluidTemperature
directly.java.lang.Class<T>
type
-
Constructor Summary
Constructors Constructor Description FluidProperty(net.minecraft.util.Identifier id, java.lang.Class<T> type, T defaultValue)
FluidProperty(net.minecraft.util.Identifier id, java.lang.Class<T> type, T defaultValue, FluidTemperature.ContinuousFluidTemperature temperature)
-
Method Summary
Modifier and Type Method Description void
addTooltipExtras(FluidKey fluid, FluidTooltipContext context, java.util.List<net.minecraft.text.Text> tooltip)
Adds tooltip extras for this fluid property when getting the tooltip for just theFluidKey
.void
addTooltipExtras(FluidVolume fluid, FluidTooltipContext context, java.util.List<net.minecraft.text.Text> tooltip)
Adds tooltip extras for this fluid property when getting the tooltip for a fullFluidVolume
.protected T
fromJson(com.google.gson.JsonElement json)
Reads the value from aJsonElement
.protected abstract T
fromTag(net.minecraft.nbt.Tag tag)
Reads the value from the given tag.T
get(FluidVolume volume)
Helper method to obtain this property from the given fluid.protected abstract T
merge(FluidVolume volumeA, FluidVolume volumeB, FluidAmount amount, T valueA, T valueB)
Merges two values together, using the twoFluidVolume
s for context.protected T
readFromBuffer(net.minecraft.network.PacketByteBuf buffer)
Writes the given value out to a packet buffer.void
set(FluidVolume volume, T value)
Helper method to set a value for this property to the given fluid.protected com.google.gson.JsonElement
toJson(T value)
Writes the given value to aJsonElement
.protected abstract net.minecraft.nbt.Tag
toTag(T value)
protected void
writeToBuffer(net.minecraft.network.PacketByteBuf buffer, T value)
Writes the given value out to a packet buffer.
-
Field Details
-
id
public final net.minecraft.util.Identifier idUsed for reading and writing this property. -
type
-
defaultValue
-
temperature
The temperature scale, if this fluid property provides one, or implementsFluidTemperature.ContinuousFluidTemperature
directly.
-
-
Constructor Details
-
FluidProperty
-
FluidProperty
public FluidProperty(net.minecraft.util.Identifier id, java.lang.Class<T> type, T defaultValue, FluidTemperature.ContinuousFluidTemperature temperature)- Parameters:
temperature
- The temperature property to use for this property. Must be null if this implementsFluidTemperature.ContinuousFluidTemperature
directly.
-
-
Method Details
-
get
Helper method to obtain this property from the given fluid.- Throws:
java.lang.IllegalArgumentException
- if the given property hasn't been registered to theFluidKey
.- See Also:
FluidVolume.getProperty(FluidProperty)
-
set
Helper method to set a value for this property to the given fluid.- Throws:
java.lang.IllegalArgumentException
- if the given property hasn't been registered to theFluidKey
.
-
toTag
- Returns:
- The tag that will be passed to
fromTag(Tag)
to return the value. Returning null indicates that nothing should be written out, and as such the default value will be used when reading.
-
fromTag
Reads the value from the given tag.- Parameters:
tag
- A tag, which will probably have been generated fromtoTag(Object)
, but can also come from the user (via /give or similar). This will never be null, as a null or missing tag will instead usedefaultValue
.
-
writeToBuffer
Writes the given value out to a packet buffer. By default this writes theTag
returned bytoTag(Object)
in aCompoundTag
to the buffer, so it's recommenced that you override this to write out the value using a more efficient method. -
readFromBuffer
Writes the given value out to a packet buffer. By default this reads aCompoundTag
from the buffer, so it's recommenced that you override this to read out the value using a more efficient method.- Returns:
- The read value, or null if the default value should be used instead. (Which will most likely be stored as null instead).
-
fromJson
Reads the value from aJsonElement
. It is highly recommended that you throw aJsonSyntaxException
rather than other internal exceptions if the given element isn't valid, as this is likely to appear in a recipe json file.- Throws:
com.google.gson.JsonSyntaxException
-
toJson
Writes the given value to aJsonElement
.- Returns:
- The JsonElement that
fromJson(JsonElement)
can read. The returned element won't be included if this returnsJsonNull
.
-
merge
protected abstract T merge(FluidVolume volumeA, FluidVolume volumeB, FluidAmount amount, T valueA, T valueB)Merges two values together, using the twoFluidVolume
s for context. The volumes are only given for context - most fluid properties will have no use for them.This must never modify either of the passed
FluidVolume
s!- Parameters:
volumeA
- One of the volumes that will be merged.volumeB
- The other volume that will be merged.amount
- The new (merged) amount. This might not be exactly equal to volumeA.getAmount_F().add(volumeB.getAmount_F()) due to rounding.valueA
- The value in volumeA, provided for convenience.valueB
- The value in volumeB, provided for convenience.- Returns:
- The merged value.
-
addTooltipExtras
public void addTooltipExtras(FluidKey fluid, FluidTooltipContext context, java.util.List<net.minecraft.text.Text> tooltip)Adds tooltip extras for this fluid property when getting the tooltip for just theFluidKey
. -
addTooltipExtras
public void addTooltipExtras(FluidVolume fluid, FluidTooltipContext context, java.util.List<net.minecraft.text.Text> tooltip)Adds tooltip extras for this fluid property when getting the tooltip for a fullFluidVolume
.
-