Class ConfigurateOps
- java.lang.Object
-
- org.spongepowered.configurate.extra.dfu.v4.ConfigurateOps
-
- All Implemented Interfaces:
DynamicOps<ConfigurationNode>
public final class ConfigurateOps extends Object implements 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 aCommentedConfigurationNode
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. IfcompressMaps()
, a string may be parsed as a byte as well. - short
- numeric value,
coerced
to a short. IfcompressMaps()
, a string may be parsed as a short as well. - int
- numeric value,
coerced
to an integer. IfcompressMaps()
, a string may be parsed as an integer as well. - long
- numeric value,
coerced
to a long. IfcompressMaps()
, a string may be parsed as a long as well. - float
- numeric value,
coerced
to a float. IfcompressMaps()
, a string may be parsed as a float as well. - double
- numeric value,
coerced
to a double. IfcompressMaps()
, 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.0.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ConfigurateOps.Protection
Protection level for configuration node accesses through ops instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ConfigurateOpsBuilder
builder()
Create a new builder for an ops instance.boolean
compressMaps()
Whether data passed through this ops will be compressed or not.<U> U
convertTo(DynamicOps<U> targetOps, ConfigurationNode source)
Create a copy of the source node converted to a different data structure.ConfigurationNode
createBoolean(boolean value)
Create a new node using this ops instance's node factory, and set its value to the provided boolean.ConfigurationNode
createList(Stream<ConfigurationNode> input)
Create a new node containing values emitted byinput
as list elements.ConfigurationNode
createMap(Map<ConfigurationNode,ConfigurationNode> values)
Create a new node containing the map entries from the mapvalues
.ConfigurationNode
createMap(Stream<Pair<ConfigurationNode,ConfigurationNode>> values)
Create a new node containing the map entries from the streamvalues
.ConfigurationNode
createNumeric(Number value)
Create a new node using this ops instance's node factory, and set its value to the provided number.ConfigurationNode
createString(String value)
Create a new node using this ops instance's node factory, and set its value to the provided string.ConfigurationNode
empty()
Create a new empty node using this ops instance's factory.ConfigurationNode
emptyList()
ConfigurationNode
emptyMap()
static DynamicOps<ConfigurationNode>
forSerializers(TypeSerializerCollection collection)
Get an ops instance that will create nodes using the provided collection.static DynamicOps<ConfigurationNode>
fromNode(ConfigurationNode value)
Configure an ops instance using the options of an existing node.DataResult<ConfigurationNode>
get(ConfigurationNode input, String key)
Attempt to get the child ofinput
atkey
.DataResult<ConfigurationNode>
getGeneric(ConfigurationNode input, ConfigurationNode key)
Get a child of the provided node atkey
.DataResult<Consumer<Consumer<ConfigurationNode>>>
getList(ConfigurationNode input)
Get a consumer that takes an action to perform on every element of list nodeinput
.DataResult<MapLike<ConfigurationNode>>
getMap(ConfigurationNode input)
Get a map-like view of a copy of the contents ofinput
.DataResult<Stream<Pair<ConfigurationNode,ConfigurationNode>>>
getMapValues(ConfigurationNode input)
Return a stream of pairs of (key, value) for map data in the input node.DataResult<Number>
getNumberValue(ConfigurationNode input)
Get the value of the provided node if it is a number or boolean.DataResult<Stream<ConfigurationNode>>
getStream(ConfigurationNode input)
Get the contents of list nodeinput
as aStream
of nodes.DataResult<String>
getStringValue(ConfigurationNode input)
Get the value of the provided node if it is a scalar, converted to aString
.static DynamicOps<ConfigurationNode>
instance()
Get the shared instance of this class, which creates new nodes using the default factory.static DynamicOps<ConfigurationNode>
instance(boolean compressed)
Get the shared instance of this class, which creates new nodes using the default factory.DataResult<ConfigurationNode>
mergeToList(ConfigurationNode input, List<ConfigurationNode> values)
Appends nodes invalues
to copy of list nodeinput
.DataResult<ConfigurationNode>
mergeToList(ConfigurationNode input, ConfigurationNode value)
Appends elementvalue
to list nodeinput
.DataResult<ConfigurationNode>
mergeToMap(ConfigurationNode input, ConfigurationNode key, ConfigurationNode value)
Update the child ofinput
atkey
withvalue
.DataResult<ConfigurationNode>
mergeToPrimitive(ConfigurationNode prefix, ConfigurationNode value)
Return a result where ifprefix
is empty, the node isvalue
, but otherwise returns an error.ConfigurationNode
remove(ConfigurationNode input, String key)
Get a copy ofinput
without the value at nodekey
.ConfigurationNode
set(ConfigurationNode input, String key, ConfigurationNode value)
Update a copy ofinput
withvalue
at pathkey
.String
toString()
ConfigurationNode
update(ConfigurationNode input, String key, Function<ConfigurationNode,ConfigurationNode> function)
Copies the input node and transform its child atkey
.ConfigurationNode
updateGeneric(ConfigurationNode input, ConfigurationNode wrappedKey, Function<ConfigurationNode,ConfigurationNode> function)
Copies the input node and transform the node atwrappedKey
.static Dynamic<ConfigurationNode>
wrap(ConfigurationNode node)
Wrap a ConfigurationNode in aDynamic
instance.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.mojang.serialization.DynamicOps
convertList, convertMap, createByte, createByteList, createDouble, createFloat, createInt, createIntList, createLong, createLongList, createShort, getBooleanValue, getByteBuffer, getIntStream, getLongStream, getMapEntries, getNumberValue, list, list, listBuilder, map, mapBuilder, mergeToMap, mergeToMap, readMap, withDecoder, withEncoder, withParser
-
-
-
-
Method Detail
-
instance
public static 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.0.0
-
instance
public static 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 thecompressed
parameter does.- Parameters:
compressed
- whether keys should be compressed in the output of this serializer- Returns:
- the shared instance
- Since:
- 4.0.0
-
forSerializers
public static 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.0.0
-
wrap
public static Dynamic<ConfigurationNode> wrap(ConfigurationNode node)
Wrap a ConfigurationNode in aDynamic
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.0.0
-
fromNode
public static 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.0.0
-
builder
public static ConfigurateOpsBuilder builder()
Create a new builder for an ops instance.- Returns:
- builder
- Since:
- 4.0.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 aKeyable
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 interfaceDynamicOps<ConfigurationNode>
- Returns:
- whether maps are compressed
-
empty
public ConfigurationNode empty()
Create a new empty node using this ops instance's factory.- Specified by:
empty
in interfaceDynamicOps<ConfigurationNode>
- Returns:
- the new node
-
emptyMap
public ConfigurationNode emptyMap()
- Specified by:
emptyMap
in interfaceDynamicOps<ConfigurationNode>
-
emptyList
public ConfigurationNode emptyList()
- Specified by:
emptyList
in interfaceDynamicOps<ConfigurationNode>
-
convertTo
public <U> U convertTo(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 interfaceDynamicOps<ConfigurationNode>
- Type Parameters:
U
- output type- Parameters:
targetOps
- output typesource
- source value- Returns:
- output value
-
getNumberValue
public 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 interfaceDynamicOps<ConfigurationNode>
- Parameters:
input
- data source- Returns:
- extracted number
-
getStringValue
public DataResult<String> getStringValue(ConfigurationNode input)
Get the value of the provided node if it is a scalar, converted to aString
.- Specified by:
getStringValue
in interfaceDynamicOps<ConfigurationNode>
- Parameters:
input
- data source- Returns:
- string | error
-
createNumeric
public ConfigurationNode createNumeric(Number value)
Create a new node using this ops instance's node factory, and set its value to the provided number.- Specified by:
createNumeric
in interfaceDynamicOps<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 interfaceDynamicOps<ConfigurationNode>
- Parameters:
value
- value- Returns:
- new node with value
-
createString
public ConfigurationNode createString(String value)
Create a new node using this ops instance's node factory, and set its value to the provided string.- Specified by:
createString
in interfaceDynamicOps<ConfigurationNode>
- Parameters:
value
- value- Returns:
- new node with value
-
mergeToPrimitive
public DataResult<ConfigurationNode> mergeToPrimitive(ConfigurationNode prefix, ConfigurationNode value)
Return a result where ifprefix
is empty, the node isvalue
, but otherwise returns an error.- Specified by:
mergeToPrimitive
in interfaceDynamicOps<ConfigurationNode>
- Parameters:
prefix
- starting valuevalue
- to update base with- Returns:
- result of updated node or error
-
mergeToList
public DataResult<ConfigurationNode> mergeToList(ConfigurationNode input, ConfigurationNode value)
Appends elementvalue
to list nodeinput
.- Specified by:
mergeToList
in interfaceDynamicOps<ConfigurationNode>
- Parameters:
input
- base node. Must be empty or of list typevalue
- value to add as element to the list- Returns:
- success with modified node, or error if
input
contains a non-list
value
-
mergeToList
public DataResult<ConfigurationNode> mergeToList(ConfigurationNode input, List<ConfigurationNode> values)
Appends nodes invalues
to copy of list nodeinput
.- Specified by:
mergeToList
in interfaceDynamicOps<ConfigurationNode>
- Parameters:
input
- base node. Must be empty or of list typevalues
- list of values to append to base node- Returns:
- success with modified node, or error if
input
contains a non-list
value
-
mergeToMap
public DataResult<ConfigurationNode> mergeToMap(ConfigurationNode input, ConfigurationNode key, ConfigurationNode value)
Update the child ofinput
atkey
withvalue
.This operation will only affect the returned copy of the input node
- Specified by:
mergeToMap
in interfaceDynamicOps<ConfigurationNode>
- Parameters:
input
- base node. Must be empty or of map typekey
- key relative to base nodevalue
- value to set at empty node- Returns:
- success with modified node, or error if
input
contains a non-list
value
-
getMapValues
public DataResult<Stream<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 interfaceDynamicOps<ConfigurationNode>
- Parameters:
input
- input node- Returns:
- result, if successful, of a stream of pairs (key, value) of entries in the input node.
-
getMap
public DataResult<MapLike<ConfigurationNode>> getMap(ConfigurationNode input)
Get a map-like view of a copy of the contents ofinput
.If the input node is non-empty and not a map, the result will be a failure.
- Specified by:
getMap
in interfaceDynamicOps<ConfigurationNode>
- Parameters:
input
- input node- Returns:
- result, if successful, of map-like view of a copy of the input
-
getList
public DataResult<Consumer<Consumer<ConfigurationNode>>> getList(ConfigurationNode input)
Get a consumer that takes an action to perform on every element of list nodeinput
.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 interfaceDynamicOps<ConfigurationNode>
- Parameters:
input
- data source- Returns:
- result, that if successful will take an action to perform on every element
-
getStream
public DataResult<Stream<ConfigurationNode>> getStream(ConfigurationNode input)
Get the contents of list nodeinput
as aStream
of nodes.- Specified by:
getStream
in interfaceDynamicOps<ConfigurationNode>
- Parameters:
input
- data source- Returns:
- if node is empty or a list, stream of nodes
-
createMap
public ConfigurationNode createMap(Stream<Pair<ConfigurationNode,ConfigurationNode>> values)
Create a new node containing the map entries from the streamvalues
.Keys will be interpreted as a single Object, and can only currently access direct children.
- Specified by:
createMap
in interfaceDynamicOps<ConfigurationNode>
- Parameters:
values
- entries in the map- Returns:
- newly created node
-
createMap
public ConfigurationNode createMap(Map<ConfigurationNode,ConfigurationNode> values)
Create a new node containing the map entries from the mapvalues
.Keys will be interpreted as a single Object, and can only currently access direct children.
- Specified by:
createMap
in interfaceDynamicOps<ConfigurationNode>
- Parameters:
values
- unwrapped node map- Returns:
- newly created node
-
createList
public ConfigurationNode createList(Stream<ConfigurationNode> input)
Create a new node containing values emitted byinput
as list elements.- Specified by:
createList
in interfaceDynamicOps<ConfigurationNode>
- Parameters:
input
- data source- Returns:
- newly created node
-
remove
public ConfigurationNode remove(ConfigurationNode input, String key)
Get a copy ofinput
without the value at nodekey
.If the input node is not a map, the input node will be returned.
- Specified by:
remove
in interfaceDynamicOps<ConfigurationNode>
- Parameters:
input
- data sourcekey
- key to the node to be removed- Returns:
- if node removed, a copy of the input without node, otherwise input
-
get
public DataResult<ConfigurationNode> get(ConfigurationNode input, String key)
Attempt to get the child ofinput
atkey
.- Specified by:
get
in interfaceDynamicOps<ConfigurationNode>
- Parameters:
input
- data sourcekey
- child key- Returns:
- success containing child if child is non-virtual, otherwise failure
-
getGeneric
public DataResult<ConfigurationNode> getGeneric(ConfigurationNode input, ConfigurationNode key)
Get a child of the provided node atkey
.Keys will be interpreted as a single Object, and can only currently access direct children.
- Specified by:
getGeneric
in interfaceDynamicOps<ConfigurationNode>
- Parameters:
input
- parent nodekey
- wrapped key of child- Returns:
- success containing child if child is non-virtual, otherwise failure
-
set
public ConfigurationNode set(ConfigurationNode input, String key, ConfigurationNode value)
Update a copy ofinput
withvalue
at pathkey
.- Specified by:
set
in interfaceDynamicOps<ConfigurationNode>
- Parameters:
input
- data sourcekey
- key of child nodevalue
- value for child node- Returns:
- updated parent node
-
update
public ConfigurationNode update(ConfigurationNode input, String key, Function<ConfigurationNode,ConfigurationNode> function)
Copies the input node and transform its child atkey
.Return a copy of the input node with the child at
key
transformed by the provided functionIf there is no value at
key
, the input node will be returned unmodified.- Specified by:
update
in interfaceDynamicOps<ConfigurationNode>
- Parameters:
input
- base valuekey
- key to changefunction
- function to process the node atwrappedKey
- Returns:
- an updated copy of input node
-
updateGeneric
public ConfigurationNode updateGeneric(ConfigurationNode input, ConfigurationNode wrappedKey, Function<ConfigurationNode,ConfigurationNode> function)
Copies the input node and transform the node atwrappedKey
.Return a copy of the input node with the child at
wrappedKey
transformed by the provided functionIf 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 interfaceDynamicOps<ConfigurationNode>
- Parameters:
input
- base valuewrappedKey
- key to changefunction
- function to process the node atwrappedKey
- Returns:
- an updated copy of input node
-
-