Module net.minestom.server
Package net.minestom.server.codec
Interface Codec<T extends @UnknownNullability Object>
- Type Parameters:
T
- The type to be represented by this codec, nullable T will provide nullable results.
- All Known Subinterfaces:
DataComponent<T>
,StructCodec<R>
A Codec
represents a combined Encoder
and Decoder
for a value.
Enabling easy encoding and decoding of values to and from a between formats, making serialization simple, reusable and type safe.
Going between formats is handled by Transcoder
.
Most of the primitive or commonly used codecs are provided as static fields in this interface.
For example, INT
is a codec for integers, and STRING
is a codec for strings.
You can even use Enum(Class)
for enums, which will convert the enum to a string
representation and back.
Codec<@Nullable String> codec = Codec.STRING.optional()
Codec<Set<@Nullable String>> setCodec = codec.set();
Heavily inspired by Mojang/DataFixerUpper, licensed under the MIT license.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interface
A raw value wrapper for entry is an object combined with its current decoder. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Codec
<byte[]> static final Codec
<int[]> static final Codec
<long[]> static final Codec
<CompoundBinaryTag> static final Codec
<@UnknownNullability Codec.RawValue> -
Method Summary
Modifier and TypeMethodDescriptionCreates an Either Codec, depending on the value of Either decides which codec to use.Creates an enum codec from a given class
Converts theEnum.name()
into lowercase when encoding and uppercase into decoding then passing it toEnum.valueOf(Class, String)
static <T> Codec
<T> ForwardRef
(Supplier<Codec<T>> supplier) Lazily gets the reference of a codec; considered immutably lazy.list()
Creates an unbounded list codec.list
(int maxSize) Creates a list codec ofCodec
where its size is no larger thanmaxSize
.Returns an unbounded list or the first element or null if no such element exists.listOrSingle
(int maxSize) Returns a list or the first element or null if no such element exists.optional()
Creates an optional codec, where null is encodable intoTranscoder.createNull()
.default Codec
<@UnknownNullability T> Creates an optional codec, where null is encodable and is encoded when value equalsdefaultValue
or null throughTranscoder.createNull()
.Creates a or else codec where it will attempt to use the first codec then use the second one if it fails.static <T> Codec
<T> Create a recursive codec from the parent codec
Useful when you want to keep encoding/decoding until there is nothing left.static <T> StructCodec
<T> RegistryTaggedUnion
(Registries.Selector<StructCodec<? extends T>> registrySelector, Function<T, StructCodec<? extends T>> serializerGetter, String key) Creates aStructCodec
to bidirectionally map values ofCodec
to their encoded values
Registry selectors will be used to lookup values of codecs ofCodec
.static <T> StructCodec
<T> RegistryTaggedUnion
(Registry<StructCodec<? extends T>> registry, Function<T, StructCodec<? extends T>> serializerGetter, String key) set()
Creates an unbounded set.set
(int maxSize) Creates a set where its max is no larger thanmaxSize
default <S extends @UnknownNullability Object>
Codec<S> transform
(ThrowingFunction<T, S> to, ThrowingFunction<S, T> from) default <R> StructCodec
<R> unionType
(String keyField, Function<T, StructCodec<? extends R>> serializers, Function<R, ? extends T> keyFunc) default <R> StructCodec
<R> Creates a union type of typeCodec
.
-
Field Details
-
RAW_VALUE
-
UNIT
-
BOOLEAN
-
BYTE
-
SHORT
-
INT
-
LONG
-
FLOAT
-
DOUBLE
-
STRING
-
KEY
-
BYTE_ARRAY
-
INT_ARRAY
-
LONG_ARRAY
-
UUID
-
UUID_STRING
-
UUID_COERCED
-
COMPONENT
-
COMPONENT_STYLE
-
BLOCK_POSITION
-
VECTOR3D
-
NBT
-
NBT_COMPOUND
-
-
Method Details
-
Enum
Creates an enum codec from a given class
Converts theEnum.name()
into lowercase when encoding and uppercase into decoding then passing it toEnum.valueOf(Class, String)
- Type Parameters:
E
- Enum type, E must be an enum- Parameters:
enumClass
- the enum class- Returns:
- the codec enum
-
Recursive
Create a recursive codec from the parent codec
Useful when you want to keep encoding/decoding until there is nothing left.- Type Parameters:
T
- The codec Type- Parameters:
func
- the function to get the codec from.- Returns:
- the recursive codec
-
ForwardRef
Lazily gets the reference of a codec; considered immutably lazy.
Useful for breaking possible cyclic loading of recursive codecs. This may become a stable value in the future; don't rely on supplier getting called multiple times.- Type Parameters:
T
- the codec type- Parameters:
supplier
- the supplier to load the codec from.- Returns:
- the supplier
-
RegistryTaggedUnion
@Contract(pure=true) static <T> StructCodec<T> RegistryTaggedUnion(Registry<StructCodec<? extends T>> registry, Function<T, StructCodec<? extends T>> serializerGetter, String key) - Type Parameters:
T
- the struct codec type.- Parameters:
registry
- the codec registryserializerGetter
- the codec getterkey
- the map key- Returns:
- a
StructCodec
-
RegistryTaggedUnion
@Contract(pure=true) static <T> StructCodec<T> RegistryTaggedUnion(Registries.Selector<StructCodec<? extends T>> registrySelector, Function<T, StructCodec<? extends T>> serializerGetter, String key) Creates aStructCodec
to bidirectionally map values ofCodec
to their encoded values
Registry selectors will be used to lookup values of codecs ofCodec
. Then will be used to map to objectCodec
fromkey
- Type Parameters:
T
- the codec type- Parameters:
registrySelector
- the registery selector used during lookup.serializerGetter
- the serializer for each value ofCodec
key
- the map key forCodec
- Returns:
- a
StructCodec
bidirectionally mapping values ofCodec
-
Either
@Contract(pure=true) static <L,R> Codec<Either<L,R>> Either(Codec<L> leftCodec, Codec<R> rightCodec) Creates an Either Codec, depending on the value of Either decides which codec to use. -
optional
Creates an optional codec, where null is encodable intoTranscoder.createNull()
.- Returns:
- the optional codec of type
Codec
-
optional
Creates an optional codec, where null is encodable and is encoded when value equalsdefaultValue
or null throughTranscoder.createNull()
.
The default value will be used if the decoding is null or fails to decode.- Parameters:
defaultValue
- the default value- Returns:
- the optional codec of type
Codec
- Throws:
NullPointerException
- if defaultValue is null, useoptional()
instead.
-
transform
@Contract(pure=true) default <S extends @UnknownNullability Object> Codec<S> transform(ThrowingFunction<T, S> to, ThrowingFunction<S, T> from) -
list
Creates a list codec ofCodec
where its size is no larger thanmaxSize
.- Parameters:
maxSize
- the max size of the list before returning an error result.- Returns:
- the list codec of type
Codec
-
list
Creates an unbounded list codec. Seelist(int)
- Returns:
- the unbounded list codec of type
Codec
-
listOrSingle
Returns a list or the first element or null if no such element exists.- Parameters:
maxSize
- the max size of the list before returning an error result- Returns:
- the list codec of type
Codec
-
listOrSingle
Returns an unbounded list or the first element or null if no such element exists. SeelistOrSingle(int)
- Returns:
- the list codec of type
Codec
-
set
Creates a set where its max is no larger thanmaxSize
- Parameters:
maxSize
- the max size before returning an error result- Returns:
- the set codec of type
Codec
-
set
Creates an unbounded set. Seeset(int)
- Returns:
- the set codec of type
Codec
-
mapValue
-
mapValue
-
unionType
@Contract(pure=true) default <R> StructCodec<R> unionType(Function<T, StructCodec<? extends R>> serializers, Function<R, ? extends T> keyFunc) Creates a union type of typeCodec
. SeeunionType(String, Function, Function)
Useful when you have an interface ofCodec
and want a codec subclasses ofCodec
-
unionType
@Contract(pure=true) default <R> StructCodec<R> unionType(String keyField, Function<T, StructCodec<? extends R>> serializers, Function<R, ? extends T> keyFunc) -
orElse
Creates a or else codec where it will attempt to use the first codec then use the second one if it fails.
If both codecs fail the first error will be returned instead.- Parameters:
other
- the other codec- Returns:
- the or else codec of
Codec
-