Skip to content

Types Support

Since 2.0.0

LDLib2 has provided massive types support for synchronization and persistence already.

Builtin Supports

INFO

Check github to get the latest support list.

  • native types in java (number, boolean, string, enum, etc)
Type(s)PriorityRead-only
int / Integer-1-
long / Long-1-
float / Float-1-
double / Double-1-
boolean / Boolean-1-
byte / Byte-1-
short / Short-1-
char / Character-1-
String-1-
Enum<?>-1-
Number1000-
UUID100-
T[]-1depeends on T
Collection<?>-1
Map<K, V>-1

Map support

Since 2.2.8

Since 2.2.8, LDLib2 supports Map<K, V> for synchronization and persistence. Both K and V are resolved through their own accessors, so the key and value types must also be supported types.

Maps are managed as read-only containers. When both key and value accessors are direct, LDLib2 can clear and rebuild the map during deserialization. If the key or value type is read-only, the map structure must already match on both sides, or the field should use @ReadOnlyManaged to serialize and rebuild the structure.

  • types in minecraft (Block, Item, Fluid, etc)
Type(s)PriorityRead-only
Block100-
Item100-
Fluid100-
EntityType<?>100-
BlockEntityType<?>100-
BlockState100-
ResourceLocation100-
AABB1000-
BlockPos1000-
FluidStack1000-
ItemStack1000-
RecipeHolder<?>1000-
Tag2000-
Component2000-
INBTSerializable<?>2000
  • types in LDLib2 or others.
Type(s)PriorityRead-only
UIEvent100-
Position100-
Size100-
Pivot100-
Range100-
Vector3f1000-
Vector4f1000-
Vector2f1000-
Vector2i1000-
Quaternionf1000-
IGuiTexture1000-
IRenderer1000-
IResourcePath1000-
IManaged1500

Add Custom Type Support

To add a support of a new type, you need to register an IAccessor<TYPE> of this type. All types can be divided into two group, direct and read-only.

INFO

  • direct refer to the type that can be null, and there is known method to create a new instance of this type during management life cycle.
  • read-only refer to the type that can not be null and immutable during management life cycle. (e.g. INBTSerializable<?> amd Collection<?>). All modification should be done by its APIs.

You can register accessor by using AccessorRegistries.registerAccessor. In general, you can register your accessors anywhere you want, while we'd recommend to do it in the LDLibPlugin#onLoad.


Register for a direct type

You can use CustomDirectAccessor to register new type easily.

What is Mark?

Mark is a snapshot during management life cycle. LDLib2 will generate mark for current value and caompare it later to determine whether it has changes. If Mark is not defined. It will store current value as mark. It works if the type's internal values are immutable. (e.g. UUID, ResoruceLocation). Otherwise, you'd better set a way to obtain mark.

MethodOptionalNote
codecRequiredProvide a codec for persistence
streamCodecRequiredProvide a StreamCodec for synchronization
customMarkOptionalProvide functions to get and compare marks
copyMarkOptionalCopy the mark from the value. This will use the Objects#equals(Object, Object) to compare the mark. Make sure the object supports Object#equals(Object).
codecMarkOptionalThis will use the JavaOps to generate the mark based on current value.
java
AccessorRegistries.registerAccessor(CustomDirectAccessor.builder(Quaternionf.class)
    .codec(ExtraCodecs.QUATERNIONF)
    .streamCodec(ByteBufCodecs.QUATERNIONF)
    .copyMark(Quaternionf::new)
    .build());

AccessorRegistries.registerAccessor(CustomDirectAccessor.builder(ItemStack.class)
    .codec(ItemStack.OPTIONAL_CODEC)
    .streamCodec(ItemStack.OPTIONAL_STREAM_CODEC)
    .customMark(ItemStack::copy, ItemStack::matches)
    .build());

Register for a read-only type

In general, you don't really need it. Cuz, you can make your own class to inherite from INBTSerializable. If you do need it, please implement IReadOnlyAccessor&lt;TYPE&gt; and register it, check code comments for more usage details .

Released under the MIT License.