Skip to content

Developer API Data Containers

ImIllusion edited this page May 5, 2023 · 2 revisions

Cosmos allows you to fetch/store TemplatedArea(s) in a local/remote database. To do this, you will be using the CosmosDataContainer class.

How can I obtain such a class?

To get a CosmosDataContainer, we must fetch it from the registry.

CosmosPlugin cosmos = ...;
CosmosContainerRegistry containerRegistry = cosmos.getContainerRegistry();

CosmosDataContainer defaultContainer = containerRegistry.getDefaultContainer();
CosmosDataContainer mongoContainer = containerRegistry.getContainer("mongo"); // This will return null if your mongo credentials are invalid.

How can I fetch/store a template?

Each template should be managed with an identifier, which is set when you're storing the template.

Given you have the identifier, you can just fetch the template through the container itself.

Let's say we want to read a template:

CosmosDataContainer container = ...;
String templateId = "arena";
Location pasteLocation = ...;

container.fetchTemplate(templateId).thenAccept(template -> {
    // If the template is null, it means it doesn't exist.
    if(template == null) {
        return;
    }

    template.paste(pasteLocation);
});

Now, let's say you want to store a template:

TemplatedArea template = ...;
CosmosDataContainer container = ...;
String templateId = "arena";

container.saveTemplate(templateId, template).thenRun(() -> {
    // template has been saved
});

Or, to delete a template

CosmosDataContainer container = ...;
String templateId = "arena";

container.deleteTemplate(templateId);

How would I go about creating my own containers?

If you want to add your own container implementation, so you can support some fancy database type (like Amazon S3, for example), you'll simply need to implement the CosmosDataContainer class, and handle each method individually.

If your container requires any sort of authenthication (usually the case for remote databases), make sure to define requiresCredentials(); as true, and override the enable(ConfigurationSection section) method, returning a future indicating whether the connection was successful.