From afeb359b73e684f97922f20752f9ad5362a1f924 Mon Sep 17 00:00:00 2001 From: DNx5 Date: Wed, 9 Sep 2015 01:28:27 +0700 Subject: [PATCH] cleanup & improvements --- src/main/java/fr/xephi/authme/AuthMe.java | 128 +++++------------- .../java/fr/xephi/authme/ConsoleLogger.java | 4 + src/main/java/fr/xephi/authme/Utils.java | 39 ++++-- src/main/java/fr/xephi/authme/api/NewAPI.java | 12 +- .../xephi/authme/cache/backup/FileCache.java | 98 +++++--------- .../xephi/authme/cache/limbo/LimboCache.java | 9 +- .../commands/ChangePasswordCommand.java | 53 ++------ .../authme/listener/AuthMePlayerListener.java | 15 +- .../xephi/authme/task/ChangePasswordTask.java | 55 ++++++++ 9 files changed, 169 insertions(+), 244 deletions(-) create mode 100644 src/main/java/fr/xephi/authme/task/ChangePasswordTask.java diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index ff8a6babb..71d7c7e83 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -79,7 +79,7 @@ import net.milkbowl.vault.permission.Permission; public class AuthMe extends JavaPlugin { - public DataSource database = null; + public DataSource database; private Settings settings; private Messages m; public OtherAccounts otherAccounts; @@ -177,9 +177,7 @@ public class AuthMe extends JavaPlugin { try { Class.forName("org.apache.logging.log4j.core.Filter"); setLog4JFilter(); - } catch (ClassNotFoundException e) { - ConsoleLogger.info("You're using Minecraft 1.6.x or older, Log4J support will be disabled"); - } catch (NoClassDefFoundError e) { + } catch (ClassNotFoundException | NoClassDefFoundError e) { ConsoleLogger.info("You're using Minecraft 1.6.x or older, Log4J support will be disabled"); } } @@ -218,13 +216,8 @@ public class AuthMe extends JavaPlugin { try { setupDatabase(); - } catch (ClassNotFoundException nfe) { - ConsoleLogger.showError("Fatal error occurred! Authme initialization ABORTED!"); - return; - } catch (SQLException sqle) { - ConsoleLogger.showError("Fatal error occurred! Authme initialization ABORTED!"); - return; - } catch (PoolInitializationException pie) { + } catch (ClassNotFoundException | SQLException | PoolInitializationException ex) { + ConsoleLogger.writeStackTrace(ex); ConsoleLogger.showError("Fatal error occurred! Authme initialization ABORTED!"); return; } @@ -274,25 +267,18 @@ public class AuthMe extends JavaPlugin { if (Settings.reloadSupport) { try { - int playersOnline = 0; - try { - if (Bukkit.class.getMethod("getOnlinePlayers", new Class[0]).getReturnType() == Collection.class) - playersOnline = ((Collection) Bukkit.class.getMethod("getOnlinePlayers", new Class[0]).invoke(null, new Object[0])).size(); - else playersOnline = ((Player[]) Bukkit.class.getMethod("getOnlinePlayers", new Class[0]).invoke(null, new Object[0])).length; - } catch (Exception ex) { - } - if (playersOnline < 1) { - try { + int playersOnline = Utils.getOnlinePlayers().length; + if (database != null) { + if (playersOnline < 1) { database.purgeLogged(); - } catch (NullPointerException npe) { - } - } else { - for (PlayerAuth auth : database.getLoggedPlayers()) { - if (auth == null) - continue; - auth.setLastLogin(new Date().getTime()); - database.updateSession(auth); - PlayerCache.getInstance().addPlayer(auth); + } else { + for (PlayerAuth auth : database.getLoggedPlayers()) { + if (auth == null) + continue; + auth.setLastLogin(new Date().getTime()); + database.updateSession(auth); + PlayerCache.getInstance().addPlayer(auth); + } } } } catch (Exception ex) { @@ -398,38 +384,27 @@ public class AuthMe extends JavaPlugin { multiverse = null; return; } - if (this.getServer().getPluginManager().getPlugin("Multiverse-Core") != null && this.getServer().getPluginManager().getPlugin("Multiverse-Core").isEnabled()) { + if (this.getServer().getPluginManager().isPluginEnabled("Multiverse-Core")) { try { multiverse = (MultiverseCore) this.getServer().getPluginManager().getPlugin("Multiverse-Core"); ConsoleLogger.info("Hooked correctly with Multiverse-Core"); - } catch (NullPointerException npe) { - multiverse = null; - } catch (ClassCastException cce) { - multiverse = null; - } catch (NoClassDefFoundError ncdfe) { - multiverse = null; + } catch (Exception | NoClassDefFoundError ignored) { } - } else { - multiverse = null; } } public void checkEssentials() { - if (this.getServer().getPluginManager().getPlugin("Essentials") != null && this.getServer().getPluginManager().getPlugin("Essentials").isEnabled()) { + if (this.getServer().getPluginManager().isPluginEnabled("Essentials")) { try { ess = (Essentials) this.getServer().getPluginManager().getPlugin("Essentials"); ConsoleLogger.info("Hooked correctly with Essentials"); - } catch (NullPointerException npe) { - ess = null; - } catch (ClassCastException cce) { - ess = null; - } catch (NoClassDefFoundError ncdfe) { + } catch (Exception | NoClassDefFoundError e) { ess = null; } } else { ess = null; } - if (this.getServer().getPluginManager().getPlugin("EssentialsSpawn") != null && this.getServer().getPluginManager().getPlugin("EssentialsSpawn").isEnabled()) { + if (this.getServer().getPluginManager().isPluginEnabled("EssentialsSpawn")) { try { essentialsSpawn = new EssSpawn().getLocation(); ConsoleLogger.info("Hooked correctly with EssentialsSpawn"); @@ -443,42 +418,24 @@ public class AuthMe extends JavaPlugin { } public void checkCombatTag() { - if (this.getServer().getPluginManager().getPlugin("CombatTag") != null && this.getServer().getPluginManager().getPlugin("CombatTag").isEnabled()) { - this.CombatTag = true; - } else { - this.CombatTag = false; - } + this.CombatTag = this.getServer().getPluginManager().isPluginEnabled("CombatTag"); } public void checkCitizens() { - if (this.getServer().getPluginManager().getPlugin("Citizens") != null && this.getServer().getPluginManager().getPlugin("Citizens").isEnabled()) - this.isCitizensActive = true; - else this.isCitizensActive = false; + this.isCitizensActive = this.getServer().getPluginManager().isPluginEnabled("Citizens"); } @Override public void onDisable() { - int playersOnline = 0; - try { - if (Bukkit.class.getMethod("getOnlinePlayers", new Class[0]).getReturnType() == Collection.class) - playersOnline = ((Collection) Bukkit.class.getMethod("getOnlinePlayers", new Class[0]).invoke(null, new Object[0])).size(); - else playersOnline = ((Player[]) Bukkit.class.getMethod("getOnlinePlayers", new Class[0]).invoke(null, new Object[0])).length; - } catch (NoSuchMethodException ex) { - } // can never happen - catch (InvocationTargetException ex) { - } // can also never happen - catch (IllegalAccessException ex) { - } // can still never happen - if (playersOnline != 0) - for (Player player : Bukkit.getOnlinePlayers()) { + Player[] players = Utils.getOnlinePlayers(); + if (players != null) { + for (Player player : players) { this.savePlayer(player); } + } if (database != null) { - try { - database.close(); - } catch (Exception e) { - } + database.close(); } if (Settings.isBackupActivated && Settings.isBackupOnStop) { @@ -495,11 +452,8 @@ public class AuthMe extends JavaPlugin { } public void savePlayer(Player player) { - try { - if ((citizens.isNPC(player)) || (Utils.getInstance().isUnrestricted(player)) || (CombatTagComunicator.isNPC(player))) { - return; - } - } catch (Exception e) { + if ((citizens.isNPC(player)) || (Utils.getInstance().isUnrestricted(player)) || (CombatTagComunicator.isNPC(player))) { + return; } try { String name = player.getName().toLowerCase(); @@ -509,7 +463,7 @@ public class AuthMe extends JavaPlugin { } if (LimboCache.getInstance().hasLimboPlayer(name)) { LimboPlayer limbo = LimboCache.getInstance().getLimboPlayer(name); - if (Settings.protectInventoryBeforeLogInEnabled.booleanValue()) { + if (Settings.protectInventoryBeforeLogInEnabled) { player.getInventory().setArmorContents(limbo.getArmour()); player.getInventory().setContents(limbo.getInventory()); } @@ -728,18 +682,8 @@ public class AuthMe extends JavaPlugin { } public String replaceAllInfos(String message, Player player) { - int playersOnline = 0; - try { - if (Bukkit.class.getMethod("getOnlinePlayers", new Class[0]).getReturnType() == Collection.class) - playersOnline = ((Collection) Bukkit.class.getMethod("getOnlinePlayers", new Class[0]).invoke(null, new Object[0])).size(); - else playersOnline = ((Player[]) Bukkit.class.getMethod("getOnlinePlayers", new Class[0]).invoke(null, new Object[0])).length; - } catch (NoSuchMethodException ex) { - } // can never happen - catch (InvocationTargetException ex) { - } // can also never happen - catch (IllegalAccessException ex) { - } // can still never happen try { + int playersOnline = Utils.getOnlinePlayers().length; message = message.replace("&", "\u00a7"); message = message.replace("{PLAYER}", player.getName()); message = message.replace("{ONLINE}", "" + playersOnline); @@ -774,9 +718,7 @@ public class AuthMe extends JavaPlugin { if (ip.equalsIgnoreCase(getIP(player)) && database.isLogged(player.getName().toLowerCase()) && !player.getName().equalsIgnoreCase(name)) count++; } - if (count >= Settings.getMaxLoginPerIp) - return true; - return false; + return count >= Settings.getMaxLoginPerIp; } public boolean hasJoinedIp(String name, String ip) { @@ -785,14 +727,12 @@ public class AuthMe extends JavaPlugin { if (ip.equalsIgnoreCase(getIP(player)) && !player.getName().equalsIgnoreCase(name)) count++; } - if (count >= Settings.getMaxJoinPerIp) - return true; - return false; + return count >= Settings.getMaxJoinPerIp; } /** * Get Player real IP through VeryGames method - * + * * @param player * player */ diff --git a/src/main/java/fr/xephi/authme/ConsoleLogger.java b/src/main/java/fr/xephi/authme/ConsoleLogger.java index 010b66192..f455f9168 100644 --- a/src/main/java/fr/xephi/authme/ConsoleLogger.java +++ b/src/main/java/fr/xephi/authme/ConsoleLogger.java @@ -1,5 +1,6 @@ package fr.xephi.authme; +import com.google.common.base.Throwables; import fr.xephi.authme.settings.Settings; import org.bukkit.Bukkit; @@ -59,4 +60,7 @@ public class ConsoleLogger { } } + public static void writeStackTrace(Exception ex){ + writeLog(Throwables.getStackTraceAsString(ex)); + } } diff --git a/src/main/java/fr/xephi/authme/Utils.java b/src/main/java/fr/xephi/authme/Utils.java index c19754868..39d5bf554 100644 --- a/src/main/java/fr/xephi/authme/Utils.java +++ b/src/main/java/fr/xephi/authme/Utils.java @@ -1,18 +1,19 @@ package fr.xephi.authme; -import java.io.File; -import java.util.Iterator; - +import fr.xephi.authme.cache.limbo.LimboCache; +import fr.xephi.authme.cache.limbo.LimboPlayer; +import fr.xephi.authme.events.AuthMeTeleportEvent; +import fr.xephi.authme.settings.Settings; import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.entity.Player; -import fr.xephi.authme.cache.limbo.LimboCache; -import fr.xephi.authme.cache.limbo.LimboPlayer; -import fr.xephi.authme.events.AuthMeTeleportEvent; -import fr.xephi.authme.settings.Settings; +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.util.Collection; +import java.util.Iterator; public class Utils { @@ -130,7 +131,7 @@ public class Utils { } public void packCoords(double x, double y, double z, String w, - final Player pl) { + final Player pl) { World theWorld; if (w.equals("unavailableworld")) { theWorld = pl.getWorld(); @@ -171,13 +172,13 @@ public class Utils { NOTLOGGEDIN, LOGGEDIN } - - public static void purgeDirectory(File file){ + + public static void purgeDirectory(File file) { String files[] = file.list(); - if (files != null && files.length != 0){ + if (files != null && files.length != 0) { for (String temp : files) { File fileDelete = new File(file, temp); - if (fileDelete.isDirectory()){ + if (fileDelete.isDirectory()) { purgeDirectory(fileDelete); fileDelete.delete(); } else { @@ -186,4 +187,18 @@ public class Utils { } } } + + public static Player[] getOnlinePlayers() { + Player[] players = null; + try { + if (Bukkit.class.getMethod("getOnlinePlayers", new Class[0]).getReturnType() == Collection.class) { + players = (Player[]) ((Collection) Bukkit.class.getMethod("getOnlinePlayers", new Class[0]).invoke(null)).toArray(); + } else { + players = ((Player[]) Bukkit.class.getMethod("getOnlinePlayers", new Class[0]).invoke(null)); + } + } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) { + // can never happen + } + return players; + } } diff --git a/src/main/java/fr/xephi/authme/api/NewAPI.java b/src/main/java/fr/xephi/authme/api/NewAPI.java index a632ea607..48ce0705a 100644 --- a/src/main/java/fr/xephi/authme/api/NewAPI.java +++ b/src/main/java/fr/xephi/authme/api/NewAPI.java @@ -70,9 +70,7 @@ public class NewAPI { * @return true if player is a npc */ public boolean isNPC(Player player) { - if (plugin.getCitizensCommunicator().isNPC(player)) - return true; - return CombatTagComunicator.isNPC(player); + return plugin.getCitizensCommunicator().isNPC(player) || CombatTagComunicator.isNPC(player); } /** @@ -89,8 +87,7 @@ public class NewAPI { 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; + return new Location(Bukkit.getWorld(auth.getWorld()), auth.getQuitLocX(), auth.getQuitLocY(), auth.getQuitLocZ()); } else { return null; } @@ -152,10 +149,7 @@ public class NewAPI { return false; } PlayerAuth auth = new PlayerAuth(name, hash, "192.168.0.1", 0, "your@email.com"); - if (!plugin.database.saveAuth(auth)) { - return false; - } - return true; + return plugin.database.saveAuth(auth); } catch (NoSuchAlgorithmException ex) { return false; } diff --git a/src/main/java/fr/xephi/authme/cache/backup/FileCache.java b/src/main/java/fr/xephi/authme/cache/backup/FileCache.java index f06b63f03..3a3e7cbd1 100644 --- a/src/main/java/fr/xephi/authme/cache/backup/FileCache.java +++ b/src/main/java/fr/xephi/authme/cache/backup/FileCache.java @@ -2,11 +2,7 @@ package fr.xephi.authme.cache.backup; import java.io.File; import java.io.FileWriter; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Scanner; -import java.util.UUID; +import java.util.*; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; @@ -44,9 +40,7 @@ public class FileCache { return; try { path = player.getUniqueId().toString(); - } catch (Exception e) { - path = player.getName().toLowerCase(); - } catch (Error e) { + } catch (Exception | Error e) { path = player.getName().toLowerCase(); } File file = new File(plugin.getDataFolder() + File.separator + "cache" + File.separator + path + File.separator + "playerdatas.cache"); @@ -109,22 +103,19 @@ public class FileCache { if (Settings.customAttributes) { try { Attributes attributes = new Attributes(item); - if (attributes != null) { - Iterator iter = attributes.values().iterator(); - Attribute a = null; - while (iter.hasNext()) { - Attribute b = iter.next(); - if (a != null && a == b) - break; - a = b; - if (a != null) { - if (a.getName() != null && a.getAttributeType() != null && a.getOperation() != null && a.getUUID() != null) - writer.write("attribute=" + a.getName() + ";" + a.getAttributeType().getMinecraftId() + ";" + a.getAmount() + ";" + a.getOperation().getId() + ";" + a.getUUID().toString()); - } + Iterator iter = attributes.values().iterator(); + Attribute a = null; + while (iter.hasNext()) { + Attribute b = iter.next(); + if (a != null && a == b) + break; + a = b; + if (a != null) { + if (a.getName() != null && a.getAttributeType() != null && a.getOperation() != null && a.getUUID() != null) + writer.write("attribute=" + a.getName() + ";" + a.getAttributeType().getMinecraftId() + ";" + a.getAmount() + ";" + a.getOperation().getId() + ";" + a.getUUID().toString()); } } - } catch (Exception e) { - } catch (Error e) { + } catch (Exception | Error e) { } } } else { @@ -172,14 +163,13 @@ public class FileCache { if (Settings.customAttributes) { try { Attributes attributes = new Attributes(item); - if (attributes != null) - while (attributes.values().iterator().hasNext()) { - Attribute a = attributes.values().iterator().next(); - if (a != null) { - if (a.getName() != null && a.getAttributeType() != null && a.getOperation() != null && a.getUUID() != null && a.getAttributeType().getMinecraftId() != null) - writer.write("attribute=" + a.getName() + ";" + a.getAttributeType().getMinecraftId() + ";" + a.getAmount() + ";" + a.getOperation().getId() + ";" + a.getUUID().toString()); - } + while (attributes.values().iterator().hasNext()) { + Attribute a = attributes.values().iterator().next(); + if (a != null) { + if (a.getName() != null && a.getAttributeType() != null && a.getOperation() != null && a.getUUID() != null && a.getAttributeType().getMinecraftId() != null) + writer.write("attribute=" + a.getName() + ";" + a.getAttributeType().getMinecraftId() + ";" + a.getAmount() + ";" + a.getOperation().getId() + ";" + a.getUUID().toString()); } + } } catch (Exception e) { } } @@ -197,9 +187,7 @@ public class FileCache { String path = ""; try { path = player.getUniqueId().toString(); - } catch (Exception e) { - path = player.getName().toLowerCase(); - } catch (Error e) { + } catch (Exception | Error e) { path = player.getName().toLowerCase(); } try { @@ -232,13 +220,9 @@ public class FileCache { final String[] playerInfo = line.split(";"); group = playerInfo[0]; - if (Integer.parseInt(playerInfo[1]) == 1) { - op = true; - } else op = false; + op = Integer.parseInt(playerInfo[1]) == 1; if (playerInfo.length > 2) { - if (Integer.parseInt(playerInfo[2]) == 1) - flying = true; - else flying = false; + flying = Integer.parseInt(playerInfo[2]) == 1; } continue; @@ -275,9 +259,7 @@ public class FileCache { } if (!lores.isEmpty()) { List loreList = new ArrayList(); - for (String s : lores.split("%newline%")) { - loreList.add(s); - } + Collections.addAll(loreList, lores.split("%newline%")); meta.setLore(loreList); } if (meta != null) @@ -296,9 +278,7 @@ public class FileCache { meta.setDisplayName(name); if (!lores.isEmpty()) { List loreList = new ArrayList(); - for (String s : lores.split("%newline%")) { - loreList.add(s); - } + Collections.addAll(loreList, lores.split("%newline%")); meta.setLore(loreList); } if (meta != null) @@ -348,14 +328,13 @@ public class FileCache { Attributes attributes = null; count = 1; boolean v = true; - while (reader.hasNextLine() && v == true) { + while (reader.hasNextLine() && v) { String line = reader.nextLine(); switch (count) { case 1: item = new ItemStack(Material.getMaterial(line)); if (item.getType() == Material.AIR) v = false; - meta = item.getItemMeta(); count++; continue; case 2: @@ -379,8 +358,7 @@ public class FileCache { if (line.startsWith("lore=")) { line = line.substring(5); List lore = new ArrayList(); - for (String s : line.split("%newline%")) - lore.add(s); + Collections.addAll(lore, line.split("%newline%")); meta.setLore(lore); item.setItemMeta(meta); continue; @@ -430,7 +408,7 @@ public class FileCache { Attributes attributes = null; count = 1; boolean v = true; - while (reader.hasNextLine() && v == true) { + while (reader.hasNextLine() && v) { String line = reader.nextLine(); switch (count) { case 1: @@ -460,9 +438,8 @@ public class FileCache { } if (line.startsWith("lore=")) { line = line.substring(5); - List lore = new ArrayList(); - for (String s : line.split("%newline%")) - lore.add(s); + List lore = new ArrayList<>(); + Collections.addAll(lore, line.split("%newline%")); meta.setLore(lore); item.setItemMeta(meta); continue; @@ -504,10 +481,6 @@ public class FileCache { armours[i] = attributes.getStack(); else armours[i] = item; } - } catch (final RuntimeException e) { - //verbose - e.printStackTrace(); - ConsoleLogger.showError("Error while reading file for " + player.getName() + ", some wipe inventory incoming..."); } catch (final Exception e) { //verbose e.printStackTrace(); @@ -518,11 +491,6 @@ public class FileCache { } return new DataFileCache(inv, armours, group, op, flying); } - } catch (RuntimeException e) { - // Verbose - e.printStackTrace(); - ConsoleLogger.showError("Error while reading file for " + player.getName() + ", some wipe inventory incoming..."); - return null; } catch (Exception e) { // Verbose e.printStackTrace(); @@ -535,9 +503,7 @@ public class FileCache { String path = ""; try { path = player.getUniqueId().toString(); - } catch (Exception e) { - path = player.getName().toLowerCase(); - } catch (Error e) { + } catch (Exception | Error e) { path = player.getName().toLowerCase(); } try { @@ -562,9 +528,7 @@ public class FileCache { String path = ""; try { path = player.getUniqueId().toString(); - } catch (Exception e) { - path = player.getName().toLowerCase(); - } catch (Error e) { + } catch (Exception | Error e) { path = player.getName().toLowerCase(); } File file = new File(plugin.getDataFolder() + File.separator + "cache" + File.separator + path + File.separator + "playerdatas.cache"); diff --git a/src/main/java/fr/xephi/authme/cache/limbo/LimboCache.java b/src/main/java/fr/xephi/authme/cache/limbo/LimboCache.java index 2c3d89be6..96185c2e6 100644 --- a/src/main/java/fr/xephi/authme/cache/limbo/LimboCache.java +++ b/src/main/java/fr/xephi/authme/cache/limbo/LimboCache.java @@ -53,6 +53,7 @@ public class LimboCache { operator = playerData.readCache(player).getOperator(); flying = playerData.readCache(player).isFlying(); } catch (Exception e) { + ConsoleLogger.writeStackTrace(e); ConsoleLogger.showError("Some error on reading cache of " + name); } } else { @@ -65,12 +66,8 @@ public class LimboCache { inv = null; arm = null; } - if (player.isOp()) - operator = true; - else operator = false; - if (player.isFlying()) - flying = true; - else flying = false; + operator = player.isOp(); + flying = player.isFlying(); if (plugin.permission != null) { try { playerGroup = plugin.permission.getPrimaryGroup(player); diff --git a/src/main/java/fr/xephi/authme/commands/ChangePasswordCommand.java b/src/main/java/fr/xephi/authme/commands/ChangePasswordCommand.java index 0b50da50f..6d3c1f2f7 100644 --- a/src/main/java/fr/xephi/authme/commands/ChangePasswordCommand.java +++ b/src/main/java/fr/xephi/authme/commands/ChangePasswordCommand.java @@ -1,20 +1,15 @@ package fr.xephi.authme.commands; -import java.security.NoSuchAlgorithmException; - +import fr.xephi.authme.AuthMe; +import fr.xephi.authme.cache.auth.PlayerCache; +import fr.xephi.authme.settings.Messages; +import fr.xephi.authme.settings.Settings; +import fr.xephi.authme.task.ChangePasswordTask; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import fr.xephi.authme.AuthMe; -import fr.xephi.authme.ConsoleLogger; -import fr.xephi.authme.cache.auth.PlayerAuth; -import fr.xephi.authme.cache.auth.PlayerCache; -import fr.xephi.authme.security.PasswordSecurity; -import fr.xephi.authme.settings.Messages; -import fr.xephi.authme.settings.Settings; - public class ChangePasswordCommand implements CommandExecutor { private Messages m = Messages.getInstance(); @@ -25,8 +20,8 @@ public class ChangePasswordCommand implements CommandExecutor { } @Override - public boolean onCommand(final CommandSender sender, Command cmnd, String label, - final String[] args) { + public boolean onCommand(CommandSender sender, Command cmnd, String label, + String[] args) { if (!(sender instanceof Player)) { return true; } @@ -36,8 +31,8 @@ public class ChangePasswordCommand implements CommandExecutor { return true; } - final Player player = (Player) sender; - final String name = player.getName().toLowerCase(); + Player player = (Player) sender; + String name = player.getName().toLowerCase(); if (!PlayerCache.getInstance().isAuthenticated(name)) { m.send(player, "not_logged_in"); return true; @@ -67,35 +62,7 @@ public class ChangePasswordCommand implements CommandExecutor { return true; } } - plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() { - @Override - public void run() { - try { - String hashnew = PasswordSecurity.getHash(Settings.getPasswordHash, args[1], name); - - if (PasswordSecurity.comparePasswordWithHash(args[0], PlayerCache.getInstance().getAuth(name).getHash(), player.getName())) { - PlayerAuth auth = PlayerCache.getInstance().getAuth(name); - auth.setHash(hashnew); - if (PasswordSecurity.userSalt.containsKey(name) && PasswordSecurity.userSalt.get(name) != null) - auth.setSalt(PasswordSecurity.userSalt.get(name)); - else auth.setSalt(""); - if (!plugin.database.updatePassword(auth)) { - m.send(player, "error"); - return; - } - plugin.database.updateSalt(auth); - PlayerCache.getInstance().updatePlayer(auth); - m.send(player, "pwd_changed"); - ConsoleLogger.info(player.getName() + " changed his password"); - } else { - m.send(player, "wrong_pwd"); - } - } catch (NoSuchAlgorithmException ex) { - ConsoleLogger.showError(ex.getMessage()); - m.send(sender, "error"); - } - } - }); + plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new ChangePasswordTask(plugin, player, args[0])); return true; } } diff --git a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java index fda470e6d..6ffde849b 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java +++ b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java @@ -575,17 +575,7 @@ public class AuthMePlayerListener implements Listener { return; } - int playersOnline = 0; - try { - if (Bukkit.class.getMethod("getOnlinePlayers", new Class[0]).getReturnType() == Collection.class) - playersOnline = ((Collection) Bukkit.class.getMethod("getOnlinePlayers", new Class[0]).invoke(null, new Object[0])).size(); - else playersOnline = ((Player[]) Bukkit.class.getMethod("getOnlinePlayers", new Class[0]).invoke(null, new Object[0])).length; - } catch (NoSuchMethodException ex) { - } // can never happen - catch (InvocationTargetException ex) { - } // can also never happen - catch (IllegalAccessException ex) { - } // can still never happen + int playersOnline = Utils.getOnlinePlayers().length; if (playersOnline > plugin.getServer().getMaxPlayers()) { event.allow(); return; @@ -625,13 +615,12 @@ public class AuthMePlayerListener implements Listener { return; } - Player player = event.getPlayer(); - if ((!Settings.isForceSingleSessionEnabled) && (event.getReason().contains(m.getString("same_nick")))) { event.setCancelled(true); return; } + Player player = event.getPlayer(); plugin.management.performQuit(player, true); } diff --git a/src/main/java/fr/xephi/authme/task/ChangePasswordTask.java b/src/main/java/fr/xephi/authme/task/ChangePasswordTask.java new file mode 100644 index 000000000..c9cb72d66 --- /dev/null +++ b/src/main/java/fr/xephi/authme/task/ChangePasswordTask.java @@ -0,0 +1,55 @@ +package fr.xephi.authme.task; + +import fr.xephi.authme.AuthMe; +import fr.xephi.authme.ConsoleLogger; +import fr.xephi.authme.cache.auth.PlayerAuth; +import fr.xephi.authme.cache.auth.PlayerCache; +import fr.xephi.authme.security.PasswordSecurity; +import fr.xephi.authme.settings.Messages; +import fr.xephi.authme.settings.Settings; +import org.bukkit.entity.Player; + +import java.security.NoSuchAlgorithmException; + +public class ChangePasswordTask implements Runnable { + + private final AuthMe plugin; + private final Player player; + private String password; + + public ChangePasswordTask(AuthMe plugin, Player player, String password) { + this.plugin = plugin; + this.player = player; + this.password = password; + } + + @Override + public void run() { + Messages m = Messages.getInstance(); + try { + String name = player.getName().toLowerCase(); + String hashnew = PasswordSecurity.getHash(Settings.getPasswordHash, password, name); + if (PasswordSecurity.comparePasswordWithHash(password, PlayerCache.getInstance().getAuth(name).getHash(), player.getName())) { + PlayerAuth auth = PlayerCache.getInstance().getAuth(name); + auth.setHash(hashnew); + if (PasswordSecurity.userSalt.containsKey(name) && PasswordSecurity.userSalt.get(name) != null) + auth.setSalt(PasswordSecurity.userSalt.get(name)); + else auth.setSalt(""); + if (!plugin.database.updatePassword(auth)) { + m.send(player, "error"); + return; + } + plugin.database.updateSalt(auth); + PlayerCache.getInstance().updatePlayer(auth); + m.send(player, "pwd_changed"); + ConsoleLogger.info(player.getName() + " changed his password"); + } else { + m.send(player, "wrong_pwd"); + } + } catch (NoSuchAlgorithmException ex) { + ConsoleLogger.showError(ex.getMessage()); + m.send(player, "error"); + } + } +} +