Class FluidProperty<T>

java.lang.Object
alexiil.mc.lib.attributes.fluid.volume.FluidProperty<T>

public abstract class FluidProperty<T> extends Object
Some data that can be added to FluidVolumes.

They have the following requirements:

  1. Property values should be immutable. This allows multiple FluidVolumes to share the same object, and to allow defaultValue 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.
  2. 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.
  3. Property values that are equal (or are identical with ==) don't need to be merged.
  4. Property values don't need any special handling to split them - the value will be used by both the volumes.
  5. 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).
Note that it's generally more efficient to subclass 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 Details

  • Constructor Details

  • Method Details

    • get

      public final T get(FluidVolume volume)
      Helper method to obtain this property from the given fluid.
      Throws:
      IllegalArgumentException - if the given property hasn't been registered to the FluidKey.
      See Also:
      FluidVolume.getProperty(FluidProperty)
    • set

      public final void set(FluidVolume volume, T value)
      Helper method to set a value for this property to the given fluid.
      Throws:
      IllegalArgumentException - if the given property hasn't been registered to the FluidKey.
    • toTag

      @Nullable protected abstract net.minecraft.nbt.NbtElement toTag(T value)
      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

      protected abstract T fromTag(net.minecraft.nbt.NbtElement tag)
      Reads the value from the given tag.
      Parameters:
      tag - A tag, which will probably have been generated from toTag(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 use defaultValue.
    • writeToBuffer

      protected void writeToBuffer(net.minecraft.network.PacketByteBuf buffer, T value)
      Writes the given value out to a packet buffer. By default this writes the Tag returned by toTag(Object) in a NbtCompound to the buffer, so it's recommenced that you override this to write out the value using a more efficient method.
    • readFromBuffer

      @Nullable protected T readFromBuffer(net.minecraft.network.PacketByteBuf buffer)
      Writes the given value out to a packet buffer. By default this reads a NbtCompound 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

      protected T fromJson(com.google.gson.JsonElement json) throws com.google.gson.JsonSyntaxException
      Reads the value from a JsonElement. It is highly recommended that you throw a JsonSyntaxException 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

      protected com.google.gson.JsonElement toJson(T value)
      Writes the given value to a JsonElement.
      Returns:
      The JsonElement that fromJson(JsonElement) can read. The returned element won't be included if this returns JsonNull.
    • merge

      protected abstract T merge(FluidVolume volumeA, FluidVolume volumeB, FluidAmount amount, T valueA, T valueB)
      Merges two values together, using the two FluidVolumes 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 the FluidKey.
    • 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 full FluidVolume.