mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-12-20 07:37:47 +01:00
Merge branch 'master' of https://github.com/AuthMe-Team/AuthMeReloaded into 707-process-as-service
This commit is contained in:
commit
30f62547d8
@ -10,8 +10,7 @@ import fr.xephi.authme.util.BukkitService;
|
|||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import java.util.ArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static fr.xephi.authme.util.BukkitService.TICKS_PER_MINUTE;
|
import static fr.xephi.authme.util.BukkitService.TICKS_PER_MINUTE;
|
||||||
import static fr.xephi.authme.util.BukkitService.TICKS_PER_SECOND;
|
import static fr.xephi.authme.util.BukkitService.TICKS_PER_SECOND;
|
||||||
@ -25,7 +24,8 @@ public class AntiBot {
|
|||||||
private final Messages messages;
|
private final Messages messages;
|
||||||
private final PermissionsManager permissionsManager;
|
private final PermissionsManager permissionsManager;
|
||||||
private final BukkitService bukkitService;
|
private final BukkitService bukkitService;
|
||||||
private final List<String> antibotPlayers = new ArrayList<>();
|
public final CopyOnWriteArrayList<String> antibotKicked = new CopyOnWriteArrayList<String>();
|
||||||
|
private final CopyOnWriteArrayList<String> antibotPlayers = new CopyOnWriteArrayList<String>();
|
||||||
private AntiBotStatus antiBotStatus = AntiBotStatus.DISABLED;
|
private AntiBotStatus antiBotStatus = AntiBotStatus.DISABLED;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
@ -77,6 +77,7 @@ public class AntiBot {
|
|||||||
if (antiBotStatus == AntiBotStatus.ACTIVE) {
|
if (antiBotStatus == AntiBotStatus.ACTIVE) {
|
||||||
antiBotStatus = AntiBotStatus.LISTENING;
|
antiBotStatus = AntiBotStatus.LISTENING;
|
||||||
antibotPlayers.clear();
|
antibotPlayers.clear();
|
||||||
|
antibotKicked.clear();
|
||||||
for (String s : messages.retrieve(MessageKey.ANTIBOT_AUTO_DISABLED_MESSAGE)) {
|
for (String s : messages.retrieve(MessageKey.ANTIBOT_AUTO_DISABLED_MESSAGE)) {
|
||||||
bukkitService.broadcastMessage(s.replace("%m", Integer.toString(duration)));
|
bukkitService.broadcastMessage(s.replace("%m", Integer.toString(duration)));
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,6 @@ import fr.xephi.authme.listener.AuthMeInventoryPacketAdapter;
|
|||||||
import fr.xephi.authme.listener.AuthMePlayerListener;
|
import fr.xephi.authme.listener.AuthMePlayerListener;
|
||||||
import fr.xephi.authme.listener.AuthMePlayerListener16;
|
import fr.xephi.authme.listener.AuthMePlayerListener16;
|
||||||
import fr.xephi.authme.listener.AuthMePlayerListener18;
|
import fr.xephi.authme.listener.AuthMePlayerListener18;
|
||||||
import fr.xephi.authme.listener.AuthMePlayerListener19;
|
|
||||||
import fr.xephi.authme.listener.AuthMeServerListener;
|
import fr.xephi.authme.listener.AuthMeServerListener;
|
||||||
import fr.xephi.authme.listener.AuthMeTabCompletePacketAdapter;
|
import fr.xephi.authme.listener.AuthMeTabCompletePacketAdapter;
|
||||||
import fr.xephi.authme.listener.AuthMeTablistPacketAdapter;
|
import fr.xephi.authme.listener.AuthMeTablistPacketAdapter;
|
||||||
@ -374,13 +373,6 @@ public class AuthMe extends JavaPlugin {
|
|||||||
pluginManager.registerEvents(initializer.get(AuthMePlayerListener18.class), this);
|
pluginManager.registerEvents(initializer.get(AuthMePlayerListener18.class), this);
|
||||||
} catch (ClassNotFoundException ignore) {
|
} catch (ClassNotFoundException ignore) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to register 1.9 player listeners
|
|
||||||
try {
|
|
||||||
Class.forName("org.spigotmc.event.player.PlayerSpawnLocationEvent");
|
|
||||||
pluginManager.registerEvents(initializer.get(AuthMePlayerListener19.class), this);
|
|
||||||
} catch (ClassNotFoundException ignore) {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void reloadSupportHook() {
|
private void reloadSupportHook() {
|
||||||
|
@ -72,7 +72,7 @@ public class CacheDataSource implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean isAuthAvailable(String user) {
|
public boolean isAuthAvailable(String user) {
|
||||||
return getAuth(user) != null;
|
return getAuth(user) != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,13 +87,13 @@ public class CacheDataSource implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized PlayerAuth getAuth(String user) {
|
public PlayerAuth getAuth(String user) {
|
||||||
user = user.toLowerCase();
|
user = user.toLowerCase();
|
||||||
return cachedAuths.getUnchecked(user).orNull();
|
return cachedAuths.getUnchecked(user).orNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean saveAuth(PlayerAuth auth) {
|
public boolean saveAuth(PlayerAuth auth) {
|
||||||
boolean result = source.saveAuth(auth);
|
boolean result = source.saveAuth(auth);
|
||||||
if (result) {
|
if (result) {
|
||||||
cachedAuths.refresh(auth.getNickname());
|
cachedAuths.refresh(auth.getNickname());
|
||||||
@ -102,7 +102,7 @@ public class CacheDataSource implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean updatePassword(PlayerAuth auth) {
|
public boolean updatePassword(PlayerAuth auth) {
|
||||||
boolean result = source.updatePassword(auth);
|
boolean result = source.updatePassword(auth);
|
||||||
if (result) {
|
if (result) {
|
||||||
cachedAuths.refresh(auth.getNickname());
|
cachedAuths.refresh(auth.getNickname());
|
||||||
@ -111,7 +111,7 @@ public class CacheDataSource implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean updatePassword(String user, HashedPassword password) {
|
public boolean updatePassword(String user, HashedPassword password) {
|
||||||
user = user.toLowerCase();
|
user = user.toLowerCase();
|
||||||
boolean result = source.updatePassword(user, password);
|
boolean result = source.updatePassword(user, password);
|
||||||
if (result) {
|
if (result) {
|
||||||
@ -121,7 +121,7 @@ public class CacheDataSource implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean updateSession(PlayerAuth auth) {
|
public boolean updateSession(PlayerAuth auth) {
|
||||||
boolean result = source.updateSession(auth);
|
boolean result = source.updateSession(auth);
|
||||||
if (result) {
|
if (result) {
|
||||||
cachedAuths.refresh(auth.getNickname());
|
cachedAuths.refresh(auth.getNickname());
|
||||||
@ -130,7 +130,7 @@ public class CacheDataSource implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean updateQuitLoc(final PlayerAuth auth) {
|
public boolean updateQuitLoc(final PlayerAuth auth) {
|
||||||
boolean result = source.updateQuitLoc(auth);
|
boolean result = source.updateQuitLoc(auth);
|
||||||
if (result) {
|
if (result) {
|
||||||
cachedAuths.refresh(auth.getNickname());
|
cachedAuths.refresh(auth.getNickname());
|
||||||
@ -149,7 +149,7 @@ public class CacheDataSource implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean removeAuth(String name) {
|
public boolean removeAuth(String name) {
|
||||||
name = name.toLowerCase();
|
name = name.toLowerCase();
|
||||||
boolean result = source.removeAuth(name);
|
boolean result = source.removeAuth(name);
|
||||||
if (result) {
|
if (result) {
|
||||||
@ -159,7 +159,7 @@ public class CacheDataSource implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void close() {
|
public void close() {
|
||||||
source.close();
|
source.close();
|
||||||
cachedAuths.invalidateAll();
|
cachedAuths.invalidateAll();
|
||||||
executorService.shutdown();
|
executorService.shutdown();
|
||||||
@ -171,7 +171,7 @@ public class CacheDataSource implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean updateEmail(final PlayerAuth auth) {
|
public boolean updateEmail(final PlayerAuth auth) {
|
||||||
boolean result = source.updateEmail(auth);
|
boolean result = source.updateEmail(auth);
|
||||||
if (result) {
|
if (result) {
|
||||||
cachedAuths.refresh(auth.getNickname());
|
cachedAuths.refresh(auth.getNickname());
|
||||||
@ -180,17 +180,17 @@ public class CacheDataSource implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized List<String> getAllAuthsByIp(final String ip) {
|
public List<String> getAllAuthsByIp(final String ip) {
|
||||||
return source.getAllAuthsByIp(ip);
|
return source.getAllAuthsByIp(ip);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized int countAuthsByEmail(final String email) {
|
public int countAuthsByEmail(final String email) {
|
||||||
return source.countAuthsByEmail(email);
|
return source.countAuthsByEmail(email);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void purgeBanned(final Set<String> banned) {
|
public void purgeBanned(final Set<String> banned) {
|
||||||
source.purgeBanned(banned);
|
source.purgeBanned(banned);
|
||||||
cachedAuths.invalidateAll(banned);
|
cachedAuths.invalidateAll(banned);
|
||||||
}
|
}
|
||||||
@ -201,17 +201,17 @@ public class CacheDataSource implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean isLogged(String user) {
|
public boolean isLogged(String user) {
|
||||||
return PlayerCache.getInstance().isAuthenticated(user);
|
return PlayerCache.getInstance().isAuthenticated(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void setLogged(final String user) {
|
public void setLogged(final String user) {
|
||||||
source.setLogged(user.toLowerCase());
|
source.setLogged(user.toLowerCase());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void setUnlogged(final String user) {
|
public void setUnlogged(final String user) {
|
||||||
source.setUnlogged(user.toLowerCase());
|
source.setUnlogged(user.toLowerCase());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -227,7 +227,7 @@ public class CacheDataSource implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean updateRealName(String user, String realName) {
|
public boolean updateRealName(String user, String realName) {
|
||||||
boolean result = source.updateRealName(user, realName);
|
boolean result = source.updateRealName(user, realName);
|
||||||
if (result) {
|
if (result) {
|
||||||
cachedAuths.refresh(user);
|
cachedAuths.refresh(user);
|
||||||
@ -236,7 +236,7 @@ public class CacheDataSource implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean updateIp(String user, String ip) {
|
public boolean updateIp(String user, String ip) {
|
||||||
boolean result = source.updateIp(user, ip);
|
boolean result = source.updateIp(user, ip);
|
||||||
if (result) {
|
if (result) {
|
||||||
cachedAuths.refresh(user);
|
cachedAuths.refresh(user);
|
||||||
|
@ -106,34 +106,36 @@ public class MySQL implements DataSource {
|
|||||||
ds = hikariDataSource;
|
ds = hikariDataSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized void setConnectionArguments() throws RuntimeException {
|
private void setConnectionArguments() throws RuntimeException {
|
||||||
ds = new HikariDataSource();
|
ds = new HikariDataSource();
|
||||||
ds.setPoolName("AuthMeMYSQLPool");
|
ds.setPoolName("AuthMeMYSQLPool");
|
||||||
ds.setDriverClassName("com.mysql.jdbc.Driver");
|
|
||||||
ds.setJdbcUrl("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database);
|
|
||||||
ds.addDataSourceProperty("rewriteBatchedStatements", "true");
|
|
||||||
ds.addDataSourceProperty("jdbcCompliantTruncation", "false");
|
|
||||||
ds.addDataSourceProperty("cachePrepStmts", "true");
|
|
||||||
ds.addDataSourceProperty("prepStmtCacheSize", "250");
|
|
||||||
ds.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
|
|
||||||
|
|
||||||
//set utf-8 as default encoding
|
// Database URL
|
||||||
|
ds.setJdbcUrl("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database);
|
||||||
|
|
||||||
|
// Auth
|
||||||
|
ds.setUsername(this.username);
|
||||||
|
ds.setPassword(this.password);
|
||||||
|
|
||||||
|
// Encoding
|
||||||
ds.addDataSourceProperty("characterEncoding", "utf8");
|
ds.addDataSourceProperty("characterEncoding", "utf8");
|
||||||
ds.addDataSourceProperty("encoding","UTF-8");
|
ds.addDataSourceProperty("encoding","UTF-8");
|
||||||
ds.addDataSourceProperty("useUnicode", "true");
|
ds.addDataSourceProperty("useUnicode", "true");
|
||||||
|
|
||||||
ds.setUsername(this.username);
|
// Random stuff
|
||||||
ds.setPassword(this.password);
|
ds.addDataSourceProperty("rewriteBatchedStatements", "true");
|
||||||
ds.setInitializationFailFast(true); // Don't start the plugin if the database is unavailable
|
ds.addDataSourceProperty("jdbcCompliantTruncation", "false");
|
||||||
ds.setMaxLifetime(180000); // 3 Min
|
|
||||||
ds.setIdleTimeout(60000); // 1 Min
|
// Caching
|
||||||
ds.setMinimumIdle(2);
|
ds.addDataSourceProperty("cachePrepStmts", "true");
|
||||||
ds.setMaximumPoolSize((Runtime.getRuntime().availableProcessors() * 2) + 1);
|
ds.addDataSourceProperty("prepStmtCacheSize", "250");
|
||||||
|
ds.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
|
||||||
|
|
||||||
ConsoleLogger.info("Connection arguments loaded, Hikari ConnectionPool ready!");
|
ConsoleLogger.info("Connection arguments loaded, Hikari ConnectionPool ready!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void reload() throws RuntimeException {
|
public void reload() throws RuntimeException {
|
||||||
if (ds != null) {
|
if (ds != null) {
|
||||||
ds.close();
|
ds.close();
|
||||||
}
|
}
|
||||||
@ -141,11 +143,11 @@ public class MySQL implements DataSource {
|
|||||||
ConsoleLogger.info("Hikari ConnectionPool arguments reloaded!");
|
ConsoleLogger.info("Hikari ConnectionPool arguments reloaded!");
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized Connection getConnection() throws SQLException {
|
private Connection getConnection() throws SQLException {
|
||||||
return ds.getConnection();
|
return ds.getConnection();
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized void setupConnection() throws SQLException {
|
private void setupConnection() throws SQLException {
|
||||||
try (Connection con = getConnection()) {
|
try (Connection con = getConnection()) {
|
||||||
Statement st = con.createStatement();
|
Statement st = con.createStatement();
|
||||||
DatabaseMetaData md = con.getMetaData();
|
DatabaseMetaData md = con.getMetaData();
|
||||||
@ -258,7 +260,7 @@ public class MySQL implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean isAuthAvailable(String user) {
|
public boolean isAuthAvailable(String user) {
|
||||||
String sql = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
String sql = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||||
@ -294,7 +296,7 @@ public class MySQL implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized PlayerAuth getAuth(String user) {
|
public PlayerAuth getAuth(String user) {
|
||||||
String sql = "SELECT * FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
String sql = "SELECT * FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
||||||
PlayerAuth auth;
|
PlayerAuth auth;
|
||||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||||
@ -328,7 +330,7 @@ public class MySQL implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean saveAuth(PlayerAuth auth) {
|
public boolean saveAuth(PlayerAuth auth) {
|
||||||
try (Connection con = getConnection()) {
|
try (Connection con = getConnection()) {
|
||||||
PreparedStatement pst;
|
PreparedStatement pst;
|
||||||
PreparedStatement pst2;
|
PreparedStatement pst2;
|
||||||
@ -531,12 +533,12 @@ public class MySQL implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean updatePassword(PlayerAuth auth) {
|
public boolean updatePassword(PlayerAuth auth) {
|
||||||
return updatePassword(auth.getNickname(), auth.getPassword());
|
return updatePassword(auth.getNickname(), auth.getPassword());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean updatePassword(String user, HashedPassword password) {
|
public boolean updatePassword(String user, HashedPassword password) {
|
||||||
user = user.toLowerCase();
|
user = user.toLowerCase();
|
||||||
try (Connection con = getConnection()) {
|
try (Connection con = getConnection()) {
|
||||||
boolean useSalt = !col.SALT.isEmpty();
|
boolean useSalt = !col.SALT.isEmpty();
|
||||||
@ -594,7 +596,7 @@ public class MySQL implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean updateSession(PlayerAuth auth) {
|
public boolean updateSession(PlayerAuth auth) {
|
||||||
String sql = "UPDATE " + tableName + " SET "
|
String sql = "UPDATE " + tableName + " SET "
|
||||||
+ col.IP + "=?, " + col.LAST_LOGIN + "=?, " + col.REAL_NAME + "=? WHERE " + col.NAME + "=?;";
|
+ col.IP + "=?, " + col.LAST_LOGIN + "=?, " + col.REAL_NAME + "=? WHERE " + col.NAME + "=?;";
|
||||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||||
@ -611,7 +613,7 @@ public class MySQL implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized Set<String> autoPurgeDatabase(long until) {
|
public Set<String> autoPurgeDatabase(long until) {
|
||||||
Set<String> list = new HashSet<>();
|
Set<String> list = new HashSet<>();
|
||||||
String select = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.LAST_LOGIN + "<?;";
|
String select = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.LAST_LOGIN + "<?;";
|
||||||
String delete = "DELETE FROM " + tableName + " WHERE " + col.LAST_LOGIN + "<?;";
|
String delete = "DELETE FROM " + tableName + " WHERE " + col.LAST_LOGIN + "<?;";
|
||||||
@ -634,7 +636,7 @@ public class MySQL implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean removeAuth(String user) {
|
public boolean removeAuth(String user) {
|
||||||
user = user.toLowerCase();
|
user = user.toLowerCase();
|
||||||
String sql = "DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
String sql = "DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
||||||
PreparedStatement xfSelect = null;
|
PreparedStatement xfSelect = null;
|
||||||
@ -667,7 +669,7 @@ public class MySQL implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean updateQuitLoc(PlayerAuth auth) {
|
public boolean updateQuitLoc(PlayerAuth auth) {
|
||||||
String sql = "UPDATE " + tableName
|
String sql = "UPDATE " + tableName
|
||||||
+ " SET " + col.LASTLOC_X + " =?, " + col.LASTLOC_Y + "=?, " + col.LASTLOC_Z + "=?, " + col.LASTLOC_WORLD + "=?"
|
+ " SET " + col.LASTLOC_X + " =?, " + col.LASTLOC_Y + "=?, " + col.LASTLOC_Z + "=?, " + col.LASTLOC_WORLD + "=?"
|
||||||
+ " WHERE " + col.NAME + "=?;";
|
+ " WHERE " + col.NAME + "=?;";
|
||||||
@ -686,7 +688,7 @@ public class MySQL implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean updateEmail(PlayerAuth auth) {
|
public boolean updateEmail(PlayerAuth auth) {
|
||||||
String sql = "UPDATE " + tableName + " SET " + col.EMAIL + " =? WHERE " + col.NAME + "=?;";
|
String sql = "UPDATE " + tableName + " SET " + col.EMAIL + " =? WHERE " + col.NAME + "=?;";
|
||||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||||
pst.setString(1, auth.getEmail());
|
pst.setString(1, auth.getEmail());
|
||||||
@ -700,14 +702,14 @@ public class MySQL implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void close() {
|
public void close() {
|
||||||
if (ds != null && !ds.isClosed()) {
|
if (ds != null && !ds.isClosed()) {
|
||||||
ds.close();
|
ds.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized List<String> getAllAuthsByIp(String ip) {
|
public List<String> getAllAuthsByIp(String ip) {
|
||||||
List<String> result = new ArrayList<>();
|
List<String> result = new ArrayList<>();
|
||||||
String sql = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.IP + "=?;";
|
String sql = "SELECT " + col.NAME + " FROM " + tableName + " WHERE " + col.IP + "=?;";
|
||||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||||
@ -724,7 +726,7 @@ public class MySQL implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized int countAuthsByEmail(String email) {
|
public int countAuthsByEmail(String email) {
|
||||||
String sql = "SELECT COUNT(1) FROM " + tableName + " WHERE UPPER(" + col.EMAIL + ") = UPPER(?)";
|
String sql = "SELECT COUNT(1) FROM " + tableName + " WHERE UPPER(" + col.EMAIL + ") = UPPER(?)";
|
||||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||||
pst.setString(1, email);
|
pst.setString(1, email);
|
||||||
@ -740,7 +742,7 @@ public class MySQL implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void purgeBanned(Set<String> banned) {
|
public void purgeBanned(Set<String> banned) {
|
||||||
String sql = "DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
String sql = "DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
||||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||||
for (String name : banned) {
|
for (String name : banned) {
|
||||||
@ -758,7 +760,7 @@ public class MySQL implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean isLogged(String user) {
|
public boolean isLogged(String user) {
|
||||||
String sql = "SELECT " + col.IS_LOGGED + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
String sql = "SELECT " + col.IS_LOGGED + " FROM " + tableName + " WHERE " + col.NAME + "=?;";
|
||||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||||
pst.setString(1, user);
|
pst.setString(1, user);
|
||||||
@ -772,7 +774,7 @@ public class MySQL implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void setLogged(String user) {
|
public void setLogged(String user) {
|
||||||
String sql = "UPDATE " + tableName + " SET " + col.IS_LOGGED + "=? WHERE " + col.NAME + "=?;";
|
String sql = "UPDATE " + tableName + " SET " + col.IS_LOGGED + "=? WHERE " + col.NAME + "=?;";
|
||||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||||
pst.setInt(1, 1);
|
pst.setInt(1, 1);
|
||||||
@ -784,7 +786,7 @@ public class MySQL implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void setUnlogged(String user) {
|
public void setUnlogged(String user) {
|
||||||
String sql = "UPDATE " + tableName + " SET " + col.IS_LOGGED + "=? WHERE " + col.NAME + "=?;";
|
String sql = "UPDATE " + tableName + " SET " + col.IS_LOGGED + "=? WHERE " + col.NAME + "=?;";
|
||||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||||
pst.setInt(1, 0);
|
pst.setInt(1, 0);
|
||||||
@ -824,7 +826,7 @@ public class MySQL implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean updateRealName(String user, String realName) {
|
public boolean updateRealName(String user, String realName) {
|
||||||
String sql = "UPDATE " + tableName + " SET " + col.REAL_NAME + "=? WHERE " + col.NAME + "=?;";
|
String sql = "UPDATE " + tableName + " SET " + col.REAL_NAME + "=? WHERE " + col.NAME + "=?;";
|
||||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||||
pst.setString(1, realName);
|
pst.setString(1, realName);
|
||||||
@ -838,7 +840,7 @@ public class MySQL implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean updateIp(String user, String ip) {
|
public boolean updateIp(String user, String ip) {
|
||||||
String sql = "UPDATE " + tableName + " SET " + col.IP + "=? WHERE " + col.NAME + "=?;";
|
String sql = "UPDATE " + tableName + " SET " + col.IP + "=? WHERE " + col.NAME + "=?;";
|
||||||
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(sql)) {
|
||||||
pst.setString(1, ip);
|
pst.setString(1, ip);
|
||||||
|
@ -64,13 +64,13 @@ public class SQLite implements DataSource {
|
|||||||
ConsoleLogger.logException("Error while executing SQL statement:", e);
|
ConsoleLogger.logException("Error while executing SQL statement:", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized void connect() throws ClassNotFoundException, SQLException {
|
private void connect() throws ClassNotFoundException, SQLException {
|
||||||
Class.forName("org.sqlite.JDBC");
|
Class.forName("org.sqlite.JDBC");
|
||||||
ConsoleLogger.info("SQLite driver loaded");
|
ConsoleLogger.info("SQLite driver loaded");
|
||||||
this.con = DriverManager.getConnection("jdbc:sqlite:plugins/AuthMe/" + database + ".db");
|
this.con = DriverManager.getConnection("jdbc:sqlite:plugins/AuthMe/" + database + ".db");
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized void setup() throws SQLException {
|
private void setup() throws SQLException {
|
||||||
Statement st = null;
|
Statement st = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
try {
|
try {
|
||||||
@ -143,7 +143,7 @@ public class SQLite implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean isAuthAvailable(String user) {
|
public boolean isAuthAvailable(String user) {
|
||||||
PreparedStatement pst = null;
|
PreparedStatement pst = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
try {
|
try {
|
||||||
@ -181,7 +181,7 @@ public class SQLite implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized PlayerAuth getAuth(String user) {
|
public PlayerAuth getAuth(String user) {
|
||||||
PreparedStatement pst = null;
|
PreparedStatement pst = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
try {
|
try {
|
||||||
@ -201,7 +201,7 @@ public class SQLite implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean saveAuth(PlayerAuth auth) {
|
public boolean saveAuth(PlayerAuth auth) {
|
||||||
PreparedStatement pst = null;
|
PreparedStatement pst = null;
|
||||||
try {
|
try {
|
||||||
HashedPassword password = auth.getPassword();
|
HashedPassword password = auth.getPassword();
|
||||||
@ -242,12 +242,12 @@ public class SQLite implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean updatePassword(PlayerAuth auth) {
|
public boolean updatePassword(PlayerAuth auth) {
|
||||||
return updatePassword(auth.getNickname(), auth.getPassword());
|
return updatePassword(auth.getNickname(), auth.getPassword());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean updatePassword(String user, HashedPassword password) {
|
public boolean updatePassword(String user, HashedPassword password) {
|
||||||
user = user.toLowerCase();
|
user = user.toLowerCase();
|
||||||
PreparedStatement pst = null;
|
PreparedStatement pst = null;
|
||||||
try {
|
try {
|
||||||
@ -274,7 +274,7 @@ public class SQLite implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean updateSession(PlayerAuth auth) {
|
public boolean updateSession(PlayerAuth auth) {
|
||||||
PreparedStatement pst = null;
|
PreparedStatement pst = null;
|
||||||
try {
|
try {
|
||||||
pst = con.prepareStatement("UPDATE " + tableName + " SET " + col.IP + "=?, " + col.LAST_LOGIN + "=?, " + col.REAL_NAME + "=? WHERE " + col.NAME + "=?;");
|
pst = con.prepareStatement("UPDATE " + tableName + " SET " + col.IP + "=?, " + col.LAST_LOGIN + "=?, " + col.REAL_NAME + "=? WHERE " + col.NAME + "=?;");
|
||||||
@ -315,7 +315,7 @@ public class SQLite implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean removeAuth(String user) {
|
public boolean removeAuth(String user) {
|
||||||
PreparedStatement pst = null;
|
PreparedStatement pst = null;
|
||||||
try {
|
try {
|
||||||
pst = con.prepareStatement("DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;");
|
pst = con.prepareStatement("DELETE FROM " + tableName + " WHERE " + col.NAME + "=?;");
|
||||||
@ -331,7 +331,7 @@ public class SQLite implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean updateQuitLoc(PlayerAuth auth) {
|
public boolean updateQuitLoc(PlayerAuth auth) {
|
||||||
PreparedStatement pst = null;
|
PreparedStatement pst = null;
|
||||||
try {
|
try {
|
||||||
pst = con.prepareStatement("UPDATE " + tableName + " SET " + col.LASTLOC_X + "=?, " + col.LASTLOC_Y + "=?, " + col.LASTLOC_Z + "=?, " + col.LASTLOC_WORLD + "=? WHERE " + col.NAME + "=?;");
|
pst = con.prepareStatement("UPDATE " + tableName + " SET " + col.LASTLOC_X + "=?, " + col.LASTLOC_Y + "=?, " + col.LASTLOC_Z + "=?, " + col.LASTLOC_WORLD + "=? WHERE " + col.NAME + "=?;");
|
||||||
@ -351,7 +351,7 @@ public class SQLite implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean updateEmail(PlayerAuth auth) {
|
public boolean updateEmail(PlayerAuth auth) {
|
||||||
String sql = "UPDATE " + tableName + " SET " + col.EMAIL + "=? WHERE " + col.NAME + "=?;";
|
String sql = "UPDATE " + tableName + " SET " + col.EMAIL + "=? WHERE " + col.NAME + "=?;";
|
||||||
try (PreparedStatement pst = con.prepareStatement(sql)) {
|
try (PreparedStatement pst = con.prepareStatement(sql)) {
|
||||||
pst.setString(1, auth.getEmail());
|
pst.setString(1, auth.getEmail());
|
||||||
@ -365,7 +365,7 @@ public class SQLite implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void close() {
|
public void close() {
|
||||||
try {
|
try {
|
||||||
if (con != null && !con.isClosed()) {
|
if (con != null && !con.isClosed()) {
|
||||||
con.close();
|
con.close();
|
||||||
@ -462,7 +462,7 @@ public class SQLite implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean isLogged(String user) {
|
public boolean isLogged(String user) {
|
||||||
PreparedStatement pst = null;
|
PreparedStatement pst = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
try {
|
try {
|
||||||
@ -481,7 +481,7 @@ public class SQLite implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void setLogged(String user) {
|
public void setLogged(String user) {
|
||||||
PreparedStatement pst = null;
|
PreparedStatement pst = null;
|
||||||
try {
|
try {
|
||||||
pst = con.prepareStatement("UPDATE " + tableName + " SET " + col.IS_LOGGED + "=? WHERE LOWER(" + col.NAME + ")=?;");
|
pst = con.prepareStatement("UPDATE " + tableName + " SET " + col.IS_LOGGED + "=? WHERE LOWER(" + col.NAME + ")=?;");
|
||||||
@ -496,7 +496,7 @@ public class SQLite implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void setUnlogged(String user) {
|
public void setUnlogged(String user) {
|
||||||
PreparedStatement pst = null;
|
PreparedStatement pst = null;
|
||||||
if (user != null)
|
if (user != null)
|
||||||
try {
|
try {
|
||||||
@ -540,7 +540,7 @@ public class SQLite implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean updateRealName(String user, String realName) {
|
public boolean updateRealName(String user, String realName) {
|
||||||
String sql = "UPDATE " + tableName + " SET " + col.REAL_NAME + "=? WHERE " + col.NAME + "=?;";
|
String sql = "UPDATE " + tableName + " SET " + col.REAL_NAME + "=? WHERE " + col.NAME + "=?;";
|
||||||
try (PreparedStatement pst = con.prepareStatement(sql)) {
|
try (PreparedStatement pst = con.prepareStatement(sql)) {
|
||||||
pst.setString(1, realName);
|
pst.setString(1, realName);
|
||||||
@ -554,7 +554,7 @@ public class SQLite implements DataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized boolean updateIp(String user, String ip) {
|
public boolean updateIp(String user, String ip) {
|
||||||
String sql = "UPDATE " + tableName + " SET " + col.IP + "=? WHERE " + col.NAME + "=?;";
|
String sql = "UPDATE " + tableName + " SET " + col.IP + "=? WHERE " + col.NAME + "=?;";
|
||||||
try (PreparedStatement pst = con.prepareStatement(sql)) {
|
try (PreparedStatement pst = con.prepareStatement(sql)) {
|
||||||
pst.setString(1, ip);
|
pst.setString(1, ip);
|
||||||
|
@ -11,7 +11,7 @@ import java.lang.reflect.InvocationTargetException;
|
|||||||
/**
|
/**
|
||||||
* Functionality for constructor injection.
|
* Functionality for constructor injection.
|
||||||
*/
|
*/
|
||||||
class ConstructorInjection<T> implements Injection<T> {
|
public class ConstructorInjection<T> implements Injection<T> {
|
||||||
|
|
||||||
private final Constructor<T> constructor;
|
private final Constructor<T> constructor;
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ import java.util.List;
|
|||||||
/**
|
/**
|
||||||
* Functionality for field injection.
|
* Functionality for field injection.
|
||||||
*/
|
*/
|
||||||
class FieldInjection<T> implements Injection<T> {
|
public class FieldInjection<T> implements Injection<T> {
|
||||||
|
|
||||||
private final Field[] fields;
|
private final Field[] fields;
|
||||||
private final Constructor<T> defaultConstructor;
|
private final Constructor<T> defaultConstructor;
|
||||||
|
@ -5,7 +5,7 @@ package fr.xephi.authme.initialization;
|
|||||||
*
|
*
|
||||||
* @param <T> the type of the concerned object
|
* @param <T> the type of the concerned object
|
||||||
*/
|
*/
|
||||||
interface Injection<T> {
|
public interface Injection<T> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the dependencies that must be provided to instantiate the given item.
|
* Returns the dependencies that must be provided to instantiate the given item.
|
||||||
|
@ -36,9 +36,9 @@ import org.bukkit.event.block.SignChangeEvent;
|
|||||||
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
||||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||||
import org.bukkit.event.inventory.InventoryOpenEvent;
|
import org.bukkit.event.inventory.InventoryOpenEvent;
|
||||||
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
|
||||||
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
|
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
|
||||||
import org.bukkit.event.player.PlayerBedEnterEvent;
|
import org.bukkit.event.player.PlayerBedEnterEvent;
|
||||||
|
import org.bukkit.event.player.PlayerChatEvent;
|
||||||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||||
import org.bukkit.event.player.PlayerDropItemEvent;
|
import org.bukkit.event.player.PlayerDropItemEvent;
|
||||||
import org.bukkit.event.player.PlayerFishEvent;
|
import org.bukkit.event.player.PlayerFishEvent;
|
||||||
@ -55,6 +55,9 @@ import org.bukkit.event.player.PlayerRespawnEvent;
|
|||||||
import org.bukkit.event.player.PlayerShearEntityEvent;
|
import org.bukkit.event.player.PlayerShearEntityEvent;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
@ -70,6 +73,7 @@ public class AuthMePlayerListener implements Listener {
|
|||||||
|
|
||||||
public static final ConcurrentHashMap<String, String> joinMessage = new ConcurrentHashMap<>();
|
public static final ConcurrentHashMap<String, String> joinMessage = new ConcurrentHashMap<>();
|
||||||
public static final ConcurrentHashMap<String, Boolean> causeByAuthMe = new ConcurrentHashMap<>();
|
public static final ConcurrentHashMap<String, Boolean> causeByAuthMe = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private AuthMe plugin;
|
private AuthMe plugin;
|
||||||
@Inject
|
@Inject
|
||||||
@ -89,24 +93,6 @@ public class AuthMePlayerListener implements Listener {
|
|||||||
@Inject
|
@Inject
|
||||||
private ValidationService validationService;
|
private ValidationService validationService;
|
||||||
|
|
||||||
private void handleChat(AsyncPlayerChatEvent event) {
|
|
||||||
if (settings.getProperty(RestrictionSettings.ALLOW_CHAT)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
final Player player = event.getPlayer();
|
|
||||||
if (shouldCancelEvent(player)) {
|
|
||||||
event.setCancelled(true);
|
|
||||||
sendLoginOrRegisterMessage(player);
|
|
||||||
} else if (settings.getProperty(RestrictionSettings.HIDE_CHAT)) {
|
|
||||||
for (Player p : bukkitService.getOnlinePlayers()) {
|
|
||||||
if (!PlayerCache.getInstance().isAuthenticated(p.getName())) {
|
|
||||||
event.getRecipients().remove(p);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void sendLoginOrRegisterMessage(final Player player) {
|
private void sendLoginOrRegisterMessage(final Player player) {
|
||||||
bukkitService.runTaskAsynchronously(new Runnable() {
|
bukkitService.runTaskAsynchronously(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
@ -144,29 +130,31 @@ public class AuthMePlayerListener implements Listener {
|
|||||||
sendLoginOrRegisterMessage(event.getPlayer());
|
sendLoginOrRegisterMessage(event.getPlayer());
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(ignoreCancelled = true, priority = EventPriority.NORMAL)
|
|
||||||
public void onPlayerNormalChat(AsyncPlayerChatEvent event) {
|
|
||||||
handleChat(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGH)
|
|
||||||
public void onPlayerHighChat(AsyncPlayerChatEvent event) {
|
|
||||||
handleChat(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
|
|
||||||
public void onPlayerHighestChat(AsyncPlayerChatEvent event) {
|
|
||||||
handleChat(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
|
|
||||||
public void onPlayerEarlyChat(AsyncPlayerChatEvent event) {
|
|
||||||
handleChat(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOW)
|
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOW)
|
||||||
public void onPlayerLowChat(AsyncPlayerChatEvent event) {
|
public void onPlayerChat(PlayerChatEvent event) {
|
||||||
handleChat(event);
|
if (settings.getProperty(RestrictionSettings.ALLOW_CHAT)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final Player player = event.getPlayer();
|
||||||
|
if (shouldCancelEvent(player)) {
|
||||||
|
event.setCancelled(true);
|
||||||
|
bukkitService.runTaskAsynchronously(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
m.send(player, MessageKey.DENIED_CHAT_MESSAGE);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else if (settings.getProperty(RestrictionSettings.HIDE_CHAT)) {
|
||||||
|
Set<Player> recipients = event.getRecipients();
|
||||||
|
Iterator<Player> iter = recipients.iterator();
|
||||||
|
while (iter.hasNext()) {
|
||||||
|
Player p = iter.next();
|
||||||
|
if (shouldCancelEvent(p)) {
|
||||||
|
iter.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
|
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
|
||||||
@ -192,7 +180,6 @@ public class AuthMePlayerListener implements Listener {
|
|||||||
|
|
||||||
if (!settings.getProperty(RestrictionSettings.ALLOW_UNAUTHED_MOVEMENT)) {
|
if (!settings.getProperty(RestrictionSettings.ALLOW_UNAUTHED_MOVEMENT)) {
|
||||||
event.setTo(event.getFrom());
|
event.setTo(event.getFrom());
|
||||||
// sgdc3 TODO: remove this, maybe we should set the effect every x ticks, idk!
|
|
||||||
if (settings.getProperty(RestrictionSettings.REMOVE_SPEED)) {
|
if (settings.getProperty(RestrictionSettings.REMOVE_SPEED)) {
|
||||||
player.setFlySpeed(0.0f);
|
player.setFlySpeed(0.0f);
|
||||||
player.setWalkSpeed(0.0f);
|
player.setWalkSpeed(0.0f);
|
||||||
@ -253,7 +240,7 @@ public class AuthMePlayerListener implements Listener {
|
|||||||
player.setGameMode(GameMode.SURVIVAL);
|
player.setGameMode(GameMode.SURVIVAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shedule login task so works after the prelogin
|
// Schedule login task so works after the prelogin
|
||||||
// (Fix found by Koolaid5000)
|
// (Fix found by Koolaid5000)
|
||||||
bukkitService.runTask(new Runnable() {
|
bukkitService.runTask(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
@ -266,6 +253,23 @@ public class AuthMePlayerListener implements Listener {
|
|||||||
@EventHandler(priority = EventPriority.HIGHEST)
|
@EventHandler(priority = EventPriority.HIGHEST)
|
||||||
public void onPreLogin(AsyncPlayerPreLoginEvent event) {
|
public void onPreLogin(AsyncPlayerPreLoginEvent event) {
|
||||||
PlayerAuth auth = dataSource.getAuth(event.getName());
|
PlayerAuth auth = dataSource.getAuth(event.getName());
|
||||||
|
if (auth == null && antiBot.getAntiBotStatus() == AntiBotStatus.ACTIVE) {
|
||||||
|
event.setKickMessage(m.retrieveSingle(MessageKey.KICK_ANTIBOT));
|
||||||
|
event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER);
|
||||||
|
antiBot.antibotKicked.addIfAbsent(event.getName());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (auth == null && settings.getProperty(RestrictionSettings.KICK_NON_REGISTERED)) {
|
||||||
|
event.setKickMessage(m.retrieveSingle(MessageKey.MUST_REGISTER_MESSAGE));
|
||||||
|
event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final String name = event.getName().toLowerCase();
|
||||||
|
if (name.length() > settings.getProperty(RestrictionSettings.MAX_NICKNAME_LENGTH) || name.length() < settings.getProperty(RestrictionSettings.MIN_NICKNAME_LENGTH)) {
|
||||||
|
event.setKickMessage(m.retrieveSingle(MessageKey.INVALID_NAME_LENGTH));
|
||||||
|
event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (settings.getProperty(RegistrationSettings.PREVENT_OTHER_CASE) && auth != null && auth.getRealName() != null) {
|
if (settings.getProperty(RegistrationSettings.PREVENT_OTHER_CASE) && auth != null && auth.getRealName() != null) {
|
||||||
String realName = auth.getRealName();
|
String realName = auth.getRealName();
|
||||||
if (!realName.isEmpty() && !"Player".equals(realName) && !realName.equals(event.getName())) {
|
if (!realName.isEmpty() && !"Player".equals(realName) && !realName.equals(event.getName())) {
|
||||||
@ -287,7 +291,6 @@ public class AuthMePlayerListener implements Listener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final String name = event.getName().toLowerCase();
|
|
||||||
final Player player = bukkitService.getPlayerExact(name);
|
final Player player = bukkitService.getPlayerExact(name);
|
||||||
// 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
|
||||||
@ -345,6 +348,7 @@ public class AuthMePlayerListener implements Listener {
|
|||||||
if (antiBot.getAntiBotStatus() == AntiBotStatus.ACTIVE && !isAuthAvailable) {
|
if (antiBot.getAntiBotStatus() == AntiBotStatus.ACTIVE && !isAuthAvailable) {
|
||||||
event.setKickMessage(m.retrieveSingle(MessageKey.KICK_ANTIBOT));
|
event.setKickMessage(m.retrieveSingle(MessageKey.KICK_ANTIBOT));
|
||||||
event.setResult(PlayerLoginEvent.Result.KICK_OTHER);
|
event.setResult(PlayerLoginEvent.Result.KICK_OTHER);
|
||||||
|
antiBot.antibotKicked.addIfAbsent(player.getName());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -362,7 +366,7 @@ public class AuthMePlayerListener implements Listener {
|
|||||||
|
|
||||||
String nickRegEx = settings.getProperty(RestrictionSettings.ALLOWED_NICKNAME_CHARACTERS);
|
String nickRegEx = settings.getProperty(RestrictionSettings.ALLOWED_NICKNAME_CHARACTERS);
|
||||||
Pattern nickPattern = Pattern.compile(nickRegEx);
|
Pattern nickPattern = Pattern.compile(nickRegEx);
|
||||||
if (nickPattern.matcher(player.getName()).matches() || name.equalsIgnoreCase("Player")) {
|
if (name.equalsIgnoreCase("Player") || !nickPattern.matcher(player.getName()).matches()) {
|
||||||
event.setKickMessage(m.retrieveSingle(MessageKey.INVALID_NAME_CHARACTERS).replace("REG_EX", nickRegEx));
|
event.setKickMessage(m.retrieveSingle(MessageKey.INVALID_NAME_CHARACTERS).replace("REG_EX", nickRegEx));
|
||||||
event.setResult(PlayerLoginEvent.Result.KICK_OTHER);
|
event.setResult(PlayerLoginEvent.Result.KICK_OTHER);
|
||||||
return;
|
return;
|
||||||
@ -389,6 +393,10 @@ public class AuthMePlayerListener implements Listener {
|
|||||||
event.setQuitMessage(null);
|
event.setQuitMessage(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (antiBot.antibotKicked.contains(player.getName())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
management.performQuit(player, false);
|
management.performQuit(player, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -406,6 +414,10 @@ public class AuthMePlayerListener implements Listener {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (antiBot.antibotKicked.contains(player.getName())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
plugin.getManagement().performQuit(player, true);
|
plugin.getManagement().performQuit(player, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,38 +0,0 @@
|
|||||||
package fr.xephi.authme.listener;
|
|
||||||
|
|
||||||
import fr.xephi.authme.settings.NewSetting;
|
|
||||||
import fr.xephi.authme.settings.SpawnLoader;
|
|
||||||
import fr.xephi.authme.settings.properties.RestrictionSettings;
|
|
||||||
|
|
||||||
import org.bukkit.event.EventHandler;
|
|
||||||
import org.bukkit.event.EventPriority;
|
|
||||||
import org.bukkit.event.Listener;
|
|
||||||
import org.spigotmc.event.player.PlayerSpawnLocationEvent;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Listener of player events for events introduced in Minecraft 1.9.
|
|
||||||
*/
|
|
||||||
public class AuthMePlayerListener19 implements Listener {
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
private SpawnLoader spawnLoader;
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
private NewSetting settings;
|
|
||||||
|
|
||||||
/* WTF was that? We need to check all the settings before moving the player to the spawn!
|
|
||||||
*
|
|
||||||
* TODO: fixme please!
|
|
||||||
*
|
|
||||||
@EventHandler(priority = EventPriority.LOWEST)
|
|
||||||
public void onPlayerSpawn(PlayerSpawnLocationEvent event) {
|
|
||||||
if(settings.getProperty(RestrictionSettings.NO_TELEPORT)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
event.setSpawnLocation(spawnLoader.getSpawnLocation(event.getPlayer()));
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
}
|
|
@ -5,6 +5,8 @@ package fr.xephi.authme.output;
|
|||||||
*/
|
*/
|
||||||
public enum MessageKey {
|
public enum MessageKey {
|
||||||
|
|
||||||
|
DENIED_CHAT_MESSAGE("denied_chat"),
|
||||||
|
|
||||||
KICK_ANTIBOT("kick_antibot"),
|
KICK_ANTIBOT("kick_antibot"),
|
||||||
|
|
||||||
UNKNOWN_USER("unknown_user"),
|
UNKNOWN_USER("unknown_user"),
|
||||||
|
@ -61,7 +61,7 @@ public final class Settings {
|
|||||||
getSessionTimeout = configFile.getInt("settings.sessions.timeout", 10);
|
getSessionTimeout = configFile.getInt("settings.sessions.timeout", 10);
|
||||||
getMaxNickLength = configFile.getInt("settings.restrictions.maxNicknameLength", 20);
|
getMaxNickLength = configFile.getInt("settings.restrictions.maxNicknameLength", 20);
|
||||||
getMinNickLength = configFile.getInt("settings.restrictions.minNicknameLength", 3);
|
getMinNickLength = configFile.getInt("settings.restrictions.minNicknameLength", 3);
|
||||||
getNickRegex = configFile.getString("settings.restrictions.allowedNicknameCharacters", "[a-zA-Z0-9_?]*");
|
getNickRegex = load(RestrictionSettings.ALLOWED_NICKNAME_CHARACTERS);
|
||||||
nickPattern = Pattern.compile(getNickRegex);
|
nickPattern = Pattern.compile(getNickRegex);
|
||||||
isAllowRestrictedIp = load(RestrictionSettings.ENABLE_RESTRICTED_USERS);
|
isAllowRestrictedIp = load(RestrictionSettings.ENABLE_RESTRICTED_USERS);
|
||||||
isRemoveSpeedEnabled = load(RestrictionSettings.REMOVE_SPEED);
|
isRemoveSpeedEnabled = load(RestrictionSettings.REMOVE_SPEED);
|
||||||
|
@ -28,11 +28,11 @@ public class ProtectionSettings implements SettingsClass {
|
|||||||
|
|
||||||
@Comment("Do we need to enable automatic antibot system?")
|
@Comment("Do we need to enable automatic antibot system?")
|
||||||
public static final Property<Boolean> ENABLE_ANTIBOT =
|
public static final Property<Boolean> ENABLE_ANTIBOT =
|
||||||
newProperty("Protection.enableAntiBot", false);
|
newProperty("Protection.enableAntiBot", true);
|
||||||
|
|
||||||
@Comment("Max number of players allowed to login in 5 secs before the AntiBot system is enabled automatically")
|
@Comment("Max number of players allowed to login in 5 secs before the AntiBot system is enabled automatically")
|
||||||
public static final Property<Integer> ANTIBOT_SENSIBILITY =
|
public static final Property<Integer> ANTIBOT_SENSIBILITY =
|
||||||
newProperty("Protection.antiBotSensibility", 5);
|
newProperty("Protection.antiBotSensibility", 10);
|
||||||
|
|
||||||
@Comment("Duration in minutes of the antibot automatic system")
|
@Comment("Duration in minutes of the antibot automatic system")
|
||||||
public static final Property<Integer> ANTIBOT_DURATION =
|
public static final Property<Integer> ANTIBOT_DURATION =
|
||||||
|
@ -88,7 +88,7 @@ settings:
|
|||||||
# due to "Logged in from another Location"
|
# due to "Logged in from another Location"
|
||||||
# This setting will prevent potetial security exploits.
|
# This setting will prevent potetial security exploits.
|
||||||
ForceSingleSession: true
|
ForceSingleSession: true
|
||||||
ForceSpawnLocOnJoin:
|
ForceSpawnLocOnJoin:
|
||||||
# If enabled, every player will be teleported to the world spawnpoint
|
# If enabled, every player will be teleported to the world spawnpoint
|
||||||
# after successful authentication.
|
# after successful authentication.
|
||||||
# The quit location of the player will be overwritten.
|
# The quit location of the player will be overwritten.
|
||||||
@ -97,7 +97,7 @@ settings:
|
|||||||
enabled: false
|
enabled: false
|
||||||
# WorldNames where we need to force the spawn location
|
# WorldNames where we need to force the spawn location
|
||||||
# Case-sensitive!
|
# Case-sensitive!
|
||||||
worlds:
|
worlds:
|
||||||
- 'world'
|
- 'world'
|
||||||
- 'world_nether'
|
- 'world_nether'
|
||||||
- 'world_the_end'
|
- 'world_the_end'
|
||||||
@ -418,8 +418,8 @@ Protection:
|
|||||||
countriesBlacklist:
|
countriesBlacklist:
|
||||||
- 'A1'
|
- 'A1'
|
||||||
# Do we need to enable automatic antibot system?
|
# Do we need to enable automatic antibot system?
|
||||||
enableAntiBot: false
|
enableAntiBot: true
|
||||||
# Max number of player allowed to login in 5 secs before enable AntiBot system automatically
|
# Max number of player allowed to login in 5 secs before enable AntiBot system automatically
|
||||||
antiBotSensibility: 5
|
antiBotSensibility: 10
|
||||||
# Duration in minutes of the antibot automatic system
|
# Duration in minutes of the antibot automatic system
|
||||||
antiBotDuration: 10
|
antiBotDuration: 10
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
denied_chat: '&cIn order to be able to chat you must be authenticated!'
|
||||||
kick_antibot: 'AntiBot protection mode is enabled! You have to wait some minutes before joining the server.'
|
kick_antibot: 'AntiBot protection mode is enabled! You have to wait some minutes before joining the server.'
|
||||||
unknown_user: '&cCan''t find the requested user in the database!'
|
unknown_user: '&cCan''t find the requested user in the database!'
|
||||||
unsafe_spawn: '&cYour quit location was unsafe, you have been teleported to the world''s spawnpoint.'
|
unsafe_spawn: '&cYour quit location was unsafe, you have been teleported to the world''s spawnpoint.'
|
||||||
|
@ -6,7 +6,6 @@ import fr.xephi.authme.output.Messages;
|
|||||||
import fr.xephi.authme.settings.NewSetting;
|
import fr.xephi.authme.settings.NewSetting;
|
||||||
import fr.xephi.authme.settings.domain.Property;
|
import fr.xephi.authme.settings.domain.Property;
|
||||||
import fr.xephi.authme.settings.properties.SecuritySettings;
|
import fr.xephi.authme.settings.properties.SecuritySettings;
|
||||||
import fr.xephi.authme.util.BukkitService;
|
|
||||||
import fr.xephi.authme.util.ValidationService;
|
import fr.xephi.authme.util.ValidationService;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
@ -45,8 +44,6 @@ public class CommandServiceTest {
|
|||||||
private NewSetting settings;
|
private NewSetting settings;
|
||||||
@Mock
|
@Mock
|
||||||
private ValidationService validationService;
|
private ValidationService validationService;
|
||||||
@Mock
|
|
||||||
private BukkitService bukkitService;
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldSendMessage() {
|
public void shouldSendMessage() {
|
||||||
|
@ -81,7 +81,7 @@ public final class ToolsRunner {
|
|||||||
private static void collectTasksInDirectory(File dir, Map<String, ToolTask> taskCollection) {
|
private static void collectTasksInDirectory(File dir, Map<String, ToolTask> taskCollection) {
|
||||||
File[] files = dir.listFiles();
|
File[] files = dir.listFiles();
|
||||||
if (files == null) {
|
if (files == null) {
|
||||||
throw new RuntimeException("Cannot read folder '" + dir + "'");
|
throw new IllegalStateException("Cannot read folder '" + dir + "'");
|
||||||
}
|
}
|
||||||
for (File file : files) {
|
for (File file : files) {
|
||||||
if (file.isDirectory()) {
|
if (file.isDirectory()) {
|
||||||
@ -112,7 +112,7 @@ public final class ToolsRunner {
|
|||||||
return constructor.newInstance();
|
return constructor.newInstance();
|
||||||
} catch (NoSuchMethodException | InvocationTargetException |
|
} catch (NoSuchMethodException | InvocationTargetException |
|
||||||
IllegalAccessException | InstantiationException e) {
|
IllegalAccessException | InstantiationException e) {
|
||||||
throw new RuntimeException("Cannot instantiate task '" + taskClass + "'");
|
throw new IllegalStateException("Cannot instantiate task '" + taskClass + "'");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,7 +137,7 @@ public final class ToolsRunner {
|
|||||||
? (Class<? extends ToolTask>) clazz
|
? (Class<? extends ToolTask>) clazz
|
||||||
: null;
|
: null;
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
throw new RuntimeException(e);
|
throw new IllegalStateException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
171
src/test/java/tools/checktestmocks/CheckTestMocks.java
Normal file
171
src/test/java/tools/checktestmocks/CheckTestMocks.java
Normal file
@ -0,0 +1,171 @@
|
|||||||
|
package tools.checktestmocks;
|
||||||
|
|
||||||
|
import com.google.common.base.Function;
|
||||||
|
import com.google.common.collect.Collections2;
|
||||||
|
import com.google.common.collect.Sets;
|
||||||
|
import fr.xephi.authme.initialization.ConstructorInjection;
|
||||||
|
import fr.xephi.authme.initialization.FieldInjection;
|
||||||
|
import fr.xephi.authme.initialization.Injection;
|
||||||
|
import fr.xephi.authme.util.StringUtils;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import tools.utils.AutoToolTask;
|
||||||
|
import tools.utils.ToolsConstants;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Task checking if all tests' {@code @Mock} fields have a corresponding
|
||||||
|
* {@code @Inject} field in the class they are testing.
|
||||||
|
*/
|
||||||
|
public class CheckTestMocks implements AutoToolTask {
|
||||||
|
|
||||||
|
private List<String> errors = new ArrayList<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getTaskName() {
|
||||||
|
return "checkTestMocks";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(Scanner scanner) {
|
||||||
|
executeDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void executeDefault() {
|
||||||
|
readAndCheckFiles(new File(ToolsConstants.TEST_SOURCE_ROOT));
|
||||||
|
System.out.println(StringUtils.join("\n", errors));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Recursively reads directories and checks the contained classes.
|
||||||
|
*
|
||||||
|
* @param dir the directory to read
|
||||||
|
*/
|
||||||
|
private void readAndCheckFiles(File dir) {
|
||||||
|
File[] files = dir.listFiles();
|
||||||
|
if (files == null) {
|
||||||
|
throw new IllegalStateException("Cannot read folder '" + dir + "'");
|
||||||
|
}
|
||||||
|
for (File file : files) {
|
||||||
|
if (file.isDirectory()) {
|
||||||
|
readAndCheckFiles(file);
|
||||||
|
} else if (file.isFile()) {
|
||||||
|
Class<?> clazz = loadTestClass(file);
|
||||||
|
if (clazz != null) {
|
||||||
|
checkClass(clazz);
|
||||||
|
}
|
||||||
|
// else System.out.format("No @Mock fields found in class of file '%s'%n", file.getName())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks the given test class' @Mock fields against the corresponding production class' @Inject fields.
|
||||||
|
*
|
||||||
|
* @param testClass the test class to verify
|
||||||
|
*/
|
||||||
|
private void checkClass(Class<?> testClass) {
|
||||||
|
Class<?> realClass = returnRealClass(testClass);
|
||||||
|
if (realClass != null) {
|
||||||
|
Set<Class<?>> mockFields = getMocks(testClass);
|
||||||
|
Set<Class<?>> injectFields = getRealClassDependencies(realClass);
|
||||||
|
if (!injectFields.containsAll(mockFields)) {
|
||||||
|
addErrorEntry(testClass, "Error - Found the following mocks absent as @Inject: "
|
||||||
|
+ formatClassList(Sets.difference(mockFields, injectFields)));
|
||||||
|
} else if (!mockFields.containsAll(injectFields)) {
|
||||||
|
addErrorEntry(testClass, "Info - Found @Inject fields which are not present as @Mock: "
|
||||||
|
+ formatClassList(Sets.difference(injectFields, mockFields)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addErrorEntry(Class<?> clazz, String message) {
|
||||||
|
errors.add(clazz.getSimpleName() + ": " + message);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Class<?> loadTestClass(File file) {
|
||||||
|
String fileName = file.getPath();
|
||||||
|
String className = fileName
|
||||||
|
// Strip source folders and .java ending
|
||||||
|
.substring("src/test/java/".length(), fileName.length() - 5)
|
||||||
|
.replace(File.separator, ".");
|
||||||
|
try {
|
||||||
|
Class<?> clazz = CheckTestMocks.class.getClassLoader().loadClass(className);
|
||||||
|
return isTestClassWithMocks(clazz) ? clazz : null;
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
throw new UnsupportedOperationException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Set<Class<?>> getMocks(Class<?> clazz) {
|
||||||
|
Set<Class<?>> result = new HashSet<>();
|
||||||
|
for (Field field : clazz.getDeclaredFields()) {
|
||||||
|
if (field.isAnnotationPresent(Mock.class)) {
|
||||||
|
result.add(field.getType());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the production class ("real class") corresponding to the given test class.
|
||||||
|
* Returns null if the production class could not be mapped or loaded.
|
||||||
|
*
|
||||||
|
* @param testClass the test class to find the corresponding production class for
|
||||||
|
* @return production class, or null if not found
|
||||||
|
*/
|
||||||
|
private static Class<?> returnRealClass(Class<?> testClass) {
|
||||||
|
String testClassName = testClass.getName();
|
||||||
|
String realClassName = testClassName.replaceAll("(Integration|Consistency)?Test$", "");
|
||||||
|
if (realClassName.equals(testClassName)) {
|
||||||
|
System.out.format("%s doesn't match typical test class naming pattern.%n", testClassName);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return CheckTestMocks.class.getClassLoader().loadClass(realClassName);
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
System.out.format("Real class '%s' not found for test class '%s'%n", realClassName, testClassName);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Set<Class<?>> getRealClassDependencies(Class<?> realClass) {
|
||||||
|
Injection<?> injection = ConstructorInjection.provide(realClass).get();
|
||||||
|
if (injection != null) {
|
||||||
|
return Sets.newHashSet(injection.getDependencies());
|
||||||
|
}
|
||||||
|
injection = FieldInjection.provide(realClass).get();
|
||||||
|
return injection == null
|
||||||
|
? Collections.<Class<?>>emptySet()
|
||||||
|
: Sets.newHashSet(injection.getDependencies());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isTestClassWithMocks(Class<?> clazz) {
|
||||||
|
for (Field field : clazz.getDeclaredFields()) {
|
||||||
|
if (field.isAnnotationPresent(Mock.class)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String formatClassList(Collection<Class<?>> coll) {
|
||||||
|
Collection<String> classNames = Collections2.transform(coll, new Function<Class<?>, String>() {
|
||||||
|
@Override
|
||||||
|
public String apply(Class<?> input) {
|
||||||
|
return input.getSimpleName();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return StringUtils.join(", ", classNames);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -12,6 +12,9 @@ public final class ToolsConstants {
|
|||||||
|
|
||||||
public static final String MAIN_RESOURCES_ROOT = "src/main/resources/";
|
public static final String MAIN_RESOURCES_ROOT = "src/main/resources/";
|
||||||
|
|
||||||
|
// Add specific `fr.xephi.authme` package as not to include the tool tasks in the `tools` package
|
||||||
|
public static final String TEST_SOURCE_ROOT = "src/test/java/fr/xephi/authme";
|
||||||
|
|
||||||
public static final String TOOLS_SOURCE_ROOT = "src/test/java/tools/";
|
public static final String TOOLS_SOURCE_ROOT = "src/test/java/tools/";
|
||||||
|
|
||||||
public static final String DOCS_FOLDER = "docs/";
|
public static final String DOCS_FOLDER = "docs/";
|
||||||
|
Loading…
Reference in New Issue
Block a user