Repository navigation
Data Management
For a full list of wiki pages, see the Home page.
DPP-Core provides a robust and flexible data management system through the IDataHandler interface and its implementations: DataContainer and SingleDataContainer. This system simplifies data persistence by handling the loading and saving of YAML files.
This is the base interface for all data handlers. It defines the core contract for data management, including methods for saving and loading data.
The DataType enum specifies the kind of data a container will manage, which enforces type safety for keys and values.
| Type | Key Type(s) | Value Type(s) | Default Path | Description |
|---|---|---|---|---|
USER |
UUID |
YamlConfiguration |
udata/ |
Designed specifically for storing per-player data, keyed by player UUID. |
YAML |
UUID or String
|
YamlConfiguration |
data/ |
A general-purpose container for storing YAML data, keyed by UUID or a name. |
CUSTOM |
UUID or String
|
DataCargo |
data/ |
For storing custom objects that implement the DataCargo interface. |
For DataType.CUSTOM, your custom objects must implement the DataCargo interface. This allows DPP-Core to serialize your object into a YamlConfiguration for saving and deserialize it back into an object when loading.
public interface DataCargo {
// Should return a YamlConfiguration
Object serialize();
// Should return an instance of your custom object
Object deserialize(YamlConfiguration data);
}DataContainer is a HashMap-based implementation of IDataHandler. It manages a collection of data files within a specific directory, where each file corresponds to an entry in the map.
You typically create and load a DataContainer in your plugin's onEnable method using the loadDataContainer method from your DPlugin class.
// In your main plugin class that extends DPlugin
private DataContainer<UUID, YamlConfiguration> userData;
@Override
public void onEnable() {
init();
// Creates a container for player data and loads all files from the 'udata' directory
userData = loadDataContainer(new DataContainer<>(this, DataType.USER));
}DataContainer extends HashMap, so you can use standard map methods like put(), get(), remove(), and containsKey().
public void setPlayerScore(Player player, int score) {
UUID uuid = player.getUniqueId();
// Get the player's data file, or create a new one if it doesn't exist
YamlConfiguration data = userData.computeIfAbsent(uuid, k -> new YamlConfiguration());
data.set("score", score);
}
public int getPlayerScore(Player player) {
YamlConfiguration data = userData.get(player.getUniqueId());
return (data != null) ? data.getInt("score", 0) : 0;
}-
save(K key): Saves the data for a specific key to its corresponding file. -
saveAll(): Saves all entries in the container to their respective files. This is automatically called byDPlugin.saveAllData().
// Save data for a single player
userData.save(player.getUniqueId());
// Save all player data (e.g., during a scheduled task)
userData.saveAll();SingleDataContainer is designed to manage a single file, rather than a directory of files. It's useful for managing global configuration or a single data entity.
Loading is similar to DataContainer, but you typically specify the key (file name) during the load process.
// In your main plugin class
private SingleDataContainer<String, YamlConfiguration> arenaConfig;
@Override
public void onEnable() {
init();
// Creates a container and loads 'arena.yml' from the 'data' directory
arenaConfig = loadDataContainer(new SingleDataContainer<>(this, DataType.YAML).load("arena", YamlConfiguration.class));
}-
get(): Returns theYamlConfiguration(orDataCargoobject). -
set(K key, V value): Sets the internal key and value. -
save(): Saves the data to its file.
public void setArenaName(String name) {
if (arenaConfig.hasData()) {
arenaConfig.get().set("name", name);
arenaConfig.save(); // Save the change immediately
}
}
public String getArenaName() {
return arenaConfig.hasData() ? arenaConfig.get().getString("name") : "Default Arena";
}