Interface ValueMapper<T,E extends JsonElement>

Type Parameters:
T - The type that this mapper can serialise.
E - The JSON element type this mapper uses.

public interface ValueMapper<T,E extends JsonElement>
A way to serialise values of a specific type for a config file. See ConfigManager for more info.
  • Method Summary

    Modifier and Type
    Method
    Description
    default List<String>
    Returns a list of comment lines that will be added to the values specified in @Config.
    default Optional<T>
    correct(JsonElement json, ConfigCorrection<T> correction)
    Corrects a config value.
    Creates a ConfigEditor for this value mapper to display this config in the config menu.
    Gets the class of the JSON element type this mapper uses.
    fromJson(E json)
    Reads an object from JSON.
    default T
    fromNetwork(net.minecraft.network.FriendlyByteBuf buffer)
    Reads a value from a FriendlyByteBuf.
    toJson(T value)
    Serialises a value to JSON.
    default void
    toNetwork(T value, net.minecraft.network.FriendlyByteBuf buffer)
    Writes a value to a FriendlyByteBuf.
    Gets the class of the type that this mapper can serialise.
  • Method Details

    • type

      Class<T> type()
      Gets the class of the type that this mapper can serialise.
    • element

      Class<E> element()
      Gets the class of the JSON element type this mapper uses.
    • fromJson

      T fromJson(E json)
      Reads an object from JSON. If the json is invalid you may either throw an IllegalStateException or a JsonParseException to end config parsing with an error, or just correct errors yourself and return default values.
      Parameters:
      json - The json data
      Returns:
      The value read.
    • toJson

      E toJson(T value)
      Serialises a value to JSON.
      Parameters:
      value - The value to serialise
      Returns:
      The resulting json data.
    • fromNetwork

      default T fromNetwork(net.minecraft.network.FriendlyByteBuf buffer)
      Reads a value from a FriendlyByteBuf. The default implementation expects a JSON string and gives this string to fromJSON.
    • toNetwork

      default void toNetwork(T value, net.minecraft.network.FriendlyByteBuf buffer)
      Writes a value to a FriendlyByteBuf. The default implementation calls toJSON and writes the resulting JSON as a string.
    • comment

      default List<String> comment()
      Returns a list of comment lines that will be added to the values specified in @Config.
    • correct

      default Optional<T> correct(JsonElement json, ConfigCorrection<T> correction)
      Corrects a config value. This is only called if fromJson(JsonElement) fails. Here the raw json for the config value is passed. Also a ConfigCorrection is passed that allows to correct json of types contained in this value. For example a list value mapper can use the ConfigCorrection to try to get as many values as possible out of a json array.
      Returns:
      An Optional containing the corrected value or an empty Optional the correction failed.
    • createEditor

      ConfigEditor<T> createEditor(ValidatorInfo<?> validator)
      Creates a ConfigEditor for this value mapper to display this config in the config menu. To display that this value can't be edited through the GUI, use ConfigEditor.unsupported(Object) with a default value that is used if for example elements of this type are created in a list.
      Parameters:
      validator - Access to the current validator used. Can be used to create different editors based on validators.