Class ConfigurateOps

java.lang.Object
org.spongepowered.configurate.extra.dfu.v7.ConfigurateOps
All Implemented Interfaces:
com.mojang.serialization.DynamicOps<ConfigurationNode>

public final class ConfigurateOps extends Object implements com.mojang.serialization.DynamicOps<ConfigurationNode>
Implementation of DataFixerUpper's DynamicOps.

The DynamicOps interface should be thought of essentially as a way to perform operations on a type without having to directly implement that interface on the type. Rather than taking an object that implements an interface, DFU methods take the implementation of the DynamicOps interface plus the type implemented onto.

When possible, the first node's ConfigurationNode.copy() method will be used to create a new node to contain results. Otherwise, the provided factory will be used. The default factory creates a CommentedConfigurationNode with the default serializer collection but a custom factory may be provided.

DynamicOps has the following primitive types (as determined by those codecs that implement PrimitiveCodec):

boolean
literal boolean, or numeric 1 for true, 0 for false
byte
numeric value, coerced to a byte. If compressMaps(), a string may be parsed as a byte as well.
short
numeric value, coerced to a short. If compressMaps(), a string may be parsed as a short as well.
int
numeric value, coerced to an integer. If compressMaps(), a string may be parsed as an integer as well.
long
numeric value, coerced to a long. If compressMaps(), a string may be parsed as a long as well.
float
numeric value, coerced to a float. If compressMaps(), a string may be parsed as a float as well.
double
numeric value, coerced to a double. If compressMaps(), a string may be parsed as a double as well.
String
Any scalar value, as a string
ByteBuffer
An array of bytes. Either a native byte array in the node, or (by default impl) a list of bytes
IntStream
A sequence of integers. Either a native int array in the node, or (by default impl) a list of integers
LongStream
A sequence of longs. Either a native long array in the node, or (by default impl) a list of longs
Since:
4.3.0
  • Method Details

    • instance

      public static com.mojang.serialization.DynamicOps<ConfigurationNode> instance()
      Get the shared instance of this class, which creates new nodes using the default factory. The returned instance will not be compressed
      Returns:
      the shared instance
      Since:
      4.3.0
    • instance

      public static com.mojang.serialization.DynamicOps<ConfigurationNode> instance(boolean compressed)
      Get the shared instance of this class, which creates new nodes using the default factory.

      See compressMaps() for a description of what the

      compressed
      parameter does.
      Parameters:
      compressed - whether keys should be compressed in the output of this serializer
      Returns:
      the shared instance
      Since:
      4.3.0
    • forSerializers

      public static com.mojang.serialization.DynamicOps<ConfigurationNode> forSerializers(TypeSerializerCollection collection)
      Get an ops instance that will create nodes using the provided collection.
      Parameters:
      collection - collection to provide through created nodes' options
      Returns:
      ops instance
      Since:
      4.3.0
    • wrap

      public static com.mojang.serialization.Dynamic<ConfigurationNode> wrap(ConfigurationNode node)
      Wrap a ConfigurationNode in a Dynamic instance. The returned Dynamic will use the same type serializer collection as the original node for its operations.
      Parameters:
      node - the node to wrap
      Returns:
      a wrapped node
      Since:
      4.3.0
    • fromNode

      public static com.mojang.serialization.DynamicOps<ConfigurationNode> fromNode(ConfigurationNode value)
      Configure an ops instance using the options of an existing node.
      Parameters:
      value - the value type
      Returns:
      values
      Since:
      4.3.0
    • builder

      public static ConfigurateOpsBuilder builder()
      Create a new builder for an ops instance.
      Returns:
      builder
      Since:
      4.3.0
    • compressMaps

      public boolean compressMaps()
      Whether data passed through this ops will be compressed or not.

      In the context of DFU,

      compressed
      means that in situations where values are of a Keyable type (as is with types like Minecraft Registries) rather than fully encoding each value, its index into the container is encoded.

      While data encoded this way may take less space to store, the compressed data will also require an explicit mapping of indices to values. If this is not stored with the node, the indices of values must be preserved to correctly deserialize compressed values.

      For example, for an enum new values could only be appended, not added in the middle of the constants.

      Specified by:
      compressMaps in interface com.mojang.serialization.DynamicOps<ConfigurationNode>
      Returns:
      whether maps are compressed
    • empty

      Create a new empty node using this ops instance's factory.
      Specified by:
      empty in interface com.mojang.serialization.DynamicOps<ConfigurationNode>
      Returns:
      the new node
    • emptyMap

      Specified by:
      emptyMap in interface com.mojang.serialization.DynamicOps<ConfigurationNode>
    • emptyList

      Specified by:
      emptyList in interface com.mojang.serialization.DynamicOps<ConfigurationNode>
    • convertTo

      public <U> U convertTo(com.mojang.serialization.DynamicOps<U> targetOps, ConfigurationNode source)
      Create a copy of the source node converted to a different data structure.

      Value types will be preserved as much as possible, but a reverse conversion will most likely be lossy

      Specified by:
      convertTo in interface com.mojang.serialization.DynamicOps<ConfigurationNode>
      Type Parameters:
      U - output type
      Parameters:
      targetOps - output type
      source - source value
      Returns:
      output value
    • getNumberValue

      public com.mojang.serialization.DataResult<Number> getNumberValue(ConfigurationNode input)
      Get the value of the provided node if it is a number or boolean.

      If compressMaps() is true, values may be coerced from another type.

      Specified by:
      getNumberValue in interface com.mojang.serialization.DynamicOps<ConfigurationNode>
      Parameters:
      input - data source
      Returns:
      extracted number
    • getStringValue

      public com.mojang.serialization.DataResult<String> getStringValue(ConfigurationNode input)
      Get the value of the provided node if it is a scalar, converted to a String.
      Specified by:
      getStringValue in interface com.mojang.serialization.DynamicOps<ConfigurationNode>
      Parameters:
      input - data source
      Returns:
      string | error
    • createNumeric

      Create a new node using this ops instance's node factory, and set its value to the provided number.
      Specified by:
      createNumeric in interface com.mojang.serialization.DynamicOps<ConfigurationNode>
      Parameters:
      value - value
      Returns:
      new node with value
    • createBoolean

      public ConfigurationNode createBoolean(boolean value)
      Create a new node using this ops instance's node factory, and set its value to the provided boolean.
      Specified by:
      createBoolean in interface com.mojang.serialization.DynamicOps<ConfigurationNode>
      Parameters:
      value - value
      Returns:
      new node with value
    • createString

      Create a new node using this ops instance's node factory, and set its value to the provided string.
      Specified by:
      createString in interface com.mojang.serialization.DynamicOps<ConfigurationNode>
      Parameters:
      value - value
      Returns:
      new node with value
    • mergeToPrimitive

      public com.mojang.serialization.DataResult<ConfigurationNode> mergeToPrimitive(ConfigurationNode prefix, ConfigurationNode value)
      Return a result where if prefix is empty, the node is value, but otherwise returns an error.
      Specified by:
      mergeToPrimitive in interface com.mojang.serialization.DynamicOps<ConfigurationNode>
      Parameters:
      prefix - starting value
      value - to update base with
      Returns:
      result of updated node or error
    • mergeToList

      public com.mojang.serialization.DataResult<ConfigurationNode> mergeToList(ConfigurationNode input, ConfigurationNode value)
      Appends element value to list node input.
      Specified by:
      mergeToList in interface com.mojang.serialization.DynamicOps<ConfigurationNode>
      Parameters:
      input - base node. Must be empty or of list type
      value - value to add as element to the list
      Returns:
      success with modified node, or error if input contains a non-list value
    • mergeToList

      public com.mojang.serialization.DataResult<ConfigurationNode> mergeToList(ConfigurationNode input, List<ConfigurationNode> values)
      Appends nodes in values to copy of list node input.
      Specified by:
      mergeToList in interface com.mojang.serialization.DynamicOps<ConfigurationNode>
      Parameters:
      input - base node. Must be empty or of list type
      values - list of values to append to base node
      Returns:
      success with modified node, or error if input contains a non-list value
    • mergeToMap

      public com.mojang.serialization.DataResult<ConfigurationNode> mergeToMap(ConfigurationNode input, ConfigurationNode key, ConfigurationNode value)
      Update the child of input at key with value.

      This operation will only affect the returned copy of the input node

      Specified by:
      mergeToMap in interface com.mojang.serialization.DynamicOps<ConfigurationNode>
      Parameters:
      input - base node. Must be empty or of map type
      key - key relative to base node
      value - value to set at empty node
      Returns:
      success with modified node, or error if input contains a non-list value
    • getMapValues

      public com.mojang.serialization.DataResult<Stream<com.mojang.datafixers.util.Pair<ConfigurationNode,ConfigurationNode>>> getMapValues(ConfigurationNode input)
      Return a stream of pairs of (key, value) for map data in the input node.

      If the input node is non-empty and not a map, the result will be a failure.

      Specified by:
      getMapValues in interface com.mojang.serialization.DynamicOps<ConfigurationNode>
      Parameters:
      input - input node
      Returns:
      result, if successful, of a stream of pairs (key, value) of entries in the input node.
    • getMap

      public com.mojang.serialization.DataResult<com.mojang.serialization.MapLike<ConfigurationNode>> getMap(ConfigurationNode input)
      Get a map-like view of a copy of the contents of input.

      If the input node is non-empty and not a map, the result will be a failure.

      Specified by:
      getMap in interface com.mojang.serialization.DynamicOps<ConfigurationNode>
      Parameters:
      input - input node
      Returns:
      result, if successful, of map-like view of a copy of the input
    • getList

      public com.mojang.serialization.DataResult<Consumer<Consumer<ConfigurationNode>>> getList(ConfigurationNode input)
      Get a consumer that takes an action to perform on every element of list node input.

      As an example, to print out every node in a list (minus error checking):

               getList(listNode).result().get()
                   .accept(element -> System.out.println(element);
           
      Specified by:
      getList in interface com.mojang.serialization.DynamicOps<ConfigurationNode>
      Parameters:
      input - data source
      Returns:
      result, that if successful will take an action to perform on every element
    • getStream

      public com.mojang.serialization.DataResult<Stream<ConfigurationNode>> getStream(ConfigurationNode input)
      Get the contents of list node input as a Stream of nodes.
      Specified by:
      getStream in interface com.mojang.serialization.DynamicOps<ConfigurationNode>
      Parameters:
      input - data source
      Returns:
      if node is empty or a list, stream of nodes
    • createMap

      public ConfigurationNode createMap(Stream<com.mojang.datafixers.util.Pair<ConfigurationNode,ConfigurationNode>> values)
      Create a new node containing the map entries from the stream values.

      Keys will be interpreted as a single Object, and can only currently access direct children.

      Specified by:
      createMap in interface com.mojang.serialization.DynamicOps<ConfigurationNode>
      Parameters:
      values - entries in the map
      Returns:
      newly created node
    • createMap

      Create a new node containing the map entries from the map values.

      Keys will be interpreted as a single Object, and can only currently access direct children.

      Specified by:
      createMap in interface com.mojang.serialization.DynamicOps<ConfigurationNode>
      Parameters:
      values - unwrapped node map
      Returns:
      newly created node
    • createList

      Create a new node containing values emitted by input as list elements.
      Specified by:
      createList in interface com.mojang.serialization.DynamicOps<ConfigurationNode>
      Parameters:
      input - data source
      Returns:
      newly created node
    • remove

      Get a copy of input without the value at node key.

      If the input node is not a map, the input node will be returned.

      Specified by:
      remove in interface com.mojang.serialization.DynamicOps<ConfigurationNode>
      Parameters:
      input - data source
      key - key to the node to be removed
      Returns:
      if node removed, a copy of the input without node, otherwise input
    • get

      public com.mojang.serialization.DataResult<ConfigurationNode> get(ConfigurationNode input, String key)
      Attempt to get the child of input at key.
      Specified by:
      get in interface com.mojang.serialization.DynamicOps<ConfigurationNode>
      Parameters:
      input - data source
      key - child key
      Returns:
      success containing child if child is non-virtual, otherwise failure
    • getGeneric

      public com.mojang.serialization.DataResult<ConfigurationNode> getGeneric(ConfigurationNode input, ConfigurationNode key)
      Get a child of the provided node at key.

      Keys will be interpreted as a single Object, and can only currently access direct children.

      Specified by:
      getGeneric in interface com.mojang.serialization.DynamicOps<ConfigurationNode>
      Parameters:
      input - parent node
      key - wrapped key of child
      Returns:
      success containing child if child is non-virtual, otherwise failure
    • set

      Update a copy of input with value at path key.
      Specified by:
      set in interface com.mojang.serialization.DynamicOps<ConfigurationNode>
      Parameters:
      input - data source
      key - key of child node
      value - value for child node
      Returns:
      updated parent node
    • update

      Copies the input node and transform its child at key.

      Return a copy of the input node with the child at key transformed by the provided function

      If there is no value at key, the input node will be returned unmodified.

      Specified by:
      update in interface com.mojang.serialization.DynamicOps<ConfigurationNode>
      Parameters:
      input - base value
      key - key to change
      function - function to process the node at wrappedKey
      Returns:
      an updated copy of input node
    • updateGeneric

      Copies the input node and transform the node at wrappedKey.

      Return a copy of the input node with the child at wrappedKey transformed by the provided function

      If there is no value at wrappedKey, the input node will be returned unmodified.

      Keys will be interpreted as a single Object, and can only currently access direct children.

      Specified by:
      updateGeneric in interface com.mojang.serialization.DynamicOps<ConfigurationNode>
      Parameters:
      input - base value
      wrappedKey - key to change
      function - function to process the node at wrappedKey
      Returns:
      an updated copy of input node
    • toString

      public String toString()
      Overrides:
      toString in class Object