From 45f604534de6b121d16650816523bdfa7b496d7e Mon Sep 17 00:00:00 2001 From: DNx5 Date: Sat, 12 Dec 2015 19:16:52 +0700 Subject: [PATCH] Use Guava cache for caching PlayerAuth object. --- src/main/java/fr/xephi/authme/AuthMe.java | 2 +- .../xephi/authme/cache/auth/PlayerAuth.java | 8 +- .../xephi/authme/cache/auth/PlayerCache.java | 5 +- .../executable/authme/AccountsCommand.java | 45 +-- .../executable/authme/ResetNameCommand.java | 3 +- .../executable/authme/SetEmailCommand.java | 52 +-- .../authme/datasource/CacheDataSource.java | 361 +++++++----------- .../xephi/authme/datasource/DataSource.java | 4 +- .../fr/xephi/authme/datasource/FlatFile.java | 14 +- .../fr/xephi/authme/datasource/MySQL.java | 9 +- .../xephi/authme/hooks/BungeeCordMessage.java | 43 ++- 11 files changed, 229 insertions(+), 317 deletions(-) diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index 2f6bf18f1..d49abed83 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -603,7 +603,7 @@ public class AuthMe extends JavaPlugin { } if (Settings.isCachingEnabled) { - database = new CacheDataSource(this, database); + database = new CacheDataSource(database); } if (Settings.getDataSource == DataSource.DataSourceType.FILE) { diff --git a/src/main/java/fr/xephi/authme/cache/auth/PlayerAuth.java b/src/main/java/fr/xephi/authme/cache/auth/PlayerAuth.java index 32c4f7fdc..c501e8eb6 100644 --- a/src/main/java/fr/xephi/authme/cache/auth/PlayerAuth.java +++ b/src/main/java/fr/xephi/authme/cache/auth/PlayerAuth.java @@ -194,7 +194,7 @@ public class PlayerAuth { this.setHash(auth.getHash()); this.setIp(auth.getIp()); this.setLastLogin(auth.getLastLogin()); - this.setName(auth.getNickname()); + this.setNickname(auth.getNickname()); this.setQuitLocX(auth.getQuitLocX()); this.setQuitLocY(auth.getQuitLocY()); this.setQuitLocZ(auth.getQuitLocZ()); @@ -204,11 +204,11 @@ public class PlayerAuth { } /** - * Method setName. + * Method setNickname. * * @param nickname String */ - public void setName(String nickname) { + public void setNickname(String nickname) { this.nickname = nickname.toLowerCase(); } @@ -522,7 +522,7 @@ public class PlayerAuth { public PlayerAuth build() { return new PlayerAuth( - checkNotNull(name).toLowerCase(), + checkNotNull(name), nullToEmpty(hash), nullToEmpty(salt), groupId, diff --git a/src/main/java/fr/xephi/authme/cache/auth/PlayerCache.java b/src/main/java/fr/xephi/authme/cache/auth/PlayerCache.java index 4f9d21878..17cdca55e 100644 --- a/src/main/java/fr/xephi/authme/cache/auth/PlayerCache.java +++ b/src/main/java/fr/xephi/authme/cache/auth/PlayerCache.java @@ -40,8 +40,7 @@ public class PlayerCache { * @param auth PlayerAuth */ public void updatePlayer(PlayerAuth auth) { - cache.remove(auth.getNickname().toLowerCase()); - cache.put(auth.getNickname().toLowerCase(), auth); + cache.put(auth.getNickname(), auth); } /** @@ -87,7 +86,7 @@ public class PlayerCache { /** * Method getCache. * - * @return ConcurrentHashMap + * @return ConcurrentHashMap */ public ConcurrentHashMap getCache() { return this.cache; diff --git a/src/main/java/fr/xephi/authme/command/executable/authme/AccountsCommand.java b/src/main/java/fr/xephi/authme/command/executable/authme/AccountsCommand.java index ba34491a6..2c629ca2a 100644 --- a/src/main/java/fr/xephi/authme/command/executable/authme/AccountsCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/authme/AccountsCommand.java @@ -1,13 +1,11 @@ package fr.xephi.authme.command.executable.authme; import fr.xephi.authme.AuthMe; -import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.command.CommandParts; import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.Messages; -import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import java.util.List; @@ -26,30 +24,24 @@ public class AccountsCommand extends ExecutableCommand { // Get the player query String playerQuery = sender.getName(); - if (commandArguments.getCount() >= 1) + if (commandArguments.getCount() >= 1) { playerQuery = commandArguments.get(0); + } final String playerQueryFinal = playerQuery; // Command logic - if (!playerQuery.contains(".")) { - Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { - + if (!playerQueryFinal.contains(".")) { + plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() { @Override public void run() { - PlayerAuth auth; - StringBuilder message = new StringBuilder("[AuthMe] "); - try { - auth = plugin.database.getAuth(playerQueryFinal.toLowerCase()); - } catch (NullPointerException npe) { - m.send(sender, MessageKey.UNKNOWN_USER); - return; - } + PlayerAuth auth = plugin.database.getAuth(playerQueryFinal.toLowerCase()); if (auth == null) { m.send(sender, MessageKey.UNKNOWN_USER); return; } + StringBuilder message = new StringBuilder("[AuthMe] "); List accountList = plugin.database.getAllAuthsByName(auth); - if (accountList == null || accountList.isEmpty()) { + if (accountList.isEmpty()) { m.send(sender, MessageKey.USER_NOT_REGISTERED); return; } @@ -67,28 +59,20 @@ public class AccountsCommand extends ExecutableCommand { message.append('.'); } } - sender.sendMessage("[AuthMe] " + playerQueryFinal + " has " + String.valueOf(accountList.size()) + " accounts"); + sender.sendMessage("[AuthMe] " + playerQueryFinal + " has " + + String.valueOf(accountList.size()) + " accounts."); sender.sendMessage(message.toString()); } }); return true; } else { - Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { + plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() { @Override public void run() { - List accountList; - try { - accountList = plugin.database.getAllAuthsByIp(playerQueryFinal); - } catch (Exception e) { - ConsoleLogger.showError(e.getMessage()); - ConsoleLogger.writeStackTrace(e); - m.send(sender, MessageKey.ERROR); - return; - } - + List accountList = plugin.database.getAllAuthsByIp(playerQueryFinal); StringBuilder message = new StringBuilder("[AuthMe] "); - if (accountList == null || accountList.isEmpty()) { - sender.sendMessage("[AuthMe] This IP does not exist in the database"); + if (accountList.isEmpty()) { + sender.sendMessage("[AuthMe] This IP does not exist in the database."); return; } if (accountList.size() == 1) { @@ -105,7 +89,8 @@ public class AccountsCommand extends ExecutableCommand { message.append('.'); } } - sender.sendMessage("[AuthMe] " + playerQueryFinal + " has " + String.valueOf(accountList.size()) + " accounts"); + sender.sendMessage("[AuthMe] " + playerQueryFinal + " has " + + String.valueOf(accountList.size()) + " accounts."); sender.sendMessage(message.toString()); } }); diff --git a/src/main/java/fr/xephi/authme/command/executable/authme/ResetNameCommand.java b/src/main/java/fr/xephi/authme/command/executable/authme/ResetNameCommand.java index ae1f38400..0029104fb 100644 --- a/src/main/java/fr/xephi/authme/command/executable/authme/ResetNameCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/authme/ResetNameCommand.java @@ -11,6 +11,7 @@ import java.util.List; /** */ +// TODO: remove if not used public class ResetNameCommand extends ExecutableCommand { /** @@ -28,7 +29,7 @@ public class ResetNameCommand extends ExecutableCommand { final AuthMe plugin = AuthMe.getInstance(); // Command logic - Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { + plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() { @Override public void run() { List authentications = plugin.database.getAllAuths(); diff --git a/src/main/java/fr/xephi/authme/command/executable/authme/SetEmailCommand.java b/src/main/java/fr/xephi/authme/command/executable/authme/SetEmailCommand.java index 278bf13c4..813443f4a 100644 --- a/src/main/java/fr/xephi/authme/command/executable/authme/SetEmailCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/authme/SetEmailCommand.java @@ -24,16 +24,16 @@ public class SetEmailCommand extends ExecutableCommand { * @return True if the command was executed successfully, false otherwise. */ @Override - public boolean executeCommand(CommandSender sender, CommandParts commandReference, CommandParts commandArguments) { + public boolean executeCommand(final CommandSender sender, CommandParts commandReference, CommandParts commandArguments) { // AuthMe plugin instance - AuthMe plugin = AuthMe.getInstance(); + final AuthMe plugin = AuthMe.getInstance(); // Messages instance - Messages m = plugin.getMessages(); + final Messages m = plugin.getMessages(); // Get the player name and email address - String playerName = commandArguments.get(0); - String playerEmail = commandArguments.get(1); + final String playerName = commandArguments.get(0); + final String playerEmail = commandArguments.get(1); // Validate the email address if (!Settings.isEmailCorrect(playerEmail)) { @@ -41,26 +41,34 @@ public class SetEmailCommand extends ExecutableCommand { return true; } - // Validate the user - PlayerAuth auth = plugin.database.getAuth(playerName.toLowerCase()); - if (auth == null) { - m.send(sender, MessageKey.UNKNOWN_USER); - return true; - } + plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() { + @Override + public void run() { + // Validate the user + PlayerAuth auth = plugin.database.getAuth(playerName); + if (auth == null) { + m.send(sender, MessageKey.UNKNOWN_USER); + return; + } - // Set the email address - auth.setEmail(playerEmail); - if (!plugin.database.updateEmail(auth)) { - m.send(sender, MessageKey.ERROR); - return true; - } + // Set the email address - // Update the player cache - if (PlayerCache.getInstance().getAuth(playerName.toLowerCase()) != null) - PlayerCache.getInstance().updatePlayer(auth); + auth.setEmail(playerEmail); + if (!plugin.database.updateEmail(auth)) { + m.send(sender, MessageKey.ERROR); + return; + } - // Show a status message - m.send(sender, MessageKey.EMAIL_CHANGED_SUCCESS); + // Update the player cache + if (PlayerCache.getInstance().getAuth(playerName) != null) { + PlayerCache.getInstance().updatePlayer(auth); + } + + // Show a status message + m.send(sender, MessageKey.EMAIL_CHANGED_SUCCESS); + + } + }); return true; } } diff --git a/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java b/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java index 55e00246a..a1f74dd37 100644 --- a/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java +++ b/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java @@ -1,18 +1,16 @@ package fr.xephi.authme.datasource; -import fr.xephi.authme.AuthMe; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerCache; -import fr.xephi.authme.util.Utils; -import org.bukkit.entity.Player; import java.util.ArrayList; import java.util.List; -import java.util.Map; -import java.util.concurrent.Callable; -import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; /** */ @@ -20,37 +18,24 @@ public class CacheDataSource implements DataSource { private final DataSource source; private final ExecutorService exec; - private final ConcurrentHashMap cache = new ConcurrentHashMap<>(); + private final LoadingCache cachedAuths; /** * Constructor for CacheDataSource. * - * @param pl AuthMe * @param src DataSource */ - public CacheDataSource(final AuthMe pl, DataSource src) { + public CacheDataSource(DataSource src) { this.source = src; this.exec = Executors.newCachedThreadPool(); - pl.setCanConnect(false); - - /* - * We need to load all players in cache ... It will took more time to - * load the server, but it will be much easier to check for an - * isAuthAvailable ! - */ - exec.execute(new Runnable() { - @Override - public void run() { - for (PlayerAuth auth : source.getAllAuths()) { - cache.put(auth.getNickname().toLowerCase(), auth); - } - pl.setCanConnect(true); - } - }); - } - - public void addAuthToCache(PlayerAuth auth) { - cache.put(auth.getNickname().toLowerCase(), auth); + cachedAuths = CacheBuilder.newBuilder() + .expireAfterWrite(5, TimeUnit.MINUTES) + .build( + new CacheLoader() { + public PlayerAuth load(String key) { + return source.getAuth(key); + } + }); } /** @@ -58,11 +43,13 @@ public class CacheDataSource implements DataSource { * * @param user String * - * @return boolean * @see fr.xephi.authme.datasource.DataSource#isAuthAvailable(String) + * @return boolean + * + * @see fr.xephi.authme.datasource.DataSource#isAuthAvailable(String) */ @Override public synchronized boolean isAuthAvailable(String user) { - return cache.containsKey(user.toLowerCase()); + return getAuth(user) != null; } /** @@ -70,15 +57,14 @@ public class CacheDataSource implements DataSource { * * @param user String * - * @return PlayerAuth * @see fr.xephi.authme.datasource.DataSource#getAuth(String) + * @return PlayerAuth + * + * @see fr.xephi.authme.datasource.DataSource#getAuth(String) */ @Override public synchronized PlayerAuth getAuth(String user) { user = user.toLowerCase(); - if (cache.containsKey(user)) { - return cache.get(user); - } - return null; + return cachedAuths.getUnchecked(user); } /** @@ -86,20 +72,17 @@ public class CacheDataSource implements DataSource { * * @param auth PlayerAuth * - * @return boolean * @see fr.xephi.authme.datasource.DataSource#saveAuth(PlayerAuth) + * @return boolean + * + * @see fr.xephi.authme.datasource.DataSource#saveAuth(PlayerAuth) */ @Override - public synchronized boolean saveAuth(final PlayerAuth auth) { - cache.put(auth.getNickname(), auth); - exec.execute(new Runnable() { - @Override - public void run() { - if (!source.saveAuth(auth)) { - cache.remove(auth.getNickname()); - } - } - }); - return true; + public synchronized boolean saveAuth(PlayerAuth auth) { + boolean result = source.saveAuth(auth); + if (result) { + cachedAuths.refresh(auth.getNickname()); + } + return result; } /** @@ -107,26 +90,17 @@ public class CacheDataSource implements DataSource { * * @param auth PlayerAuth * - * @return boolean * @see fr.xephi.authme.datasource.DataSource#updatePassword(PlayerAuth) + * @return boolean + * + * @see fr.xephi.authme.datasource.DataSource#updatePassword(PlayerAuth) */ @Override - public synchronized boolean updatePassword(final PlayerAuth auth) { - if (!cache.containsKey(auth.getNickname())) { - return false; + public synchronized boolean updatePassword(PlayerAuth auth) { + boolean result = source.updatePassword(auth); + if (result) { + cachedAuths.refresh(auth.getNickname()); } - final String oldHash = cache.get(auth.getNickname()).getHash(); - cache.get(auth.getNickname()).setHash(auth.getHash()); - exec.execute(new Runnable() { - @Override - public void run() { - if (!source.updatePassword(auth)) { - if (cache.containsKey(auth.getNickname())) { - cache.get(auth.getNickname()).setHash(oldHash); - } - } - } - }); - return true; + return result; } /** @@ -134,35 +108,17 @@ public class CacheDataSource implements DataSource { * * @param auth PlayerAuth * - * @return boolean * @see fr.xephi.authme.datasource.DataSource#updateSession(PlayerAuth) + * @return boolean + * + * @see fr.xephi.authme.datasource.DataSource#updateSession(PlayerAuth) */ @Override - public boolean updateSession(final PlayerAuth auth) { - if (!cache.containsKey(auth.getNickname())) { - return false; + public boolean updateSession(PlayerAuth auth) { + boolean result = source.updateSession(auth); + if (result) { + cachedAuths.refresh(auth.getNickname()); } - PlayerAuth cachedAuth = cache.get(auth.getNickname()); - final String oldIp = cachedAuth.getIp(); - final long oldLastLogin = cachedAuth.getLastLogin(); - final String oldRealName = cachedAuth.getRealName(); - - cachedAuth.setIp(auth.getIp()); - cachedAuth.setLastLogin(auth.getLastLogin()); - cachedAuth.setRealName(auth.getRealName()); - exec.execute(new Runnable() { - @Override - public void run() { - if (!source.updateSession(auth)) { - if (cache.containsKey(auth.getNickname())) { - PlayerAuth cachedAuth = cache.get(auth.getNickname()); - cachedAuth.setIp(oldIp); - cachedAuth.setLastLogin(oldLastLogin); - cachedAuth.setRealName(oldRealName); - } - } - } - }); - return true; + return result; } /** @@ -170,38 +126,17 @@ public class CacheDataSource implements DataSource { * * @param auth PlayerAuth * - * @return boolean * @see fr.xephi.authme.datasource.DataSource#updateQuitLoc(PlayerAuth) + * @return boolean + * + * @see fr.xephi.authme.datasource.DataSource#updateQuitLoc(PlayerAuth) */ @Override public boolean updateQuitLoc(final PlayerAuth auth) { - if (!cache.containsKey(auth.getNickname())) { - return false; + boolean result = source.updateSession(auth); + if (result) { + cachedAuths.refresh(auth.getNickname()); } - final PlayerAuth cachedAuth = cache.get(auth.getNickname()); - final double oldX = cachedAuth.getQuitLocX(); - final double oldY = cachedAuth.getQuitLocY(); - final double oldZ = cachedAuth.getQuitLocZ(); - final String oldWorld = cachedAuth.getWorld(); - - cachedAuth.setQuitLocX(auth.getQuitLocX()); - cachedAuth.setQuitLocY(auth.getQuitLocY()); - cachedAuth.setQuitLocZ(auth.getQuitLocZ()); - cachedAuth.setWorld(auth.getWorld()); - exec.execute(new Runnable() { - @Override - public void run() { - if (!source.updateQuitLoc(auth)) { - if (cache.containsKey(auth.getNickname())) { - PlayerAuth cachedAuth = cache.get(auth.getNickname()); - cachedAuth.setQuitLocX(oldX); - cachedAuth.setQuitLocY(oldY); - cachedAuth.setQuitLocZ(oldZ); - cachedAuth.setWorld(oldWorld); - } - } - } - }); - return true; + return result; } /** @@ -209,17 +144,13 @@ public class CacheDataSource implements DataSource { * * @param ip String * - * @return int * @see fr.xephi.authme.datasource.DataSource#getIps(String) + * @return int + * + * @see fr.xephi.authme.datasource.DataSource#getIps(String) */ @Override public int getIps(String ip) { - int count = 0; - for (Map.Entry p : cache.entrySet()) { - if (p.getValue().getIp().equals(ip)) { - count++; - } - } - return count; + return source.getIps(ip); } /** @@ -227,15 +158,17 @@ public class CacheDataSource implements DataSource { * * @param until long * - * @return int * @see fr.xephi.authme.datasource.DataSource#purgeDatabase(long) + * @return int + * + * @see fr.xephi.authme.datasource.DataSource#purgeDatabase(long) */ @Override public int purgeDatabase(long until) { int cleared = source.purgeDatabase(until); if (cleared > 0) { - for (PlayerAuth auth : cache.values()) { - if (auth.getLastLogin() < until) { - cache.remove(auth.getNickname()); + for (PlayerAuth auth : cachedAuths.asMap().values()) { + if (auth != null && auth.getLastLogin() < until) { + cachedAuths.invalidate(auth.getNickname()); } } } @@ -247,17 +180,15 @@ public class CacheDataSource implements DataSource { * * @param until long * - * @return List * @see fr.xephi.authme.datasource.DataSource#autoPurgeDatabase(long) + * @return List + * + * @see fr.xephi.authme.datasource.DataSource#autoPurgeDatabase(long) */ @Override public List autoPurgeDatabase(long until) { List cleared = source.autoPurgeDatabase(until); - if (cleared.size() > 0) { - for (PlayerAuth auth : cache.values()) { - if (auth.getLastLogin() < until) { - cache.remove(auth.getNickname()); - } - } + for (String name : cleared) { + cachedAuths.invalidate(name); } return cleared; } @@ -265,24 +196,20 @@ public class CacheDataSource implements DataSource { /** * Method removeAuth. * - * @param username String + * @param name String * - * @return boolean * @see fr.xephi.authme.datasource.DataSource#removeAuth(String) + * @return boolean + * + * @see fr.xephi.authme.datasource.DataSource#removeAuth(String) */ @Override - public synchronized boolean removeAuth(String username) { - final String user = username.toLowerCase(); - final PlayerAuth auth = cache.get(user); - cache.remove(user); - exec.execute(new Runnable() { - @Override - public void run() { - if (!source.removeAuth(user)) { - cache.put(user, auth); - } - } - }); - return true; + public synchronized boolean removeAuth(String name) { + name = name.toLowerCase(); + boolean result = source.removeAuth(name); + if (result) { + cachedAuths.invalidate(name); + } + return result; } /** @@ -302,19 +229,12 @@ public class CacheDataSource implements DataSource { * @see fr.xephi.authme.datasource.DataSource#reload() */ @Override - public void reload() { + public void reload() { // unused method exec.execute(new Runnable() { @Override public void run() { - cache.clear(); source.reload(); - for (Player player : Utils.getOnlinePlayers()) { - String user = player.getName().toLowerCase(); - if (PlayerCache.getInstance().isAuthenticated(user)) { - PlayerAuth auth = source.getAuth(user); - cache.put(user, auth); - } - } + cachedAuths.invalidateAll(); } }); } @@ -324,19 +244,17 @@ public class CacheDataSource implements DataSource { * * @param auth PlayerAuth * - * @return boolean * @see fr.xephi.authme.datasource.DataSource#updateEmail(PlayerAuth) + * @return boolean + * + * @see fr.xephi.authme.datasource.DataSource#updateEmail(PlayerAuth) */ @Override public synchronized boolean updateEmail(final PlayerAuth auth) { - try { - return exec.submit(new Callable() { - public Boolean call() { - return source.updateEmail(auth); - } - }).get(); - } catch (Exception e) { - return false; + boolean result = source.updateEmail(auth); + if (result) { + cachedAuths.refresh(auth.getNickname()); } + return result; } /** @@ -344,27 +262,17 @@ public class CacheDataSource implements DataSource { * * @param auth PlayerAuth * - * @return boolean * @see fr.xephi.authme.datasource.DataSource#updateSalt(PlayerAuth) + * @return boolean + * + * @see fr.xephi.authme.datasource.DataSource#updateSalt(PlayerAuth) */ @Override public synchronized boolean updateSalt(final PlayerAuth auth) { - if (!cache.containsKey(auth.getNickname())) { - return false; + boolean result = source.updateSalt(auth); + if (result) { + cachedAuths.refresh(auth.getNickname()); } - PlayerAuth cachedAuth = cache.get(auth.getNickname()); - final String oldSalt = cachedAuth.getSalt(); - cachedAuth.setSalt(auth.getSalt()); - exec.execute(new Runnable() { - @Override - public void run() { - if (!source.updateSalt(auth)) { - if (cache.containsKey(auth.getNickname())) { - cache.get(auth.getNickname()).setSalt(oldSalt); - } - } - } - }); - return true; + return result; } /** @@ -372,17 +280,13 @@ public class CacheDataSource implements DataSource { * * @param auth PlayerAuth * - * @return List * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByName(PlayerAuth) + * @return List + * + * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByName(PlayerAuth) */ @Override public synchronized List getAllAuthsByName(PlayerAuth auth) { - List result = new ArrayList<>(); - for (Map.Entry stringPlayerAuthEntry : cache.entrySet()) { - PlayerAuth p = stringPlayerAuthEntry.getValue(); - if (p.getIp().equals(auth.getIp())) - result.add(p.getNickname()); - } - return result; + return source.getAllAuthsByName(auth); } /** @@ -390,15 +294,13 @@ public class CacheDataSource implements DataSource { * * @param ip String * - * @return List * @throws Exception * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByIp(String) + * @return List + * + * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByIp(String) */ @Override - public synchronized List getAllAuthsByIp(final String ip) throws Exception { - return exec.submit(new Callable>() { - public List call() throws Exception { - return source.getAllAuthsByIp(ip); - } - }).get(); + public synchronized List getAllAuthsByIp(final String ip) { + return source.getAllAuthsByIp(ip); } /** @@ -406,15 +308,13 @@ public class CacheDataSource implements DataSource { * * @param email String * - * @return List * @throws Exception * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByEmail(String) + * @return List + * + * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByEmail(String) */ @Override - public synchronized List getAllAuthsByEmail(final String email) throws Exception { - return exec.submit(new Callable>() { - public List call() throws Exception { - return source.getAllAuthsByEmail(email); - } - }).get(); + public synchronized List getAllAuthsByEmail(final String email) { + return source.getAllAuthsByEmail(email); } /** @@ -422,7 +322,7 @@ public class CacheDataSource implements DataSource { * * @param banned List * - * @see fr.xephi.authme.datasource.DataSource#purgeBanned(List) + * @see fr.xephi.authme.datasource.DataSource#purgeBanned(List) */ @Override public synchronized void purgeBanned(final List banned) { @@ -430,11 +330,7 @@ public class CacheDataSource implements DataSource { @Override public void run() { source.purgeBanned(banned); - for (PlayerAuth auth : cache.values()) { - if (banned.contains(auth.getNickname())) { - cache.remove(auth.getNickname()); - } - } + cachedAuths.invalidateAll(banned); } }); } @@ -442,7 +338,9 @@ public class CacheDataSource implements DataSource { /** * Method getType. * - * @return DataSourceType * @see fr.xephi.authme.datasource.DataSource#getType() + * @return DataSourceType + * + * @see fr.xephi.authme.datasource.DataSource#getType() */ @Override public DataSourceType getType() { @@ -454,12 +352,13 @@ public class CacheDataSource implements DataSource { * * @param user String * - * @return boolean * @see fr.xephi.authme.datasource.DataSource#isLogged(String) + * @return boolean + * + * @see fr.xephi.authme.datasource.DataSource#isLogged(String) */ @Override public boolean isLogged(String user) { - user = user.toLowerCase(); - return PlayerCache.getInstance().getCache().containsKey(user); + return PlayerCache.getInstance().isAuthenticated(user); } /** @@ -507,6 +406,7 @@ public class CacheDataSource implements DataSource { @Override public void run() { source.purgeLogged(); + cachedAuths.invalidateAll(); } }); } @@ -514,11 +414,13 @@ public class CacheDataSource implements DataSource { /** * Method getAccountsRegistered. * - * @return int * @see fr.xephi.authme.datasource.DataSource#getAccountsRegistered() + * @return int + * + * @see fr.xephi.authme.datasource.DataSource#getAccountsRegistered() */ @Override public int getAccountsRegistered() { - return cache.size(); + return source.getAccountsRegistered(); } /** @@ -531,14 +433,11 @@ public class CacheDataSource implements DataSource { */ @Override public void updateName(final String oldOne, final String newOne) { - if (cache.containsKey(oldOne)) { - cache.put(newOne, cache.get(oldOne)); - cache.remove(oldOne); - } exec.execute(new Runnable() { @Override public void run() { source.updateName(oldOne, newOne); + cachedAuths.invalidate(oldOne); } }); } @@ -546,17 +445,21 @@ public class CacheDataSource implements DataSource { /** * Method getAllAuths. * - * @return List * @see fr.xephi.authme.datasource.DataSource#getAllAuths() + * @return List + * + * @see fr.xephi.authme.datasource.DataSource#getAllAuths() */ @Override public List getAllAuths() { - return new ArrayList<>(cache.values()); + return source.getAllAuths(); } /** * Method getLoggedPlayers. * - * @return List * @see fr.xephi.authme.datasource.DataSource#getLoggedPlayers() + * @return List + * + * @see fr.xephi.authme.datasource.DataSource#getLoggedPlayers() */ @Override public List getLoggedPlayers() { diff --git a/src/main/java/fr/xephi/authme/datasource/DataSource.java b/src/main/java/fr/xephi/authme/datasource/DataSource.java index 39251b0ad..e43dffa85 100644 --- a/src/main/java/fr/xephi/authme/datasource/DataSource.java +++ b/src/main/java/fr/xephi/authme/datasource/DataSource.java @@ -114,7 +114,7 @@ public interface DataSource { * * @return List * @throws Exception */ - List getAllAuthsByIp(String ip) throws Exception; + List getAllAuthsByIp(String ip); /** * Method getAllAuthsByEmail. @@ -123,7 +123,7 @@ public interface DataSource { * * @return List * @throws Exception */ - List getAllAuthsByEmail(String email) throws Exception; + List getAllAuthsByEmail(String email); /** * Method updateEmail. diff --git a/src/main/java/fr/xephi/authme/datasource/FlatFile.java b/src/main/java/fr/xephi/authme/datasource/FlatFile.java index 19c93ea3a..40047979d 100644 --- a/src/main/java/fr/xephi/authme/datasource/FlatFile.java +++ b/src/main/java/fr/xephi/authme/datasource/FlatFile.java @@ -6,7 +6,13 @@ import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.settings.Settings; -import java.io.*; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -564,7 +570,7 @@ public class FlatFile implements DataSource { BufferedReader br = null; try { br = new BufferedReader(new FileReader(source)); - String line = ""; + String line; while ((line = br.readLine()) != null) { String[] args = line.split(":"); if (args[0].equals(auth.getNickname())) { @@ -721,7 +727,7 @@ public class FlatFile implements DataSource { * * @param banned List * - * @see fr.xephi.authme.datasource.DataSource#purgeBanned(List) + * @see fr.xephi.authme.datasource.DataSource#purgeBanned(List) */ @Override public void purgeBanned(List banned) { @@ -856,7 +862,7 @@ public class FlatFile implements DataSource { @Override public void updateName(String oldOne, String newOne) { PlayerAuth auth = this.getAuth(oldOne); - auth.setName(newOne); + auth.setNickname(newOne); this.saveAuth(auth); this.removeAuth(oldOne); } diff --git a/src/main/java/fr/xephi/authme/datasource/MySQL.java b/src/main/java/fr/xephi/authme/datasource/MySQL.java index 93a2d3456..e779afff6 100644 --- a/src/main/java/fr/xephi/authme/datasource/MySQL.java +++ b/src/main/java/fr/xephi/authme/datasource/MySQL.java @@ -655,6 +655,7 @@ public class MySQL implements DataSource { while (rs.next()) { list.add(rs.getString(columnName)); } + rs.close(); sql = "DELETE FROM " + tableName + " WHERE " + columnLastLogin + "<" + until; st.executeUpdate(sql); st.close(); @@ -915,11 +916,10 @@ public class MySQL implements DataSource { * * @return List * - * @throws SQLException * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByEmail(String) */ @Override - public synchronized List getAllAuthsByEmail(String email) throws SQLException { + public synchronized List getAllAuthsByEmail(String email){ List countEmail = new ArrayList<>(); try (Connection con = getConnection()) { String sql = "SELECT " + columnName + " FROM " + tableName + " WHERE " + columnEmail + "=?;"; @@ -931,8 +931,11 @@ public class MySQL implements DataSource { } rs.close(); pst.close(); - return countEmail; + } catch (SQLException ex) { + ConsoleLogger.showError(ex.getMessage()); + ConsoleLogger.writeStackTrace(ex); } + return countEmail; } /** diff --git a/src/main/java/fr/xephi/authme/hooks/BungeeCordMessage.java b/src/main/java/fr/xephi/authme/hooks/BungeeCordMessage.java index d6e25117d..f8a1b8d55 100644 --- a/src/main/java/fr/xephi/authme/hooks/BungeeCordMessage.java +++ b/src/main/java/fr/xephi/authme/hooks/BungeeCordMessage.java @@ -49,25 +49,32 @@ public class BungeeCordMessage implements PluginMessageListener { if (subChannel.equalsIgnoreCase("AuthMe")) { String str = in.readUTF(); String[] args = str.split(";"); - String name = args[1]; - PlayerAuth auth = plugin.database.getAuth(name); - if (auth == null) { - return; - } - if ("login".equals(args[0])) { - PlayerCache.getInstance().updatePlayer(auth); - plugin.database.setLogged(name); - ConsoleLogger.info("Player " + auth.getNickname() + " has logged in from one of your server!"); - } else if ("logout".equals(args[0])) { - PlayerCache.getInstance().removePlayer(name); - plugin.database.setUnlogged(name); - ConsoleLogger.info("Player " + auth.getNickname() + " has logged out from one of your server!"); - } else if ("register".equals(args[0])) { - if (plugin.database instanceof CacheDataSource) { - ((CacheDataSource)plugin.database).addAuthToCache(auth); + final String act = args[0]; + final String name = args[1]; + plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() { + @Override + public void run() { + PlayerAuth auth = plugin.database.getAuth(name); + if (auth == null) { + return; + } + if ("login".equals(act)) { + PlayerCache.getInstance().updatePlayer(auth); + plugin.database.setLogged(name); + ConsoleLogger.info("Player " + auth.getNickname() + + " has logged in from one of your server!"); + } else if ("logout".equals(act)) { + PlayerCache.getInstance().removePlayer(name); + plugin.database.setUnlogged(name); + ConsoleLogger.info("Player " + auth.getNickname() + + " has logged out from one of your server!"); + } else if ("register".equals(act)) { + ConsoleLogger.info("Player " + auth.getNickname() + + " has registered out from one of your server!"); + } + } - ConsoleLogger.info("Player " + auth.getNickname() + " has registered out from one of your server!"); - } + }); } }