A simple, lightweight, and unified economy API library for PocketMine-MP plugins.
EcoAPI allows plugin developers to support multiple economy plugins with a single, clean interface. No need to write separate code for each economy plugin — EcoAPI handles it all.
- Features
- Supported Economy Plugins
- Comparison with Other Libraries
- Requirements
- Installation
- Quick Start
- API Reference
- Usage Examples
- Creating Custom Providers
- Example Plugin
- FAQ
- License
- Simple API — Just 4 methods:
getMoney(),setMoney(),addMoney(),takeMoney() - Auto-detect — Automatically finds the installed economy plugin
- Async-ready — Callback-based API works with both sync and async economy plugins
- Extensible — Register your own custom providers easily
- Type-safe — Full PHP 8.0+ strict typing
- Lightweight — Zero external dependencies, minimal code footprint
- PM5 Compatible — Built for PocketMine-MP 5.x
| Plugin | Provider Name | Aliases |
|---|---|---|
| EconomyAPI | economyapi |
economy |
| BedrockEconomy (v4.0+) | bedrockeconomy |
bedrock |
| SimpleEconomy | simpleeconomy |
simple |
| Built-in XP (Experience Levels) | xp |
exp, experience |
Note: You only need ONE economy plugin installed on your server. EcoAPI will auto-detect it.
| Feature | EcoAPI | libPiggyEconomy | MoneyConnector | libEco | Economizer |
|---|---|---|---|---|---|
| PM5 Support | ✅ | ✅ | ❌ | ✅ | ❌ |
| Async Support | ✅ | ✅ | ❌ | Partial | ❌ |
| Auto-detect | ✅ | ❌ | ✅ | ✅ | ❌ |
| Custom Providers | ✅ | ✅ | ❌ | ❌ | ❌ |
| EconomyAPI | ✅ | ✅ | ✅ | ✅ | ✅ |
| BedrockEconomy | ✅ | ✅ | ❌ | ✅ | ❌ |
| SimpleEconomy | ✅ | ✅ | ❌ | ❌ | ❌ |
| XP as Currency | ✅ | ✅ | ❌ | ❌ | ❌ |
setMoney() |
✅ | ✅ | ✅ | ❌ | ✅ |
| Error Handling | ✅ | ✅ | ❌ | ❌ | ❌ |
| Example Plugin | ✅ | ❌ | ✅ | ❌ | ❌ |
| Detailed README | ✅ | Partial | Partial | Partial | ❌ |
- PocketMine-MP 5.x
- PHP 8.0+
- At least one supported economy plugin installed on your server (or use XP)
Add EcoAPI as a library dependency in your .poggit.yml:
projects:
YourPlugin:
libs:
- src: NhanAZ/EcoAPI/EcoAPI
version: ^1.0.0{
"require": {
"nhanaz/ecoapi": "^1.0.0"
}
}- Download or clone this repository
- Copy the
src/NhanAZ/EcoAPIdirectory into your plugin's source
Call EcoAPI::init() once in your plugin's onEnable():
use NhanAZ\EcoAPI\EcoAPI;
public function onEnable(): void {
EcoAPI::init();
}// Auto-detect (recommended)
$provider = EcoAPI::detectProvider();
if ($provider === null) {
$this->getLogger()->error("No economy plugin found!");
return;
}
// Or by name
$provider = EcoAPI::getProvider("economyapi");// Get balance
$provider->getMoney($player, function(float|int $balance): void {
echo "Balance: $balance";
});
// Add money
$provider->addMoney($player, 1000.0, function(bool $success): void {
echo $success ? "Added!" : "Failed!";
});
// Take money
$provider->takeMoney($player, 500.0, function(bool $success): void {
echo $success ? "Taken!" : "Failed!";
});
// Set balance
$provider->setMoney($player, 5000.0, function(bool $success): void {
echo $success ? "Set!" : "Failed!";
});That's it! Your plugin now supports all economy plugins automatically.
NhanAZ\EcoAPI\EcoAPI — Static utility class. All methods are static.
| Method | Description | Returns |
|---|---|---|
init() |
Initialize with built-in providers. Call once in onEnable(). |
void |
detectProvider() |
Auto-detect first available economy plugin. | ?EconomyProvider |
getProvider(string $name) |
Get provider by name (case-insensitive). | EconomyProvider |
isAvailable() |
Check if any economy plugin is installed. | bool |
getAvailableProviders() |
List names of installed economy plugins. | string[] |
getRegisteredProviders() |
List all registered provider names. | string[] |
registerProvider(string $class, string ...$names) |
Register a custom provider. | void |
NhanAZ\EcoAPI\EconomyProvider — Interface implemented by all providers.
| Method | Description | Callback |
|---|---|---|
getName(): string |
Provider display name (e.g., "EconomyAPI") | — |
getCurrencySymbol(): string |
Currency symbol (e.g., "$") | — |
isAvailable(): bool |
Is the economy plugin installed? | — |
getMoney(Player, Closure) |
Get player balance | function(float|int $balance): void |
setMoney(Player, float, ?Closure) |
Set player balance | function(bool $success): void |
addMoney(Player, float, ?Closure) |
Add to player balance | function(bool $success): void |
takeMoney(Player, float, ?Closure) |
Take from player balance | function(bool $success): void |
| Exception | When |
|---|---|
UnknownProviderException |
Provider name is not registered (typo?) |
MissingProviderDependencyException |
Provider exists but economy plugin is not installed |
Both exceptions extend RuntimeException.
The simplest way — let EcoAPI find whatever economy plugin is installed:
use NhanAZ\EcoAPI\EcoAPI;
use NhanAZ\EcoAPI\EconomyProvider;
use pocketmine\event\Listener;
use pocketmine\event\player\PlayerJoinEvent;
use pocketmine\plugin\PluginBase;
class MyPlugin extends PluginBase implements Listener {
private EconomyProvider $economy;
public function onEnable(): void {
EcoAPI::init();
$provider = EcoAPI::detectProvider();
if ($provider === null) {
$this->getLogger()->error("No economy plugin found!");
$this->getServer()->getPluginManager()->disablePlugin($this);
return;
}
$this->economy = $provider;
$this->getLogger()->info("Using: " . $this->economy->getName());
$this->getServer()->getPluginManager()->registerEvents($this, $this);
}
public function onJoin(PlayerJoinEvent $event): void {
$player = $event->getPlayer();
$this->economy->getMoney($player, function(float|int $balance) use ($player): void {
$symbol = $this->economy->getCurrencySymbol();
$player->sendMessage("§aYour balance: {$symbol}{$balance}");
});
}
}Let server owners choose their preferred economy plugin in config:
config.yml:
# Options: auto, economyapi, bedrockeconomy, simpleeconomy, xp
economy-provider: autoPlugin:
use NhanAZ\EcoAPI\EcoAPI;
use NhanAZ\EcoAPI\EconomyProvider;
class MyPlugin extends PluginBase {
private EconomyProvider $economy;
public function onEnable(): void {
$this->saveDefaultConfig();
EcoAPI::init();
$providerName = $this->getConfig()->get("economy-provider", "auto");
if ($providerName === "auto") {
$provider = EcoAPI::detectProvider();
if ($provider === null) {
$this->getLogger()->error("No economy plugin found!");
$this->getServer()->getPluginManager()->disablePlugin($this);
return;
}
$this->economy = $provider;
} else {
try {
$this->economy = EcoAPI::getProvider($providerName);
} catch (\Exception $e) {
$this->getLogger()->error($e->getMessage());
$this->getServer()->getPluginManager()->disablePlugin($this);
return;
}
}
$this->getLogger()->info("Economy: " . $this->economy->getName());
}
}public function buyItem(Player $player, string $itemName, int $price): void {
$this->economy->getMoney($player, function(float|int $balance) use ($player, $itemName, $price): void {
$symbol = $this->economy->getCurrencySymbol();
if ($balance < $price) {
$player->sendMessage("§cNot enough money! You need {$symbol}{$price} but have {$symbol}{$balance}.");
return;
}
$this->economy->takeMoney($player, (float) $price, function(bool $success) use ($player, $itemName, $symbol, $price): void {
if ($success) {
// Give the item to the player here
$player->sendMessage("§aBought {$itemName} for {$symbol}{$price}!");
} else {
$player->sendMessage("§cPurchase failed. Please try again.");
}
});
});
}public function pay(Player $sender, Player $receiver, float $amount): void {
$symbol = $this->economy->getCurrencySymbol();
$this->economy->takeMoney($sender, $amount, function(bool $taken) use ($sender, $receiver, $amount, $symbol): void {
if (!$taken) {
$sender->sendMessage("§cPayment failed! Do you have enough money?");
return;
}
$this->economy->addMoney($receiver, $amount, function(bool $added) use ($sender, $receiver, $amount, $symbol): void {
if ($added) {
$sender->sendMessage("§aSent {$symbol}{$amount} to " . $receiver->getName());
$receiver->sendMessage("§aReceived {$symbol}{$amount} from " . $sender->getName());
} else {
// Refund the sender if adding to receiver failed
$this->economy->addMoney($sender, $amount);
$sender->sendMessage("§cPayment failed. Your money has been refunded.");
}
});
});
}public function canAfford(Player $player, float $cost, Closure $callback): void {
$this->economy->getMoney($player, function(float|int $balance) use ($cost, $callback): void {
$callback($balance >= $cost);
});
}
// Usage:
$this->canAfford($player, 500.0, function(bool $canAfford) use ($player): void {
if ($canAfford) {
$player->sendMessage("§aYou can afford it!");
} else {
$player->sendMessage("§cYou can't afford it.");
}
});You can add support for any economy plugin by implementing the EconomyProvider interface:
use NhanAZ\EcoAPI\EcoAPI;
use NhanAZ\EcoAPI\EconomyProvider;
use Closure;
use pocketmine\player\Player;
use pocketmine\Server;
class MyCustomProvider implements EconomyProvider {
public function getName(): string {
return "MyEconomyPlugin";
}
public function getCurrencySymbol(): string {
return "G";
}
public static function isAvailable(): bool {
return Server::getInstance()->getPluginManager()->getPlugin("MyEconomyPlugin") !== null;
}
public function getMoney(Player $player, Closure $callback): void {
// Replace with your economy plugin's API
$balance = MyEconomyPlugin::getInstance()->getBalance($player->getName());
$callback((float) $balance);
}
public function setMoney(Player $player, float $amount, ?Closure $callback = null): void {
$success = MyEconomyPlugin::getInstance()->setBalance($player->getName(), $amount);
if ($callback !== null) {
$callback($success);
}
}
public function addMoney(Player $player, float $amount, ?Closure $callback = null): void {
$success = MyEconomyPlugin::getInstance()->deposit($player->getName(), $amount);
if ($callback !== null) {
$callback($success);
}
}
public function takeMoney(Player $player, float $amount, ?Closure $callback = null): void {
$success = MyEconomyPlugin::getInstance()->withdraw($player->getName(), $amount);
if ($callback !== null) {
$callback($success);
}
}
}Then register it in your plugin's onEnable():
EcoAPI::init();
EcoAPI::registerProvider(MyCustomProvider::class, "myeconomyplugin", "myeco");
// Now you can use it:
$provider = EcoAPI::getProvider("myeconomyplugin");A complete working example plugin is included in the example/ directory.
It demonstrates:
- Initializing EcoAPI
- Auto-detecting the economy plugin
- Getting player balance on join
- A
/balancecommand
To use it:
- Build the example plugin with Poggit CI
- Install it along with any supported economy plugin
- Join the server — you'll see your balance on join
- Use
/balanceto check your balance anytime
A: No! You only need ONE economy plugin installed on your server. EcoAPI will auto-detect it. If no economy plugin is installed, you can still use XP (experience levels) as currency.
A: EconomyAPI is the most popular and widely used. BedrockEconomy is a modern alternative with async database support.
A: EcoAPI::detectProvider() returns null. EcoAPI::getProvider() throws a MissingProviderDependencyException. Always handle these cases in your plugin!
A: Yes! The XP provider uses player experience levels as currency and requires no additional plugin. Just use EcoAPI::getProvider("xp").
A: Some economy plugins (like BedrockEconomy) use asynchronous database operations. Callbacks ensure compatibility with both sync and async plugins. For sync plugins like EconomyAPI, the callback is invoked immediately — so there's no delay.
A: For getMoney(), the callback is required (you always want the balance). For setMoney(), addMoney(), and takeMoney(), the callback is optional — pass null if you don't need to know whether it succeeded.
A: Yes! init() only runs once. Subsequent calls are silently ignored.
A: Capital uses complex async generators and a DI system. You can create a custom provider for it using the Creating Custom Providers section. Note that bridging Capital's generator-based API with callbacks requires the await-generator library.
A: Yes! EcoAPI follows all Poggit rules:
- Uses virion format (rule A6)
- Unique namespace
NhanAZ\EcoAPI(rule C1a) - All classes under the namespace (rule C1b)
- Apache 2.0 license included (rule D6)
- No obfuscation (rule B2)
- No startup messages (rule B3)
This project is licensed under the Apache License 2.0 — see the LICENSE file for details.