Class FluidProperty<T>
java.lang.Object
alexiil.mc.lib.attributes.fluid.volume.FluidProperty<T>
Some data that can be added to
FluidVolumes.
They have the following requirements:
- Property values should be immutable. This allows multiple
FluidVolumes to share the same object, and to allowdefaultValueto 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
FluidVolumes 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
FieldsModifier and TypeFieldDescriptionfinal Tfinal net.minecraft.util.IdentifierUsed for reading and writing this property.The temperature scale, if this fluid property provides one, or implementsFluidTemperature.ContinuousFluidTemperaturedirectly. -
Constructor Summary
ConstructorsConstructorDescriptionFluidProperty(net.minecraft.util.Identifier id, Class<T> type, T defaultValue) FluidProperty(net.minecraft.util.Identifier id, Class<T> type, T defaultValue, FluidTemperature.ContinuousFluidTemperature temperature) -
Method Summary
Modifier and TypeMethodDescriptionvoidaddTooltipExtras(FluidKey fluid, FluidTooltipContext context, List<net.minecraft.text.Text> tooltip) Adds tooltip extras for this fluid property when getting the tooltip for just theFluidKey.voidaddTooltipExtras(FluidVolume fluid, FluidTooltipContext context, List<net.minecraft.text.Text> tooltip) Adds tooltip extras for this fluid property when getting the tooltip for a fullFluidVolume.protected TfromJson(com.google.gson.JsonElement json) Reads the value from aJsonElement.protected abstract TfromTag(net.minecraft.nbt.NbtElement tag) Reads the value from the given tag.final Tget(FluidVolume volume) Helper method to obtain this property from the given fluid.protected abstract Tmerge(FluidVolume volumeA, FluidVolume volumeB, FluidAmount amount, T valueA, T valueB) Merges two values together, using the twoFluidVolumes for context.protected TreadFromBuffer(net.minecraft.network.PacketByteBuf buffer) Writes the given value out to a packet buffer.final voidset(FluidVolume volume, T value) Helper method to set a value for this property to the given fluid.protected com.google.gson.JsonElementWrites the given value to aJsonElement.protected abstract net.minecraft.nbt.NbtElementprotected voidwriteToBuffer(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.ContinuousFluidTemperaturedirectly.
-
-
Constructor Details
-
FluidProperty
-
FluidProperty
public FluidProperty(net.minecraft.util.Identifier id, Class<T> type, T defaultValue, FluidTemperature.ContinuousFluidTemperature temperature) - Parameters:
temperature- The temperature property to use for this property. Must be null if this implementsFluidTemperature.ContinuousFluidTemperaturedirectly.
-
-
Method Details
-
get
Helper method to obtain this property from the given fluid.- Throws:
IllegalArgumentException- if the given property hasn't been registered to theFluidKey.- See Also:
-
set
Helper method to set a value for this property to the given fluid.- Throws:
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 theTagreturned bytoTag(Object)in aNbtCompoundto 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 aNbtCompoundfrom 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 aJsonSyntaxExceptionrather 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 twoFluidVolumes 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
FluidVolumes!- 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, 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, List<net.minecraft.text.Text> tooltip) Adds tooltip extras for this fluid property when getting the tooltip for a fullFluidVolume.
-