Merge branch 'master' of https://github.com/AuthMe-Team/AuthMeReloaded into commands-refactor

Conflicts:
	src/main/java/fr/xephi/authme/command/executable/authme/AccountsCommand.java
	src/main/java/fr/xephi/authme/command/executable/authme/ResetNameCommand.java
	src/main/java/fr/xephi/authme/command/executable/authme/SetEmailCommand.java
This commit is contained in:
ljacqu 2015-12-12 14:25:01 +01:00
commit 4d8d9de733
11 changed files with 256 additions and 323 deletions

View File

@ -601,7 +601,7 @@ public class AuthMe extends JavaPlugin {
} }
if (Settings.isCachingEnabled) { if (Settings.isCachingEnabled) {
database = new CacheDataSource(this, database); database = new CacheDataSource(database);
} }
if (Settings.getDataSource == DataSource.DataSourceType.FILE) { if (Settings.getDataSource == DataSource.DataSourceType.FILE) {

View File

@ -194,7 +194,7 @@ public class PlayerAuth {
this.setHash(auth.getHash()); this.setHash(auth.getHash());
this.setIp(auth.getIp()); this.setIp(auth.getIp());
this.setLastLogin(auth.getLastLogin()); this.setLastLogin(auth.getLastLogin());
this.setName(auth.getNickname()); this.setNickname(auth.getNickname());
this.setQuitLocX(auth.getQuitLocX()); this.setQuitLocX(auth.getQuitLocX());
this.setQuitLocY(auth.getQuitLocY()); this.setQuitLocY(auth.getQuitLocY());
this.setQuitLocZ(auth.getQuitLocZ()); this.setQuitLocZ(auth.getQuitLocZ());
@ -204,11 +204,11 @@ public class PlayerAuth {
} }
/** /**
* Method setName. * Method setNickname.
* *
* @param nickname String * @param nickname String
*/ */
public void setName(String nickname) { public void setNickname(String nickname) {
this.nickname = nickname.toLowerCase(); this.nickname = nickname.toLowerCase();
} }
@ -522,7 +522,7 @@ public class PlayerAuth {
public PlayerAuth build() { public PlayerAuth build() {
return new PlayerAuth( return new PlayerAuth(
checkNotNull(name).toLowerCase(), checkNotNull(name),
nullToEmpty(hash), nullToEmpty(hash),
nullToEmpty(salt), nullToEmpty(salt),
groupId, groupId,

View File

@ -40,8 +40,7 @@ public class PlayerCache {
* @param auth PlayerAuth * @param auth PlayerAuth
*/ */
public void updatePlayer(PlayerAuth auth) { public void updatePlayer(PlayerAuth auth) {
cache.remove(auth.getNickname().toLowerCase()); cache.put(auth.getNickname(), auth);
cache.put(auth.getNickname().toLowerCase(), auth);
} }
/** /**
@ -87,7 +86,7 @@ public class PlayerCache {
/** /**
* Method getCache. * Method getCache.
* *
* @return ConcurrentHashMap<String,PlayerAuth> * @return ConcurrentHashMap
*/ */
public ConcurrentHashMap<String, PlayerAuth> getCache() { public ConcurrentHashMap<String, PlayerAuth> getCache() {
return this.cache; return this.cache;

View File

@ -1,12 +1,10 @@
package fr.xephi.authme.command.executable.authme; package fr.xephi.authme.command.executable.authme;
import fr.xephi.authme.AuthMe; import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages; import fr.xephi.authme.output.Messages;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import java.util.List; import java.util.List;
@ -20,30 +18,24 @@ public class AccountsCommand extends ExecutableCommand {
// Get the player query // Get the player query
String playerQuery = sender.getName(); String playerQuery = sender.getName();
if (arguments.size() >= 1) if (commandArguments.getCount() >= 1) {
playerQuery = arguments.get(0); playerQuery = commandArguments.get(0);
}
final String playerQueryFinal = playerQuery; final String playerQueryFinal = playerQuery;
// Command logic // Command logic
if (!playerQuery.contains(".")) { if (!playerQueryFinal.contains(".")) {
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override @Override
public void run() { public void run() {
PlayerAuth auth; PlayerAuth auth = plugin.database.getAuth(playerQueryFinal.toLowerCase());
StringBuilder message = new StringBuilder("[AuthMe] ");
try {
auth = plugin.database.getAuth(playerQueryFinal.toLowerCase());
} catch (NullPointerException npe) {
m.send(sender, MessageKey.UNKNOWN_USER);
return;
}
if (auth == null) { if (auth == null) {
m.send(sender, MessageKey.UNKNOWN_USER); m.send(sender, MessageKey.UNKNOWN_USER);
return; return;
} }
StringBuilder message = new StringBuilder("[AuthMe] ");
List<String> accountList = plugin.database.getAllAuthsByName(auth); List<String> accountList = plugin.database.getAllAuthsByName(auth);
if (accountList == null || accountList.isEmpty()) { if (accountList.isEmpty()) {
m.send(sender, MessageKey.USER_NOT_REGISTERED); m.send(sender, MessageKey.USER_NOT_REGISTERED);
return; return;
} }
@ -61,28 +53,20 @@ public class AccountsCommand extends ExecutableCommand {
message.append('.'); 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()); sender.sendMessage(message.toString());
} }
}); });
return; return;
} else { } else {
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
@Override @Override
public void run() { public void run() {
List<String> accountList; List<String> accountList = plugin.database.getAllAuthsByIp(playerQueryFinal);
try {
accountList = plugin.database.getAllAuthsByIp(playerQueryFinal);
} catch (Exception e) {
ConsoleLogger.showError(e.getMessage());
ConsoleLogger.writeStackTrace(e);
m.send(sender, MessageKey.ERROR);
return;
}
StringBuilder message = new StringBuilder("[AuthMe] "); StringBuilder message = new StringBuilder("[AuthMe] ");
if (accountList == null || accountList.isEmpty()) { if (accountList.isEmpty()) {
sender.sendMessage("[AuthMe] This IP does not exist in the database"); sender.sendMessage("[AuthMe] This IP does not exist in the database.");
return; return;
} }
if (accountList.size() == 1) { if (accountList.size() == 1) {
@ -99,7 +83,8 @@ public class AccountsCommand extends ExecutableCommand {
message.append('.'); 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()); sender.sendMessage(message.toString());
} }
}); });

View File

@ -16,14 +16,14 @@ public class SetEmailCommand extends ExecutableCommand {
@Override @Override
public void executeCommand(CommandSender sender, List<String> arguments) { public void executeCommand(CommandSender sender, List<String> arguments) {
// AuthMe plugin instance // AuthMe plugin instance
AuthMe plugin = AuthMe.getInstance(); final AuthMe plugin = AuthMe.getInstance();
// Messages instance // Messages instance
Messages m = plugin.getMessages(); final Messages m = plugin.getMessages();
// Get the player name and email address // Get the player name and email address
String playerName = arguments.get(0); final String playerName = arguments.get(0);
String playerEmail = arguments.get(1); final String playerEmail = arguments.get(1);
// Validate the email address // Validate the email address
if (!Settings.isEmailCorrect(playerEmail)) { if (!Settings.isEmailCorrect(playerEmail)) {
@ -31,26 +31,34 @@ public class SetEmailCommand extends ExecutableCommand {
return; return;
} }
// Validate the user plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
PlayerAuth auth = plugin.database.getAuth(playerName.toLowerCase()); @Override
if (auth == null) { public void run() {
m.send(sender, MessageKey.UNKNOWN_USER); // Validate the user
return; PlayerAuth auth = plugin.database.getAuth(playerName);
} if (auth == null) {
m.send(sender, MessageKey.UNKNOWN_USER);
return;
}
// Set the email address // Set the email address
auth.setEmail(playerEmail);
if (!plugin.database.updateEmail(auth)) {
m.send(sender, MessageKey.ERROR);
return;
}
// Update the player cache auth.setEmail(playerEmail);
if (PlayerCache.getInstance().getAuth(playerName.toLowerCase()) != null) if (!plugin.database.updateEmail(auth)) {
PlayerCache.getInstance().updatePlayer(auth); m.send(sender, MessageKey.ERROR);
return;
}
// Show a status message // Update the player cache
m.send(sender, MessageKey.EMAIL_CHANGED_SUCCESS); if (PlayerCache.getInstance().getAuth(playerName) != null) {
return; PlayerCache.getInstance().updatePlayer(auth);
}
// Show a status message
m.send(sender, MessageKey.EMAIL_CHANGED_SUCCESS);
}
});
return true;
} }
} }

View File

@ -1,18 +1,19 @@
package fr.xephi.authme.datasource; 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 com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalListeners;
import com.google.common.cache.RemovalNotification;
import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache; 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.ArrayList;
import java.util.List; 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.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/** /**
*/ */
@ -20,37 +21,33 @@ public class CacheDataSource implements DataSource {
private final DataSource source; private final DataSource source;
private final ExecutorService exec; private final ExecutorService exec;
private final ConcurrentHashMap<String, PlayerAuth> cache = new ConcurrentHashMap<>(); private final LoadingCache<String, PlayerAuth> cachedAuths;
/** /**
* Constructor for CacheDataSource. * Constructor for CacheDataSource.
* *
* @param pl AuthMe
* @param src DataSource * @param src DataSource
*/ */
public CacheDataSource(final AuthMe pl, DataSource src) { public CacheDataSource(DataSource src) {
this.source = src; this.source = src;
this.exec = Executors.newCachedThreadPool(); this.exec = Executors.newCachedThreadPool();
pl.setCanConnect(false); cachedAuths = CacheBuilder.newBuilder()
.expireAfterWrite(5, TimeUnit.MINUTES)
/* .removalListener(RemovalListeners.asynchronous(new RemovalListener<String, PlayerAuth>() {
* We need to load all players in cache ... It will took more time to @Override
* load the server, but it will be much easier to check for an public void onRemoval(RemovalNotification<String, PlayerAuth> removalNotification) {
* isAuthAvailable ! String name = removalNotification.getKey();
*/ if (PlayerCache.getInstance().isAuthenticated(name)) {
exec.execute(new Runnable() { cachedAuths.getUnchecked(name);
@Override }
public void run() {
for (PlayerAuth auth : source.getAllAuths()) {
cache.put(auth.getNickname().toLowerCase(), auth);
} }
pl.setCanConnect(true); }, exec))
} .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); });
} }
/** /**
@ -58,11 +55,13 @@ public class CacheDataSource implements DataSource {
* *
* @param user String * @param user String
* *
* @return boolean * @see fr.xephi.authme.datasource.DataSource#isAuthAvailable(String) * @return boolean
*
* @see fr.xephi.authme.datasource.DataSource#isAuthAvailable(String)
*/ */
@Override @Override
public synchronized boolean isAuthAvailable(String user) { public synchronized boolean isAuthAvailable(String user) {
return cache.containsKey(user.toLowerCase()); return getAuth(user) != null;
} }
/** /**
@ -70,15 +69,14 @@ public class CacheDataSource implements DataSource {
* *
* @param user String * @param user String
* *
* @return PlayerAuth * @see fr.xephi.authme.datasource.DataSource#getAuth(String) * @return PlayerAuth
*
* @see fr.xephi.authme.datasource.DataSource#getAuth(String)
*/ */
@Override @Override
public synchronized PlayerAuth getAuth(String user) { public synchronized PlayerAuth getAuth(String user) {
user = user.toLowerCase(); user = user.toLowerCase();
if (cache.containsKey(user)) { return cachedAuths.getUnchecked(user);
return cache.get(user);
}
return null;
} }
/** /**
@ -86,20 +84,17 @@ public class CacheDataSource implements DataSource {
* *
* @param auth PlayerAuth * @param auth PlayerAuth
* *
* @return boolean * @see fr.xephi.authme.datasource.DataSource#saveAuth(PlayerAuth) * @return boolean
*
* @see fr.xephi.authme.datasource.DataSource#saveAuth(PlayerAuth)
*/ */
@Override @Override
public synchronized boolean saveAuth(final PlayerAuth auth) { public synchronized boolean saveAuth(PlayerAuth auth) {
cache.put(auth.getNickname(), auth); boolean result = source.saveAuth(auth);
exec.execute(new Runnable() { if (result) {
@Override cachedAuths.refresh(auth.getNickname());
public void run() { }
if (!source.saveAuth(auth)) { return result;
cache.remove(auth.getNickname());
}
}
});
return true;
} }
/** /**
@ -107,26 +102,17 @@ public class CacheDataSource implements DataSource {
* *
* @param auth PlayerAuth * @param auth PlayerAuth
* *
* @return boolean * @see fr.xephi.authme.datasource.DataSource#updatePassword(PlayerAuth) * @return boolean
*
* @see fr.xephi.authme.datasource.DataSource#updatePassword(PlayerAuth)
*/ */
@Override @Override
public synchronized boolean updatePassword(final PlayerAuth auth) { public synchronized boolean updatePassword(PlayerAuth auth) {
if (!cache.containsKey(auth.getNickname())) { boolean result = source.updatePassword(auth);
return false; if (result) {
cachedAuths.refresh(auth.getNickname());
} }
final String oldHash = cache.get(auth.getNickname()).getHash(); return result;
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;
} }
/** /**
@ -134,35 +120,17 @@ public class CacheDataSource implements DataSource {
* *
* @param auth PlayerAuth * @param auth PlayerAuth
* *
* @return boolean * @see fr.xephi.authme.datasource.DataSource#updateSession(PlayerAuth) * @return boolean
*
* @see fr.xephi.authme.datasource.DataSource#updateSession(PlayerAuth)
*/ */
@Override @Override
public boolean updateSession(final PlayerAuth auth) { public boolean updateSession(PlayerAuth auth) {
if (!cache.containsKey(auth.getNickname())) { boolean result = source.updateSession(auth);
return false; if (result) {
cachedAuths.refresh(auth.getNickname());
} }
PlayerAuth cachedAuth = cache.get(auth.getNickname()); return result;
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;
} }
/** /**
@ -170,38 +138,17 @@ public class CacheDataSource implements DataSource {
* *
* @param auth PlayerAuth * @param auth PlayerAuth
* *
* @return boolean * @see fr.xephi.authme.datasource.DataSource#updateQuitLoc(PlayerAuth) * @return boolean
*
* @see fr.xephi.authme.datasource.DataSource#updateQuitLoc(PlayerAuth)
*/ */
@Override @Override
public boolean updateQuitLoc(final PlayerAuth auth) { public boolean updateQuitLoc(final PlayerAuth auth) {
if (!cache.containsKey(auth.getNickname())) { boolean result = source.updateSession(auth);
return false; if (result) {
cachedAuths.refresh(auth.getNickname());
} }
final PlayerAuth cachedAuth = cache.get(auth.getNickname()); return result;
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;
} }
/** /**
@ -209,17 +156,13 @@ public class CacheDataSource implements DataSource {
* *
* @param ip String * @param ip String
* *
* @return int * @see fr.xephi.authme.datasource.DataSource#getIps(String) * @return int
*
* @see fr.xephi.authme.datasource.DataSource#getIps(String)
*/ */
@Override @Override
public int getIps(String ip) { public int getIps(String ip) {
int count = 0; return source.getIps(ip);
for (Map.Entry<String, PlayerAuth> p : cache.entrySet()) {
if (p.getValue().getIp().equals(ip)) {
count++;
}
}
return count;
} }
/** /**
@ -227,15 +170,17 @@ public class CacheDataSource implements DataSource {
* *
* @param until long * @param until long
* *
* @return int * @see fr.xephi.authme.datasource.DataSource#purgeDatabase(long) * @return int
*
* @see fr.xephi.authme.datasource.DataSource#purgeDatabase(long)
*/ */
@Override @Override
public int purgeDatabase(long until) { public int purgeDatabase(long until) {
int cleared = source.purgeDatabase(until); int cleared = source.purgeDatabase(until);
if (cleared > 0) { if (cleared > 0) {
for (PlayerAuth auth : cache.values()) { for (PlayerAuth auth : cachedAuths.asMap().values()) {
if (auth.getLastLogin() < until) { if (auth != null && auth.getLastLogin() < until) {
cache.remove(auth.getNickname()); cachedAuths.invalidate(auth.getNickname());
} }
} }
} }
@ -247,17 +192,15 @@ public class CacheDataSource implements DataSource {
* *
* @param until long * @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 @Override
public List<String> autoPurgeDatabase(long until) { public List<String> autoPurgeDatabase(long until) {
List<String> cleared = source.autoPurgeDatabase(until); List<String> cleared = source.autoPurgeDatabase(until);
if (cleared.size() > 0) { for (String name : cleared) {
for (PlayerAuth auth : cache.values()) { cachedAuths.invalidate(name);
if (auth.getLastLogin() < until) {
cache.remove(auth.getNickname());
}
}
} }
return cleared; return cleared;
} }
@ -265,24 +208,20 @@ public class CacheDataSource implements DataSource {
/** /**
* Method removeAuth. * 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 @Override
public synchronized boolean removeAuth(String username) { public synchronized boolean removeAuth(String name) {
final String user = username.toLowerCase(); name = name.toLowerCase();
final PlayerAuth auth = cache.get(user); boolean result = source.removeAuth(name);
cache.remove(user); if (result) {
exec.execute(new Runnable() { cachedAuths.invalidate(name);
@Override }
public void run() { return result;
if (!source.removeAuth(user)) {
cache.put(user, auth);
}
}
});
return true;
} }
/** /**
@ -302,19 +241,12 @@ public class CacheDataSource implements DataSource {
* @see fr.xephi.authme.datasource.DataSource#reload() * @see fr.xephi.authme.datasource.DataSource#reload()
*/ */
@Override @Override
public void reload() { public void reload() { // unused method
exec.execute(new Runnable() { exec.execute(new Runnable() {
@Override @Override
public void run() { public void run() {
cache.clear();
source.reload(); source.reload();
for (Player player : Utils.getOnlinePlayers()) { cachedAuths.invalidateAll();
String user = player.getName().toLowerCase();
if (PlayerCache.getInstance().isAuthenticated(user)) {
PlayerAuth auth = source.getAuth(user);
cache.put(user, auth);
}
}
} }
}); });
} }
@ -324,19 +256,17 @@ public class CacheDataSource implements DataSource {
* *
* @param auth PlayerAuth * @param auth PlayerAuth
* *
* @return boolean * @see fr.xephi.authme.datasource.DataSource#updateEmail(PlayerAuth) * @return boolean
*
* @see fr.xephi.authme.datasource.DataSource#updateEmail(PlayerAuth)
*/ */
@Override @Override
public synchronized boolean updateEmail(final PlayerAuth auth) { public synchronized boolean updateEmail(final PlayerAuth auth) {
try { boolean result = source.updateEmail(auth);
return exec.submit(new Callable<Boolean>() { if (result) {
public Boolean call() { cachedAuths.refresh(auth.getNickname());
return source.updateEmail(auth);
}
}).get();
} catch (Exception e) {
return false;
} }
return result;
} }
/** /**
@ -344,27 +274,17 @@ public class CacheDataSource implements DataSource {
* *
* @param auth PlayerAuth * @param auth PlayerAuth
* *
* @return boolean * @see fr.xephi.authme.datasource.DataSource#updateSalt(PlayerAuth) * @return boolean
*
* @see fr.xephi.authme.datasource.DataSource#updateSalt(PlayerAuth)
*/ */
@Override @Override
public synchronized boolean updateSalt(final PlayerAuth auth) { public synchronized boolean updateSalt(final PlayerAuth auth) {
if (!cache.containsKey(auth.getNickname())) { boolean result = source.updateSalt(auth);
return false; if (result) {
cachedAuths.refresh(auth.getNickname());
} }
PlayerAuth cachedAuth = cache.get(auth.getNickname()); return result;
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;
} }
/** /**
@ -372,17 +292,13 @@ public class CacheDataSource implements DataSource {
* *
* @param auth PlayerAuth * @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 @Override
public synchronized List<String> getAllAuthsByName(PlayerAuth auth) { public synchronized List<String> getAllAuthsByName(PlayerAuth auth) {
List<String> result = new ArrayList<>(); return source.getAllAuthsByName(auth);
for (Map.Entry<String, PlayerAuth> stringPlayerAuthEntry : cache.entrySet()) {
PlayerAuth p = stringPlayerAuthEntry.getValue();
if (p.getIp().equals(auth.getIp()))
result.add(p.getNickname());
}
return result;
} }
/** /**
@ -390,15 +306,13 @@ public class CacheDataSource implements DataSource {
* *
* @param ip String * @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 @Override
public synchronized List<String> getAllAuthsByIp(final String ip) throws Exception { public synchronized List<String> getAllAuthsByIp(final String ip) {
return exec.submit(new Callable<List<String>>() { return source.getAllAuthsByIp(ip);
public List<String> call() throws Exception {
return source.getAllAuthsByIp(ip);
}
}).get();
} }
/** /**
@ -406,15 +320,13 @@ public class CacheDataSource implements DataSource {
* *
* @param email String * @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 @Override
public synchronized List<String> getAllAuthsByEmail(final String email) throws Exception { public synchronized List<String> getAllAuthsByEmail(final String email) {
return exec.submit(new Callable<List<String>>() { return source.getAllAuthsByEmail(email);
public List<String> call() throws Exception {
return source.getAllAuthsByEmail(email);
}
}).get();
} }
/** /**
@ -422,7 +334,7 @@ public class CacheDataSource implements DataSource {
* *
* @param banned List<String> * @param banned List<String>
* *
* @see fr.xephi.authme.datasource.DataSource#purgeBanned(List<String>) * @see fr.xephi.authme.datasource.DataSource#purgeBanned(List)
*/ */
@Override @Override
public synchronized void purgeBanned(final List<String> banned) { public synchronized void purgeBanned(final List<String> banned) {
@ -430,11 +342,7 @@ public class CacheDataSource implements DataSource {
@Override @Override
public void run() { public void run() {
source.purgeBanned(banned); source.purgeBanned(banned);
for (PlayerAuth auth : cache.values()) { cachedAuths.invalidateAll(banned);
if (banned.contains(auth.getNickname())) {
cache.remove(auth.getNickname());
}
}
} }
}); });
} }
@ -442,7 +350,9 @@ public class CacheDataSource implements DataSource {
/** /**
* Method getType. * Method getType.
* *
* @return DataSourceType * @see fr.xephi.authme.datasource.DataSource#getType() * @return DataSourceType
*
* @see fr.xephi.authme.datasource.DataSource#getType()
*/ */
@Override @Override
public DataSourceType getType() { public DataSourceType getType() {
@ -454,12 +364,13 @@ public class CacheDataSource implements DataSource {
* *
* @param user String * @param user String
* *
* @return boolean * @see fr.xephi.authme.datasource.DataSource#isLogged(String) * @return boolean
*
* @see fr.xephi.authme.datasource.DataSource#isLogged(String)
*/ */
@Override @Override
public boolean isLogged(String user) { public boolean isLogged(String user) {
user = user.toLowerCase(); return PlayerCache.getInstance().isAuthenticated(user);
return PlayerCache.getInstance().getCache().containsKey(user);
} }
/** /**
@ -507,6 +418,7 @@ public class CacheDataSource implements DataSource {
@Override @Override
public void run() { public void run() {
source.purgeLogged(); source.purgeLogged();
cachedAuths.invalidateAll();
} }
}); });
} }
@ -514,11 +426,13 @@ public class CacheDataSource implements DataSource {
/** /**
* Method getAccountsRegistered. * Method getAccountsRegistered.
* *
* @return int * @see fr.xephi.authme.datasource.DataSource#getAccountsRegistered() * @return int
*
* @see fr.xephi.authme.datasource.DataSource#getAccountsRegistered()
*/ */
@Override @Override
public int getAccountsRegistered() { public int getAccountsRegistered() {
return cache.size(); return source.getAccountsRegistered();
} }
/** /**
@ -531,14 +445,11 @@ public class CacheDataSource implements DataSource {
*/ */
@Override @Override
public void updateName(final String oldOne, final String newOne) { 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() { exec.execute(new Runnable() {
@Override @Override
public void run() { public void run() {
source.updateName(oldOne, newOne); source.updateName(oldOne, newOne);
cachedAuths.invalidate(oldOne);
} }
}); });
} }
@ -546,17 +457,21 @@ public class CacheDataSource implements DataSource {
/** /**
* Method getAllAuths. * Method getAllAuths.
* *
* @return List<PlayerAuth> * @see fr.xephi.authme.datasource.DataSource#getAllAuths() * @return List
*
* @see fr.xephi.authme.datasource.DataSource#getAllAuths()
*/ */
@Override @Override
public List<PlayerAuth> getAllAuths() { public List<PlayerAuth> getAllAuths() {
return new ArrayList<>(cache.values()); return source.getAllAuths();
} }
/** /**
* Method getLoggedPlayers. * Method getLoggedPlayers.
* *
* @return List<PlayerAuth> * @see fr.xephi.authme.datasource.DataSource#getLoggedPlayers() * @return List
*
* @see fr.xephi.authme.datasource.DataSource#getLoggedPlayers()
*/ */
@Override @Override
public List<PlayerAuth> getLoggedPlayers() { public List<PlayerAuth> getLoggedPlayers() {

View File

@ -114,7 +114,7 @@ public interface DataSource {
* *
* @return List<String> * @throws Exception * @return List<String> * @throws Exception
*/ */
List<String> getAllAuthsByIp(String ip) throws Exception; List<String> getAllAuthsByIp(String ip);
/** /**
* Method getAllAuthsByEmail. * Method getAllAuthsByEmail.
@ -123,7 +123,7 @@ public interface DataSource {
* *
* @return List<String> * @throws Exception * @return List<String> * @throws Exception
*/ */
List<String> getAllAuthsByEmail(String email) throws Exception; List<String> getAllAuthsByEmail(String email);
/** /**
* Method updateEmail. * Method updateEmail.

View File

@ -6,7 +6,13 @@ import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache; import fr.xephi.authme.cache.auth.PlayerCache;
import fr.xephi.authme.settings.Settings; 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.ArrayList;
import java.util.List; import java.util.List;
@ -564,7 +570,7 @@ public class FlatFile implements DataSource {
BufferedReader br = null; BufferedReader br = null;
try { try {
br = new BufferedReader(new FileReader(source)); br = new BufferedReader(new FileReader(source));
String line = ""; String line;
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
String[] args = line.split(":"); String[] args = line.split(":");
if (args[0].equals(auth.getNickname())) { if (args[0].equals(auth.getNickname())) {
@ -721,7 +727,7 @@ public class FlatFile implements DataSource {
* *
* @param banned List<String> * @param banned List<String>
* *
* @see fr.xephi.authme.datasource.DataSource#purgeBanned(List<String>) * @see fr.xephi.authme.datasource.DataSource#purgeBanned(List)
*/ */
@Override @Override
public void purgeBanned(List<String> banned) { public void purgeBanned(List<String> banned) {
@ -856,7 +862,7 @@ public class FlatFile implements DataSource {
@Override @Override
public void updateName(String oldOne, String newOne) { public void updateName(String oldOne, String newOne) {
PlayerAuth auth = this.getAuth(oldOne); PlayerAuth auth = this.getAuth(oldOne);
auth.setName(newOne); auth.setNickname(newOne);
this.saveAuth(auth); this.saveAuth(auth);
this.removeAuth(oldOne); this.removeAuth(oldOne);
} }

View File

@ -655,6 +655,7 @@ public class MySQL implements DataSource {
while (rs.next()) { while (rs.next()) {
list.add(rs.getString(columnName)); list.add(rs.getString(columnName));
} }
rs.close();
sql = "DELETE FROM " + tableName + " WHERE " + columnLastLogin + "<" + until; sql = "DELETE FROM " + tableName + " WHERE " + columnLastLogin + "<" + until;
st.executeUpdate(sql); st.executeUpdate(sql);
st.close(); st.close();
@ -915,11 +916,10 @@ public class MySQL implements DataSource {
* *
* @return List * @return List
* *
* @throws SQLException
* @see fr.xephi.authme.datasource.DataSource#getAllAuthsByEmail(String) * @see fr.xephi.authme.datasource.DataSource#getAllAuthsByEmail(String)
*/ */
@Override @Override
public synchronized List<String> getAllAuthsByEmail(String email) throws SQLException { public synchronized List<String> getAllAuthsByEmail(String email){
List<String> countEmail = new ArrayList<>(); List<String> countEmail = new ArrayList<>();
try (Connection con = getConnection()) { try (Connection con = getConnection()) {
String sql = "SELECT " + columnName + " FROM " + tableName + " WHERE " + columnEmail + "=?;"; String sql = "SELECT " + columnName + " FROM " + tableName + " WHERE " + columnEmail + "=?;";
@ -931,8 +931,11 @@ public class MySQL implements DataSource {
} }
rs.close(); rs.close();
pst.close(); pst.close();
return countEmail; } catch (SQLException ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.writeStackTrace(ex);
} }
return countEmail;
} }
/** /**

View File

@ -49,25 +49,32 @@ public class BungeeCordMessage implements PluginMessageListener {
if (subChannel.equalsIgnoreCase("AuthMe")) { if (subChannel.equalsIgnoreCase("AuthMe")) {
String str = in.readUTF(); String str = in.readUTF();
String[] args = str.split(";"); String[] args = str.split(";");
String name = args[1]; final String act = args[0];
PlayerAuth auth = plugin.database.getAuth(name); final String name = args[1];
if (auth == null) { plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
return; @Override
} public void run() {
if ("login".equals(args[0])) { PlayerAuth auth = plugin.database.getAuth(name);
PlayerCache.getInstance().updatePlayer(auth); if (auth == null) {
plugin.database.setLogged(name); return;
ConsoleLogger.info("Player " + auth.getNickname() + " has logged in from one of your server!"); }
} else if ("logout".equals(args[0])) { if ("login".equals(act)) {
PlayerCache.getInstance().removePlayer(name); PlayerCache.getInstance().updatePlayer(auth);
plugin.database.setUnlogged(name); plugin.database.setLogged(name);
ConsoleLogger.info("Player " + auth.getNickname() + " has logged out from one of your server!"); ConsoleLogger.info("Player " + auth.getNickname()
} else if ("register".equals(args[0])) { + " has logged in from one of your server!");
if (plugin.database instanceof CacheDataSource) { } else if ("logout".equals(act)) {
((CacheDataSource)plugin.database).addAuthToCache(auth); PlayerCache.getInstance().removePlayer(name);
plugin.database.setUnlogged(name);
ConsoleLogger.info("Player " + auth.getNickname()
+ " has logged out from one of your server!");
} else if ("register".equals(act)) {
ConsoleLogger.info("Player " + auth.getNickname()
+ " has registered out from one of your server!");
}
} }
ConsoleLogger.info("Player " + auth.getNickname() + " has registered out from one of your server!"); });
}
} }
} }

View File

@ -17,6 +17,7 @@ import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.GeoLiteAPI; import fr.xephi.authme.util.GeoLiteAPI;
import fr.xephi.authme.util.Utils; import fr.xephi.authme.util.Utils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
@ -224,7 +225,20 @@ public class AuthMePlayerListener implements Listener {
return; return;
} }
if (Settings.enableProtection) { PlayerAuth auth = plugin.database.getAuth(event.getName());
if (auth != null && !auth.getRealName().equals("Player") && !auth.getRealName().equals(event.getName())) {
event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER);
event.setKickMessage("You should join using username: " + ChatColor.AQUA + auth.getRealName() +
ChatColor.RESET + "\nnot :" + ChatColor.RED + event.getName()); // TODO: write a better message
return;
}
if (auth != null && auth.getRealName().equals("Player")) {
auth.setRealName(event.getName());
plugin.database.saveAuth(auth);
}
if (auth == null && Settings.enableProtection) {
String countryCode = GeoLiteAPI.getCountryCode(event.getAddress().getHostAddress()); String countryCode = GeoLiteAPI.getCountryCode(event.getAddress().getHostAddress());
if (!Settings.countriesBlacklist.isEmpty() && Settings.countriesBlacklist.contains(countryCode)) { if (!Settings.countriesBlacklist.isEmpty() && Settings.countriesBlacklist.contains(countryCode)) {
event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER); event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER);
@ -240,13 +254,9 @@ public class AuthMePlayerListener implements Listener {
final String name = event.getName().toLowerCase(); final String name = event.getName().toLowerCase();
final Player player = Utils.getPlayer(name); final Player player = Utils.getPlayer(name);
if (player == null) {
return;
}
// Check if forceSingleSession is set to true, so kick player that has // Check if forceSingleSession is set to true, so kick player that has
// joined with same nick of online player // joined with same nick of online player
if (Settings.isForceSingleSessionEnabled) { if (player != null && Settings.isForceSingleSessionEnabled) {
event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER); event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER);
event.setKickMessage(m.retrieveSingle(MessageKey.USERNAME_ALREADY_ONLINE_ERROR)); event.setKickMessage(m.retrieveSingle(MessageKey.USERNAME_ALREADY_ONLINE_ERROR));
LimboPlayer limbo = LimboCache.getInstance().getLimboPlayer(name); LimboPlayer limbo = LimboCache.getInstance().getLimboPlayer(name);
@ -435,7 +445,7 @@ public class AuthMePlayerListener implements Listener {
} }
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
public void playerHitPlayerEvent(EntityDamageByEntityEvent event) { public void onPlayerHitPlayerEvent(EntityDamageByEntityEvent event) {
Entity damager = event.getDamager(); Entity damager = event.getDamager();
if (!(damager instanceof Player)) { if (!(damager instanceof Player)) {
return; return;