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.
importme.fergs.phantomdungeons.modules.economy.api.EconomyAPI;// Get the singleton instanceEconomyAPI economyAPI =EconomyAPI.get();
Note: This will throw an exception if PhantomDungeons isn't loaded yet. Make sure to add depend: [PhantomDungeons] to your plugin.yml.
2
Cache for Performance
Don't call .get() every time. Store it in your main plugin class for easy access.
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 playerdouble balance =economy.getBalance(player,"money");// Works with UUIDs for offline playersdouble souls =economy.getBalance(offlinePlayerUUID,"souls");
Adding Currency
Use this to give rewards. It automatically fires events (so boosters work) and saves asynchronously.
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
Is this thread-safe?
Yes. Data is read from memory (safe) and writes are handled via CompletableFuture (async).
Can balances go negative?
Generally no. removeBalance will fail if the result is negative, unless the specific currency config allows debt.
How do I create new currencies?
Just add a new YAML file in the economies/ folder of the PhantomDungeons plugin.
Why use `addBalance` over `setBalance`?
setBalance overrides the value directly. addBalance calculates the difference and fires events, ensuring compatibility with boosters.