Interface ISerializer<T>
- Type Parameters:
T- The data type that is handled by the serializer.
- All Known Implementing Classes:
SerializerAttributeModifier,SerializerBlockPos,SerializerBlockState,SerializerBoolean,SerializerByte,SerializerCodec,SerializerCompoundTag,SerializerDouble,SerializerEffectInstance,SerializerEnchantmentInstance,SerializerEnum,SerializerFloat,SerializerIngredient,SerializerInteger,SerializerItemStack,SerializerLong,SerializerRegistryEntry,SerializerResourceLocation,SerializerShort,SerializerString,SerializerTagKey,SerializerText,SerializerUUID,SerializerVector3f,SerializerVector4f,Sound.Serializer
public interface ISerializer<T>
Provides a structure for serializing common data types into the various data exchange formats used in the game. These
currently support JSON, NBT, and byte buffers (networking).
-
Method Summary
Modifier and TypeMethodDescriptionfromByteBuf(net.minecraft.network.FriendlyByteBuf buffer) Reads a value from a byte buffer.fromByteBufList(net.minecraft.network.FriendlyByteBuf buffer) Reads a list of values from a byte buffer.default TfromByteBufNullable(net.minecraft.network.FriendlyByteBuf buffer) fromByteBufOptional(net.minecraft.network.FriendlyByteBuf buffer) Read an optional value from a byte buffer.default net.minecraft.util.random.WeightedEntry.Wrapper<T>fromByteBufWeighted(net.minecraft.network.FriendlyByteBuf buffer) Reads a weighted wrapped value from a byte buffer.default net.minecraft.util.random.SimpleWeightedRandomList<T>fromByteBufWeightedList(net.minecraft.network.FriendlyByteBuf buffer) Reads a list of weighted wrapped entries from a byte buffer.fromJSON(com.google.gson.JsonElement json) Reads the value from a specific JSON element.default TReads a value from a member on a parent JSON object.default TReads a value from a member on a parent JSON object.default TReads a value from a member on a parent JSON object.fromJSONList(com.google.gson.JsonElement json) Reads a list of values from JSON.fromJSONList(com.google.gson.JsonObject json, String memberName) Reads a list of values from a child element on a JSON object.fromJSONList(com.google.gson.JsonObject json, String memberName, Supplier<List<T>> fallback) Reads a list of values from a child element on a JSON object.fromJSONList(com.google.gson.JsonObject json, String memberName, List<T> fallback) Reads a list of values from a child element on a JSON object.default TfromJSONNullable(com.google.gson.JsonElement json) default TfromJSONNullable(com.google.gson.JsonObject json, String memberName) fromJSONOptional(com.google.gson.JsonElement json) Reads an optional value from a JSON element.fromJSONOptional(com.google.gson.JsonObject json, String memberName) Read an optional value from a child JSON element.fromJSONSet(com.google.gson.JsonElement json) Reads a set of values from a JSON element.fromJSONSet(com.google.gson.JsonObject json, String memberName) Reads a set of values from a child member from the parent JSON element.fromJSONSet(com.google.gson.JsonObject json, String memberName, Supplier<Set<T>> fallback) Reads a set of values from a child member from the parent JSON element.fromJSONSet(com.google.gson.JsonObject json, String memberName, Set<T> fallback) Reads a set of values from a child member from the parent JSON element.default TfromJSONString(String jsonString) Reads the value from a JSON string.default net.minecraft.util.random.WeightedEntry.Wrapper<T>fromJSONWeighted(com.google.gson.JsonElement element) Reads a wrapped weighted entry from a JSON element.default net.minecraft.util.random.SimpleWeightedRandomList<T>fromJSONWeightedList(com.google.gson.JsonElement json) Reads a list of weighted wrapped values from the json.default Tdefault TfromNBT(net.minecraft.nbt.Tag nbt) Reads a value from a named binary tag (NBT).fromNBTList(net.minecraft.nbt.CompoundTag tag, String name) fromNBTList(net.minecraft.nbt.Tag toRead) readByteBufSet(net.minecraft.network.FriendlyByteBuf buffer) Reads a set of values from a byte buffer.voidWrites a value to a byte buffer.default voidtoByteBufList(net.minecraft.network.FriendlyByteBuf buffer, List<T> toWrite) Writes a list of values to a byte buffer.default voidtoByteBufNullable(net.minecraft.network.FriendlyByteBuf buffer, T value) default voidtoByteBufOptional(net.minecraft.network.FriendlyByteBuf buffer, Optional<T> optional) Writes an optional value to the byte buffer.default voidtoByteBufWeighted(net.minecraft.network.FriendlyByteBuf buffer, net.minecraft.util.random.WeightedEntry.Wrapper<T> toWrite) Writes a weighted wrapped value to a byte buffer.default voidtoByteBufWeightedList(net.minecraft.network.FriendlyByteBuf buffer, net.minecraft.util.random.SimpleWeightedRandomList<T> list) Writes a list of weighted wrapped entries to a byte buffer.default voidcom.google.gson.JsonElementWrites a value to a JSON element.default voidtoJSONList(com.google.gson.JsonObject json, String memberName, List<T> toWrite) Writes a list of values to a JSON object, if the list is null or empty it will be ignored.default com.google.gson.JsonElementtoJSONList(List<T> toWrite) Writes a list of values to a JSON element.default voidtoJSONNullable(com.google.gson.JsonObject json, String memberName, T value) default com.google.gson.JsonElementtoJSONNullable(T value) default voidtoJSONOptional(com.google.gson.JsonObject json, String memberName, Optional<T> value) Write an optional value to a JSON object.default com.google.gson.JsonElementtoJSONOptional(Optional<T> value) Writes an optional value to a JSON element.default com.google.gson.JsonElementtoJSONOptional(T value) Writes an optional value to a JSON element.default com.google.gson.JsonElementWrites a set of values to a JSON element.default StringtoJSONString(T toWrite) Writes the value to a string representation of a JSON element.default com.google.gson.JsonElementtoJSONWeighted(net.minecraft.util.random.WeightedEntry.Wrapper<T> weightedEntry) Writes a weighted entry to a JSON element.default com.google.gson.JsonElementtoJSONWeightedList(net.minecraft.util.random.SimpleWeightedRandomList<T> list) Writes a list of weighted wrapped entries to JSON.default voidnet.minecraft.nbt.TagWrites a value to a named binary tag (NBT).default net.minecraft.nbt.ListTagdefault voiddefault voidwriteByteBufSet(net.minecraft.network.FriendlyByteBuf buffer, Set<T> toWrite) Writes a set of values from a byte buffer.
-
Method Details
-
fromJSON
Reads the value from a specific JSON element.- Parameters:
json- The JSON element to read data from.- Returns:
- The value that was read.
-
fromJSON
Reads a value from a member on a parent JSON object.- Parameters:
json- The parent JSON object to read from.memberName- The name of the child element to read from.- Returns:
- The value that was read.
-
fromJSON
Reads a value from a member on a parent JSON object.- Parameters:
json- The parent JSON object to read from.memberName- The name of the child element to read from.fallback- A fallback value used when the child element does not exist.- Returns:
- The value that was read or the fallback value.
-
fromJSON
Reads a value from a member on a parent JSON object.- Parameters:
json- The parent JSON object to read from.memberName- The name of the child element to read from.fallback- A fallback value used when the child element does not exist.- Returns:
- The value that was read or the fallback value.
-
toJSON
-
toJSON
Writes a value to a JSON element.- Parameters:
toWrite- The object to serialize into JSON data.- Returns:
- The serialized JSON element.
-
fromByteBuf
Reads a value from a byte buffer. This is commonly used for networking.- Parameters:
buffer- The byte buffer to read from.- Returns:
- The value that was read.
-
toByteBuf
Writes a value to a byte buffer. This is commonly used for networking.- Parameters:
buffer- The byte buffer to write to.toWrite- The value to write.
-
toNBT
Writes a value to a named binary tag (NBT).- Parameters:
toWrite- The value to write.- Returns:
- The serialized NBT data.
-
fromNBT
Reads a value from a named binary tag (NBT).- Parameters:
nbt- The tag to read data from.- Returns:
- The value that was read from the data.
-
fromNBT
-
fromNBT
-
toNBT
-
toNBTList
-
toNBTList
-
fromNBTList
-
fromNBTList
-
fromJSONList
Reads a list of values from JSON. When given a JSON array each entry is added to the list. When given a JSON object it will be treated as a list of one element.- Parameters:
json- The JSON element to read from.- Returns:
- A list of values read from the JSON.
-
fromJSONList
Reads a list of values from a child element on a JSON object. If the child element is an array each entry will be added to the list. If the child element is an object it will be treated as a list of one.- Parameters:
json- The parent JSON element.memberName- The member of the element to read from.- Returns:
- The list of values read from the JSON.
-
fromJSONList
Reads a list of values from a child element on a JSON object. If the child element is an array each entry will be added to the list. If the child element is an object it will be treated as a list of one.- Parameters:
json- The parent JSON element.memberName- The member of the element to read from.fallback- A list of fallback values to use when the child member is not present.- Returns:
- The list of values read from the JSON.
-
fromJSONList
default List<T> fromJSONList(com.google.gson.JsonObject json, String memberName, Supplier<List<T>> fallback) Reads a list of values from a child element on a JSON object. If the child element is an array each entry will be added to the list. If the child element is an object it will be treated as a list of one.- Parameters:
json- The parent JSON element.memberName- The member of the element to read from.fallback- A list of fallback values to use when the child member is not present.- Returns:
- The list of values read from the JSON.
-
toJSONList
Writes a list of values to a JSON element.- Parameters:
toWrite- The values to write.- Returns:
- The serialized JSON element.
-
toJSONList
default void toJSONList(com.google.gson.JsonObject json, String memberName, @Nullable List<T> toWrite) Writes a list of values to a JSON object, if the list is null or empty it will be ignored.- Parameters:
json- The JSON object to add the list to.memberName- The name to assign the list member.toWrite- The list to write.
-
fromByteBufList
Reads a list of values from a byte buffer.- Parameters:
buffer- The buffer to read data from.- Returns:
- The list of values read from the buffer.
-
toByteBufList
Writes a list of values to a byte buffer.- Parameters:
buffer- The buffer to write to.toWrite- The value to write.
-
fromJSONSet
Reads a set of values from a JSON element. A JSON array will have each element added to the set. A JSON object will be treated as a set of one element.- Parameters:
json- The JSON element to read from.- Returns:
- The set of values that were read.
-
fromJSONSet
Reads a set of values from a child member from the parent JSON element.- Parameters:
json- The parent JSON element.memberName- The name of the child member.- Returns:
- The set of values that were read.
-
fromJSONSet
Reads a set of values from a child member from the parent JSON element. If the member does not exist the fallback set will be used.- Parameters:
json- The parent JSON element.memberName- The name of the child member.fallback- The fallback set to use when the member is not present.- Returns:
- The set of values that were read.
-
fromJSONSet
default Set<T> fromJSONSet(com.google.gson.JsonObject json, String memberName, Supplier<Set<T>> fallback) Reads a set of values from a child member from the parent JSON element. If the member does not exist the fallback set will be used.- Parameters:
json- The parent JSON element.memberName- The name of the child member.fallback- The fallback set to use when the member is not present.- Returns:
- The set of values that were read.
-
toJSONSet
Writes a set of values to a JSON element.- Parameters:
toWrite- The set of values to write.- Returns:
- The JSON value that was written.
-
readByteBufSet
Reads a set of values from a byte buffer.- Parameters:
buffer- The byte buffer to read from.- Returns:
- The set of values that were read from the buffer.
-
writeByteBufSet
Writes a set of values from a byte buffer.- Parameters:
buffer- The byte buffer to write to.toWrite- The set of values to write.
-
fromJSONOptional
Read an optional value from a child JSON element. If the child JSON member does not exist an empty optional will be used.- Parameters:
json- The parent JSON element to read from.memberName- The name of the child member to read.- Returns:
- An optional containing the value that was read.
-
fromJSONOptional
Reads an optional value from a JSON element. If the element is null an empty optional will be returned.- Parameters:
json- The JSON to read data from.- Returns:
- An optional containing the value that was read.
-
toJSONOptional
Writes an optional value to a JSON element. If the value is not present a null value will be returned.- Parameters:
value- The optional value to write.- Returns:
- The written JSON element. If the optional value was not present this will be null.
-
toJSONOptional
Writes an optional value to a JSON element. If the value is not present a null value will be returned.- Parameters:
value- The optional value to write.- Returns:
- The written JSON element. If the optional value was not present this will be null.
-
toJSONOptional
Write an optional value to a JSON object.- Parameters:
json- The JSON object to write to.memberName- The name of the member to write the value to.value- The value to write.
-
fromByteBufOptional
Read an optional value from a byte buffer.- Parameters:
buffer- The buffer to read from.- Returns:
- An optional containing the value that was read.
-
toByteBufOptional
Writes an optional value to the byte buffer.- Parameters:
buffer- The buffer to write to.optional- The optional value to write.
-
toJSONString
Writes the value to a string representation of a JSON element.- Parameters:
toWrite- The value to write.- Returns:
- A string representation of the element when written to JSON.
-
fromJSONString
Reads the value from a JSON string.- Parameters:
jsonString- A string representation of the JSON element to read data from.- Returns:
- The value read from the JSON data.
-
fromJSONWeighted
default net.minecraft.util.random.WeightedEntry.Wrapper<T> fromJSONWeighted(com.google.gson.JsonElement element) Reads a wrapped weighted entry from a JSON element. The weight is taken from a JSON Object property named "weight". If the JSON is not an Object or the property does not exist a weight of 0 will be used.- Parameters:
element- The JSON element to read from.- Returns:
- The weighted wrapper.
-
toJSONWeighted
default com.google.gson.JsonElement toJSONWeighted(net.minecraft.util.random.WeightedEntry.Wrapper<T> weightedEntry) Writes a weighted entry to a JSON element. The value and weight are stored as separate properties.- Parameters:
weightedEntry- The weighted entry to write.- Returns:
- The serialized JSON.
-
toByteBufWeighted
default void toByteBufWeighted(net.minecraft.network.FriendlyByteBuf buffer, net.minecraft.util.random.WeightedEntry.Wrapper<T> toWrite) Writes a weighted wrapped value to a byte buffer.- Parameters:
buffer- The buffer to write to.toWrite- The data to write.
-
fromByteBufWeighted
default net.minecraft.util.random.WeightedEntry.Wrapper<T> fromByteBufWeighted(net.minecraft.network.FriendlyByteBuf buffer) Reads a weighted wrapped value from a byte buffer.- Parameters:
buffer- The buffer to read from.- Returns:
- The value that was read from the buffer.
-
fromJSONWeightedList
default net.minecraft.util.random.SimpleWeightedRandomList<T> fromJSONWeightedList(com.google.gson.JsonElement json) Reads a list of weighted wrapped values from the json.- Parameters:
json- The JSON data to read from.- Returns:
- A list of weighted wrapped values.
-
toJSONWeightedList
default com.google.gson.JsonElement toJSONWeightedList(net.minecraft.util.random.SimpleWeightedRandomList<T> list) Writes a list of weighted wrapped entries to JSON.- Parameters:
list- The list to write.- Returns:
- The JSON output.
-
fromByteBufWeightedList
default net.minecraft.util.random.SimpleWeightedRandomList<T> fromByteBufWeightedList(net.minecraft.network.FriendlyByteBuf buffer) Reads a list of weighted wrapped entries from a byte buffer.- Parameters:
buffer- The buffer to read from.- Returns:
- The list of weighted wrapped entries.
-
toByteBufWeightedList
default void toByteBufWeightedList(net.minecraft.network.FriendlyByteBuf buffer, net.minecraft.util.random.SimpleWeightedRandomList<T> list) Writes a list of weighted wrapped entries to a byte buffer.- Parameters:
buffer- The buffer to write to.list- The list of entries to write.
-
fromJSONNullable
-
fromJSONNullable
-
toJSONNullable
-
toJSONNullable
-
fromByteBufNullable
-
toByteBufNullable
-