MorePersistentDataTypes 2.4.0 API

MorePersistentDataTypes adds a ton of custom PersistentDataTypes that can be used in conjunction with Bukkit's PersistentDataContainer.

In the following examples, the variable pdc is any PersistentDataContainer and key is a NamespacedKey.

Basics

The class DataType contains all custom data types. It also includes Bukkit's already existing PersistentDataTypes. For example, here's you can store an ItemStack in a PersistentDataContainer:
pdc.set(key, DataType.ITEM_STACK, someItemStack);

Collections, Maps and Arrays

It also allows to use any kind of Collection, Map or Array. You can easily convert any given PersistentDataType to hold a collection, map or array instead. For example, here's how you can store a List<ItemStack>:

List<ItemStack> map = pdc.get(key, DataType.asList(DataType.ITEM_STACK));

All the collection and map data types can of course be nested infinitely. For example, a Map<String, List<EntityType>>:

PersistentDataType<?, Map<String, List<EntityType>>> dataType =
    DataType.asMap(DataType.STRING, DataType.asList(DataType.asEnum(EntityType.class)));
Map<String, List<EntityType>> map = pdc.get(key, dataType);
Or, a bit more complicated, a LinkedHashMap<String, EnumMap<Material, Set<Integer>>>:
PersistentDataType<?, LinkedHashMap<String, EnumMap<Material, Set<Integer>>>> dataType =
    DataType.asLinkedHashMap(DataType.STRING, DataType.asEnumMap(Material.class,DataType.asSet(DataType.INTEGER)));
LinkedHashMap<String, EnumMap<Material, Set<Integer>>> map = pdc.get(key, dataType);

Collection types and arrays are interchangable

Data saved as Collection can also be read as Array and vice versa. All Collections are interchangeable, too. This means you can save a ArrayList in a PersistentDataContainer and then later retrieve it as Array, or as Set.

Maps are also interchangeable - you can save a Map and then later retrieve it as LinkedHashMap. Maps are not interchangeable with Collections or Arrays, though.

Native array data types

MorePersistentDataTypes also has a few builtin native array data types, such as DataType.STRING_ARRAY. When possible, you should use the native array data types as they are a bit more performant and take up less space to store. They are however not interchangeable with generic array data types. That means that a DataType.STRING_ARRAY can not be read using DataType.asArray(DataType.STRING) and vice versa.

Enums

To store enums, you can use DataType.asEnum(Class). For example, here's how you can read an EntityType:
EntityType entityType = pdc.get(key, DataType.asEnum(EntityType.class));
As shown above, this can of course also be used in conjunction with DataType.asList(PersistentDataType), etc.

Maven repository

Check the README on GitHub for the maven dependency information.

Links