mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-14 02:34:10 +00:00
Fix integration with bukkit and plugins codewise
This commit is contained in:
1 parent
d0dc8a4578
commit
f6342b2335
7 files changed
+146
-205
No files matched your search
@@ -2,8 +2,8 @@ package kamkeel.npcs.util;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
|
||||
/**
|
||||
* Utility class for Bukkit integration detection and common operations.
|
||||
@@ -15,24 +15,6 @@ public class BukkitUtil {
|
||||
private static boolean initialized = false;
|
||||
private static boolean bukkitEnabled = false;
|
||||
|
||||
// Bukkit classes
|
||||
private static Class<?> bukkitClass;
|
||||
private static Class<?> serverClass;
|
||||
private static Class<?> servicesManagerClass;
|
||||
private static Class<?> registeredServiceProviderClass;
|
||||
private static Class<?> offlinePlayerClass;
|
||||
private static Class<?> playerClass;
|
||||
|
||||
// Bukkit methods
|
||||
private static Method getServer;
|
||||
private static Method getServicesManager;
|
||||
private static Method getRegistration;
|
||||
private static Method getProvider;
|
||||
private static Method getOfflinePlayer;
|
||||
private static Method getPlayer;
|
||||
private static Method getPluginManager;
|
||||
private static Method isPluginEnabled;
|
||||
|
||||
/**
|
||||
* Initializes Bukkit integration using reflection.
|
||||
* Should be called during FMLServerStartedEvent to ensure Bukkit is fully loaded.
|
||||
@@ -44,25 +26,24 @@ public class BukkitUtil {
|
||||
initialized = true;
|
||||
|
||||
try {
|
||||
// Load Bukkit classes
|
||||
bukkitClass = Class.forName("org.bukkit.Bukkit");
|
||||
serverClass = Class.forName("org.bukkit.Server");
|
||||
servicesManagerClass = Class.forName("org.bukkit.plugin.ServicesManager");
|
||||
registeredServiceProviderClass = Class.forName("org.bukkit.plugin.RegisteredServiceProvider");
|
||||
offlinePlayerClass = Class.forName("org.bukkit.OfflinePlayer");
|
||||
playerClass = Class.forName("org.bukkit.entity.Player");
|
||||
// Make sure all necessary bukkit methods and classes are available.
|
||||
Class<?> bukkitClass = Class.forName("org.bukkit.Bukkit");
|
||||
Class.forName("org.bukkit.Server");
|
||||
Class<?> servicesManagerClass =Class.forName("org.bukkit.plugin.ServicesManager");
|
||||
Class<?> registeredServiceProviderClass = Class.forName("org.bukkit.plugin.RegisteredServiceProvider");
|
||||
Class.forName("org.bukkit.OfflinePlayer");
|
||||
Class.forName("org.bukkit.entity.Player");
|
||||
|
||||
// Get Bukkit methods
|
||||
getServer = bukkitClass.getMethod("getServer");
|
||||
getServicesManager = bukkitClass.getMethod("getServicesManager");
|
||||
getRegistration = servicesManagerClass.getMethod("getRegistration", Class.class);
|
||||
getProvider = registeredServiceProviderClass.getMethod("getProvider");
|
||||
getOfflinePlayer = bukkitClass.getMethod("getOfflinePlayer", String.class);
|
||||
getPlayer = bukkitClass.getMethod("getPlayer", String.class);
|
||||
getPluginManager = bukkitClass.getMethod("getPluginManager");
|
||||
bukkitClass.getMethod("getServer");
|
||||
bukkitClass.getMethod("getServicesManager");
|
||||
servicesManagerClass.getMethod("getRegistration", Class.class);
|
||||
registeredServiceProviderClass.getMethod("getProvider");
|
||||
bukkitClass.getMethod("getOfflinePlayer", String.class);
|
||||
bukkitClass.getMethod("getPlayer", String.class);
|
||||
bukkitClass.getMethod("getPluginManager");
|
||||
|
||||
Class<?> pluginManagerClass = Class.forName("org.bukkit.plugin.PluginManager");
|
||||
isPluginEnabled = pluginManagerClass.getMethod("isPluginEnabled", String.class);
|
||||
pluginManagerClass.getMethod("isPluginEnabled", String.class);
|
||||
|
||||
bukkitEnabled = true;
|
||||
logger.info("Bukkit integration enabled");
|
||||
@@ -99,29 +80,44 @@ public class BukkitUtil {
|
||||
* @return true if the plugin is enabled
|
||||
*/
|
||||
public static boolean isPluginEnabled(String pluginName) {
|
||||
if (!bukkitEnabled) return false;
|
||||
if (!isEnabled()) return false;
|
||||
|
||||
try {
|
||||
Object pluginManager = getPluginManager.invoke(null);
|
||||
return (Boolean) isPluginEnabled.invoke(pluginManager, pluginName);
|
||||
return Bukkit.getPluginManager().isPluginEnabled(pluginName);
|
||||
} catch (Exception e) {
|
||||
logger.error("Error checking if plugin is enabled: " + pluginName, e);
|
||||
logger.error("Error checking if plugin is enabled: {}", pluginName, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param pluginName name of the plugin to get.
|
||||
* @return {@linkplain org.bukkit.plugin.Plugin Plugin} object for the name. Cast to correct class manually.
|
||||
*/
|
||||
public static Object getPlugin(String pluginName) {
|
||||
if (!isEnabled()) return null;
|
||||
|
||||
try {
|
||||
return Bukkit.getPluginManager().getPlugin(pluginName);
|
||||
} catch (Exception e) {
|
||||
logger.error("Error getting plugin object: {}", pluginName, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a Bukkit OfflinePlayer by name.
|
||||
* @param playerName the player name
|
||||
* @return the OfflinePlayer object, or null if not available
|
||||
* @return the {@link org.bukkit.OfflinePlayer} object, or null if not available
|
||||
*/
|
||||
@SuppressWarnings({"deprecation"})
|
||||
public static Object getOfflinePlayer(String playerName) {
|
||||
if (!bukkitEnabled) return null;
|
||||
if (!isEnabled()) return null;
|
||||
|
||||
try {
|
||||
return getOfflinePlayer.invoke(null, playerName);
|
||||
return Bukkit.getOfflinePlayer(playerName);
|
||||
} catch (Exception e) {
|
||||
logger.error("Error getting OfflinePlayer: " + playerName, e);
|
||||
logger.error("Error getting OfflinePlayer: {}", playerName, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -129,15 +125,15 @@ public class BukkitUtil {
|
||||
/**
|
||||
* Gets a Bukkit Player by name (online players only).
|
||||
* @param playerName the player name
|
||||
* @return the Player object, or null if not online or not available
|
||||
* @return the {@link org.bukkit.entity.Player} object, or null if not online or not available
|
||||
*/
|
||||
public static Object getPlayer(String playerName) {
|
||||
if (!bukkitEnabled) return null;
|
||||
if (!isEnabled()) return null;
|
||||
|
||||
try {
|
||||
return getPlayer.invoke(null, playerName);
|
||||
return Bukkit.getPlayer(playerName);
|
||||
} catch (Exception e) {
|
||||
logger.error("Error getting Player: " + playerName, e);
|
||||
logger.error("Error getting Player: {}", playerName, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -147,39 +143,17 @@ public class BukkitUtil {
|
||||
* @param serviceClass the service class to get
|
||||
* @return the service provider, or null if not available
|
||||
*/
|
||||
public static Object getServiceProvider(Class<?> serviceClass) {
|
||||
if (!bukkitEnabled) return null;
|
||||
public static <T> T getServiceProvider(Class<T> serviceClass) {
|
||||
if (!isEnabled()) return null;
|
||||
|
||||
try {
|
||||
Object servicesManager = getServicesManager.invoke(null);
|
||||
Object registration = getRegistration.invoke(servicesManager, serviceClass);
|
||||
RegisteredServiceProvider<T> registration = Bukkit.getServicesManager().getRegistration(serviceClass);
|
||||
if (registration != null) {
|
||||
return getProvider.invoke(registration);
|
||||
return registration.getProvider();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Error getting service provider: " + serviceClass.getName(), e);
|
||||
logger.error("Error getting service provider: {}", serviceClass.getName(), e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// Getters for classes (for use by other utils like VaultUtil)
|
||||
public static Class<?> getBukkitClass() {
|
||||
return bukkitClass;
|
||||
}
|
||||
|
||||
public static Class<?> getServicesManagerClass() {
|
||||
return servicesManagerClass;
|
||||
}
|
||||
|
||||
public static Class<?> getRegisteredServiceProviderClass() {
|
||||
return registeredServiceProviderClass;
|
||||
}
|
||||
|
||||
public static Class<?> getOfflinePlayerClass() {
|
||||
return offlinePlayerClass;
|
||||
}
|
||||
|
||||
public static Class<?> getPlayerClass() {
|
||||
return playerClass;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,14 @@
|
||||
package kamkeel.npcs.util;
|
||||
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
import net.milkbowl.vault.economy.EconomyResponse;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
@@ -16,41 +22,43 @@ public class VaultUtil {
|
||||
|
||||
private static boolean initialized = false;
|
||||
private static boolean vaultEnabled = false;
|
||||
//
|
||||
// // Vault classes
|
||||
// private static Class<?> economyClass;
|
||||
// private static Class<?> economyResponseClass;
|
||||
//
|
||||
// // Economy methods
|
||||
// private static Method isEnabled;
|
||||
// private static Method getName;
|
||||
// private static Method hasAccount;
|
||||
// private static Method hasAccountOffline;
|
||||
// private static Method getBalance;
|
||||
// private static Method getBalanceOffline;
|
||||
// private static Method has;
|
||||
// private static Method hasOffline;
|
||||
// private static Method withdrawPlayer;
|
||||
// private static Method withdrawPlayerOffline;
|
||||
// private static Method depositPlayer;
|
||||
// private static Method depositPlayerOffline;
|
||||
// private static Method format;
|
||||
// private static Method currencyNamePlural;
|
||||
// private static Method currencyNameSingular;
|
||||
// private static Method createPlayerAccount;
|
||||
// private static Method createPlayerAccountOffline;
|
||||
// private static Method hasBankSupport;
|
||||
// private static Method getBanks;
|
||||
// private static Method bankBalance;
|
||||
// private static Method bankHas;
|
||||
// private static Method bankWithdraw;
|
||||
// private static Method bankDeposit;
|
||||
//
|
||||
// // EconomyResponse methods
|
||||
// private static Method transactionSuccess;
|
||||
//
|
||||
// // Cached economy instance
|
||||
// private static Object economyInstance;
|
||||
|
||||
// Vault classes
|
||||
private static Class<?> economyClass;
|
||||
private static Class<?> economyResponseClass;
|
||||
|
||||
// Economy methods
|
||||
private static Method isEnabled;
|
||||
private static Method getName;
|
||||
private static Method hasAccount;
|
||||
private static Method hasAccountOffline;
|
||||
private static Method getBalance;
|
||||
private static Method getBalanceOffline;
|
||||
private static Method has;
|
||||
private static Method hasOffline;
|
||||
private static Method withdrawPlayer;
|
||||
private static Method withdrawPlayerOffline;
|
||||
private static Method depositPlayer;
|
||||
private static Method depositPlayerOffline;
|
||||
private static Method format;
|
||||
private static Method currencyNamePlural;
|
||||
private static Method currencyNameSingular;
|
||||
private static Method createPlayerAccount;
|
||||
private static Method createPlayerAccountOffline;
|
||||
private static Method hasBankSupport;
|
||||
private static Method getBanks;
|
||||
private static Method bankBalance;
|
||||
private static Method bankHas;
|
||||
private static Method bankWithdraw;
|
||||
private static Method bankDeposit;
|
||||
|
||||
// EconomyResponse methods
|
||||
private static Method transactionSuccess;
|
||||
|
||||
// Cached economy instance
|
||||
private static Object economyInstance;
|
||||
private static Economy economyInstance;
|
||||
|
||||
/**
|
||||
* Initializes the Vault integration using reflection.
|
||||
@@ -68,47 +76,9 @@ public class VaultUtil {
|
||||
}
|
||||
|
||||
try {
|
||||
Class<?> offlinePlayerClass = BukkitUtil.getOfflinePlayerClass();
|
||||
if (offlinePlayerClass == null) {
|
||||
logger.error("Could not get OfflinePlayer class from BukkitUtil");
|
||||
return;
|
||||
}
|
||||
|
||||
// Load Vault classes
|
||||
economyClass = Class.forName("net.milkbowl.vault.economy.Economy");
|
||||
economyResponseClass = Class.forName("net.milkbowl.vault.economy.EconomyResponse");
|
||||
|
||||
// Get Economy methods (using deprecated String methods for broader compatibility)
|
||||
isEnabled = economyClass.getMethod("isEnabled");
|
||||
getName = economyClass.getMethod("getName");
|
||||
hasAccount = economyClass.getMethod("hasAccount", String.class);
|
||||
hasAccountOffline = economyClass.getMethod("hasAccount", offlinePlayerClass);
|
||||
getBalance = economyClass.getMethod("getBalance", String.class);
|
||||
getBalanceOffline = economyClass.getMethod("getBalance", offlinePlayerClass);
|
||||
has = economyClass.getMethod("has", String.class, double.class);
|
||||
hasOffline = economyClass.getMethod("has", offlinePlayerClass, double.class);
|
||||
withdrawPlayer = economyClass.getMethod("withdrawPlayer", String.class, double.class);
|
||||
withdrawPlayerOffline = economyClass.getMethod("withdrawPlayer", offlinePlayerClass, double.class);
|
||||
depositPlayer = economyClass.getMethod("depositPlayer", String.class, double.class);
|
||||
depositPlayerOffline = economyClass.getMethod("depositPlayer", offlinePlayerClass, double.class);
|
||||
format = economyClass.getMethod("format", double.class);
|
||||
currencyNamePlural = economyClass.getMethod("currencyNamePlural");
|
||||
currencyNameSingular = economyClass.getMethod("currencyNameSingular");
|
||||
createPlayerAccount = economyClass.getMethod("createPlayerAccount", String.class);
|
||||
createPlayerAccountOffline = economyClass.getMethod("createPlayerAccount", offlinePlayerClass);
|
||||
hasBankSupport = economyClass.getMethod("hasBankSupport");
|
||||
getBanks = economyClass.getMethod("getBanks");
|
||||
bankBalance = economyClass.getMethod("bankBalance", String.class);
|
||||
bankHas = economyClass.getMethod("bankHas", String.class, double.class);
|
||||
bankWithdraw = economyClass.getMethod("bankWithdraw", String.class, double.class);
|
||||
bankDeposit = economyClass.getMethod("bankDeposit", String.class, double.class);
|
||||
|
||||
// Get EconomyResponse methods
|
||||
transactionSuccess = economyResponseClass.getMethod("transactionSuccess");
|
||||
|
||||
// Get the economy instance
|
||||
economyInstance = BukkitUtil.getServiceProvider(economyClass);
|
||||
|
||||
RegisteredServiceProvider<Economy> economyRegistration = Bukkit.getServicesManager().getRegistration(Economy.class);
|
||||
economyInstance = economyRegistration.getProvider();
|
||||
if (economyInstance != null) {
|
||||
vaultEnabled = true;
|
||||
logger.info("Vault Economy integration enabled - Provider: " + getEconomyName());
|
||||
@@ -116,10 +86,6 @@ public class VaultUtil {
|
||||
logger.info("Vault found but no Economy provider registered");
|
||||
}
|
||||
|
||||
} catch (ClassNotFoundException e) {
|
||||
logger.debug("Vault not found, economy integration disabled");
|
||||
} catch (NoSuchMethodException e) {
|
||||
logger.error("Vault API method not found, economy integration disabled", e);
|
||||
} catch (Exception e) {
|
||||
logger.error("Error initializing Vault integration", e);
|
||||
}
|
||||
@@ -134,7 +100,8 @@ public class VaultUtil {
|
||||
}
|
||||
|
||||
try {
|
||||
economyInstance = BukkitUtil.getServiceProvider(economyClass);
|
||||
RegisteredServiceProvider<Economy> economyRegistration = Bukkit.getServicesManager().getRegistration(Economy.class);
|
||||
economyInstance = economyRegistration.getProvider();
|
||||
if (economyInstance != null) {
|
||||
vaultEnabled = true;
|
||||
logger.info("Vault Economy provider refreshed: " + getEconomyName());
|
||||
@@ -151,7 +118,7 @@ public class VaultUtil {
|
||||
if (!vaultEnabled || economyInstance == null) return false;
|
||||
|
||||
try {
|
||||
return (Boolean) isEnabled.invoke(economyInstance);
|
||||
return economyInstance.isEnabled();
|
||||
} catch (Exception e) {
|
||||
logger.error("Error checking if economy is enabled", e);
|
||||
return false;
|
||||
@@ -165,7 +132,7 @@ public class VaultUtil {
|
||||
if (!vaultEnabled || economyInstance == null) return null;
|
||||
|
||||
try {
|
||||
return (String) getName.invoke(economyInstance);
|
||||
return economyInstance.getName();
|
||||
} catch (Exception e) {
|
||||
logger.error("Error getting economy name", e);
|
||||
return null;
|
||||
@@ -190,11 +157,11 @@ public class VaultUtil {
|
||||
if (!vaultEnabled || economyInstance == null) return false;
|
||||
|
||||
try {
|
||||
Object offlinePlayer = BukkitUtil.getOfflinePlayer(playerName);
|
||||
OfflinePlayer offlinePlayer = (OfflinePlayer) BukkitUtil.getOfflinePlayer(playerName);
|
||||
if (offlinePlayer != null) {
|
||||
return (Boolean) hasAccountOffline.invoke(economyInstance, offlinePlayer);
|
||||
return economyInstance.hasAccount(offlinePlayer);
|
||||
}
|
||||
return (Boolean) hasAccount.invoke(economyInstance, playerName);
|
||||
return economyInstance.hasAccount(playerName);
|
||||
} catch (Exception e) {
|
||||
logger.error("Error checking if player has account: " + playerName, e);
|
||||
return false;
|
||||
@@ -219,11 +186,11 @@ public class VaultUtil {
|
||||
if (!vaultEnabled || economyInstance == null) return 0;
|
||||
|
||||
try {
|
||||
Object offlinePlayer = BukkitUtil.getOfflinePlayer(playerName);
|
||||
OfflinePlayer offlinePlayer = (OfflinePlayer) BukkitUtil.getOfflinePlayer(playerName);
|
||||
if (offlinePlayer != null) {
|
||||
return (Double) getBalanceOffline.invoke(economyInstance, offlinePlayer);
|
||||
return economyInstance.getBalance(offlinePlayer);
|
||||
}
|
||||
return (Double) getBalance.invoke(economyInstance, playerName);
|
||||
return economyInstance.getBalance(playerName);
|
||||
} catch (Exception e) {
|
||||
logger.error("Error getting balance for player: " + playerName, e);
|
||||
return 0;
|
||||
@@ -250,11 +217,11 @@ public class VaultUtil {
|
||||
if (!vaultEnabled || economyInstance == null) return false;
|
||||
|
||||
try {
|
||||
Object offlinePlayer = BukkitUtil.getOfflinePlayer(playerName);
|
||||
OfflinePlayer offlinePlayer = (OfflinePlayer) BukkitUtil.getOfflinePlayer(playerName);
|
||||
if (offlinePlayer != null) {
|
||||
return (Boolean) hasOffline.invoke(economyInstance, offlinePlayer, amount);
|
||||
return economyInstance.has(offlinePlayer, amount);
|
||||
}
|
||||
return (Boolean) has.invoke(economyInstance, playerName, amount);
|
||||
return economyInstance.has(playerName, amount);
|
||||
} catch (Exception e) {
|
||||
logger.error("Error checking if player has amount: " + playerName, e);
|
||||
return false;
|
||||
@@ -281,14 +248,14 @@ public class VaultUtil {
|
||||
if (!vaultEnabled || economyInstance == null) return false;
|
||||
|
||||
try {
|
||||
Object offlinePlayer = BukkitUtil.getOfflinePlayer(playerName);
|
||||
Object response;
|
||||
OfflinePlayer offlinePlayer = (OfflinePlayer) BukkitUtil.getOfflinePlayer(playerName);
|
||||
EconomyResponse response;
|
||||
if (offlinePlayer != null) {
|
||||
response = withdrawPlayerOffline.invoke(economyInstance, offlinePlayer, amount);
|
||||
response = economyInstance.withdrawPlayer(offlinePlayer, amount);
|
||||
} else {
|
||||
response = withdrawPlayer.invoke(economyInstance, playerName, amount);
|
||||
response = economyInstance.withdrawPlayer(playerName, amount);
|
||||
}
|
||||
return (Boolean) transactionSuccess.invoke(response);
|
||||
return response.transactionSuccess();
|
||||
} catch (Exception e) {
|
||||
logger.error("Error withdrawing money from player: " + playerName, e);
|
||||
return false;
|
||||
@@ -315,14 +282,14 @@ public class VaultUtil {
|
||||
if (!vaultEnabled || economyInstance == null) return false;
|
||||
|
||||
try {
|
||||
Object offlinePlayer = BukkitUtil.getOfflinePlayer(playerName);
|
||||
Object response;
|
||||
OfflinePlayer offlinePlayer = (OfflinePlayer) BukkitUtil.getOfflinePlayer(playerName);
|
||||
EconomyResponse response;
|
||||
if (offlinePlayer != null) {
|
||||
response = depositPlayerOffline.invoke(economyInstance, offlinePlayer, amount);
|
||||
response = economyInstance.depositPlayer(offlinePlayer, amount);
|
||||
} else {
|
||||
response = depositPlayer.invoke(economyInstance, playerName, amount);
|
||||
response = economyInstance.depositPlayer(playerName, amount);
|
||||
}
|
||||
return (Boolean) transactionSuccess.invoke(response);
|
||||
return response.transactionSuccess();
|
||||
} catch (Exception e) {
|
||||
logger.error("Error adding money to player: " + playerName, e);
|
||||
return false;
|
||||
@@ -371,7 +338,7 @@ public class VaultUtil {
|
||||
if (!vaultEnabled || economyInstance == null) return String.valueOf(amount);
|
||||
|
||||
try {
|
||||
return (String) format.invoke(economyInstance, amount);
|
||||
return economyInstance.format(amount);
|
||||
} catch (Exception e) {
|
||||
logger.error("Error formatting amount", e);
|
||||
return String.valueOf(amount);
|
||||
@@ -386,7 +353,7 @@ public class VaultUtil {
|
||||
if (!vaultEnabled || economyInstance == null) return "coins";
|
||||
|
||||
try {
|
||||
return (String) currencyNamePlural.invoke(economyInstance);
|
||||
return economyInstance.currencyNamePlural();
|
||||
} catch (Exception e) {
|
||||
logger.error("Error getting currency name plural", e);
|
||||
return "coins";
|
||||
@@ -401,7 +368,7 @@ public class VaultUtil {
|
||||
if (!vaultEnabled || economyInstance == null) return "coin";
|
||||
|
||||
try {
|
||||
return (String) currencyNameSingular.invoke(economyInstance);
|
||||
return economyInstance.currencyNameSingular();
|
||||
} catch (Exception e) {
|
||||
logger.error("Error getting currency name singular", e);
|
||||
return "coin";
|
||||
@@ -428,11 +395,11 @@ public class VaultUtil {
|
||||
if (hasAccount(playerName)) return true;
|
||||
|
||||
try {
|
||||
Object offlinePlayer = BukkitUtil.getOfflinePlayer(playerName);
|
||||
OfflinePlayer offlinePlayer = (OfflinePlayer) BukkitUtil.getOfflinePlayer(playerName);
|
||||
if (offlinePlayer != null) {
|
||||
return (Boolean) createPlayerAccountOffline.invoke(economyInstance, offlinePlayer);
|
||||
return economyInstance.createPlayerAccount(offlinePlayer);
|
||||
}
|
||||
return (Boolean) createPlayerAccount.invoke(economyInstance, playerName);
|
||||
return economyInstance.createPlayerAccount(playerName);
|
||||
} catch (Exception e) {
|
||||
logger.error("Error creating account for player: " + playerName, e);
|
||||
return false;
|
||||
@@ -446,7 +413,7 @@ public class VaultUtil {
|
||||
if (!vaultEnabled || economyInstance == null) return false;
|
||||
|
||||
try {
|
||||
return (Boolean) hasBankSupport.invoke(economyInstance);
|
||||
return economyInstance.hasBankSupport();
|
||||
} catch (Exception e) {
|
||||
logger.error("Error checking bank support", e);
|
||||
return false;
|
||||
@@ -462,7 +429,7 @@ public class VaultUtil {
|
||||
if (!vaultEnabled || economyInstance == null) return java.util.Collections.emptyList();
|
||||
|
||||
try {
|
||||
return (java.util.List<String>) getBanks.invoke(economyInstance);
|
||||
return economyInstance.getBanks();
|
||||
} catch (Exception e) {
|
||||
logger.error("Error getting banks", e);
|
||||
return java.util.Collections.emptyList();
|
||||
@@ -478,8 +445,8 @@ public class VaultUtil {
|
||||
if (!vaultEnabled || economyInstance == null) return 0;
|
||||
|
||||
try {
|
||||
Object response = bankBalance.invoke(economyInstance, bankName);
|
||||
return economyResponseClass.getField("balance").getDouble(response);
|
||||
EconomyResponse response = economyInstance.bankBalance(bankName);
|
||||
return response.balance;
|
||||
} catch (Exception e) {
|
||||
logger.error("Error getting bank balance: " + bankName, e);
|
||||
return 0;
|
||||
@@ -496,8 +463,8 @@ public class VaultUtil {
|
||||
if (!vaultEnabled || economyInstance == null) return false;
|
||||
|
||||
try {
|
||||
Object response = bankHas.invoke(economyInstance, bankName, amount);
|
||||
return (Boolean) transactionSuccess.invoke(response);
|
||||
EconomyResponse response = economyInstance.bankHas(bankName, amount);
|
||||
return response.transactionSuccess();
|
||||
} catch (Exception e) {
|
||||
logger.error("Error checking bank balance: " + bankName, e);
|
||||
return false;
|
||||
@@ -514,8 +481,8 @@ public class VaultUtil {
|
||||
if (!vaultEnabled || economyInstance == null) return false;
|
||||
|
||||
try {
|
||||
Object response = bankWithdraw.invoke(economyInstance, bankName, amount);
|
||||
return (Boolean) transactionSuccess.invoke(response);
|
||||
EconomyResponse response = economyInstance.bankWithdraw(bankName, amount);
|
||||
return response.transactionSuccess();
|
||||
} catch (Exception e) {
|
||||
logger.error("Error withdrawing from bank: " + bankName, e);
|
||||
return false;
|
||||
@@ -532,8 +499,8 @@ public class VaultUtil {
|
||||
if (!vaultEnabled || economyInstance == null) return false;
|
||||
|
||||
try {
|
||||
Object response = bankDeposit.invoke(economyInstance, bankName, amount);
|
||||
return (Boolean) transactionSuccess.invoke(response);
|
||||
EconomyResponse response = economyInstance.bankDeposit(bankName, amount);
|
||||
return response.transactionSuccess();
|
||||
} catch (Exception e) {
|
||||
logger.error("Error depositing to bank: " + bankName, e);
|
||||
return false;
|
||||
|
||||
@@ -7,6 +7,7 @@ import kamkeel.npcs.util.BukkitUtil;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
@@ -111,7 +112,7 @@ public class CustomNpcsPermissions {
|
||||
public static final Permission PROFILE_MAX = new Permission("customnpcs.profile.max.*");
|
||||
|
||||
public static CustomNpcsPermissions Instance;
|
||||
private Method hasPermission;
|
||||
private static boolean isEnabled;
|
||||
|
||||
public CustomNpcsPermissions() {
|
||||
Instance = this;
|
||||
@@ -127,16 +128,16 @@ public class CustomNpcsPermissions {
|
||||
}
|
||||
|
||||
try {
|
||||
hasPermission = BukkitUtil.getPlayerClass().getMethod("hasPermission", String.class);
|
||||
Class.forName("org.bukkit.entity.Player").getMethod("hasPermission", String.class);
|
||||
isEnabled = true;
|
||||
|
||||
LogManager.getLogger(CustomNpcs.class).info("Bukkit permissions enabled");
|
||||
LogManager.getLogger(CustomNpcs.class).info("Permissions available:");
|
||||
Collections.sort(Permission.permissions, String.CASE_INSENSITIVE_ORDER);
|
||||
for (String p : Permission.permissions) {
|
||||
LogManager.getLogger(CustomNpcs.class).info(p);
|
||||
}
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
} catch (SecurityException e) {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
@@ -170,17 +171,13 @@ public class CustomNpcsPermissions {
|
||||
}
|
||||
|
||||
private boolean bukkitPermission(String username, String permission) {
|
||||
if (hasPermission == null) return false;
|
||||
if (!BukkitUtil.isEnabled() || !isEnabled) return false;
|
||||
|
||||
try {
|
||||
Object player = BukkitUtil.getPlayer(username);
|
||||
if (player == null) return false;
|
||||
return (Boolean) hasPermission.invoke(player, permission);
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
return ((Player) player).hasPermission(permission);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
@@ -205,6 +202,6 @@ public class CustomNpcsPermissions {
|
||||
}
|
||||
|
||||
public static boolean enabled() {
|
||||
return BukkitUtil.isEnabled();
|
||||
return BukkitUtil.isEnabled() && isEnabled;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user