getAllCurrencySettings();
}
```
---
## Code Examples
### Check if a Player Can Afford Something
```java
public boolean canAfford(Player player, double price) {
return economyAPI.hasBalance(player.getUniqueId(), CurrencyType.MONEY, price);
}
```
### Deduct Money for a Purchase
```java
public boolean processPurchase(Player player, double price) {
UUID uuid = player.getUniqueId();
if (!economyAPI.hasBalance(uuid, CurrencyType.MONEY, price)) {
player.sendMessage(ChatColor.RED + "You can't afford this!");
return false;
}
boolean success = economyAPI.removeCurrency(uuid, CurrencyType.MONEY, price);
if (success) {
player.sendMessage(ChatColor.GREEN + "Purchase complete.");
}
return success;
}
```
### Give MobCoins as a Reward
```java
public void rewardMobCoins(Player player, int amount) {
economyAPI.addCurrency(player.getUniqueId(), CurrencyType.MOBCOIN, amount);
player.sendMessage(ChatColor.GREEN + "+" + amount + " MobCoins!");
}
```
### Transfer Between Players
```java
public boolean payPlayer(Player sender, Player receiver, double amount) {
boolean success = economyAPI.transferCurrency(
sender.getUniqueId(),
receiver.getUniqueId(),
CurrencyType.MONEY,
amount
);
if (success) {
sender.sendMessage(ChatColor.GREEN + "Sent $" + amount + " to " + receiver.getName());
} else {
sender.sendMessage(ChatColor.RED + "Transfer failed. Check the balance.");
}
return success;
}
```
### Get a Player's Rank
```java
public void showRankInfo(Player player) {
Rank rank = economyAPI.getPlayerRank(player.getUniqueId());
if (rank != null) {
player.sendMessage("Your rank: " + rank.getDisplayName());
player.sendMessage("Money multiplier: " + rank.getMultiplier("money"));
}
}
```
### Convert Currencies
```java
public void convertMoneyToGems(Player player, double moneyAmount) {
double rate = economyAPI.getConversionRate(CurrencyType.MONEY, CurrencyType.GEM);
boolean success = economyAPI.convertCurrency(
player.getUniqueId(),
CurrencyType.MONEY,
CurrencyType.GEM,
moneyAmount
);
if (success) {
player.sendMessage("Converted $" + moneyAmount + " to " + (moneyAmount * rate) + " Gems!");
} else {
player.sendMessage("Conversion failed. Check your balance.");
}
}
```
### Format Currency
```java
public void showBalance(Player player) {
double money = economyAPI.getBalance(player.getUniqueId(), CurrencyType.MONEY);
player.sendMessage("Balance: " + economyAPI.formatCurrency(money, CurrencyType.MONEY));
player.sendMessage("Short: " + economyAPI.formatCurrencyShort(money));
}
```
---
## Best Practices
### Always Check for Null
```java
DZEconomy dzeconomy = (DZEconomy) Bukkit.getPluginManager().getPlugin("DZEconomy");
if (dzeconomy == null) {
return;
}
DZEconomyAPI api = dzeconomy.getAPI();
```
### Prefer Soft Dependencies
```yaml
softdepend: [DZEconomy]
```
Your plugin keeps working when DZEconomy is absent, just without economy features.
### Validate User Input with fromString
```java
CurrencyType type = CurrencyType.fromString(userInput);
if (type == null) {
sender.sendMessage("Unknown currency type!");
return;
}
```
### Mind the Thread
Balance methods are thread-safe. Bukkit calls like `player.sendMessage()` are not, so schedule them on the right thread. On Folia, use the entity or region schedulers; on regular Bukkit, use the main thread.
---
## API Versioning
| API Version | Plugin Version | Notes |
|-------------|----------------|-------|
| 2 | 2.0.0 | Initial public API |
Check it at runtime if you rely on newer features:
```java
int version = economyAPI.getAPIVersion();
if (version < 2) {
getLogger().warning("Unsupported DZEconomy API version: " + version);
}
```
---
See the GitHub repository for the latest API source code.
---
### Quick Links
[**DZEconomy GitHub**](https://github.com/DemonZ-Development/DZEconomy) • [**Discord Support**](https://discord.com/invite/GYsTt96ypf) • [**Wiki Home**](https://github.com/DemonZ-Development/DZEconomy/wiki/Home)
*Developed by **[DemonZ Development](https://github.com/DemonZ-Development)***