From d22607e8bd2d9c05af7b019161027a62b6402ead Mon Sep 17 00:00:00 2001 From: Xephi59 Date: Tue, 7 Jul 2015 00:21:27 +0200 Subject: [PATCH] IMPORTANT : Change how API works ! --- src/main/java/fr/xephi/authme/api/API.java | 362 +++++++++--------- .../authme/listener/AuthMePlayerListener.java | 8 +- .../authme/process/join/AsyncronousJoin.java | 3 +- .../login/ProcessSyncronousPlayerLogin.java | 6 +- .../authme/process/quit/AsyncronousQuit.java | 2 +- .../quit/ProcessSyncronousPlayerQuit.java | 10 +- 6 files changed, 203 insertions(+), 188 deletions(-) diff --git a/src/main/java/fr/xephi/authme/api/API.java b/src/main/java/fr/xephi/authme/api/API.java index 75220e7f4..1117dbdfa 100644 --- a/src/main/java/fr/xephi/authme/api/API.java +++ b/src/main/java/fr/xephi/authme/api/API.java @@ -1,174 +1,188 @@ -package fr.xephi.authme.api; - -import java.security.NoSuchAlgorithmException; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; -import org.bukkit.plugin.Plugin; -import fr.xephi.authme.AuthMe; -import fr.xephi.authme.Utils; -import fr.xephi.authme.cache.auth.PlayerAuth; -import fr.xephi.authme.cache.auth.PlayerCache; -import fr.xephi.authme.datasource.DataSource; -import fr.xephi.authme.plugin.manager.CombatTagComunicator; -import fr.xephi.authme.security.PasswordSecurity; -import fr.xephi.authme.settings.Settings; - -public class API { - - public static final String newline = System.getProperty("line.separator"); - public static AuthMe instance; - public static DataSource database; - - public API(AuthMe instance, DataSource database) { - API.instance = instance; - API.database = database; - } - - /** - * Hook into AuthMe - * - * @return AuthMe instance - */ - public static AuthMe hookAuthMe() { - Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("AuthMe"); - if (plugin == null || !(plugin instanceof AuthMe)) { - return null; - } - return (AuthMe) plugin; - } - - public AuthMe getPlugin() { - return instance; - } - - /** - * - * @param player - * @return true if player is authenticate - */ - public static boolean isAuthenticated(Player player) { - return PlayerCache.getInstance().isAuthenticated(player.getName()); - } - - /** - * - * @param player - * @return true if player is a npc - */ - @Deprecated - public boolean isaNPC(Player player) { - if (instance.getCitizensCommunicator().isNPC(player, instance)) - return true; - return CombatTagComunicator.isNPC(player); - } - - /** - * - * @param player - * @return true if player is a npc - */ - public boolean isNPC(Player player) { - if (instance.getCitizensCommunicator().isNPC(player, instance)) - return true; - return CombatTagComunicator.isNPC(player); - } - - /** - * - * @param player - * @return true if the player is unrestricted - */ - public static boolean isUnrestricted(Player player) { - return Utils.getInstance().isUnrestricted(player); - } - - public static Location getLastLocation(Player player) { - try { - PlayerAuth auth = PlayerCache.getInstance().getAuth(player.getName().toLowerCase()); - - if (auth != null) { - Location loc = new Location(Bukkit.getWorld(auth.getWorld()), auth.getQuitLocX(), auth.getQuitLocY(), auth.getQuitLocZ()); - return loc; - } else { - return null; - } - - } catch (NullPointerException ex) { - return null; - } - } - - public static void setPlayerInventory(Player player, ItemStack[] content, - ItemStack[] armor) { - try { - player.getInventory().setContents(content); - player.getInventory().setArmorContents(armor); - } catch (NullPointerException npe) { - } - } - - /** - * - * @param playerName - * @return true if player is registered - */ - public static boolean isRegistered(String playerName) { - String player = playerName.toLowerCase(); - return database.isAuthAvailable(player); - } - - /** - * @param String - * playerName, String passwordToCheck - * @return true if the password is correct , false else - */ - public static boolean checkPassword(String playerName, - String passwordToCheck) { - if (!isRegistered(playerName)) - return false; - String player = playerName.toLowerCase(); - PlayerAuth auth = database.getAuth(player); - try { - return PasswordSecurity.comparePasswordWithHash(passwordToCheck, auth.getHash(), playerName); - } catch (NoSuchAlgorithmException e) { - return false; - } - } - - /** - * Register a player - * - * @param String - * playerName, String password - * @return true if the player is register correctly - */ - public static boolean registerPlayer(String playerName, String password) { - try { - String name = playerName.toLowerCase(); - String hash = PasswordSecurity.getHash(Settings.getPasswordHash, password, name); - if (isRegistered(name)) { - return false; - } - PlayerAuth auth = new PlayerAuth(name, hash, "198.18.0.1", 0, "your@email.com"); - if (!database.saveAuth(auth)) { - return false; - } - return true; - } catch (NoSuchAlgorithmException ex) { - return false; - } - } - - /** - * Force a player to login - * - * @param Player - * player - */ - public static void forceLogin(Player player) { - instance.management.performLogin(player, "dontneed", true); - } - -} +package fr.xephi.authme.api; + +import java.security.NoSuchAlgorithmException; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Server; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.plugin.Plugin; + +import fr.xephi.authme.AuthMe; +import fr.xephi.authme.Utils; +import fr.xephi.authme.cache.auth.PlayerAuth; +import fr.xephi.authme.cache.auth.PlayerCache; +import fr.xephi.authme.datasource.DataSource; +import fr.xephi.authme.plugin.manager.CombatTagComunicator; +import fr.xephi.authme.security.PasswordSecurity; +import fr.xephi.authme.settings.Settings; + +public class API { + + public static final String newline = System.getProperty("line.separator"); + public static API singleton; + public AuthMe plugin; + public DataSource database; + + public API(AuthMe plugin, DataSource database) { + this.plugin = plugin; + this.database = database; + } + + public API(Server serv) { + this.plugin = (AuthMe) serv.getPluginManager().getPlugin("AuthMe"); + this.database = this.plugin.database; + } + + /** + * Hook into AuthMe + * + * @return + * + * @return AuthMe plugin + */ + public static API getInstance() { + if (singleton != null) + return singleton; + Plugin p = Bukkit.getServer().getPluginManager().getPlugin("AuthMe"); + if (p == null || !(p instanceof AuthMe)) { + return null; + } + AuthMe authme = (AuthMe) p; + singleton = (new API(authme, authme.database)); + return singleton; + } + + public AuthMe getPlugin() { + return plugin; + } + + /** + * + * @param player + * @return true if player is authenticate + */ + public boolean isAuthenticated(Player player) { + return PlayerCache.getInstance().isAuthenticated(player.getName()); + } + + /** + * + * @param player + * @return true if player is a npc + */ + @Deprecated + public boolean isaNPC(Player player) { + if (plugin.getCitizensCommunicator().isNPC(player, plugin)) + return true; + return CombatTagComunicator.isNPC(player); + } + + /** + * + * @param player + * @return true if player is a npc + */ + public boolean isNPC(Player player) { + if (plugin.getCitizensCommunicator().isNPC(player, plugin)) + return true; + return CombatTagComunicator.isNPC(player); + } + + /** + * + * @param player + * @return true if the player is unrestricted + */ + public boolean isUnrestricted(Player player) { + return Utils.getInstance().isUnrestricted(player); + } + + public Location getLastLocation(Player player) { + try { + PlayerAuth auth = PlayerCache.getInstance().getAuth(player.getName().toLowerCase()); + + if (auth != null) { + Location loc = new Location(Bukkit.getWorld(auth.getWorld()), auth.getQuitLocX(), auth.getQuitLocY(), auth.getQuitLocZ()); + return loc; + } else { + return null; + } + + } catch (NullPointerException ex) { + return null; + } + } + + public void setPlayerInventory(Player player, ItemStack[] content, + ItemStack[] armor) { + try { + player.getInventory().setContents(content); + player.getInventory().setArmorContents(armor); + } catch (NullPointerException npe) { + } + } + + /** + * + * @param playerName + * @return true if player is registered + */ + public boolean isRegistered(String playerName) { + String player = playerName.toLowerCase(); + return database.isAuthAvailable(player); + } + + /** + * @param String + * playerName, String passwordToCheck + * @return true if the password is correct , false else + */ + public boolean checkPassword(String playerName, String passwordToCheck) { + if (!isRegistered(playerName)) + return false; + String player = playerName.toLowerCase(); + PlayerAuth auth = database.getAuth(player); + try { + return PasswordSecurity.comparePasswordWithHash(passwordToCheck, auth.getHash(), playerName); + } catch (NoSuchAlgorithmException e) { + return false; + } + } + + /** + * Register a player + * + * @param String + * playerName, String password + * @return true if the player is register correctly + */ + public boolean registerPlayer(String playerName, String password) { + try { + String name = playerName.toLowerCase(); + String hash = PasswordSecurity.getHash(Settings.getPasswordHash, password, name); + if (isRegistered(name)) { + return false; + } + PlayerAuth auth = new PlayerAuth(name, hash, "198.18.0.1", 0, "your@email.com"); + if (!database.saveAuth(auth)) { + return false; + } + return true; + } catch (NoSuchAlgorithmException ex) { + return false; + } + } + + /** + * Force a player to login + * + * @param Player + * player + */ + public void forceLogin(Player player) { + plugin.management.performLogin(player, "dontneed", true); + } + +} diff --git a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java index afa6c5aaf..fafa78793 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java +++ b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java @@ -39,7 +39,6 @@ import org.bukkit.event.player.PlayerRespawnEvent; import fr.xephi.authme.AuthMe; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.Utils; -import fr.xephi.authme.api.API; import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.cache.limbo.LimboCache; @@ -405,6 +404,7 @@ public class AuthMePlayerListener implements Listener { if (player == null) return; final String name = player.getName().toLowerCase(); + boolean isAuthAvailable = data.isAuthAvailable(name); if (plugin.getCitizensCommunicator().isNPC(player, plugin) || Utils.getInstance().isUnrestricted(player) || CombatTagComunicator.isNPC(player)) { return; @@ -412,7 +412,7 @@ public class AuthMePlayerListener implements Listener { if (Settings.enablePasspartu && !Settings.countriesBlacklist.isEmpty()) { String code = plugin.getCountryCode(event.getAddress().getHostAddress()); - if (((code == null) || (Settings.countriesBlacklist.contains(code) && !API.isRegistered(name))) && !plugin.authmePermissible(player, "authme.bypassantibot")) { + if (((code == null) || (Settings.countriesBlacklist.contains(code) && !isAuthAvailable)) && !plugin.authmePermissible(player, "authme.bypassantibot")) { event.setKickMessage(m.send("country_banned")[0]); event.setResult(PlayerLoginEvent.Result.KICK_OTHER); return; @@ -420,7 +420,7 @@ public class AuthMePlayerListener implements Listener { } if (Settings.enableProtection && !Settings.countries.isEmpty()) { String code = plugin.getCountryCode(event.getAddress().getHostAddress()); - if (((code == null) || (!Settings.countries.contains(code) && !API.isRegistered(name))) && !plugin.authmePermissible(player, "authme.bypassantibot")) { + if (((code == null) || (!Settings.countries.contains(code) && !isAuthAvailable)) && !plugin.authmePermissible(player, "authme.bypassantibot")) { event.setKickMessage(m.send("country_banned")[0]); event.setResult(PlayerLoginEvent.Result.KICK_OTHER); return; @@ -441,7 +441,7 @@ public class AuthMePlayerListener implements Listener { return; } - if (data.isAuthAvailable(name) && LimboCache.getInstance().hasLimboPlayer(name)) + if (isAuthAvailable && LimboCache.getInstance().hasLimboPlayer(name)) if (Settings.isSessionsEnabled) if (PlayerCache.getInstance().isAuthenticated(name)) if (!Settings.sessionExpireOnIpChange) diff --git a/src/main/java/fr/xephi/authme/process/join/AsyncronousJoin.java b/src/main/java/fr/xephi/authme/process/join/AsyncronousJoin.java index 35b6df0d8..29c9e9df5 100644 --- a/src/main/java/fr/xephi/authme/process/join/AsyncronousJoin.java +++ b/src/main/java/fr/xephi/authme/process/join/AsyncronousJoin.java @@ -17,7 +17,6 @@ import fr.xephi.authme.AuthMe; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.Utils; import fr.xephi.authme.Utils.groupType; -import fr.xephi.authme.api.API; import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.cache.backup.DataFileCache; @@ -255,7 +254,7 @@ public class AsyncronousJoin { if (!Settings.noConsoleSpam) ConsoleLogger.info("ProtectInventoryEvent has been cancelled for " + player.getName() + " ..."); } else { - API.setPlayerInventory(player, ev.getEmptyInventory(), ev.getEmptyArmor()); + plugin.api.setPlayerInventory(player, ev.getEmptyInventory(), ev.getEmptyArmor()); } } catch (NullPointerException ex) { } diff --git a/src/main/java/fr/xephi/authme/process/login/ProcessSyncronousPlayerLogin.java b/src/main/java/fr/xephi/authme/process/login/ProcessSyncronousPlayerLogin.java index 58951c5dd..8c4774334 100644 --- a/src/main/java/fr/xephi/authme/process/login/ProcessSyncronousPlayerLogin.java +++ b/src/main/java/fr/xephi/authme/process/login/ProcessSyncronousPlayerLogin.java @@ -10,7 +10,6 @@ import org.bukkit.potion.PotionEffectType; import fr.xephi.authme.AuthMe; import fr.xephi.authme.Utils; import fr.xephi.authme.Utils.groupType; -import fr.xephi.authme.api.API; import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.backup.FileCache; import fr.xephi.authme.cache.limbo.LimboCache; @@ -91,7 +90,7 @@ public class ProcessSyncronousPlayerLogin implements Runnable { RestoreInventoryEvent event = new RestoreInventoryEvent(player, limbo.getInventory(), limbo.getArmour()); Bukkit.getServer().getPluginManager().callEvent(event); if (!event.isCancelled()) { - API.setPlayerInventory(player, event.getInventory(), event.getArmor()); + plugin.api.setPlayerInventory(player, event.getInventory(), event.getArmor()); } } @@ -146,7 +145,8 @@ public class ProcessSyncronousPlayerLogin implements Runnable { } else { teleportBackFromSpawn(); } - } else if (Settings.isForceSpawnLocOnJoinEnabled && Settings.getForcedWorlds.contains(player.getWorld().getName())) { + } else + if (Settings.isForceSpawnLocOnJoinEnabled && Settings.getForcedWorlds.contains(player.getWorld().getName())) { teleportToSpawn(); } else if (Settings.isSaveQuitLocationEnabled && auth.getQuitLocY() != 0) { packQuitLocation(); diff --git a/src/main/java/fr/xephi/authme/process/quit/AsyncronousQuit.java b/src/main/java/fr/xephi/authme/process/quit/AsyncronousQuit.java index 05aa45c1a..b0b80053a 100644 --- a/src/main/java/fr/xephi/authme/process/quit/AsyncronousQuit.java +++ b/src/main/java/fr/xephi/authme/process/quit/AsyncronousQuit.java @@ -82,6 +82,6 @@ public class AsyncronousQuit { PlayerCache.getInstance().removePlayer(name); database.setUnlogged(name); AuthMePlayerListener.gameMode.remove(name); - Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new ProcessSyncronousPlayerQuit(player, inv, armor, isOp, isFlying, needToChange)); + Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new ProcessSyncronousPlayerQuit(plugin, player, inv, armor, isOp, isFlying, needToChange)); } } diff --git a/src/main/java/fr/xephi/authme/process/quit/ProcessSyncronousPlayerQuit.java b/src/main/java/fr/xephi/authme/process/quit/ProcessSyncronousPlayerQuit.java index 83e088ff7..b21a78bfe 100644 --- a/src/main/java/fr/xephi/authme/process/quit/ProcessSyncronousPlayerQuit.java +++ b/src/main/java/fr/xephi/authme/process/quit/ProcessSyncronousPlayerQuit.java @@ -4,12 +4,13 @@ import org.bukkit.GameMode; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -import fr.xephi.authme.api.API; +import fr.xephi.authme.AuthMe; import fr.xephi.authme.events.RestoreInventoryEvent; import fr.xephi.authme.settings.Settings; public class ProcessSyncronousPlayerQuit implements Runnable { + protected AuthMe plugin; protected Player player; protected boolean isOp; protected boolean isFlying; @@ -17,9 +18,10 @@ public class ProcessSyncronousPlayerQuit implements Runnable { protected ItemStack[] armor; protected boolean needToChange; - public ProcessSyncronousPlayerQuit(Player player, ItemStack[] inv, - ItemStack[] armor, boolean isOp, boolean isFlying, + public ProcessSyncronousPlayerQuit(AuthMe plugin, Player player, + ItemStack[] inv, ItemStack[] armor, boolean isOp, boolean isFlying, boolean needToChange) { + this.plugin = plugin; this.player = player; this.isOp = isOp; this.isFlying = isFlying; @@ -34,7 +36,7 @@ public class ProcessSyncronousPlayerQuit implements Runnable { RestoreInventoryEvent ev = new RestoreInventoryEvent(player, inv, armor); player.getServer().getPluginManager().callEvent(ev); if (!ev.isCancelled()) { - API.setPlayerInventory(player, ev.getInventory(), ev.getArmor()); + plugin.api.setPlayerInventory(player, ev.getInventory(), ev.getArmor()); } } if (needToChange) {