sack-dollarEconomy API

Overview

The Economy API provides a clean, simple interface for external plugins to interact with PhantomDungeons economy system. Handle payments, transfers, balance checks, and formatting without worrying about the underlying database operations.

Getting Started

Follow these steps to hook into the economy system safely.

1

Get the Instance

In your plugin's onEnable or initialization logic, grab the API instance.

import me.fergs.phantomdungeons.modules.economy.api.EconomyAPI;

// Get the singleton instance
EconomyAPI economyAPI = EconomyAPI.get();
circle-exclamation
2

Cache for Performance

Don't call .get() every time. Store it in your main plugin class for easy access.

public final class MyPlugin extends JavaPlugin {
    private EconomyAPI economy;
    
    @Override
    public void onEnable() {
        if (getServer().getPluginManager().getPlugin("PhantomDungeons") != null) {
            this.economy = EconomyAPI.get();
            getLogger().info("hooked.");
        }
    }
    
    public EconomyAPI getEconomy() {
        return economy;
    }
}
3

Safe Null Checks

Always ensure the economy exists before using it to prevent potential errors or bad/messy startups.

if (economy == null) {
    getLogger().warning("Economy not available!");
    return;
}

Core Operations

Checking Balances

Reading a balance is synchronous and instant because values are cached in memory. You do not need to use CompletableFuture for this.

// Get specific currency balance for a player
double balance = economy.getBalance(player, "money");

// Works with UUIDs for offline players
double souls = economy.getBalance(offlinePlayerUUID, "souls");

Adding Currency

Use this to give rewards. It automatically fires events (so boosters work) and saves asynchronously.

circle-info

Tip: Always use addBalance instead of setBalance. addBalance triggers the CurrencyGainEvent, ensuring multipliers and boosters are applied correctly.

Removing Currency

Use this for shop purchases or penalties.

Validating Funds

Before removing money, check if they can afford it.


Advanced Features

Transfers

Send currency from one player to another safely. The API handles the math and database syncing for both sides.

Formatting

Don't display raw numbers (e.g., 1000000.0). Use the formatter to get nice strings like 1M or 1,000,000.

Currency Metadata

Need to know the name of a currency dynamically?


Example: Creating a Shop Command

Here is a full example of a command that lets a player buy an item.


Events API

You can listen for when players gain currency to add custom logic (like achievements).


FAQ

chevron-rightIs this thread-safe?hashtag

Yes. Data is read from memory (safe) and writes are handled via CompletableFuture (async).

chevron-rightCan balances go negative?hashtag

Generally no. removeBalance will fail if the result is negative, unless the specific currency config allows debt.

chevron-rightHow do I create new currencies?hashtag

Just add a new YAML file in the economies/ folder of the PhantomDungeons plugin.

chevron-rightWhy use `addBalance` over `setBalance`?hashtag

setBalance overrides the value directly. addBalance calculates the difference and fires events, ensuring compatibility with boosters.

Last updated