MorePersistentDataTypes 2.4.0 API
MorePersistentDataTypes adds a ton of custom PersistentDataType
s 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 classDataType
contains all custom data types. It also includes Bukkit's already existing PersistentDataType
s. 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 asCollection
can also be read as Array and vice versa. All Collection
s are interchangeable, too. This means you
can save a ArrayList
in a PersistentDataContainer
and then later retrieve it as Array, or as Set
.
Map
s are also interchangeable - you can save a Map
and then later retrieve it as LinkedHashMap
. Map
s are not interchangeable with Collections
or Arrays, though.
Native array data types
MorePersistentDataTypes also has a few builtin native array data types, such asDataType.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 useDataType.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
Package
Description
Main package. Everything you'll need is in this package.
Specific
PersistentDataType
sGeneric
PersistentDataType
s for ConfigurationSerializable
s