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) {
database = new CacheDataSource(this, database);
database = new CacheDataSource(database);
}
if (Settings.getDataSource == DataSource.DataSourceType.FILE) {

View 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,

View File

@ -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;

View File

@ -1,12 +1,10 @@
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.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;
@ -20,30 +18,24 @@ public class AccountsCommand extends ExecutableCommand {
// Get the player query
String playerQuery = sender.getName();
if (arguments.size() >= 1)
playerQuery = arguments.get(0);
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;
}
@ -61,28 +53,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;
} 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) {
@ -99,7 +83,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());
}
});

View File

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

View File

@ -1,18 +1,19 @@
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.PlayerCache;
import fr.xephi.authme.util.Utils;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
*/
@ -20,37 +21,33 @@ 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);
cachedAuths = CacheBuilder.newBuilder()
.expireAfterWrite(5, TimeUnit.MINUTES)
.removalListener(RemovalListeners.asynchronous(new RemovalListener<String, PlayerAuth>() {
@Override
public void onRemoval(RemovalNotification<String, PlayerAuth> removalNotification) {
String name = removalNotification.getKey();
if (PlayerCache.getInstance().isAuthenticated(name)) {
cachedAuths.getUnchecked(name);
}
}
pl.setCanConnect(true);
}
});
}
public void addAuthToCache(PlayerAuth auth) {
cache.put(auth.getNickname().toLowerCase(), auth);
}, exec))
.build(
new CacheLoader<String, PlayerAuth>() {
public PlayerAuth load(String key) {
return source.getAuth(key);
}
});
}
/**
@ -58,11 +55,13 @@ public class CacheDataSource implements DataSource {
*
* @param user String
*
* @return boolean * @see fr.xephi.authme.datasource.DataSource#isAuthAvailable(String)
* @return boolean
*
* @see fr.xephi.authme.datasource.DataSource#isAuthAvailable(String)
*/
@Override
public synchronized boolean isAuthAvailable(String user) {
return cache.containsKey(user.toLowerCase());
return getAuth(user) != null;
}
/**
@ -70,15 +69,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 +84,17 @@ public class CacheDataSource implements DataSource {
*
* @param auth PlayerAuth
*
* @return boolean * @see fr.xephi.authme.datasource.DataSource#saveAuth(PlayerAuth)
* @return boolean
*
* @see fr.xephi.authme.datasource.DataSource#saveAuth(PlayerAuth)
*/
@Override
public synchronized boolean saveAuth(final PlayerAuth auth) {
cache.put(auth.getNickname(), auth);
exec.execute(new Runnable() {
@Override
public void run() {
if (!source.saveAuth(auth)) {
cache.remove(auth.getNickname());
}
}
});
return true;
public synchronized boolean saveAuth(PlayerAuth auth) {
boolean result = source.saveAuth(auth);
if (result) {
cachedAuths.refresh(auth.getNickname());
}
return result;
}
/**
@ -107,26 +102,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 +120,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 +138,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 +156,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 +170,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 +192,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 +208,20 @@ public class CacheDataSource implements DataSource {
/**
* Method removeAuth.
*
* @param username String
* @param name String
*
* @return boolean * @see fr.xephi.authme.datasource.DataSource#removeAuth(String)
* @return boolean
*
* @see fr.xephi.authme.datasource.DataSource#removeAuth(String)
*/
@Override
public synchronized boolean removeAuth(String username) {
final String user = username.toLowerCase();
final PlayerAuth auth = cache.get(user);
cache.remove(user);
exec.execute(new Runnable() {
@Override
public void run() {
if (!source.removeAuth(user)) {
cache.put(user, auth);
}
}
});
return true;
public synchronized boolean removeAuth(String name) {
name = name.toLowerCase();
boolean result = source.removeAuth(name);
if (result) {
cachedAuths.invalidate(name);
}
return result;
}
/**
@ -302,19 +241,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 +256,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 +274,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 +292,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,15 +306,13 @@ 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 {
return source.getAllAuthsByIp(ip);
}
}).get();
public synchronized List<String> getAllAuthsByIp(final String ip) {
return source.getAllAuthsByIp(ip);
}
/**
@ -406,15 +320,13 @@ public class CacheDataSource implements DataSource {
*
* @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 {
return source.getAllAuthsByEmail(email);
}
}).get();
public synchronized List<String> getAllAuthsByEmail(final String email) {
return source.getAllAuthsByEmail(email);
}
/**
@ -422,7 +334,7 @@ public class CacheDataSource 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 synchronized void purgeBanned(final List<String> banned) {
@ -430,11 +342,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 +350,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 +364,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 +418,7 @@ public class CacheDataSource implements DataSource {
@Override
public void run() {
source.purgeLogged();
cachedAuths.invalidateAll();
}
});
}
@ -514,11 +426,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 +445,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 +457,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() {

View File

@ -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.

View File

@ -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);
}

View File

@ -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;
}
/**

View File

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

View File

@ -17,6 +17,7 @@ import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.GeoLiteAPI;
import fr.xephi.authme.util.Utils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
@ -224,7 +225,20 @@ public class AuthMePlayerListener implements Listener {
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());
if (!Settings.countriesBlacklist.isEmpty() && Settings.countriesBlacklist.contains(countryCode)) {
event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER);
@ -240,13 +254,9 @@ public class AuthMePlayerListener implements Listener {
final String name = event.getName().toLowerCase();
final Player player = Utils.getPlayer(name);
if (player == null) {
return;
}
// Check if forceSingleSession is set to true, so kick player that has
// joined with same nick of online player
if (Settings.isForceSingleSessionEnabled) {
if (player != null && Settings.isForceSingleSessionEnabled) {
event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER);
event.setKickMessage(m.retrieveSingle(MessageKey.USERNAME_ALREADY_ONLINE_ERROR));
LimboPlayer limbo = LimboCache.getInstance().getLimboPlayer(name);
@ -435,7 +445,7 @@ public class AuthMePlayerListener implements Listener {
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
public void playerHitPlayerEvent(EntityDamageByEntityEvent event) {
public void onPlayerHitPlayerEvent(EntityDamageByEntityEvent event) {
Entity damager = event.getDamager();
if (!(damager instanceof Player)) {
return;