From 030e9b599e86b38073a2cdf73f58507df876ed7b Mon Sep 17 00:00:00 2001 From: Xephi59 Date: Sat, 27 Jun 2015 04:19:59 +0200 Subject: [PATCH] Dramastically increase performance Maybe fix low tps :O --- src/main/java/fr/xephi/authme/AuthMe.java | 36 +- .../java/fr/xephi/authme/DataManager.java | 292 ++-- .../xephi/authme/cache/auth/PlayerAuth.java | 13 + .../fr/xephi/authme/converter/SqlToFlat.java | 7 +- .../authme/datasource/CacheDataSource.java | 495 +++--- .../xephi/authme/datasource/DataSource.java | 136 +- .../authme/datasource/DatabaseCalls.java | 382 +++++ .../{FlatFileThread.java => FlatFile.java} | 1484 ++++++++--------- .../{MySQLThread.java => MySQL.java} | 5 +- .../{SQLiteThread.java => SQLite.java} | 5 +- .../authme/listener/AuthMePlayerListener.java | 9 +- .../fr/xephi/authme/process/Management.java | 109 +- 12 files changed, 1673 insertions(+), 1300 deletions(-) create mode 100644 src/main/java/fr/xephi/authme/datasource/DatabaseCalls.java rename src/main/java/fr/xephi/authme/datasource/{FlatFileThread.java => FlatFile.java} (97%) rename src/main/java/fr/xephi/authme/datasource/{MySQLThread.java => MySQL.java} (97%) rename src/main/java/fr/xephi/authme/datasource/{SQLiteThread.java => SQLite.java} (96%) diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index cd7e19a6..4f44363a 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -54,9 +54,10 @@ import fr.xephi.authme.commands.RegisterCommand; import fr.xephi.authme.commands.UnregisterCommand; import fr.xephi.authme.datasource.CacheDataSource; import fr.xephi.authme.datasource.DataSource; -import fr.xephi.authme.datasource.FlatFileThread; -import fr.xephi.authme.datasource.MySQLThread; -import fr.xephi.authme.datasource.SQLiteThread; +import fr.xephi.authme.datasource.DatabaseCalls; +import fr.xephi.authme.datasource.FlatFile; +import fr.xephi.authme.datasource.MySQL; +import fr.xephi.authme.datasource.SQLite; import fr.xephi.authme.listener.AuthMeBlockListener; import fr.xephi.authme.listener.AuthMeChestShopListener; import fr.xephi.authme.listener.AuthMeEntityListener; @@ -102,7 +103,6 @@ public class AuthMe extends JavaPlugin { public HashMap realIp = new HashMap(); public MultiverseCore multiverse = null; public Location essentialsSpawn; - public Thread databaseThread = null; public LookupService ls = null; public boolean antibotMod = false; public boolean delayedAntiBot = true; @@ -205,26 +205,20 @@ public class AuthMe extends JavaPlugin { */ switch (Settings.getDataSource) { case FILE: - FlatFileThread fileThread = new FlatFileThread(); - fileThread.start(); + FlatFile fileThread = new FlatFile(); database = fileThread; - databaseThread = fileThread; final int a = database.getAccountsRegistered(); if (a >= 1000) { ConsoleLogger.showError("YOU'RE USING FILE DATABASE WITH " + a + "+ ACCOUNTS, FOR BETTER PERFORMANCES, PLEASE USE MYSQL!!"); } break; case MYSQL: - MySQLThread sqlThread = new MySQLThread(); - sqlThread.start(); + MySQL sqlThread = new MySQL(); database = sqlThread; - databaseThread = sqlThread; break; case SQLITE: - SQLiteThread sqliteThread = new SQLiteThread(); - sqliteThread.start(); + SQLite sqliteThread = new SQLite(); database = sqliteThread; - databaseThread = sqliteThread; final int b = database.getAccountsRegistered(); if (b >= 2000) { ConsoleLogger.showError("YOU'RE USING SQLITE DATABASE WITH " + b + "+ ACCOUNTS, FOR BETTER PERFORMANCES, PLEASE USE MYSQL!!"); @@ -234,19 +228,17 @@ public class AuthMe extends JavaPlugin { if (Settings.isCachingEnabled) { database = new CacheDataSource(this, database); - if (database instanceof CacheDataSource) - ((CacheDataSource) database).start(); } + database = new DatabaseCalls(this, database); + dataManager = new DataManager(this, database); - dataManager.start(); // Setup API api = new API(this, database); // Setup Management management = new Management(database, this); - management.start(); PluginManager pm = getServer().getPluginManager(); if (Settings.bungee) { @@ -485,16 +477,6 @@ public class AuthMe extends JavaPlugin { database.close(); } - if (databaseThread != null) { - if (databaseThread.isAlive()) - databaseThread.interrupt(); - } - - if (dataManager != null) { - if (dataManager.isAlive()) - dataManager.interrupt(); - } - if (Settings.isBackupActivated && Settings.isBackupOnStop) { Boolean Backup = new PerformBackup(this).DoBackup(); if (Backup) diff --git a/src/main/java/fr/xephi/authme/DataManager.java b/src/main/java/fr/xephi/authme/DataManager.java index 5d2cd9fe..a1de30f3 100644 --- a/src/main/java/fr/xephi/authme/DataManager.java +++ b/src/main/java/fr/xephi/authme/DataManager.java @@ -1,138 +1,154 @@ -package fr.xephi.authme; - -import java.io.File; -import java.util.List; - -import net.milkbowl.vault.permission.Permission; - -import org.bukkit.Bukkit; -import org.bukkit.OfflinePlayer; -import org.bukkit.entity.Player; - -import fr.xephi.authme.datasource.DataSource; -import fr.xephi.authme.settings.Settings; - -public class DataManager extends Thread { - - public AuthMe plugin; - public DataSource database; - - public DataManager(AuthMe plugin, DataSource database) { - this.plugin = plugin; - this.database = database; - } - - public void run() { - } - - public OfflinePlayer getOfflinePlayer(String name) { - OfflinePlayer result = null; - try { - for (OfflinePlayer op : Bukkit.getOfflinePlayers()) - if (op.getName().equalsIgnoreCase(name)) { - result = op; - break; - } - } catch (Exception e) { - } - return result; - } - - public void purgeAntiXray(List cleared) { - int i = 0; - for (String name : cleared) { - try { - org.bukkit.OfflinePlayer player = getOfflinePlayer(name); - if (player == null) - continue; - String playerName = player.getName(); - File playerFile = new File("." + File.separator + "plugins" + File.separator + "AntiXRayData" + File.separator + "PlayerData" + File.separator + playerName); - if (playerFile.exists()) { - playerFile.delete(); - i++; - } - } catch (Exception e) { - } - } - ConsoleLogger.info("AutoPurgeDatabase : Remove " + i + " AntiXRayData Files"); - } - - public void purgeLimitedCreative(List cleared) { - int i = 0; - for (String name : cleared) { - try { - org.bukkit.OfflinePlayer player = getOfflinePlayer(name); - if (player == null) - continue; - String playerName = player.getName(); - File playerFile = new File("." + File.separator + "plugins" + File.separator + "LimitedCreative" + File.separator + "inventories" + File.separator + playerName + ".yml"); - if (playerFile.exists()) { - playerFile.delete(); - i++; - } - playerFile = new File("." + File.separator + "plugins" + File.separator + "LimitedCreative" + File.separator + "inventories" + File.separator + playerName + "_creative.yml"); - if (playerFile.exists()) { - playerFile.delete(); - i++; - } - playerFile = new File("." + File.separator + "plugins" + File.separator + "LimitedCreative" + File.separator + "inventories" + File.separator + playerName + "_adventure.yml"); - if (playerFile.exists()) { - playerFile.delete(); - i++; - } - } catch (Exception e) { - } - } - ConsoleLogger.info("AutoPurgeDatabase : Remove " + i + " LimitedCreative Survival, Creative and Adventure files"); - } - - public void purgeDat(List cleared) { - int i = 0; - for (String name : cleared) { - try { - org.bukkit.OfflinePlayer player = getOfflinePlayer(name); - if (player == null) - continue; - String playerName = player.getName(); - File playerFile = new File(plugin.getServer().getWorldContainer() + File.separator + Settings.defaultWorld + File.separator + "players" + File.separator + playerName + ".dat"); - if (playerFile.exists()) { - playerFile.delete(); - i++; - } - } catch (Exception e) { - } - } - ConsoleLogger.info("AutoPurgeDatabase : Remove " + i + " .dat Files"); - } - - public void purgeEssentials(List cleared) { - int i = 0; - for (String name : cleared) { - try { - File playerFile = new File(plugin.ess.getDataFolder() + File.separator + "userdata" + File.separator + name + ".yml"); - if (playerFile.exists()) { - playerFile.delete(); - i++; - } - } catch (Exception e) { - } - } - ConsoleLogger.info("AutoPurgeDatabase : Remove " + i + " EssentialsFiles"); - } - - @SuppressWarnings("deprecation") - public void purgePermissions(List cleared, Permission permission) { - int i = 0; - for (String name : cleared) { - try { - OfflinePlayer p = Bukkit.getOfflinePlayer(name); - for (String group : permission.getPlayerGroups((Player) p)) { - permission.playerRemoveGroup(null, p, group); - } - i++; - } catch (Exception e) { - } - } - ConsoleLogger.info("AutoPurgeDatabase : Remove " + i + " Permissions"); - } -} +package fr.xephi.authme; + +import java.io.File; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import net.milkbowl.vault.permission.Permission; + +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; + +import fr.xephi.authme.datasource.DataSource; +import fr.xephi.authme.settings.Settings; + +public class DataManager { + + public AuthMe plugin; + public DataSource database; + + public DataManager(AuthMe plugin, DataSource database) { + this.plugin = plugin; + this.database = database; + } + + public void run() { + } + + public synchronized OfflinePlayer getOfflinePlayer(final String name) { + ExecutorService executor = Executors.newSingleThreadExecutor(); + Future result = executor.submit(new Callable() { + + public synchronized OfflinePlayer call() throws Exception { + OfflinePlayer result = null; + try { + for (OfflinePlayer op : Bukkit.getOfflinePlayers()) + if (op.getName().equalsIgnoreCase(name)) { + result = op; + break; + } + } catch (Exception e) { + } + return result; + } + }); + try { + return result.get(); + } catch (InterruptedException | ExecutionException e) { + return (null); + } + } + + public synchronized void purgeAntiXray(List cleared) { + int i = 0; + for (String name : cleared) { + try { + org.bukkit.OfflinePlayer player = getOfflinePlayer(name); + if (player == null) + continue; + String playerName = player.getName(); + File playerFile = new File("." + File.separator + "plugins" + File.separator + "AntiXRayData" + File.separator + "PlayerData" + File.separator + playerName); + if (playerFile.exists()) { + playerFile.delete(); + i++; + } + } catch (Exception e) { + } + } + ConsoleLogger.info("AutoPurgeDatabase : Remove " + i + " AntiXRayData Files"); + } + + public synchronized void purgeLimitedCreative(List cleared) { + int i = 0; + for (String name : cleared) { + try { + org.bukkit.OfflinePlayer player = getOfflinePlayer(name); + if (player == null) + continue; + String playerName = player.getName(); + File playerFile = new File("." + File.separator + "plugins" + File.separator + "LimitedCreative" + File.separator + "inventories" + File.separator + playerName + ".yml"); + if (playerFile.exists()) { + playerFile.delete(); + i++; + } + playerFile = new File("." + File.separator + "plugins" + File.separator + "LimitedCreative" + File.separator + "inventories" + File.separator + playerName + "_creative.yml"); + if (playerFile.exists()) { + playerFile.delete(); + i++; + } + playerFile = new File("." + File.separator + "plugins" + File.separator + "LimitedCreative" + File.separator + "inventories" + File.separator + playerName + "_adventure.yml"); + if (playerFile.exists()) { + playerFile.delete(); + i++; + } + } catch (Exception e) { + } + } + ConsoleLogger.info("AutoPurgeDatabase : Remove " + i + " LimitedCreative Survival, Creative and Adventure files"); + } + + public synchronized void purgeDat(List cleared) { + int i = 0; + for (String name : cleared) { + try { + org.bukkit.OfflinePlayer player = getOfflinePlayer(name); + if (player == null) + continue; + String playerName = player.getName(); + File playerFile = new File(plugin.getServer().getWorldContainer() + File.separator + Settings.defaultWorld + File.separator + "players" + File.separator + playerName + ".dat"); + if (playerFile.exists()) { + playerFile.delete(); + i++; + } + } catch (Exception e) { + } + } + ConsoleLogger.info("AutoPurgeDatabase : Remove " + i + " .dat Files"); + } + + public void purgeEssentials(List cleared) { + int i = 0; + for (String name : cleared) { + try { + File playerFile = new File(plugin.ess.getDataFolder() + File.separator + "userdata" + File.separator + name + ".yml"); + if (playerFile.exists()) { + playerFile.delete(); + i++; + } + } catch (Exception e) { + } + } + ConsoleLogger.info("AutoPurgeDatabase : Remove " + i + " EssentialsFiles"); + } + + public synchronized void purgePermissions(List cleared, + Permission permission) { + int i = 0; + for (String name : cleared) { + try { + OfflinePlayer p = this.getOfflinePlayer(name); + for (String group : permission.getPlayerGroups((Player) p)) { + permission.playerRemoveGroup(null, p, group); + } + i++; + } catch (Exception e) { + } + } + ConsoleLogger.info("AutoPurgeDatabase : Remove " + i + " Permissions"); + } +} 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 5a20575c..9a83f9d7 100644 --- a/src/main/java/fr/xephi/authme/cache/auth/PlayerAuth.java +++ b/src/main/java/fr/xephi/authme/cache/auth/PlayerAuth.java @@ -243,4 +243,17 @@ public class PlayerAuth { this.nickname = nickname; } + public void set(PlayerAuth auth) { + this.setEmail(auth.getEmail()); + this.setHash(auth.getHash()); + this.setIp(auth.getIp()); + this.setLastLogin(auth.getLastLogin()); + this.setName(auth.getNickname()); + this.setQuitLocX(auth.getQuitLocX()); + this.setQuitLocY(auth.getQuitLocY()); + this.setQuitLocZ(auth.getQuitLocZ()); + this.setSalt(auth.getSalt()); + this.setWorld(auth.getWorld()); + } + } diff --git a/src/main/java/fr/xephi/authme/converter/SqlToFlat.java b/src/main/java/fr/xephi/authme/converter/SqlToFlat.java index a44cc2bf..0194e412 100644 --- a/src/main/java/fr/xephi/authme/converter/SqlToFlat.java +++ b/src/main/java/fr/xephi/authme/converter/SqlToFlat.java @@ -8,7 +8,7 @@ import fr.xephi.authme.AuthMe; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.datasource.DataSource; -import fr.xephi.authme.datasource.FlatFileThread; +import fr.xephi.authme.datasource.FlatFile; import fr.xephi.authme.settings.Messages; public class SqlToFlat implements Converter { @@ -26,8 +26,7 @@ public class SqlToFlat implements Converter { @Override public void run() { try { - FlatFileThread flat = new FlatFileThread(); - flat.start(); + FlatFile flat = new FlatFile(); List auths = database.getAllAuths(); int i = 0; final int size = auths.size(); @@ -38,8 +37,6 @@ public class SqlToFlat implements Converter { sender.sendMessage("Conversion Status : " + i + " / " + size); } } - if (flat != null && flat.isAlive()) - flat.interrupt(); sender.sendMessage("Successfully convert from SQL table to file auths.db"); return; } catch (Exception ex) { diff --git a/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java b/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java index fb764438..88a39b13 100644 --- a/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java +++ b/src/main/java/fr/xephi/authme/datasource/CacheDataSource.java @@ -1,250 +1,245 @@ -package fr.xephi.authme.datasource; - -import java.util.List; -import java.util.concurrent.ConcurrentHashMap; -import org.bukkit.entity.Player; - -import fr.xephi.authme.AuthMe; -import fr.xephi.authme.cache.auth.PlayerAuth; -import fr.xephi.authme.cache.auth.PlayerCache; - -public class CacheDataSource extends Thread implements DataSource { - - private DataSource source; - public AuthMe plugin; - private ConcurrentHashMap cache = new ConcurrentHashMap(); - - public CacheDataSource(AuthMe plugin, DataSource source) { - this.plugin = plugin; - this.source = source; - /* - * 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 ! - */ - for(PlayerAuth auth : source.getAllAuths()) - cache.put(auth.getNickname().toLowerCase(), auth); - } - - public void run() - { - this.setName("AuthMeCacheThread"); - } - - @Override - public synchronized boolean isAuthAvailable(String user) { - if (cache.containsKey(user.toLowerCase())) - return true; - return false; - } - - @Override - public synchronized PlayerAuth getAuth(String user) { - user = user.toLowerCase(); - if (cache.containsKey(user)) { - return cache.get(user); - } else { - PlayerAuth auth = source.getAuth(user); - if (auth != null) - cache.put(user, auth); - return auth; - } - } - - @Override - public synchronized boolean saveAuth(PlayerAuth auth) { - if (source.saveAuth(auth)) { - cache.put(auth.getNickname(), auth); - return true; - } - return false; - } - - @Override - public synchronized boolean updatePassword(PlayerAuth auth) { - if (source.updatePassword(auth)) { - if (cache.containsKey(auth.getNickname())) - cache.get(auth.getNickname()).setHash(auth.getHash()); - return true; - } - return false; - } - - @Override - public boolean updateSession(PlayerAuth auth) { - if (source.updateSession(auth)) { - if (cache.containsKey(auth.getNickname())) { - cache.get(auth.getNickname()).setIp(auth.getIp()); - cache.get(auth.getNickname()).setLastLogin(auth.getLastLogin()); - } - return true; - } - return false; - } - - @Override - public boolean updateQuitLoc(PlayerAuth auth) { - if (source.updateQuitLoc(auth)) { - if (cache.containsKey(auth.getNickname())) { - cache.get(auth.getNickname()).setQuitLocX(auth.getQuitLocX()); - cache.get(auth.getNickname()).setQuitLocY(auth.getQuitLocY()); - cache.get(auth.getNickname()).setQuitLocZ(auth.getQuitLocZ()); - cache.get(auth.getNickname()).setWorld(auth.getWorld()); - } - return true; - } - return false; - } - - @Override - public int getIps(String ip) { - return source.getIps(ip); - } - - @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()); - } - } - } - return cleared; - } - - @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()); - } - } - } - return cleared; - } - - @Override - public synchronized boolean removeAuth(String user) { - if (source.removeAuth(user)) { - cache.remove(user); - return true; - } - return false; - } - - @Override - public synchronized void close() { - source.close(); - this.interrupt(); - } - - @Override - public void reload() { - cache.clear(); - source.reload(); - for (Player player : plugin.getServer().getOnlinePlayers()) { - String user = player.getName().toLowerCase(); - if (PlayerCache.getInstance().isAuthenticated(user)) { - try { - PlayerAuth auth = source.getAuth(user); - cache.put(user, auth); - } catch (NullPointerException npe) { - } - - } - } - } - - @Override - public synchronized boolean updateEmail(PlayerAuth auth) { - if (source.updateEmail(auth)) { - if (cache.containsKey(auth.getNickname())) - cache.get(auth.getNickname()).setEmail(auth.getEmail()); - return true; - } - return false; - } - - @Override - public synchronized boolean updateSalt(PlayerAuth auth) { - if (source.updateSalt(auth)) { - if (cache.containsKey(auth.getNickname())) - cache.get(auth.getNickname()).setSalt(auth.getSalt()); - return true; - } - return false; - } - - @Override - public synchronized List getAllAuthsByName(PlayerAuth auth) { - return source.getAllAuthsByName(auth); - } - - @Override - public synchronized List getAllAuthsByIp(String ip) { - return source.getAllAuthsByIp(ip); - } - - @Override - public synchronized List getAllAuthsByEmail(String email) { - return source.getAllAuthsByEmail(email); - } - - @Override - public synchronized void purgeBanned(List banned) { - source.purgeBanned(banned); - for (PlayerAuth auth : cache.values()) { - if (banned.contains(auth.getNickname())) { - cache.remove(auth.getNickname()); - } - } - } - - @Override - public DataSourceType getType() { - return source.getType(); - } - - @Override - public boolean isLogged(String user) { - return source.isLogged(user.toLowerCase()); - } - - @Override - public void setLogged(String user) { - source.setLogged(user.toLowerCase()); - } - - @Override - public void setUnlogged(String user) { - source.setUnlogged(user.toLowerCase()); - } - - @Override - public void purgeLogged() { - source.purgeLogged(); - } - - @Override - public int getAccountsRegistered() { - return source.getAccountsRegistered(); - } - - @Override - public void updateName(String oldone, String newone) { - if (cache.containsKey(oldone)) { - cache.put(newone, cache.get(oldone)); - cache.remove(oldone); - } - source.updateName(oldone, newone); - } - - @Override - public List getAllAuths() { - return source.getAllAuths(); - } -} +package fr.xephi.authme.datasource; + +import java.util.List; +import java.util.concurrent.ConcurrentHashMap; + +import org.bukkit.entity.Player; + +import fr.xephi.authme.AuthMe; +import fr.xephi.authme.cache.auth.PlayerAuth; +import fr.xephi.authme.cache.auth.PlayerCache; + +public class CacheDataSource implements DataSource { + + private DataSource source; + public AuthMe plugin; + private ConcurrentHashMap cache = new ConcurrentHashMap(); + + public CacheDataSource(AuthMe plugin, DataSource source) { + this.plugin = plugin; + this.source = source; + /* + * 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 ! + */ + for (PlayerAuth auth : source.getAllAuths()) + cache.put(auth.getNickname().toLowerCase(), auth); + } + + @Override + public synchronized boolean isAuthAvailable(String user) { + if (cache.containsKey(user.toLowerCase())) + return true; + return false; + } + + @Override + public synchronized PlayerAuth getAuth(String user) { + user = user.toLowerCase(); + if (cache.containsKey(user)) { + return cache.get(user); + } else { + PlayerAuth auth = source.getAuth(user); + if (auth != null) + cache.put(user, auth); + return auth; + } + } + + @Override + public synchronized boolean saveAuth(PlayerAuth auth) { + if (source.saveAuth(auth)) { + cache.put(auth.getNickname(), auth); + return true; + } + return false; + } + + @Override + public synchronized boolean updatePassword(PlayerAuth auth) { + if (source.updatePassword(auth)) { + if (cache.containsKey(auth.getNickname())) + cache.get(auth.getNickname()).setHash(auth.getHash()); + return true; + } + return false; + } + + @Override + public boolean updateSession(PlayerAuth auth) { + if (source.updateSession(auth)) { + if (cache.containsKey(auth.getNickname())) { + cache.get(auth.getNickname()).setIp(auth.getIp()); + cache.get(auth.getNickname()).setLastLogin(auth.getLastLogin()); + } + return true; + } + return false; + } + + @Override + public boolean updateQuitLoc(PlayerAuth auth) { + if (source.updateQuitLoc(auth)) { + if (cache.containsKey(auth.getNickname())) { + cache.get(auth.getNickname()).setQuitLocX(auth.getQuitLocX()); + cache.get(auth.getNickname()).setQuitLocY(auth.getQuitLocY()); + cache.get(auth.getNickname()).setQuitLocZ(auth.getQuitLocZ()); + cache.get(auth.getNickname()).setWorld(auth.getWorld()); + } + return true; + } + return false; + } + + @Override + public int getIps(String ip) { + return source.getIps(ip); + } + + @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()); + } + } + } + return cleared; + } + + @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()); + } + } + } + return cleared; + } + + @Override + public synchronized boolean removeAuth(String user) { + if (source.removeAuth(user)) { + cache.remove(user); + return true; + } + return false; + } + + @Override + public synchronized void close() { + source.close(); + } + + @Override + public void reload() { + cache.clear(); + source.reload(); + for (Player player : plugin.getServer().getOnlinePlayers()) { + String user = player.getName().toLowerCase(); + if (PlayerCache.getInstance().isAuthenticated(user)) { + try { + PlayerAuth auth = source.getAuth(user); + cache.put(user, auth); + } catch (NullPointerException npe) { + } + + } + } + } + + @Override + public synchronized boolean updateEmail(PlayerAuth auth) { + if (source.updateEmail(auth)) { + if (cache.containsKey(auth.getNickname())) + cache.get(auth.getNickname()).setEmail(auth.getEmail()); + return true; + } + return false; + } + + @Override + public synchronized boolean updateSalt(PlayerAuth auth) { + if (source.updateSalt(auth)) { + if (cache.containsKey(auth.getNickname())) + cache.get(auth.getNickname()).setSalt(auth.getSalt()); + return true; + } + return false; + } + + @Override + public synchronized List getAllAuthsByName(PlayerAuth auth) { + return source.getAllAuthsByName(auth); + } + + @Override + public synchronized List getAllAuthsByIp(String ip) { + return source.getAllAuthsByIp(ip); + } + + @Override + public synchronized List getAllAuthsByEmail(String email) { + return source.getAllAuthsByEmail(email); + } + + @Override + public synchronized void purgeBanned(List banned) { + source.purgeBanned(banned); + for (PlayerAuth auth : cache.values()) { + if (banned.contains(auth.getNickname())) { + cache.remove(auth.getNickname()); + } + } + } + + @Override + public DataSourceType getType() { + return source.getType(); + } + + @Override + public boolean isLogged(String user) { + return source.isLogged(user.toLowerCase()); + } + + @Override + public void setLogged(String user) { + source.setLogged(user.toLowerCase()); + } + + @Override + public void setUnlogged(String user) { + source.setUnlogged(user.toLowerCase()); + } + + @Override + public void purgeLogged() { + source.purgeLogged(); + } + + @Override + public int getAccountsRegistered() { + return source.getAccountsRegistered(); + } + + @Override + public void updateName(String oldone, String newone) { + if (cache.containsKey(oldone)) { + cache.put(newone, cache.get(oldone)); + cache.remove(oldone); + } + source.updateName(oldone, newone); + } + + @Override + public List getAllAuths() { + return source.getAllAuths(); + } +} diff --git a/src/main/java/fr/xephi/authme/datasource/DataSource.java b/src/main/java/fr/xephi/authme/datasource/DataSource.java index 0c893d6a..4c940051 100644 --- a/src/main/java/fr/xephi/authme/datasource/DataSource.java +++ b/src/main/java/fr/xephi/authme/datasource/DataSource.java @@ -1,68 +1,68 @@ -package fr.xephi.authme.datasource; - -import java.util.List; - -import fr.xephi.authme.cache.auth.PlayerAuth; - -public interface DataSource { - - public enum DataSourceType { - - MYSQL, - FILE, - SQLITE - } - - boolean isAuthAvailable(String user); - - PlayerAuth getAuth(String user); - - boolean saveAuth(PlayerAuth auth); - - boolean updateSession(PlayerAuth auth); - - boolean updatePassword(PlayerAuth auth); - - int purgeDatabase(long until); - - List autoPurgeDatabase(long until); - - boolean removeAuth(String user); - - boolean updateQuitLoc(PlayerAuth auth); - - int getIps(String ip); - - List getAllAuthsByName(PlayerAuth auth); - - List getAllAuthsByIp(String ip); - - List getAllAuthsByEmail(String email); - - boolean updateEmail(PlayerAuth auth); - - boolean updateSalt(PlayerAuth auth); - - void close(); - - void reload(); - - void purgeBanned(List banned); - - DataSourceType getType(); - - boolean isLogged(String user); - - void setLogged(String user); - - void setUnlogged(String user); - - void purgeLogged(); - - int getAccountsRegistered(); - - void updateName(String oldone, String newone); - - List getAllAuths(); - -} +package fr.xephi.authme.datasource; + +import java.util.List; + +import fr.xephi.authme.cache.auth.PlayerAuth; + +public interface DataSource { + + public enum DataSourceType { + + MYSQL, + FILE, + SQLITE + } + + boolean isAuthAvailable(String user); + + PlayerAuth getAuth(String user); + + boolean saveAuth(PlayerAuth auth); + + boolean updateSession(PlayerAuth auth); + + boolean updatePassword(PlayerAuth auth); + + int purgeDatabase(long until); + + List autoPurgeDatabase(long until); + + boolean removeAuth(String user); + + boolean updateQuitLoc(PlayerAuth auth); + + int getIps(String ip); + + List getAllAuthsByName(PlayerAuth auth); + + List getAllAuthsByIp(String ip); + + List getAllAuthsByEmail(String email); + + boolean updateEmail(PlayerAuth auth); + + boolean updateSalt(PlayerAuth auth); + + void close(); + + void reload(); + + void purgeBanned(List banned); + + DataSourceType getType(); + + boolean isLogged(String user); + + void setLogged(String user); + + void setUnlogged(String user); + + void purgeLogged(); + + int getAccountsRegistered(); + + void updateName(String oldone, String newone); + + List getAllAuths(); + +} diff --git a/src/main/java/fr/xephi/authme/datasource/DatabaseCalls.java b/src/main/java/fr/xephi/authme/datasource/DatabaseCalls.java new file mode 100644 index 00000000..0091ec65 --- /dev/null +++ b/src/main/java/fr/xephi/authme/datasource/DatabaseCalls.java @@ -0,0 +1,382 @@ +package fr.xephi.authme.datasource; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import fr.xephi.authme.AuthMe; +import fr.xephi.authme.cache.auth.PlayerAuth; + +public class DatabaseCalls implements DataSource { + + private AuthMe plugin; + private DataSource database; + + public DatabaseCalls(AuthMe plugin, DataSource database) { + this.plugin = plugin; + this.database = database; + } + + @Override + public synchronized boolean isAuthAvailable(final String user) { + ExecutorService executor = Executors.newSingleThreadExecutor(); + Future result = executor.submit(new Callable() { + + public Boolean call() throws Exception { + return database.isAuthAvailable(user); + } + }); + try { + return result.get(); + } catch (InterruptedException | ExecutionException e) { + return (false); + } + } + + @Override + public synchronized PlayerAuth getAuth(final String user) { + ExecutorService executor = Executors.newSingleThreadExecutor(); + Future result = executor.submit(new Callable() { + + public PlayerAuth call() throws Exception { + return database.getAuth(user); + } + }); + try { + return result.get(); + } catch (InterruptedException | ExecutionException e) { + return null; + } + } + + @Override + public synchronized boolean saveAuth(final PlayerAuth auth) { + ExecutorService executor = Executors.newSingleThreadExecutor(); + Future result = executor.submit(new Callable() { + + public Boolean call() throws Exception { + return database.saveAuth(auth); + } + }); + try { + return result.get(); + } catch (InterruptedException | ExecutionException e) { + return (false); + } + } + + @Override + public synchronized boolean updateSession(final PlayerAuth auth) { + ExecutorService executor = Executors.newSingleThreadExecutor(); + Future result = executor.submit(new Callable() { + + public Boolean call() throws Exception { + return database.updateSession(auth); + } + }); + try { + return result.get(); + } catch (InterruptedException | ExecutionException e) { + return (false); + } + } + + @Override + public synchronized boolean updatePassword(final PlayerAuth auth) { + ExecutorService executor = Executors.newSingleThreadExecutor(); + Future result = executor.submit(new Callable() { + + public Boolean call() throws Exception { + return database.updatePassword(auth); + } + }); + try { + return result.get(); + } catch (InterruptedException | ExecutionException e) { + return (false); + } + } + + @Override + public synchronized int purgeDatabase(final long until) { + ExecutorService executor = Executors.newSingleThreadExecutor(); + Future result = executor.submit(new Callable() { + + public Integer call() throws Exception { + return database.purgeDatabase(until); + } + }); + try { + return result.get(); + } catch (InterruptedException | ExecutionException e) { + return (0); + } + } + + @Override + public synchronized List autoPurgeDatabase(final long until) { + ExecutorService executor = Executors.newSingleThreadExecutor(); + Future> result = executor.submit(new Callable>() { + + public List call() throws Exception { + return database.autoPurgeDatabase(until); + } + }); + try { + return result.get(); + } catch (InterruptedException | ExecutionException e) { + return (new ArrayList()); + } + } + + @Override + public synchronized boolean removeAuth(final String user) { + ExecutorService executor = Executors.newSingleThreadExecutor(); + Future result = executor.submit(new Callable() { + + public Boolean call() throws Exception { + return database.removeAuth(user); + } + }); + try { + return result.get(); + } catch (InterruptedException | ExecutionException e) { + return (false); + } + } + + @Override + public synchronized boolean updateQuitLoc(final PlayerAuth auth) { + ExecutorService executor = Executors.newSingleThreadExecutor(); + Future result = executor.submit(new Callable() { + + public Boolean call() throws Exception { + return database.updateQuitLoc(auth); + } + }); + try { + return result.get(); + } catch (InterruptedException | ExecutionException e) { + return (false); + } + } + + @Override + public synchronized int getIps(final String ip) { + ExecutorService executor = Executors.newSingleThreadExecutor(); + Future result = executor.submit(new Callable() { + + public Integer call() throws Exception { + return database.getIps(ip); + } + }); + try { + return result.get(); + } catch (InterruptedException | ExecutionException e) { + return (0); + } + } + + @Override + public synchronized List getAllAuthsByName(final PlayerAuth auth) { + ExecutorService executor = Executors.newSingleThreadExecutor(); + Future> result = executor.submit(new Callable>() { + + public List call() throws Exception { + return database.getAllAuthsByName(auth); + } + }); + try { + return result.get(); + } catch (InterruptedException | ExecutionException e) { + return (new ArrayList()); + } + } + + @Override + public synchronized List getAllAuthsByIp(final String ip) { + ExecutorService executor = Executors.newSingleThreadExecutor(); + Future> result = executor.submit(new Callable>() { + + public List call() throws Exception { + return database.getAllAuthsByIp(ip); + } + }); + try { + return result.get(); + } catch (InterruptedException | ExecutionException e) { + return (new ArrayList()); + } + } + + @Override + public synchronized List getAllAuthsByEmail(final String email) { + ExecutorService executor = Executors.newSingleThreadExecutor(); + Future> result = executor.submit(new Callable>() { + + public List call() throws Exception { + return database.getAllAuthsByEmail(email); + } + }); + try { + return result.get(); + } catch (InterruptedException | ExecutionException e) { + return (new ArrayList()); + } + } + + @Override + public synchronized boolean updateEmail(final PlayerAuth auth) { + ExecutorService executor = Executors.newSingleThreadExecutor(); + Future result = executor.submit(new Callable() { + + public Boolean call() throws Exception { + return database.updateEmail(auth); + } + }); + try { + return result.get(); + } catch (InterruptedException | ExecutionException e) { + return (false); + } + } + + @Override + public synchronized boolean updateSalt(final PlayerAuth auth) { + ExecutorService executor = Executors.newSingleThreadExecutor(); + Future result = executor.submit(new Callable() { + + public Boolean call() throws Exception { + return database.updateSalt(auth); + } + }); + try { + return result.get(); + } catch (InterruptedException | ExecutionException e) { + return (false); + } + } + + @Override + public synchronized void close() { + database.close(); + } + + @Override + public synchronized void reload() { + database.reload(); + } + + @Override + public synchronized void purgeBanned(final List banned) { + new Thread(new Runnable() { + + @Override + public synchronized void run() { + database.purgeBanned(banned); + } + }).start(); + } + + @Override + public synchronized DataSourceType getType() { + return database.getType(); + } + + @Override + public synchronized boolean isLogged(final String user) { + ExecutorService executor = Executors.newSingleThreadExecutor(); + Future result = executor.submit(new Callable() { + + public Boolean call() throws Exception { + return database.isLogged(user); + } + }); + try { + return result.get(); + } catch (InterruptedException | ExecutionException e) { + return (false); + } + } + + @Override + public synchronized void setLogged(final String user) { + new Thread(new Runnable() { + + @Override + public synchronized void run() { + database.setLogged(user); + } + }).start(); + } + + @Override + public synchronized void setUnlogged(final String user) { + new Thread(new Runnable() { + + @Override + public synchronized void run() { + database.setUnlogged(user); + } + }).start(); + } + + @Override + public synchronized void purgeLogged() { + new Thread(new Runnable() { + + @Override + public synchronized void run() { + database.purgeLogged(); + } + }).start(); + } + + @Override + public synchronized int getAccountsRegistered() { + ExecutorService executor = Executors.newSingleThreadExecutor(); + Future result = executor.submit(new Callable() { + + public Integer call() throws Exception { + return database.getAccountsRegistered(); + } + }); + try { + return result.get(); + } catch (InterruptedException | ExecutionException e) { + return (0); + } + } + + @Override + public synchronized void updateName(final String oldone, final String newone) { + new Thread(new Runnable() { + + @Override + public synchronized void run() { + database.updateName(oldone, newone); + } + }).start(); + } + + @Override + public synchronized List getAllAuths() { + ExecutorService executor = Executors.newSingleThreadExecutor(); + Future> result = executor.submit(new Callable>() { + + public List call() throws Exception { + return database.getAllAuths(); + } + }); + try { + return result.get(); + } catch (InterruptedException | ExecutionException e) { + return (new ArrayList()); + } + } + +} diff --git a/src/main/java/fr/xephi/authme/datasource/FlatFileThread.java b/src/main/java/fr/xephi/authme/datasource/FlatFile.java similarity index 97% rename from src/main/java/fr/xephi/authme/datasource/FlatFileThread.java rename to src/main/java/fr/xephi/authme/datasource/FlatFile.java index 262a64bf..654f72e5 100644 --- a/src/main/java/fr/xephi/authme/datasource/FlatFileThread.java +++ b/src/main/java/fr/xephi/authme/datasource/FlatFile.java @@ -1,744 +1,740 @@ -package fr.xephi.authme.datasource; - -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; - -import fr.xephi.authme.AuthMe; -import fr.xephi.authme.ConsoleLogger; -import fr.xephi.authme.cache.auth.PlayerAuth; -import fr.xephi.authme.settings.PlayersLogs; -import fr.xephi.authme.settings.Settings; - -public class FlatFileThread extends Thread implements DataSource { - - /* - * file layout: - * - * PLAYERNAME:HASHSUM:IP:LOGININMILLIESECONDS:LASTPOSX:LASTPOSY:LASTPOSZ: - * LASTPOSWORLD:EMAIL - * - * Old but compatible: - * PLAYERNAME:HASHSUM:IP:LOGININMILLIESECONDS:LASTPOSX:LASTPOSY - * :LASTPOSZ:LASTPOSWORLD PLAYERNAME:HASHSUM:IP:LOGININMILLIESECONDS - * PLAYERNAME:HASHSUM:IP PLAYERNAME:HASHSUM - */ - private File source; - - public FlatFileThread() { - source = new File(Settings.AUTH_FILE); - } - - public void run() { - try { - source.createNewFile(); - } catch (IOException e) { - ConsoleLogger.showError(e.getMessage()); - if (Settings.isStopEnabled) { - ConsoleLogger.showError("Can't use FLAT FILE... SHUTDOWN..."); - AuthMe.getInstance().getServer().shutdown(); - } - if (!Settings.isStopEnabled) - AuthMe.getInstance().getServer().getPluginManager().disablePlugin(AuthMe.getInstance()); - return; - } - this.setName("AuthMeFlatFileThread"); - } - - @Override - public synchronized boolean isAuthAvailable(String user) { - BufferedReader br = null; - try { - br = new BufferedReader(new FileReader(source)); - String line; - while ((line = br.readLine()) != null) { - String[] args = line.split(":"); - if (args.length > 1 && args[0].equalsIgnoreCase(user)) { - return true; - } - } - } catch (FileNotFoundException ex) { - ConsoleLogger.showError(ex.getMessage()); - return false; - } catch (IOException ex) { - ConsoleLogger.showError(ex.getMessage()); - return false; - } finally { - if (br != null) { - try { - br.close(); - } catch (IOException ex) { - } - } - } - return false; - } - - @Override - public synchronized boolean saveAuth(PlayerAuth auth) { - if (isAuthAvailable(auth.getNickname())) { - return false; - } - BufferedWriter bw = null; - try { - bw = new BufferedWriter(new FileWriter(source, true)); - bw.write(auth.getNickname() + ":" + auth.getHash() + ":" + auth.getIp() + ":" + auth.getLastLogin() + ":" + auth.getQuitLocX() + ":" + auth.getQuitLocY() + ":" + auth.getQuitLocZ() + ":" + auth.getWorld() + ":" + auth.getEmail() + "\n"); - } catch (IOException ex) { - ConsoleLogger.showError(ex.getMessage()); - return false; - } finally { - if (bw != null) { - try { - bw.close(); - } catch (IOException ex) { - } - } - } - return true; - } - - @Override - public synchronized boolean updatePassword(PlayerAuth auth) { - if (!isAuthAvailable(auth.getNickname())) { - return false; - } - PlayerAuth newAuth = null; - BufferedReader br = null; - try { - br = new BufferedReader(new FileReader(source)); - String line = ""; - while ((line = br.readLine()) != null) { - String[] args = line.split(":"); - if (args[0].equals(auth.getNickname())) { - switch (args.length) { - case 4: { - newAuth = new PlayerAuth(args[0], auth.getHash(), args[2], Long.parseLong(args[3]), 0, 0, 0, "world", "your@email.com"); - break; - } - case 7: { - newAuth = new PlayerAuth(args[0], auth.getHash(), args[2], Long.parseLong(args[3]), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), "world", "your@email.com"); - break; - } - case 8: { - newAuth = new PlayerAuth(args[0], auth.getHash(), args[2], Long.parseLong(args[3]), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), args[7], "your@email.com"); - break; - } - case 9: { - newAuth = new PlayerAuth(args[0], auth.getHash(), args[2], Long.parseLong(args[3]), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), args[7], args[8]); - break; - } - default: { - newAuth = new PlayerAuth(args[0], auth.getHash(), args[2], 0, 0, 0, 0, "world", "your@email.com"); - break; - } - } - break; - } - } - } catch (FileNotFoundException ex) { - ConsoleLogger.showError(ex.getMessage()); - return false; - } catch (IOException ex) { - ConsoleLogger.showError(ex.getMessage()); - return false; - } finally { - if (br != null) { - try { - br.close(); - } catch (IOException ex) { - } - } - } - removeAuth(auth.getNickname()); - saveAuth(newAuth); - return true; - } - - @Override - public boolean updateSession(PlayerAuth auth) { - if (!isAuthAvailable(auth.getNickname())) { - return false; - } - PlayerAuth newAuth = null; - BufferedReader br = null; - try { - br = new BufferedReader(new FileReader(source)); - String line = ""; - while ((line = br.readLine()) != null) { - String[] args = line.split(":"); - if (args[0].equalsIgnoreCase(auth.getNickname())) { - switch (args.length) { - case 4: { - newAuth = new PlayerAuth(args[0], args[1], auth.getIp(), auth.getLastLogin(), 0, 0, 0, "world", "your@email.com"); - break; - } - case 7: { - newAuth = new PlayerAuth(args[0], args[1], auth.getIp(), auth.getLastLogin(), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), "world", "your@email.com"); - break; - } - case 8: { - newAuth = new PlayerAuth(args[0], args[1], auth.getIp(), auth.getLastLogin(), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), args[7], "your@email.com"); - break; - } - case 9: { - newAuth = new PlayerAuth(args[0], args[1], auth.getIp(), auth.getLastLogin(), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), args[7], args[8]); - break; - } - default: { - newAuth = new PlayerAuth(args[0], args[1], auth.getIp(), auth.getLastLogin(), 0, 0, 0, "world", "your@email.com"); - break; - } - } - break; - } - } - } catch (FileNotFoundException ex) { - ConsoleLogger.showError(ex.getMessage()); - return false; - } catch (IOException ex) { - ConsoleLogger.showError(ex.getMessage()); - return false; - } finally { - if (br != null) { - try { - br.close(); - } catch (IOException ex) { - } - } - } - removeAuth(auth.getNickname()); - saveAuth(newAuth); - return true; - } - - @Override - public boolean updateQuitLoc(PlayerAuth auth) { - if (!isAuthAvailable(auth.getNickname())) { - return false; - } - PlayerAuth newAuth = null; - BufferedReader br = null; - try { - br = new BufferedReader(new FileReader(source)); - String line = ""; - while ((line = br.readLine()) != null) { - String[] args = line.split(":"); - if (args[0].equalsIgnoreCase(auth.getNickname())) { - newAuth = new PlayerAuth(args[0], args[1], args[2], Long.parseLong(args[3]), auth.getQuitLocX(), auth.getQuitLocY(), auth.getQuitLocZ(), auth.getWorld(), auth.getEmail()); - break; - } - } - } catch (FileNotFoundException ex) { - ConsoleLogger.showError(ex.getMessage()); - return false; - } catch (IOException ex) { - ConsoleLogger.showError(ex.getMessage()); - return false; - } finally { - if (br != null) { - try { - br.close(); - } catch (IOException ex) { - } - } - } - removeAuth(auth.getNickname()); - saveAuth(newAuth); - return true; - } - - @Override - public int getIps(String ip) { - BufferedReader br = null; - int countIp = 0; - try { - br = new BufferedReader(new FileReader(source)); - String line; - while ((line = br.readLine()) != null) { - String[] args = line.split(":"); - if (args.length > 3 && args[2].equals(ip)) { - countIp++; - } - } - return countIp; - } catch (FileNotFoundException ex) { - ConsoleLogger.showError(ex.getMessage()); - return 0; - } catch (IOException ex) { - ConsoleLogger.showError(ex.getMessage()); - return 0; - } finally { - if (br != null) { - try { - br.close(); - } catch (IOException ex) { - } - } - } - } - - @Override - public int purgeDatabase(long until) { - BufferedReader br = null; - BufferedWriter bw = null; - ArrayList lines = new ArrayList(); - int cleared = 0; - try { - br = new BufferedReader(new FileReader(source)); - String line; - while ((line = br.readLine()) != null) { - String[] args = line.split(":"); - if (args.length >= 4) { - if (Long.parseLong(args[3]) >= until) { - lines.add(line); - continue; - } - } - cleared++; - } - bw = new BufferedWriter(new FileWriter(source)); - for (String l : lines) { - bw.write(l + "\n"); - } - } catch (FileNotFoundException ex) { - ConsoleLogger.showError(ex.getMessage()); - return cleared; - } catch (IOException ex) { - ConsoleLogger.showError(ex.getMessage()); - return cleared; - } finally { - if (br != null) { - try { - br.close(); - } catch (IOException ex) { - } - } - if (bw != null) { - try { - bw.close(); - } catch (IOException ex) { - } - } - } - return cleared; - } - - @Override - public List autoPurgeDatabase(long until) { - BufferedReader br = null; - BufferedWriter bw = null; - ArrayList lines = new ArrayList(); - List cleared = new ArrayList(); - try { - br = new BufferedReader(new FileReader(source)); - String line; - while ((line = br.readLine()) != null) { - String[] args = line.split(":"); - if (args.length >= 4) { - if (Long.parseLong(args[3]) >= until) { - lines.add(line); - continue; - } - } - cleared.add(args[0]); - } - bw = new BufferedWriter(new FileWriter(source)); - for (String l : lines) { - bw.write(l + "\n"); - } - } catch (FileNotFoundException ex) { - ConsoleLogger.showError(ex.getMessage()); - return cleared; - } catch (IOException ex) { - ConsoleLogger.showError(ex.getMessage()); - return cleared; - } finally { - if (br != null) { - try { - br.close(); - } catch (IOException ex) { - } - } - if (bw != null) { - try { - bw.close(); - } catch (IOException ex) { - } - } - } - return cleared; - } - - @Override - public synchronized boolean removeAuth(String user) { - if (!isAuthAvailable(user)) { - return false; - } - BufferedReader br = null; - BufferedWriter bw = null; - ArrayList lines = new ArrayList(); - try { - br = new BufferedReader(new FileReader(source)); - String line; - while ((line = br.readLine()) != null) { - String[] args = line.split(":"); - if (args.length > 1 && !args[0].equals(user)) { - lines.add(line); - } - } - bw = new BufferedWriter(new FileWriter(source)); - for (String l : lines) { - bw.write(l + "\n"); - } - } catch (FileNotFoundException ex) { - ConsoleLogger.showError(ex.getMessage()); - return false; - } catch (IOException ex) { - ConsoleLogger.showError(ex.getMessage()); - return false; - } finally { - if (br != null) { - try { - br.close(); - } catch (IOException ex) { - } - } - if (bw != null) { - try { - bw.close(); - } catch (IOException ex) { - } - } - } - return true; - } - - @Override - public synchronized PlayerAuth getAuth(String user) { - BufferedReader br = null; - try { - br = new BufferedReader(new FileReader(source)); - String line; - while ((line = br.readLine()) != null) { - String[] args = line.split(":"); - if (args[0].equalsIgnoreCase(user)) { - switch (args.length) { - case 2: - return new PlayerAuth(args[0], args[1], "198.18.0.1", 0, "your@email.com"); - case 3: - return new PlayerAuth(args[0], args[1], args[2], 0, "your@email.com"); - case 4: - return new PlayerAuth(args[0], args[1], args[2], Long.parseLong(args[3]), "your@email.com"); - case 7: - return new PlayerAuth(args[0], args[1], args[2], Long.parseLong(args[3]), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), "unavailableworld", "your@email.com"); - case 8: - return new PlayerAuth(args[0], args[1], args[2], Long.parseLong(args[3]), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), args[7], "your@email.com"); - case 9: - return new PlayerAuth(args[0], args[1], args[2], Long.parseLong(args[3]), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), args[7], args[8]); - } - } - } - } catch (FileNotFoundException ex) { - ConsoleLogger.showError(ex.getMessage()); - return null; - } catch (IOException ex) { - ConsoleLogger.showError(ex.getMessage()); - return null; - } finally { - if (br != null) { - try { - br.close(); - } catch (IOException ex) { - } - } - } - return null; - } - - @Override - public synchronized void close() { - } - - @Override - public void reload() { - } - - @Override - public boolean updateEmail(PlayerAuth auth) { - if (!isAuthAvailable(auth.getNickname())) { - return false; - } - PlayerAuth newAuth = null; - BufferedReader br = null; - try { - br = new BufferedReader(new FileReader(source)); - String line = ""; - while ((line = br.readLine()) != null) { - String[] args = line.split(":"); - if (args[0].equals(auth.getNickname())) { - newAuth = new PlayerAuth(args[0], args[1], args[2], Long.parseLong(args[3]), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), args[7], auth.getEmail()); - break; - } - } - } catch (FileNotFoundException ex) { - ConsoleLogger.showError(ex.getMessage()); - return false; - } catch (IOException ex) { - ConsoleLogger.showError(ex.getMessage()); - return false; - } finally { - if (br != null) { - try { - br.close(); - } catch (IOException ex) { - } - } - } - removeAuth(auth.getNickname()); - saveAuth(newAuth); - return true; - } - - @Override - public boolean updateSalt(PlayerAuth auth) { - return false; - } - - @Override - public List getAllAuthsByName(PlayerAuth auth) { - BufferedReader br = null; - List countIp = new ArrayList(); - try { - br = new BufferedReader(new FileReader(source)); - String line; - while ((line = br.readLine()) != null) { - String[] args = line.split(":"); - if (args.length > 3 && args[2].equals(auth.getIp())) { - countIp.add(args[0]); - } - } - return countIp; - } catch (FileNotFoundException ex) { - ConsoleLogger.showError(ex.getMessage()); - return new ArrayList(); - } catch (IOException ex) { - ConsoleLogger.showError(ex.getMessage()); - return new ArrayList(); - } finally { - if (br != null) { - try { - br.close(); - } catch (IOException ex) { - } - } - } - } - - @Override - public List getAllAuthsByIp(String ip) { - BufferedReader br = null; - List countIp = new ArrayList(); - try { - br = new BufferedReader(new FileReader(source)); - String line; - while ((line = br.readLine()) != null) { - String[] args = line.split(":"); - if (args.length > 3 && args[2].equals(ip)) { - countIp.add(args[0]); - } - } - return countIp; - } catch (FileNotFoundException ex) { - ConsoleLogger.showError(ex.getMessage()); - return new ArrayList(); - } catch (IOException ex) { - ConsoleLogger.showError(ex.getMessage()); - return new ArrayList(); - } finally { - if (br != null) { - try { - br.close(); - } catch (IOException ex) { - } - } - } - } - - @Override - public List getAllAuthsByEmail(String email) { - BufferedReader br = null; - List countEmail = new ArrayList(); - try { - br = new BufferedReader(new FileReader(source)); - String line; - while ((line = br.readLine()) != null) { - String[] args = line.split(":"); - if (args.length > 8 && args[8].equals(email)) { - countEmail.add(args[0]); - } - } - return countEmail; - } catch (FileNotFoundException ex) { - ConsoleLogger.showError(ex.getMessage()); - return new ArrayList(); - } catch (IOException ex) { - ConsoleLogger.showError(ex.getMessage()); - return new ArrayList(); - } finally { - if (br != null) { - try { - br.close(); - } catch (IOException ex) { - } - } - } - } - - @Override - public void purgeBanned(List banned) { - BufferedReader br = null; - BufferedWriter bw = null; - ArrayList lines = new ArrayList(); - try { - br = new BufferedReader(new FileReader(source)); - String line; - while ((line = br.readLine()) != null) { - String[] args = line.split(":"); - try { - if (banned.contains(args[0])) { - lines.add(line); - } - } catch (NullPointerException npe) { - } catch (ArrayIndexOutOfBoundsException aioobe) { - } - } - bw = new BufferedWriter(new FileWriter(source)); - for (String l : lines) { - bw.write(l + "\n"); - } - } catch (FileNotFoundException ex) { - ConsoleLogger.showError(ex.getMessage()); - return; - } catch (IOException ex) { - ConsoleLogger.showError(ex.getMessage()); - return; - } finally { - if (br != null) { - try { - br.close(); - } catch (IOException ex) { - } - } - if (bw != null) { - try { - bw.close(); - } catch (IOException ex) { - } - } - } - return; - } - - @Override - public DataSourceType getType() { - return DataSourceType.FILE; - } - - @Override - public boolean isLogged(String user) { - return PlayersLogs.getInstance().players.contains(user.toLowerCase()); - } - - @Override - public void setLogged(String user) { - PlayersLogs.getInstance().addPlayer(user.toLowerCase()); - } - - @Override - public void setUnlogged(String user) { - PlayersLogs.getInstance().removePlayer(user.toLowerCase()); - } - - @Override - public void purgeLogged() { - PlayersLogs.getInstance().clear(); - } - - @Override - public int getAccountsRegistered() { - BufferedReader br = null; - int result = 0; - try { - br = new BufferedReader(new FileReader(source)); - while ((br.readLine()) != null) { - result++; - } - } catch (Exception ex) { - ConsoleLogger.showError(ex.getMessage()); - return result; - } finally { - if (br != null) { - try { - br.close(); - } catch (IOException ex) { - } - } - } - return result; - } - - @Override - public void updateName(String oldone, String newone) { - PlayerAuth auth = this.getAuth(oldone); - auth.setName(newone); - this.saveAuth(auth); - this.removeAuth(oldone); - } - - @Override - public List getAllAuths() { - BufferedReader br = null; - List auths = new ArrayList(); - try { - br = new BufferedReader(new FileReader(source)); - String line; - while ((line = br.readLine()) != null) { - String[] args = line.split(":"); - switch (args.length) { - case 2: - auths.add(new PlayerAuth(args[0], args[1], "198.18.0.1", 0, "your@email.com")); - case 3: - auths.add(new PlayerAuth(args[0], args[1], args[2], 0, "your@email.com")); - case 4: - auths.add(new PlayerAuth(args[0], args[1], args[2], Long.parseLong(args[3]), "your@email.com")); - case 7: - auths.add(new PlayerAuth(args[0], args[1], args[2], Long.parseLong(args[3]), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), "unavailableworld", "your@email.com")); - case 8: - auths.add(new PlayerAuth(args[0], args[1], args[2], Long.parseLong(args[3]), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), args[7], "your@email.com")); - case 9: - auths.add(new PlayerAuth(args[0], args[1], args[2], Long.parseLong(args[3]), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), args[7], args[8])); - } - } - } catch (FileNotFoundException ex) { - ConsoleLogger.showError(ex.getMessage()); - return auths; - } catch (IOException ex) { - ConsoleLogger.showError(ex.getMessage()); - return auths; - } finally { - if (br != null) { - try { - br.close(); - } catch (IOException ex) { - } - } - } - return auths; - } -} +package fr.xephi.authme.datasource; + +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; + +import fr.xephi.authme.AuthMe; +import fr.xephi.authme.ConsoleLogger; +import fr.xephi.authme.cache.auth.PlayerAuth; +import fr.xephi.authme.settings.PlayersLogs; +import fr.xephi.authme.settings.Settings; + +public class FlatFile implements DataSource { + + /* + * file layout: + * + * PLAYERNAME:HASHSUM:IP:LOGININMILLIESECONDS:LASTPOSX:LASTPOSY:LASTPOSZ: + * LASTPOSWORLD:EMAIL + * + * Old but compatible: + * PLAYERNAME:HASHSUM:IP:LOGININMILLIESECONDS:LASTPOSX:LASTPOSY + * :LASTPOSZ:LASTPOSWORLD PLAYERNAME:HASHSUM:IP:LOGININMILLIESECONDS + * PLAYERNAME:HASHSUM:IP PLAYERNAME:HASHSUM + */ + private File source; + + public FlatFile() { + source = new File(Settings.AUTH_FILE); + try { + source.createNewFile(); + } catch (IOException e) { + ConsoleLogger.showError(e.getMessage()); + if (Settings.isStopEnabled) { + ConsoleLogger.showError("Can't use FLAT FILE... SHUTDOWN..."); + AuthMe.getInstance().getServer().shutdown(); + } + if (!Settings.isStopEnabled) + AuthMe.getInstance().getServer().getPluginManager().disablePlugin(AuthMe.getInstance()); + return; + } + } + + @Override + public synchronized boolean isAuthAvailable(String user) { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(source)); + String line; + while ((line = br.readLine()) != null) { + String[] args = line.split(":"); + if (args.length > 1 && args[0].equalsIgnoreCase(user)) { + return true; + } + } + } catch (FileNotFoundException ex) { + ConsoleLogger.showError(ex.getMessage()); + return false; + } catch (IOException ex) { + ConsoleLogger.showError(ex.getMessage()); + return false; + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException ex) { + } + } + } + return false; + } + + @Override + public synchronized boolean saveAuth(PlayerAuth auth) { + if (isAuthAvailable(auth.getNickname())) { + return false; + } + BufferedWriter bw = null; + try { + bw = new BufferedWriter(new FileWriter(source, true)); + bw.write(auth.getNickname() + ":" + auth.getHash() + ":" + auth.getIp() + ":" + auth.getLastLogin() + ":" + auth.getQuitLocX() + ":" + auth.getQuitLocY() + ":" + auth.getQuitLocZ() + ":" + auth.getWorld() + ":" + auth.getEmail() + "\n"); + } catch (IOException ex) { + ConsoleLogger.showError(ex.getMessage()); + return false; + } finally { + if (bw != null) { + try { + bw.close(); + } catch (IOException ex) { + } + } + } + return true; + } + + @Override + public synchronized boolean updatePassword(PlayerAuth auth) { + if (!isAuthAvailable(auth.getNickname())) { + return false; + } + PlayerAuth newAuth = null; + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(source)); + String line = ""; + while ((line = br.readLine()) != null) { + String[] args = line.split(":"); + if (args[0].equals(auth.getNickname())) { + switch (args.length) { + case 4: { + newAuth = new PlayerAuth(args[0], auth.getHash(), args[2], Long.parseLong(args[3]), 0, 0, 0, "world", "your@email.com"); + break; + } + case 7: { + newAuth = new PlayerAuth(args[0], auth.getHash(), args[2], Long.parseLong(args[3]), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), "world", "your@email.com"); + break; + } + case 8: { + newAuth = new PlayerAuth(args[0], auth.getHash(), args[2], Long.parseLong(args[3]), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), args[7], "your@email.com"); + break; + } + case 9: { + newAuth = new PlayerAuth(args[0], auth.getHash(), args[2], Long.parseLong(args[3]), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), args[7], args[8]); + break; + } + default: { + newAuth = new PlayerAuth(args[0], auth.getHash(), args[2], 0, 0, 0, 0, "world", "your@email.com"); + break; + } + } + break; + } + } + } catch (FileNotFoundException ex) { + ConsoleLogger.showError(ex.getMessage()); + return false; + } catch (IOException ex) { + ConsoleLogger.showError(ex.getMessage()); + return false; + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException ex) { + } + } + } + removeAuth(auth.getNickname()); + saveAuth(newAuth); + return true; + } + + @Override + public boolean updateSession(PlayerAuth auth) { + if (!isAuthAvailable(auth.getNickname())) { + return false; + } + PlayerAuth newAuth = null; + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(source)); + String line = ""; + while ((line = br.readLine()) != null) { + String[] args = line.split(":"); + if (args[0].equalsIgnoreCase(auth.getNickname())) { + switch (args.length) { + case 4: { + newAuth = new PlayerAuth(args[0], args[1], auth.getIp(), auth.getLastLogin(), 0, 0, 0, "world", "your@email.com"); + break; + } + case 7: { + newAuth = new PlayerAuth(args[0], args[1], auth.getIp(), auth.getLastLogin(), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), "world", "your@email.com"); + break; + } + case 8: { + newAuth = new PlayerAuth(args[0], args[1], auth.getIp(), auth.getLastLogin(), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), args[7], "your@email.com"); + break; + } + case 9: { + newAuth = new PlayerAuth(args[0], args[1], auth.getIp(), auth.getLastLogin(), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), args[7], args[8]); + break; + } + default: { + newAuth = new PlayerAuth(args[0], args[1], auth.getIp(), auth.getLastLogin(), 0, 0, 0, "world", "your@email.com"); + break; + } + } + break; + } + } + } catch (FileNotFoundException ex) { + ConsoleLogger.showError(ex.getMessage()); + return false; + } catch (IOException ex) { + ConsoleLogger.showError(ex.getMessage()); + return false; + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException ex) { + } + } + } + removeAuth(auth.getNickname()); + saveAuth(newAuth); + return true; + } + + @Override + public boolean updateQuitLoc(PlayerAuth auth) { + if (!isAuthAvailable(auth.getNickname())) { + return false; + } + PlayerAuth newAuth = null; + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(source)); + String line = ""; + while ((line = br.readLine()) != null) { + String[] args = line.split(":"); + if (args[0].equalsIgnoreCase(auth.getNickname())) { + newAuth = new PlayerAuth(args[0], args[1], args[2], Long.parseLong(args[3]), auth.getQuitLocX(), auth.getQuitLocY(), auth.getQuitLocZ(), auth.getWorld(), auth.getEmail()); + break; + } + } + } catch (FileNotFoundException ex) { + ConsoleLogger.showError(ex.getMessage()); + return false; + } catch (IOException ex) { + ConsoleLogger.showError(ex.getMessage()); + return false; + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException ex) { + } + } + } + removeAuth(auth.getNickname()); + saveAuth(newAuth); + return true; + } + + @Override + public int getIps(String ip) { + BufferedReader br = null; + int countIp = 0; + try { + br = new BufferedReader(new FileReader(source)); + String line; + while ((line = br.readLine()) != null) { + String[] args = line.split(":"); + if (args.length > 3 && args[2].equals(ip)) { + countIp++; + } + } + return countIp; + } catch (FileNotFoundException ex) { + ConsoleLogger.showError(ex.getMessage()); + return 0; + } catch (IOException ex) { + ConsoleLogger.showError(ex.getMessage()); + return 0; + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException ex) { + } + } + } + } + + @Override + public int purgeDatabase(long until) { + BufferedReader br = null; + BufferedWriter bw = null; + ArrayList lines = new ArrayList(); + int cleared = 0; + try { + br = new BufferedReader(new FileReader(source)); + String line; + while ((line = br.readLine()) != null) { + String[] args = line.split(":"); + if (args.length >= 4) { + if (Long.parseLong(args[3]) >= until) { + lines.add(line); + continue; + } + } + cleared++; + } + bw = new BufferedWriter(new FileWriter(source)); + for (String l : lines) { + bw.write(l + "\n"); + } + } catch (FileNotFoundException ex) { + ConsoleLogger.showError(ex.getMessage()); + return cleared; + } catch (IOException ex) { + ConsoleLogger.showError(ex.getMessage()); + return cleared; + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException ex) { + } + } + if (bw != null) { + try { + bw.close(); + } catch (IOException ex) { + } + } + } + return cleared; + } + + @Override + public List autoPurgeDatabase(long until) { + BufferedReader br = null; + BufferedWriter bw = null; + ArrayList lines = new ArrayList(); + List cleared = new ArrayList(); + try { + br = new BufferedReader(new FileReader(source)); + String line; + while ((line = br.readLine()) != null) { + String[] args = line.split(":"); + if (args.length >= 4) { + if (Long.parseLong(args[3]) >= until) { + lines.add(line); + continue; + } + } + cleared.add(args[0]); + } + bw = new BufferedWriter(new FileWriter(source)); + for (String l : lines) { + bw.write(l + "\n"); + } + } catch (FileNotFoundException ex) { + ConsoleLogger.showError(ex.getMessage()); + return cleared; + } catch (IOException ex) { + ConsoleLogger.showError(ex.getMessage()); + return cleared; + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException ex) { + } + } + if (bw != null) { + try { + bw.close(); + } catch (IOException ex) { + } + } + } + return cleared; + } + + @Override + public synchronized boolean removeAuth(String user) { + if (!isAuthAvailable(user)) { + return false; + } + BufferedReader br = null; + BufferedWriter bw = null; + ArrayList lines = new ArrayList(); + try { + br = new BufferedReader(new FileReader(source)); + String line; + while ((line = br.readLine()) != null) { + String[] args = line.split(":"); + if (args.length > 1 && !args[0].equals(user)) { + lines.add(line); + } + } + bw = new BufferedWriter(new FileWriter(source)); + for (String l : lines) { + bw.write(l + "\n"); + } + } catch (FileNotFoundException ex) { + ConsoleLogger.showError(ex.getMessage()); + return false; + } catch (IOException ex) { + ConsoleLogger.showError(ex.getMessage()); + return false; + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException ex) { + } + } + if (bw != null) { + try { + bw.close(); + } catch (IOException ex) { + } + } + } + return true; + } + + @Override + public synchronized PlayerAuth getAuth(String user) { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(source)); + String line; + while ((line = br.readLine()) != null) { + String[] args = line.split(":"); + if (args[0].equalsIgnoreCase(user)) { + switch (args.length) { + case 2: + return new PlayerAuth(args[0], args[1], "198.18.0.1", 0, "your@email.com"); + case 3: + return new PlayerAuth(args[0], args[1], args[2], 0, "your@email.com"); + case 4: + return new PlayerAuth(args[0], args[1], args[2], Long.parseLong(args[3]), "your@email.com"); + case 7: + return new PlayerAuth(args[0], args[1], args[2], Long.parseLong(args[3]), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), "unavailableworld", "your@email.com"); + case 8: + return new PlayerAuth(args[0], args[1], args[2], Long.parseLong(args[3]), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), args[7], "your@email.com"); + case 9: + return new PlayerAuth(args[0], args[1], args[2], Long.parseLong(args[3]), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), args[7], args[8]); + } + } + } + } catch (FileNotFoundException ex) { + ConsoleLogger.showError(ex.getMessage()); + return null; + } catch (IOException ex) { + ConsoleLogger.showError(ex.getMessage()); + return null; + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException ex) { + } + } + } + return null; + } + + @Override + public synchronized void close() { + } + + @Override + public void reload() { + } + + @Override + public boolean updateEmail(PlayerAuth auth) { + if (!isAuthAvailable(auth.getNickname())) { + return false; + } + PlayerAuth newAuth = null; + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(source)); + String line = ""; + while ((line = br.readLine()) != null) { + String[] args = line.split(":"); + if (args[0].equals(auth.getNickname())) { + newAuth = new PlayerAuth(args[0], args[1], args[2], Long.parseLong(args[3]), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), args[7], auth.getEmail()); + break; + } + } + } catch (FileNotFoundException ex) { + ConsoleLogger.showError(ex.getMessage()); + return false; + } catch (IOException ex) { + ConsoleLogger.showError(ex.getMessage()); + return false; + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException ex) { + } + } + } + removeAuth(auth.getNickname()); + saveAuth(newAuth); + return true; + } + + @Override + public boolean updateSalt(PlayerAuth auth) { + return false; + } + + @Override + public List getAllAuthsByName(PlayerAuth auth) { + BufferedReader br = null; + List countIp = new ArrayList(); + try { + br = new BufferedReader(new FileReader(source)); + String line; + while ((line = br.readLine()) != null) { + String[] args = line.split(":"); + if (args.length > 3 && args[2].equals(auth.getIp())) { + countIp.add(args[0]); + } + } + return countIp; + } catch (FileNotFoundException ex) { + ConsoleLogger.showError(ex.getMessage()); + return new ArrayList(); + } catch (IOException ex) { + ConsoleLogger.showError(ex.getMessage()); + return new ArrayList(); + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException ex) { + } + } + } + } + + @Override + public List getAllAuthsByIp(String ip) { + BufferedReader br = null; + List countIp = new ArrayList(); + try { + br = new BufferedReader(new FileReader(source)); + String line; + while ((line = br.readLine()) != null) { + String[] args = line.split(":"); + if (args.length > 3 && args[2].equals(ip)) { + countIp.add(args[0]); + } + } + return countIp; + } catch (FileNotFoundException ex) { + ConsoleLogger.showError(ex.getMessage()); + return new ArrayList(); + } catch (IOException ex) { + ConsoleLogger.showError(ex.getMessage()); + return new ArrayList(); + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException ex) { + } + } + } + } + + @Override + public List getAllAuthsByEmail(String email) { + BufferedReader br = null; + List countEmail = new ArrayList(); + try { + br = new BufferedReader(new FileReader(source)); + String line; + while ((line = br.readLine()) != null) { + String[] args = line.split(":"); + if (args.length > 8 && args[8].equals(email)) { + countEmail.add(args[0]); + } + } + return countEmail; + } catch (FileNotFoundException ex) { + ConsoleLogger.showError(ex.getMessage()); + return new ArrayList(); + } catch (IOException ex) { + ConsoleLogger.showError(ex.getMessage()); + return new ArrayList(); + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException ex) { + } + } + } + } + + @Override + public void purgeBanned(List banned) { + BufferedReader br = null; + BufferedWriter bw = null; + ArrayList lines = new ArrayList(); + try { + br = new BufferedReader(new FileReader(source)); + String line; + while ((line = br.readLine()) != null) { + String[] args = line.split(":"); + try { + if (banned.contains(args[0])) { + lines.add(line); + } + } catch (NullPointerException npe) { + } catch (ArrayIndexOutOfBoundsException aioobe) { + } + } + bw = new BufferedWriter(new FileWriter(source)); + for (String l : lines) { + bw.write(l + "\n"); + } + } catch (FileNotFoundException ex) { + ConsoleLogger.showError(ex.getMessage()); + return; + } catch (IOException ex) { + ConsoleLogger.showError(ex.getMessage()); + return; + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException ex) { + } + } + if (bw != null) { + try { + bw.close(); + } catch (IOException ex) { + } + } + } + return; + } + + @Override + public DataSourceType getType() { + return DataSourceType.FILE; + } + + @Override + public boolean isLogged(String user) { + return PlayersLogs.getInstance().players.contains(user.toLowerCase()); + } + + @Override + public void setLogged(String user) { + PlayersLogs.getInstance().addPlayer(user.toLowerCase()); + } + + @Override + public void setUnlogged(String user) { + PlayersLogs.getInstance().removePlayer(user.toLowerCase()); + } + + @Override + public void purgeLogged() { + PlayersLogs.getInstance().clear(); + } + + @Override + public int getAccountsRegistered() { + BufferedReader br = null; + int result = 0; + try { + br = new BufferedReader(new FileReader(source)); + while ((br.readLine()) != null) { + result++; + } + } catch (Exception ex) { + ConsoleLogger.showError(ex.getMessage()); + return result; + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException ex) { + } + } + } + return result; + } + + @Override + public void updateName(String oldone, String newone) { + PlayerAuth auth = this.getAuth(oldone); + auth.setName(newone); + this.saveAuth(auth); + this.removeAuth(oldone); + } + + @Override + public List getAllAuths() { + BufferedReader br = null; + List auths = new ArrayList(); + try { + br = new BufferedReader(new FileReader(source)); + String line; + while ((line = br.readLine()) != null) { + String[] args = line.split(":"); + switch (args.length) { + case 2: + auths.add(new PlayerAuth(args[0], args[1], "198.18.0.1", 0, "your@email.com")); + case 3: + auths.add(new PlayerAuth(args[0], args[1], args[2], 0, "your@email.com")); + case 4: + auths.add(new PlayerAuth(args[0], args[1], args[2], Long.parseLong(args[3]), "your@email.com")); + case 7: + auths.add(new PlayerAuth(args[0], args[1], args[2], Long.parseLong(args[3]), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), "unavailableworld", "your@email.com")); + case 8: + auths.add(new PlayerAuth(args[0], args[1], args[2], Long.parseLong(args[3]), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), args[7], "your@email.com")); + case 9: + auths.add(new PlayerAuth(args[0], args[1], args[2], Long.parseLong(args[3]), Double.parseDouble(args[4]), Double.parseDouble(args[5]), Double.parseDouble(args[6]), args[7], args[8])); + } + } + } catch (FileNotFoundException ex) { + ConsoleLogger.showError(ex.getMessage()); + return auths; + } catch (IOException ex) { + ConsoleLogger.showError(ex.getMessage()); + return auths; + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException ex) { + } + } + } + return auths; + } +} diff --git a/src/main/java/fr/xephi/authme/datasource/MySQLThread.java b/src/main/java/fr/xephi/authme/datasource/MySQL.java similarity index 97% rename from src/main/java/fr/xephi/authme/datasource/MySQLThread.java rename to src/main/java/fr/xephi/authme/datasource/MySQL.java index 58b9767c..f2d6f5ab 100644 --- a/src/main/java/fr/xephi/authme/datasource/MySQLThread.java +++ b/src/main/java/fr/xephi/authme/datasource/MySQL.java @@ -18,7 +18,7 @@ import fr.xephi.authme.datasource.MiniConnectionPoolManager.TimeoutException; import fr.xephi.authme.security.HashAlgorithm; import fr.xephi.authme.settings.Settings; -public class MySQLThread extends Thread implements DataSource { +public class MySQL implements DataSource { private String host; private String port; @@ -42,7 +42,7 @@ public class MySQLThread extends Thread implements DataSource { private List columnOthers; private MiniConnectionPoolManager conPool; - public void run() { + public MySQL() { this.host = Settings.getMySQLHost; this.port = Settings.getMySQLPort; this.username = Settings.getMySQLUsername; @@ -94,7 +94,6 @@ public class MySQLThread extends Thread implements DataSource { AuthMe.getInstance().getServer().getPluginManager().disablePlugin(AuthMe.getInstance()); return; } - this.setName("AuthMeMySQLThread"); } private synchronized void connect() throws ClassNotFoundException, diff --git a/src/main/java/fr/xephi/authme/datasource/SQLiteThread.java b/src/main/java/fr/xephi/authme/datasource/SQLite.java similarity index 96% rename from src/main/java/fr/xephi/authme/datasource/SQLiteThread.java rename to src/main/java/fr/xephi/authme/datasource/SQLite.java index 0c77851e..cdbac849 100644 --- a/src/main/java/fr/xephi/authme/datasource/SQLiteThread.java +++ b/src/main/java/fr/xephi/authme/datasource/SQLite.java @@ -16,7 +16,7 @@ import fr.xephi.authme.datasource.MiniConnectionPoolManager.TimeoutException; import fr.xephi.authme.settings.PlayersLogs; import fr.xephi.authme.settings.Settings; -public class SQLiteThread extends Thread implements DataSource { +public class SQLite implements DataSource { private String database; private String tableName; @@ -34,7 +34,7 @@ public class SQLiteThread extends Thread implements DataSource { private String columnID; private Connection con; - public void run() { + public SQLite() { this.database = Settings.getMySQLDatabase; this.tableName = Settings.getMySQLTablename; this.columnName = Settings.getMySQLColumnName; @@ -72,7 +72,6 @@ public class SQLiteThread extends Thread implements DataSource { AuthMe.getInstance().getServer().getPluginManager().disablePlugin(AuthMe.getInstance()); return; } - this.setName("AuthMeSQLiteThread"); } private synchronized void connect() throws ClassNotFoundException, diff --git a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java index 65781060..78d145d8 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java +++ b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java @@ -357,14 +357,9 @@ public class AuthMePlayerListener implements Listener { return; } - if (!data.isAuthAvailable(name)) { - if (!Settings.isForcedRegistrationEnabled) { - return; - } - } - if (!Settings.isForcedRegistrationEnabled) { - return; + if (!data.isAuthAvailable(name)) + return; } if (!Settings.isMovementAllowed) { diff --git a/src/main/java/fr/xephi/authme/process/Management.java b/src/main/java/fr/xephi/authme/process/Management.java index 4207b565..44b4fb5e 100644 --- a/src/main/java/fr/xephi/authme/process/Management.java +++ b/src/main/java/fr/xephi/authme/process/Management.java @@ -1,55 +1,54 @@ -package fr.xephi.authme.process; - -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; -import org.bukkit.plugin.PluginManager; - -import fr.xephi.authme.AuthMe; -import fr.xephi.authme.datasource.DataSource; -import fr.xephi.authme.process.login.AsyncronousLogin; -import fr.xephi.authme.process.register.AsyncronousRegister; -import fr.xephi.authme.security.RandomString; -import fr.xephi.authme.settings.Settings; - -/** - * - * @authors Xephi59, Possible - * - */ -public class Management extends Thread { - - public DataSource database; - public AuthMe plugin; - public static RandomString rdm = new RandomString(Settings.captchaLength); - public PluginManager pm; - - public Management(DataSource database, AuthMe plugin) { - this.database = database; - this.plugin = plugin; - this.pm = plugin.getServer().getPluginManager(); - } - - public void run() { - } - - public void performLogin(final Player player, final String password, - final boolean forceLogin) { - Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable(){ - @Override - public void run() { - new AsyncronousLogin(player, password, forceLogin, plugin, database).process(); - } - }); - } - - public void performRegister(final Player player, final String password, - final String email) { - Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable(){ - @Override - public void run() { - new AsyncronousRegister(player, password, email, plugin, database).process(); - } - }); - } -} +package fr.xephi.authme.process; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.plugin.PluginManager; + +import fr.xephi.authme.AuthMe; +import fr.xephi.authme.datasource.DataSource; +import fr.xephi.authme.process.login.AsyncronousLogin; +import fr.xephi.authme.process.register.AsyncronousRegister; +import fr.xephi.authme.security.RandomString; +import fr.xephi.authme.settings.Settings; + +/** + * + * @authors Xephi59, Possible + * + */ +public class Management { + + public DataSource database; + public AuthMe plugin; + public static RandomString rdm = new RandomString(Settings.captchaLength); + public PluginManager pm; + + public Management(DataSource database, AuthMe plugin) { + this.database = database; + this.plugin = plugin; + this.pm = plugin.getServer().getPluginManager(); + } + + public void performLogin(final Player player, final String password, + final boolean forceLogin) { + Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { + + @Override + public void run() { + new AsyncronousLogin(player, password, forceLogin, plugin, database).process(); + } + }); + } + + public void performRegister(final Player player, final String password, + final String email) { + Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { + + @Override + public void run() { + new AsyncronousRegister(player, password, email, plugin, database).process(); + } + }); + } +}