update
This commit is contained in:
@@ -10,89 +10,46 @@ import java.util.UUID;
|
||||
import java.util.logging.Level;
|
||||
|
||||
/**
|
||||
* Manages economy operations with support for Vault and EssentialsX fallback.
|
||||
* Manages economy operations using Vault.
|
||||
* <p>
|
||||
* Priority: Vault > EssentialsX
|
||||
* If neither is available, the plugin will disable itself.
|
||||
* Vault provides a unified economy API that works with many economy plugins
|
||||
* including EssentialsX, CMI, and others.
|
||||
*/
|
||||
public class EconomyManager {
|
||||
|
||||
private final CommunityMarket plugin;
|
||||
private Economy vaultEconomy;
|
||||
private com.earth2me.essentials.Essentials essentials;
|
||||
private EconomyProvider provider = EconomyProvider.NONE;
|
||||
|
||||
public enum EconomyProvider {
|
||||
VAULT,
|
||||
ESSENTIALS,
|
||||
NONE
|
||||
}
|
||||
|
||||
public EconomyManager(CommunityMarket plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to set up an economy provider.
|
||||
* Tries Vault first, then EssentialsX.
|
||||
* Attempts to set up the Vault economy provider.
|
||||
*
|
||||
* @return true if an economy provider was found
|
||||
* @return true if Vault economy was found
|
||||
*/
|
||||
public boolean setupEconomy() {
|
||||
// Try Vault first
|
||||
if (setupVault()) {
|
||||
provider = EconomyProvider.VAULT;
|
||||
plugin.getLogger().info("Using Vault as economy provider.");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Fallback to EssentialsX
|
||||
if (setupEssentials()) {
|
||||
provider = EconomyProvider.ESSENTIALS;
|
||||
plugin.getLogger().info("Using EssentialsX as economy provider.");
|
||||
return true;
|
||||
}
|
||||
|
||||
plugin.getLogger().severe("No economy provider found!");
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to hook into Vault economy
|
||||
*/
|
||||
private boolean setupVault() {
|
||||
if (Bukkit.getPluginManager().getPlugin("Vault") == null) {
|
||||
plugin.getLogger().severe("Vault plugin not found!");
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
RegisteredServiceProvider<Economy> rsp = Bukkit.getServicesManager().getRegistration(Economy.class);
|
||||
if (rsp == null) {
|
||||
plugin.getLogger().warning("Vault found but no economy provider registered.");
|
||||
plugin.getLogger().severe("No economy provider registered with Vault! Please install an economy plugin like EssentialsX.");
|
||||
return false;
|
||||
}
|
||||
|
||||
vaultEconomy = rsp.getProvider();
|
||||
return vaultEconomy != null;
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().log(Level.WARNING, "Failed to hook into Vault", e);
|
||||
if (vaultEconomy != null) {
|
||||
plugin.getLogger().info("Using Vault economy provider: " + vaultEconomy.getName());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to hook into EssentialsX economy
|
||||
*/
|
||||
private boolean setupEssentials() {
|
||||
if (Bukkit.getPluginManager().getPlugin("Essentials") == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
essentials = (com.earth2me.essentials.Essentials) Bukkit.getPluginManager().getPlugin("Essentials");
|
||||
return essentials != null && essentials.isEnabled();
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().log(Level.WARNING, "Failed to hook into EssentialsX", e);
|
||||
plugin.getLogger().log(Level.SEVERE, "Failed to hook into Vault economy", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -101,11 +58,7 @@ public class EconomyManager {
|
||||
* Gets the name of the active economy provider
|
||||
*/
|
||||
public String getProviderName() {
|
||||
return switch (provider) {
|
||||
case VAULT -> "Vault (" + (vaultEconomy != null ? vaultEconomy.getName() : "Unknown") + ")";
|
||||
case ESSENTIALS -> "EssentialsX";
|
||||
case NONE -> "None";
|
||||
};
|
||||
return vaultEconomy != null ? "Vault (" + vaultEconomy.getName() + ")" : "None";
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -115,20 +68,9 @@ public class EconomyManager {
|
||||
* @return The player's balance
|
||||
*/
|
||||
public double getBalance(UUID playerUuid) {
|
||||
if (vaultEconomy == null) return 0.0;
|
||||
OfflinePlayer player = Bukkit.getOfflinePlayer(playerUuid);
|
||||
|
||||
return switch (provider) {
|
||||
case VAULT -> vaultEconomy.getBalance(player);
|
||||
case ESSENTIALS -> {
|
||||
try {
|
||||
yield essentials.getUser(playerUuid).getMoney().doubleValue();
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().log(Level.WARNING, "Failed to get balance from EssentialsX", e);
|
||||
yield 0.0;
|
||||
}
|
||||
}
|
||||
case NONE -> 0.0;
|
||||
};
|
||||
return vaultEconomy.getBalance(player);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -151,31 +93,13 @@ public class EconomyManager {
|
||||
*/
|
||||
public boolean withdraw(UUID playerUuid, double amount) {
|
||||
if (amount <= 0) return true;
|
||||
if (vaultEconomy == null) return false;
|
||||
|
||||
OfflinePlayer player = Bukkit.getOfflinePlayer(playerUuid);
|
||||
|
||||
return switch (provider) {
|
||||
case VAULT -> {
|
||||
if (!vaultEconomy.has(player, amount)) {
|
||||
yield false;
|
||||
}
|
||||
yield vaultEconomy.withdrawPlayer(player, amount).transactionSuccess();
|
||||
}
|
||||
case ESSENTIALS -> {
|
||||
try {
|
||||
var user = essentials.getUser(playerUuid);
|
||||
if (user.getMoney().doubleValue() < amount) {
|
||||
yield false;
|
||||
}
|
||||
user.setMoney(user.getMoney().subtract(java.math.BigDecimal.valueOf(amount)));
|
||||
yield true;
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().log(Level.WARNING, "Failed to withdraw from EssentialsX", e);
|
||||
yield false;
|
||||
}
|
||||
}
|
||||
case NONE -> false;
|
||||
};
|
||||
if (!vaultEconomy.has(player, amount)) {
|
||||
return false;
|
||||
}
|
||||
return vaultEconomy.withdrawPlayer(player, amount).transactionSuccess();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -187,23 +111,10 @@ public class EconomyManager {
|
||||
*/
|
||||
public boolean deposit(UUID playerUuid, double amount) {
|
||||
if (amount <= 0) return true;
|
||||
if (vaultEconomy == null) return false;
|
||||
|
||||
OfflinePlayer player = Bukkit.getOfflinePlayer(playerUuid);
|
||||
|
||||
return switch (provider) {
|
||||
case VAULT -> vaultEconomy.depositPlayer(player, amount).transactionSuccess();
|
||||
case ESSENTIALS -> {
|
||||
try {
|
||||
var user = essentials.getUser(playerUuid);
|
||||
user.setMoney(user.getMoney().add(java.math.BigDecimal.valueOf(amount)));
|
||||
yield true;
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().log(Level.WARNING, "Failed to deposit to EssentialsX", e);
|
||||
yield false;
|
||||
}
|
||||
}
|
||||
case NONE -> false;
|
||||
};
|
||||
return vaultEconomy.depositPlayer(player, amount).transactionSuccess();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -238,17 +149,9 @@ public class EconomyManager {
|
||||
* @return Formatted currency string
|
||||
*/
|
||||
public String format(double amount) {
|
||||
if (provider == EconomyProvider.VAULT && vaultEconomy != null) {
|
||||
if (vaultEconomy != null) {
|
||||
return vaultEconomy.format(amount);
|
||||
}
|
||||
return plugin.getMessageManager().formatCurrency(amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the economy provider type
|
||||
*/
|
||||
public EconomyProvider getProvider() {
|
||||
return provider;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -337,7 +337,7 @@ public class CreateAuctionGui implements MarketGui {
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
if (result.isSuccess()) {
|
||||
// Remove items from inventory AFTER successful creation
|
||||
InventoryUtil.removeItems(player, auctionItem, auctionItem.getAmount());
|
||||
InventoryUtil.removeItem(player, auctionItem, auctionItem.getAmount());
|
||||
|
||||
player.sendMessage(msgManager.getPrefixed("messages.auction-created",
|
||||
"id", String.valueOf(result.getId())));
|
||||
|
||||
@@ -284,7 +284,7 @@ public class CreateListingGui implements MarketGui {
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
if (result.isSuccess()) {
|
||||
// Remove items from inventory AFTER successful creation
|
||||
InventoryUtil.removeItems(player, listItem, amount);
|
||||
InventoryUtil.removeItem(player, listItem, amount);
|
||||
|
||||
player.sendMessage(msgManager.getPrefixed("messages.listing-created",
|
||||
"id", String.valueOf(result.getId())));
|
||||
|
||||
Reference in New Issue
Block a user