mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-12 11:57:29 +00:00
Merge branch 'dev' of https://github.com/KAMKEEL/CustomNPC-Plus into dev
This commit is contained in:
commit
38d305ab99
6 files changed
+83
-127
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");
|
||||
@@ -70,7 +51,7 @@ public class BukkitUtil {
|
||||
// Initialize Vault after Bukkit is confirmed
|
||||
VaultUtil.init();
|
||||
|
||||
} catch (ClassNotFoundException e) {
|
||||
} catch (ClassNotFoundException | NoClassDefFoundError e) {
|
||||
logger.debug("Bukkit not found, Bukkit integration disabled");
|
||||
} catch (NoSuchMethodException e) {
|
||||
logger.error("Bukkit API method not found", e);
|
||||
@@ -100,30 +81,45 @@ 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;
|
||||
}
|
||||
}
|
||||
@@ -132,15 +128,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;
|
||||
}
|
||||
}
|
||||
@@ -151,39 +147,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;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,8 @@ package kamkeel.npcs.util;
|
||||
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.plugin.Plugin;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
@@ -68,15 +70,19 @@ public class VaultUtil {
|
||||
}
|
||||
|
||||
try {
|
||||
Class<?> offlinePlayerClass = BukkitUtil.getOfflinePlayerClass();
|
||||
Class<?> offlinePlayerClass = Class.forName("org.bukkit.OfflinePlayer");
|
||||
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");
|
||||
Plugin vault = Bukkit.getPluginManager().getPlugin("Vault");
|
||||
if (vault == null)
|
||||
return;
|
||||
ClassLoader vaultLoader = vault.getClass().getClassLoader();
|
||||
economyClass = vaultLoader.loadClass("net.milkbowl.vault.economy.Economy");
|
||||
economyResponseClass = vaultLoader.loadClass("net.milkbowl.vault.economy.EconomyResponse");
|
||||
|
||||
// Get Economy methods (using deprecated String methods for broader compatibility)
|
||||
isEnabled = economyClass.getMethod("isEnabled");
|
||||
@@ -174,7 +180,6 @@ public class VaultUtil {
|
||||
|
||||
/**
|
||||
* Checks if a player has an account in the economy system.
|
||||
*
|
||||
* @param player the player to check
|
||||
* @return true if the player has an account
|
||||
*/
|
||||
@@ -184,7 +189,6 @@ public class VaultUtil {
|
||||
|
||||
/**
|
||||
* Checks if a player has an account in the economy system.
|
||||
*
|
||||
* @param playerName the player name to check
|
||||
* @return true if the player has an account
|
||||
*/
|
||||
@@ -205,7 +209,6 @@ public class VaultUtil {
|
||||
|
||||
/**
|
||||
* Gets the balance of a player.
|
||||
*
|
||||
* @param player the player
|
||||
* @return the player's balance, or 0 if not available
|
||||
*/
|
||||
@@ -215,7 +218,6 @@ public class VaultUtil {
|
||||
|
||||
/**
|
||||
* Gets the balance of a player by name.
|
||||
*
|
||||
* @param playerName the player name
|
||||
* @return the player's balance, or 0 if not available
|
||||
*/
|
||||
@@ -236,7 +238,6 @@ public class VaultUtil {
|
||||
|
||||
/**
|
||||
* Checks if a player has at least the specified amount.
|
||||
*
|
||||
* @param player the player
|
||||
* @param amount the amount to check
|
||||
* @return true if the player has at least the specified amount
|
||||
@@ -247,9 +248,8 @@ public class VaultUtil {
|
||||
|
||||
/**
|
||||
* Checks if a player has at least the specified amount.
|
||||
*
|
||||
* @param playerName the player name
|
||||
* @param amount the amount to check
|
||||
* @param amount the amount to check
|
||||
* @return true if the player has at least the specified amount
|
||||
*/
|
||||
public static boolean has(String playerName, double amount) {
|
||||
@@ -269,7 +269,6 @@ public class VaultUtil {
|
||||
|
||||
/**
|
||||
* Withdraws money from a player's account.
|
||||
*
|
||||
* @param player the player
|
||||
* @param amount the amount to withdraw
|
||||
* @return true if the transaction was successful
|
||||
@@ -280,9 +279,8 @@ public class VaultUtil {
|
||||
|
||||
/**
|
||||
* Withdraws money from a player's account.
|
||||
*
|
||||
* @param playerName the player name
|
||||
* @param amount the amount to withdraw
|
||||
* @param amount the amount to withdraw
|
||||
* @return true if the transaction was successful
|
||||
*/
|
||||
public static boolean withdrawMoney(String playerName, double amount) {
|
||||
@@ -305,7 +303,6 @@ public class VaultUtil {
|
||||
|
||||
/**
|
||||
* Adds money to a player's account (deposit).
|
||||
*
|
||||
* @param player the player
|
||||
* @param amount the amount to add
|
||||
* @return true if the transaction was successful
|
||||
@@ -316,9 +313,8 @@ public class VaultUtil {
|
||||
|
||||
/**
|
||||
* Adds money to a player's account (deposit).
|
||||
*
|
||||
* @param playerName the player name
|
||||
* @param amount the amount to add
|
||||
* @param amount the amount to add
|
||||
* @return true if the transaction was successful
|
||||
*/
|
||||
public static boolean addMoney(String playerName, double amount) {
|
||||
@@ -342,7 +338,6 @@ public class VaultUtil {
|
||||
/**
|
||||
* Sets a player's balance to the specified amount.
|
||||
* This withdraws all money first, then deposits the new amount.
|
||||
*
|
||||
* @param player the player
|
||||
* @param amount the new balance
|
||||
* @return true if the transaction was successful
|
||||
@@ -354,9 +349,8 @@ public class VaultUtil {
|
||||
/**
|
||||
* Sets a player's balance to the specified amount.
|
||||
* This withdraws all money first, then deposits the new amount.
|
||||
*
|
||||
* @param playerName the player name
|
||||
* @param amount the new balance
|
||||
* @param amount the new balance
|
||||
* @return true if the transaction was successful
|
||||
*/
|
||||
public static boolean setBalance(String playerName, double amount) {
|
||||
@@ -376,7 +370,6 @@ public class VaultUtil {
|
||||
|
||||
/**
|
||||
* Formats an amount according to the economy plugin's settings.
|
||||
*
|
||||
* @param amount the amount to format
|
||||
* @return the formatted string, or the raw number as string if not available
|
||||
*/
|
||||
@@ -393,7 +386,6 @@ public class VaultUtil {
|
||||
|
||||
/**
|
||||
* Gets the plural currency name (e.g., "Dollars").
|
||||
*
|
||||
* @return the plural currency name, or "coins" if not available
|
||||
*/
|
||||
public static String getCurrencyNamePlural() {
|
||||
@@ -409,7 +401,6 @@ public class VaultUtil {
|
||||
|
||||
/**
|
||||
* Gets the singular currency name (e.g., "Dollar").
|
||||
*
|
||||
* @return the singular currency name, or "coin" if not available
|
||||
*/
|
||||
public static String getCurrencyNameSingular() {
|
||||
@@ -425,7 +416,6 @@ public class VaultUtil {
|
||||
|
||||
/**
|
||||
* Creates an account for the player if one doesn't exist.
|
||||
*
|
||||
* @param player the player
|
||||
* @return true if the account was created or already exists
|
||||
*/
|
||||
@@ -435,7 +425,6 @@ public class VaultUtil {
|
||||
|
||||
/**
|
||||
* Creates an account for the player if one doesn't exist.
|
||||
*
|
||||
* @param playerName the player name
|
||||
* @return true if the account was created or already exists
|
||||
*/
|
||||
@@ -472,7 +461,6 @@ public class VaultUtil {
|
||||
|
||||
/**
|
||||
* Gets a list of all bank names.
|
||||
*
|
||||
* @return list of bank names, or empty list if not available
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -489,7 +477,6 @@ public class VaultUtil {
|
||||
|
||||
/**
|
||||
* Gets the balance of a bank.
|
||||
*
|
||||
* @param bankName the bank name
|
||||
* @return the bank balance, or 0 if not available
|
||||
*/
|
||||
@@ -507,9 +494,8 @@ public class VaultUtil {
|
||||
|
||||
/**
|
||||
* Checks if a bank has at least the specified amount.
|
||||
*
|
||||
* @param bankName the bank name
|
||||
* @param amount the amount to check
|
||||
* @param amount the amount to check
|
||||
* @return true if the bank has at least the specified amount
|
||||
*/
|
||||
public static boolean bankHas(String bankName, double amount) {
|
||||
@@ -526,9 +512,8 @@ public class VaultUtil {
|
||||
|
||||
/**
|
||||
* Withdraws money from a bank.
|
||||
*
|
||||
* @param bankName the bank name
|
||||
* @param amount the amount to withdraw
|
||||
* @param amount the amount to withdraw
|
||||
* @return true if the transaction was successful
|
||||
*/
|
||||
public static boolean bankWithdraw(String bankName, double amount) {
|
||||
@@ -545,9 +530,8 @@ public class VaultUtil {
|
||||
|
||||
/**
|
||||
* Deposits money into a bank.
|
||||
*
|
||||
* @param bankName the bank name
|
||||
* @param amount the amount to deposit
|
||||
* @param amount the amount to deposit
|
||||
* @return true if the transaction was successful
|
||||
*/
|
||||
public static boolean bankDeposit(String bankName, double amount) {
|
||||
@@ -564,9 +548,8 @@ public class VaultUtil {
|
||||
|
||||
/**
|
||||
* Transfers money from one player to another.
|
||||
*
|
||||
* @param from the player to take money from
|
||||
* @param to the player to give money to
|
||||
* @param from the player to take money from
|
||||
* @param to the player to give money to
|
||||
* @param amount the amount to transfer
|
||||
* @return true if the transfer was successful
|
||||
*/
|
||||
@@ -576,10 +559,9 @@ public class VaultUtil {
|
||||
|
||||
/**
|
||||
* Transfers money from one player to another.
|
||||
*
|
||||
* @param fromPlayerName the player name to take money from
|
||||
* @param toPlayerName the player name to give money to
|
||||
* @param amount the amount to transfer
|
||||
* @param toPlayerName the player name to give money to
|
||||
* @param amount the amount to transfer
|
||||
* @return true if the transfer was successful
|
||||
*/
|
||||
public static boolean transfer(String fromPlayerName, String toPlayerName, double amount) {
|
||||
|
||||
@@ -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;
|
||||
@@ -115,7 +116,7 @@ public class CustomNpcsPermissions {
|
||||
public static final Permission AUCTION_TRADES_MAX = new Permission("customnpcs.auction.trades.*");
|
||||
|
||||
public static CustomNpcsPermissions Instance;
|
||||
private Method hasPermission;
|
||||
private static boolean isEnabled;
|
||||
|
||||
public CustomNpcsPermissions() {
|
||||
Instance = this;
|
||||
@@ -131,16 +132,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();
|
||||
}
|
||||
}
|
||||
@@ -174,17 +175,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;
|
||||
@@ -209,6 +206,6 @@ public class CustomNpcsPermissions {
|
||||
}
|
||||
|
||||
public static boolean enabled() {
|
||||
return BukkitUtil.isEnabled();
|
||||
return BukkitUtil.isEnabled() && isEnabled;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user