Skip to content

Data Management

darksoldier1404 edited this page Jan 28, 2026 · 1 revision

For a full list of wiki pages, see the Home page.

Data Management

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.

Core Concepts

IDataHandler

This is the base interface for all data handlers. It defines the core contract for data management, including methods for saving and loading data.

DataType Enum

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.

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

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.

Creating and Loading

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));
}

Basic Operations

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;
}

Saving Data

  • 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 by DPlugin.saveAllData().
// Save data for a single player
userData.save(player.getUniqueId());

// Save all player data (e.g., during a scheduled task)
userData.saveAll();

SingleDataContainer

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.

Creating and Loading

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));
}

Basic Operations

  • get(): Returns the YamlConfiguration (or DataCargo object).
  • 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";
}

Clone this wiki locally