mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-12-24 01:27:35 +01:00
Use Guava cache for caching PlayerAuth object.
This commit is contained in:
parent
169e38be59
commit
efcdca4087
@ -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) {
|
||||
|
@ -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,
|
||||
|
@ -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<String,PlayerAuth>
|
||||
* @return ConcurrentHashMap
|
||||
*/
|
||||
public ConcurrentHashMap<String, PlayerAuth> getCache() {
|
||||
return this.cache;
|
||||
|
@ -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<String> 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<String> accountList;
|
||||
try {
|
||||
accountList = plugin.database.getAllAuthsByIp(playerQueryFinal);
|
||||
} catch (Exception e) {
|
||||
ConsoleLogger.showError(e.getMessage());
|
||||
ConsoleLogger.writeStackTrace(e);
|
||||
m.send(sender, MessageKey.ERROR);
|
||||
return;
|
||||
}
|
||||
|
||||
List<String> 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());
|
||||
}
|
||||
});
|
||||
|
@ -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<PlayerAuth> authentications = plugin.database.getAllAuths();
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// Validate the user
|
||||
PlayerAuth auth = plugin.database.getAuth(playerName.toLowerCase());
|
||||
PlayerAuth auth = plugin.database.getAuth(playerName);
|
||||
if (auth == null) {
|
||||
m.send(sender, MessageKey.UNKNOWN_USER);
|
||||
return true;
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the email address
|
||||
|
||||
auth.setEmail(playerEmail);
|
||||
if (!plugin.database.updateEmail(auth)) {
|
||||
m.send(sender, MessageKey.ERROR);
|
||||
return true;
|
||||
return;
|
||||
}
|
||||
|
||||
// Update the player cache
|
||||
if (PlayerCache.getInstance().getAuth(playerName.toLowerCase()) != null)
|
||||
if (PlayerCache.getInstance().getAuth(playerName) != null) {
|
||||
PlayerCache.getInstance().updatePlayer(auth);
|
||||
}
|
||||
|
||||
// Show a status message
|
||||
m.send(sender, MessageKey.EMAIL_CHANGED_SUCCESS);
|
||||
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -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,49 +18,38 @@ public class CacheDataSource implements DataSource {
|
||||
|
||||
private final DataSource source;
|
||||
private final ExecutorService exec;
|
||||
private final ConcurrentHashMap<String, PlayerAuth> cache = new ConcurrentHashMap<>();
|
||||
private final LoadingCache<String, PlayerAuth> 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);
|
||||
cachedAuths = CacheBuilder.newBuilder()
|
||||
.expireAfterWrite(5, TimeUnit.MINUTES)
|
||||
.build(
|
||||
new CacheLoader<String, PlayerAuth>() {
|
||||
public PlayerAuth load(String key) {
|
||||
return source.getAuth(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void addAuthToCache(PlayerAuth auth) {
|
||||
cache.put(auth.getNickname().toLowerCase(), auth);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method isAuthAvailable.
|
||||
*
|
||||
* @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());
|
||||
public synchronized boolean saveAuth(PlayerAuth auth) {
|
||||
boolean result = source.saveAuth(auth);
|
||||
if (result) {
|
||||
cachedAuths.refresh(auth.getNickname());
|
||||
}
|
||||
}
|
||||
});
|
||||
return true;
|
||||
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<String, PlayerAuth> 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<String> * @see fr.xephi.authme.datasource.DataSource#autoPurgeDatabase(long)
|
||||
* @return List
|
||||
*
|
||||
* @see fr.xephi.authme.datasource.DataSource#autoPurgeDatabase(long)
|
||||
*/
|
||||
@Override
|
||||
public List<String> autoPurgeDatabase(long until) {
|
||||
List<String> 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);
|
||||
public synchronized boolean removeAuth(String name) {
|
||||
name = name.toLowerCase();
|
||||
boolean result = source.removeAuth(name);
|
||||
if (result) {
|
||||
cachedAuths.invalidate(name);
|
||||
}
|
||||
}
|
||||
});
|
||||
return true;
|
||||
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<Boolean>() {
|
||||
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<String> * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByName(PlayerAuth)
|
||||
* @return List
|
||||
*
|
||||
* @see fr.xephi.authme.datasource.DataSource#getAllAuthsByName(PlayerAuth)
|
||||
*/
|
||||
@Override
|
||||
public synchronized List<String> getAllAuthsByName(PlayerAuth auth) {
|
||||
List<String> result = new ArrayList<>();
|
||||
for (Map.Entry<String, PlayerAuth> stringPlayerAuthEntry : cache.entrySet()) {
|
||||
PlayerAuth p = stringPlayerAuthEntry.getValue();
|
||||
if (p.getIp().equals(auth.getIp()))
|
||||
result.add(p.getNickname());
|
||||
}
|
||||
return result;
|
||||
return source.getAllAuthsByName(auth);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -390,39 +294,35 @@ public class CacheDataSource implements DataSource {
|
||||
*
|
||||
* @param ip String
|
||||
*
|
||||
* @return List<String> * @throws Exception * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByIp(String)
|
||||
* @return List
|
||||
*
|
||||
* @see fr.xephi.authme.datasource.DataSource#getAllAuthsByIp(String)
|
||||
*/
|
||||
@Override
|
||||
public synchronized List<String> getAllAuthsByIp(final String ip) throws Exception {
|
||||
return exec.submit(new Callable<List<String>>() {
|
||||
public List<String> call() throws Exception {
|
||||
public synchronized List<String> getAllAuthsByIp(final String ip) {
|
||||
return source.getAllAuthsByIp(ip);
|
||||
}
|
||||
}).get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method getAllAuthsByEmail.
|
||||
*
|
||||
* @param email String
|
||||
*
|
||||
* @return List<String> * @throws Exception * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByEmail(String)
|
||||
* @return List
|
||||
*
|
||||
* @see fr.xephi.authme.datasource.DataSource#getAllAuthsByEmail(String)
|
||||
*/
|
||||
@Override
|
||||
public synchronized List<String> getAllAuthsByEmail(final String email) throws Exception {
|
||||
return exec.submit(new Callable<List<String>>() {
|
||||
public List<String> call() throws Exception {
|
||||
public synchronized List<String> getAllAuthsByEmail(final String email) {
|
||||
return source.getAllAuthsByEmail(email);
|
||||
}
|
||||
}).get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method purgeBanned.
|
||||
*
|
||||
* @param banned List<String>
|
||||
*
|
||||
* @see fr.xephi.authme.datasource.DataSource#purgeBanned(List<String>)
|
||||
* @see fr.xephi.authme.datasource.DataSource#purgeBanned(List)
|
||||
*/
|
||||
@Override
|
||||
public synchronized void purgeBanned(final List<String> 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<PlayerAuth> * @see fr.xephi.authme.datasource.DataSource#getAllAuths()
|
||||
* @return List
|
||||
*
|
||||
* @see fr.xephi.authme.datasource.DataSource#getAllAuths()
|
||||
*/
|
||||
@Override
|
||||
public List<PlayerAuth> getAllAuths() {
|
||||
return new ArrayList<>(cache.values());
|
||||
return source.getAllAuths();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method getLoggedPlayers.
|
||||
*
|
||||
* @return List<PlayerAuth> * @see fr.xephi.authme.datasource.DataSource#getLoggedPlayers()
|
||||
* @return List
|
||||
*
|
||||
* @see fr.xephi.authme.datasource.DataSource#getLoggedPlayers()
|
||||
*/
|
||||
@Override
|
||||
public List<PlayerAuth> getLoggedPlayers() {
|
||||
|
@ -114,7 +114,7 @@ public interface DataSource {
|
||||
*
|
||||
* @return List<String> * @throws Exception
|
||||
*/
|
||||
List<String> getAllAuthsByIp(String ip) throws Exception;
|
||||
List<String> getAllAuthsByIp(String ip);
|
||||
|
||||
/**
|
||||
* Method getAllAuthsByEmail.
|
||||
@ -123,7 +123,7 @@ public interface DataSource {
|
||||
*
|
||||
* @return List<String> * @throws Exception
|
||||
*/
|
||||
List<String> getAllAuthsByEmail(String email) throws Exception;
|
||||
List<String> getAllAuthsByEmail(String email);
|
||||
|
||||
/**
|
||||
* Method updateEmail.
|
||||
|
@ -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<String>
|
||||
*
|
||||
* @see fr.xephi.authme.datasource.DataSource#purgeBanned(List<String>)
|
||||
* @see fr.xephi.authme.datasource.DataSource#purgeBanned(List)
|
||||
*/
|
||||
@Override
|
||||
public void purgeBanned(List<String> 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);
|
||||
}
|
||||
|
@ -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<String> getAllAuthsByEmail(String email) throws SQLException {
|
||||
public synchronized List<String> getAllAuthsByEmail(String email){
|
||||
List<String> 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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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];
|
||||
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(args[0])) {
|
||||
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(args[0])) {
|
||||
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(args[0])) {
|
||||
if (plugin.database instanceof CacheDataSource) {
|
||||
((CacheDataSource)plugin.database).addAuthToCache(auth);
|
||||
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!");
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user