Class ValueEncodingHelpers
java.lang.Object
org.cyclops.integrateddynamics.core.helper.ValueEncodingHelpers
Helpers for encoding materialized values into strings, and decoding them again.
Values are encoded as a single SNBT compound, optionally gzipped and base64-encoded behind a prefix.
Only materialized values can be encoded and decoded, as other values refer to state that is meaningless,
or even abusable, in another world.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAn error while encoding or decoding a value. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final Stringstatic final Stringstatic final StringThe versions of Minecraft and this mod that a value was encoded in.static final Stringstatic final Stringstatic final StringThe prefix that marks a gzipped and base64-encoded value, standing for Integrated Dynamics Compressed Encoding. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected static voidcheckLength(int length, int maxLength) protected static voidcheckSnbtDepth(String snbt) Check that the nesting depth of compounds and lists in the given SNBT string is within limits.protected static Stringstatic net.minecraft.nbt.CompoundTagstatic IValuedecode(ValueDeseralizationContext valueDeseralizationContext, String input, int maxLength) Decode a value from the given encoded string.static IValuedecode(ValueDeseralizationContext valueDeseralizationContext, net.minecraft.nbt.CompoundTag tag, int maxLength) Decode a value from the given encoded value.protected static Stringdecompress(String base64, int maxLength) static Stringencode(ValueDeseralizationContext valueDeseralizationContext, IValue value, int maxLength) Encode the given value into a human-readable SNBT string.static StringencodeCompressed(ValueDeseralizationContext valueDeseralizationContext, IValue value, int maxLength) Encode the given value into a compressed single-line string.static net.minecraft.nbt.CompoundTagencodeToTag(ValueDeseralizationContext valueDeseralizationContext, IValue value, int maxLength) Encode the given value into a versioned tag, as it is sent over the network.static Stringformat(net.minecraft.nbt.CompoundTag tag, boolean compressed) Format the given encoded value as a string.protected static Stringstatic net.minecraft.nbt.CompoundTagParse the given encoded string into an encoded value.protected static net.minecraft.resources.Identifierprotected static Stringstatic voidvalidateMaterialized(net.minecraft.nbt.Tag tag) Check that the given tag only holds materialized state.protected static voidvalidateMaterialized(net.minecraft.nbt.Tag tag, int depth)
-
Field Details
-
PREFIX_COMPRESSED
The prefix that marks a gzipped and base64-encoded value, standing for Integrated Dynamics Compressed Encoding.- See Also:
-
KEY_VERSION
The versions of Minecraft and this mod that a value was encoded in. These are only informative, so values are never rejected based on them.- See Also:
-
KEY_VERSION_MINECRAFT
- See Also:
-
KEY_VERSION_MOD
- See Also:
-
KEY_VALUE_TYPE
- See Also:
-
KEY_PROXY_NAME
- See Also:
-
KEY_SERIALIZER
- See Also:
-
-
Constructor Details
-
ValueEncodingHelpers
public ValueEncodingHelpers()
-
-
Method Details
-
encodeToTag
public static net.minecraft.nbt.CompoundTag encodeToTag(ValueDeseralizationContext valueDeseralizationContext, IValue value, int maxLength) throws ValueEncodingHelpers.ValueEncodingException Encode the given value into a versioned tag, as it is sent over the network.- Parameters:
valueDeseralizationContext- A value deserialization context.value- A materialized value.maxLength- The maximum length of the uncompressed encoded value.- Returns:
- The encoded value.
- Throws:
ValueEncodingHelpers.ValueEncodingException- If the value is not materialized or too large.
-
createVersionTag
public static net.minecraft.nbt.CompoundTag createVersionTag()- Returns:
- The versions of Minecraft and this mod that are running.
-
getModVersion
-
format
public static String format(net.minecraft.nbt.CompoundTag tag, boolean compressed) throws ValueEncodingHelpers.ValueEncodingException Format the given encoded value as a string.- Parameters:
tag- An encoded value.compressed- If the compressed form must be used instead of the human-readable one.- Returns:
- The encoded string.
- Throws:
ValueEncodingHelpers.ValueEncodingException- If the value could not be compressed.
-
encode
public static String encode(ValueDeseralizationContext valueDeseralizationContext, IValue value, int maxLength) throws ValueEncodingHelpers.ValueEncodingException Encode the given value into a human-readable SNBT string.- Parameters:
valueDeseralizationContext- A value deserialization context.value- A materialized value.maxLength- The maximum length of the encoded value.- Returns:
- The encoded value.
- Throws:
ValueEncodingHelpers.ValueEncodingException- If the value is not materialized or too large.
-
encodeCompressed
public static String encodeCompressed(ValueDeseralizationContext valueDeseralizationContext, IValue value, int maxLength) throws ValueEncodingHelpers.ValueEncodingException Encode the given value into a compressed single-line string.- Parameters:
valueDeseralizationContext- A value deserialization context.value- A materialized value.maxLength- The maximum length of the uncompressed encoded value.- Returns:
- The encoded value.
- Throws:
ValueEncodingHelpers.ValueEncodingException- If the value is not materialized, too large, or could not be compressed.
-
compress
-
decode
public static IValue decode(ValueDeseralizationContext valueDeseralizationContext, String input, int maxLength) throws ValueEncodingHelpers.ValueEncodingException Decode a value from the given encoded string. Both the human-readable and the compressed form are accepted.- Parameters:
valueDeseralizationContext- A value deserialization context.input- An encoded string.maxLength- The maximum length of the uncompressed encoded value.- Returns:
- The value.
- Throws:
ValueEncodingHelpers.ValueEncodingException- If the string does not hold a valid materialized value.
-
parse
public static net.minecraft.nbt.CompoundTag parse(String input, int maxLength) throws ValueEncodingHelpers.ValueEncodingException Parse the given encoded string into an encoded value. Both the human-readable and the compressed form are accepted.- Parameters:
input- An encoded string.maxLength- The maximum length of the uncompressed encoded value.- Returns:
- The encoded value.
- Throws:
ValueEncodingHelpers.ValueEncodingException- If the string is malformed or too large.
-
checkSnbtDepth
protected static void checkSnbtDepth(String snbt) throws ValueEncodingHelpers.ValueEncodingException Check that the nesting depth of compounds and lists in the given SNBT string is within limits.- Parameters:
snbt- An SNBT string.- Throws:
ValueEncodingHelpers.ValueEncodingException- If the string is nested too deeply.
-
decode
public static IValue decode(ValueDeseralizationContext valueDeseralizationContext, net.minecraft.nbt.CompoundTag tag, int maxLength) throws ValueEncodingHelpers.ValueEncodingException Decode a value from the given encoded value.- Parameters:
valueDeseralizationContext- A value deserialization context.tag- An encoded value.maxLength- The maximum length of the uncompressed encoded value.- Returns:
- The value.
- Throws:
ValueEncodingHelpers.ValueEncodingException- If the tag does not hold a valid materialized value.
-
toSnbt
protected static String toSnbt(String input, int maxLength) throws ValueEncodingHelpers.ValueEncodingException - Parameters:
input- An encoded string.maxLength- The maximum length of the uncompressed encoded value.- Returns:
- The uncompressed SNBT of the given encoded string.
- Throws:
ValueEncodingHelpers.ValueEncodingException- If the string is malformed or too large.
-
decompress
protected static String decompress(String base64, int maxLength) throws ValueEncodingHelpers.ValueEncodingException -
checkLength
protected static void checkLength(int length, int maxLength) throws ValueEncodingHelpers.ValueEncodingException -
validateMaterialized
public static void validateMaterialized(net.minecraft.nbt.Tag tag) throws ValueEncodingHelpers.ValueEncodingException Check that the given tag only holds materialized state. Non-materialized values refer to a position in a world, which would allow a handcrafted value to read blocks that the player has no access to.- Parameters:
tag- An encoded value.- Throws:
ValueEncodingHelpers.ValueEncodingException- If the tag holds anything that is not materialized.
-
validateMaterialized
protected static void validateMaterialized(net.minecraft.nbt.Tag tag, int depth) throws ValueEncodingHelpers.ValueEncodingException -
parseName
protected static net.minecraft.resources.Identifier parseName(String name) throws ValueEncodingHelpers.ValueEncodingException - Parameters:
name- A name from an encoded value, which can be anything.- Returns:
- The parsed name, which never matches a registered one if the name is malformed.
- Throws:
ValueEncodingHelpers.ValueEncodingException
-