From 0aed988e22ed325d6e1bc70fef6abf3de36e210d Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Sun, 14 Jan 2018 21:41:51 +0200 Subject: [PATCH] Removed some redundant code & fixed IllegalPluginAccessError (#482) --- .../plan/settings/locale/Locale.java | 1 + .../databases/operation/FetchOperations.java | 7 +- .../system/database/databases/sql/SQLDB.java | 109 ++---------------- .../database/databases/sql/SQLiteDB.java | 53 ++++----- .../databases/sql/operation/SQLFetchOps.java | 71 +++++++++++- .../databases/sql/operation/SQLRemoveOps.java | 3 + .../tables/move/Version8TransferTable.java | 4 +- 7 files changed, 107 insertions(+), 141 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/settings/locale/Locale.java b/Plan/src/main/java/com/djrapitops/plan/settings/locale/Locale.java index 1e85e61d9..3392889e8 100644 --- a/Plan/src/main/java/com/djrapitops/plan/settings/locale/Locale.java +++ b/Plan/src/main/java/com/djrapitops/plan/settings/locale/Locale.java @@ -1,5 +1,6 @@ package com.djrapitops.plan.settings.locale; +import com.djrapitops.plan.PlanPlugin; import com.djrapitops.plan.system.file.FileSystem; import com.djrapitops.plan.system.settings.Permissions; import com.djrapitops.plan.system.settings.Settings; diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/operation/FetchOperations.java b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/operation/FetchOperations.java index 18099e2c9..311f2e68b 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/operation/FetchOperations.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/operation/FetchOperations.java @@ -4,15 +4,14 @@ import com.djrapitops.plan.api.exceptions.database.DBException; import com.djrapitops.plan.data.PlayerProfile; import com.djrapitops.plan.data.ServerProfile; -import java.util.Map; -import java.util.Optional; -import java.util.Set; -import java.util.UUID; +import java.util.*; public interface FetchOperations { ServerProfile getServerProfile(UUID serverUUID) throws DBException; + List getPlayers(UUID serverUUID) throws DBException; + PlayerProfile getPlayerProfile(UUID uuid) throws DBException; Set getSavedUUIDs() throws DBException; diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLDB.java b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLDB.java index 0e1c2ddb8..8053e84a0 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLDB.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLDB.java @@ -1,9 +1,6 @@ package com.djrapitops.plan.system.database.databases.sql; import com.djrapitops.plan.api.exceptions.database.DBInitException; -import com.djrapitops.plan.data.PlayerProfile; -import com.djrapitops.plan.data.ServerProfile; -import com.djrapitops.plan.data.container.*; import com.djrapitops.plan.system.database.databases.Database; import com.djrapitops.plan.system.database.databases.operation.BackupOperations; import com.djrapitops.plan.system.database.databases.operation.CheckOperations; @@ -15,7 +12,6 @@ import com.djrapitops.plan.system.database.databases.sql.operation.SQLFetchOps; import com.djrapitops.plan.system.database.databases.sql.operation.SQLRemoveOps; import com.djrapitops.plan.system.database.databases.sql.tables.*; import com.djrapitops.plan.system.database.databases.sql.tables.move.Version8TransferTable; -import com.djrapitops.plan.utilities.MiscUtils; import com.djrapitops.plugin.api.Benchmark; import com.djrapitops.plugin.api.TimeAmount; import com.djrapitops.plugin.api.utility.log.Log; @@ -26,7 +22,6 @@ import org.apache.commons.dbcp2.BasicDataSource; import java.sql.Connection; import java.sql.SQLException; -import java.util.*; /** * Class containing main logic for different data related save and load functionality. @@ -98,7 +93,7 @@ public abstract class SQLDB extends Database { */ @Override public void init() throws DBInitException { - setStatus("Init"); + setStatus("Initiating"); String benchName = "Init " + getConfigName(); Benchmark.start("Database", benchName); try { @@ -109,6 +104,7 @@ public abstract class SQLDB extends Database { Benchmark.stop("Database", benchName); Log.logDebug("Database"); } + setStatus("Open"); } public void scheduleClean(long secondsDelay) { @@ -137,17 +133,17 @@ public abstract class SQLDB extends Database { */ public void setupDatabase() throws DBInitException { try { - boolean newDatabase = isNewDatabase(); + boolean newDatabase = versionTable.isNewDatabase(); versionTable.createTable(); createTables(); if (newDatabase) { Log.info("New Database created."); - setVersion(13); + versionTable.setVersion(13); } - int version = getVersion(); + int version = versionTable.getVersion(); final SQLDB db = this; if (version < 10) { @@ -164,16 +160,16 @@ public abstract class SQLDB extends Database { } if (version < 11) { serverTable.alterTableV11(); - setVersion(11); + versionTable.setVersion(11); } if (version < 12) { actionsTable.alterTableV12(); ipsTable.alterTableV12(); - setVersion(12); + versionTable.setVersion(12); } if (version < 13) { ipsTable.alterTableV13(); - setVersion(13); + versionTable.setVersion(13); } } catch (SQLException e) { throw new DBInitException("Failed to set-up Database", e); @@ -245,91 +241,6 @@ public abstract class SQLDB extends Database { versionTable.setVersion(version); } - public boolean isNewDatabase() throws SQLException { - return versionTable.isNewDatabase(); - } - - @Override - public ServerProfile getServerProfile(UUID serverUUID) throws SQLException { - ServerProfile profile = new ServerProfile(serverUUID); - - profile.setPlayers(getPlayers(serverUUID)); - profile.setTps(tpsTable.getTPSData(serverUUID)); - Optional allTimePeak = tpsTable.getAllTimePeak(serverUUID); - if (allTimePeak.isPresent()) { - TPS peak = allTimePeak.get(); - profile.setAllTimePeak(peak.getDate()); - profile.setAllTimePeakPlayers(peak.getPlayers()); - } - Optional lastPeak = tpsTable.getPeakPlayerCount(serverUUID, MiscUtils.getTime() - (TimeAmount.DAY.ms() * 2L)); - if (lastPeak.isPresent()) { - TPS peak = lastPeak.get(); - profile.setLastPeakDate(peak.getDate()); - profile.setLastPeakPlayers(peak.getPlayers()); - } - - profile.setCommandUsage(commandUseTable.getCommandUse(serverUUID)); - profile.setServerWorldtimes(worldTimesTable.getWorldTimesOfServer(serverUUID)); - - return profile; - } - - private List getPlayers(UUID serverUUID) throws SQLException { - List serverUserInfo = userInfoTable.getServerUserInfo(serverUUID); - Map timesKicked = usersTable.getAllTimesKicked(); - Map> actions = actionsTable.getServerActions(serverUUID); - Map> geoInfo = ipsTable.getAllGeoInfo(); - - Map> sessions = sessionsTable.getSessionInfoOfServer(serverUUID); - Map>> map = new HashMap<>(); - map.put(serverUUID, sessions); - killsTable.addKillsToSessions(map); - worldTimesTable.addWorldTimesToSessions(map); - - List players = new ArrayList<>(); - - for (UserInfo userInfo : serverUserInfo) { - UUID uuid = userInfo.getUuid(); - PlayerProfile profile = new PlayerProfile(uuid, userInfo.getName(), userInfo.getRegistered()); - profile.setTimesKicked(timesKicked.getOrDefault(uuid, 0)); - if (userInfo.isBanned()) { - profile.bannedOnServer(serverUUID); - } - if (userInfo.isOpped()) { - profile.oppedOnServer(serverUUID); - } - profile.setActions(actions.getOrDefault(uuid, new ArrayList<>())); - profile.setGeoInformation(geoInfo.getOrDefault(uuid, new ArrayList<>())); - profile.setSessions(serverUUID, sessions.getOrDefault(uuid, new ArrayList<>())); - - players.add(profile); - } - return players; - } - - public void removeAccount(UUID uuid) throws SQLException { - if (uuid == null) { - return; - } - - try { - Log.logDebug("Database", "Removing Account: " + uuid); - Benchmark.start("Database", "Remove Account"); - - for (Table t : getAllTablesInRemoveOrder()) { - if (!(t instanceof UserIDTable)) { - continue; - } - - UserIDTable table = (UserIDTable) t; - table.removeUser(uuid); - } - } finally { - Benchmark.stop("Database", "Remove Account"); - setAvailable(); - } - } - private void clean() throws SQLException { Log.info("Cleaning the database."); tpsTable.clean(); @@ -340,10 +251,6 @@ public abstract class SQLDB extends Database { Log.logDebug("Database", status); } - public void setAvailable() { - Log.logDebug("Database"); - } - public abstract Connection getConnection() throws SQLException; /** diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLiteDB.java b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLiteDB.java index ca838b264..e9a1f1fd1 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLiteDB.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/SQLiteDB.java @@ -66,45 +66,38 @@ public class SQLiteDB extends SQLDB { Log.info("SQLite WAL mode not supported on this server version, using default. This may or may not affect performance."); } connection.setAutoCommit(false); -// connection. - -// setJournalMode(connection); return connection; } - private void setJournalMode(Connection connection) throws SQLException { - try (Statement statement = connection.createStatement()) { - statement.execute(""); - } - } - private void startConnectionPingTask() { stopConnectionPingTask(); - - // Maintains Connection. - connectionPingTask = RunnableFactory.createNew(new AbsRunnable("DBConnectionPingTask " + getName()) { - @Override - public void run() { - Statement statement = null; - try { - if (connection != null && !connection.isClosed()) { - statement = connection.createStatement(); - statement.execute("/* ping */ SELECT 1"); - } - } catch (SQLException e) { + try { + // Maintains Connection. + connectionPingTask = RunnableFactory.createNew(new AbsRunnable("DBConnectionPingTask " + getName()) { + @Override + public void run() { + Statement statement = null; try { - connection = getNewConnection(dbName); - } catch (SQLException e1) { - Log.toLog(this.getClass().getName(), e1); - Log.error("SQLite connection maintaining task had to be closed due to exception."); - this.cancel(); + if (connection != null && !connection.isClosed()) { + statement = connection.createStatement(); + statement.execute("/* ping */ SELECT 1"); + } + } catch (SQLException e) { + try { + connection = getNewConnection(dbName); + } catch (SQLException e1) { + Log.toLog(this.getClass().getName(), e1); + Log.error("SQLite connection maintaining task had to be closed due to exception."); + this.cancel(); + } + } finally { + MiscUtils.close(statement); } - } finally { - MiscUtils.close(statement); } - } - }).runTaskTimerAsynchronously(60L * 20L, 60L * 20L); + }).runTaskTimerAsynchronously(60L * 20L, 60L * 20L); + } catch (Exception ignored) { + } } private void stopConnectionPingTask() { diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLFetchOps.java b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLFetchOps.java index c901f741e..5f6511b04 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLFetchOps.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLFetchOps.java @@ -3,11 +3,12 @@ package com.djrapitops.plan.system.database.databases.sql.operation; import com.djrapitops.plan.api.exceptions.database.DBException; import com.djrapitops.plan.data.PlayerProfile; import com.djrapitops.plan.data.ServerProfile; -import com.djrapitops.plan.data.container.Session; -import com.djrapitops.plan.data.container.UserInfo; +import com.djrapitops.plan.data.container.*; import com.djrapitops.plan.system.database.databases.operation.FetchOperations; import com.djrapitops.plan.system.database.databases.sql.ErrorUtil; import com.djrapitops.plan.system.database.databases.sql.SQLDB; +import com.djrapitops.plan.utilities.MiscUtils; +import com.djrapitops.plugin.api.TimeAmount; import java.sql.SQLException; import java.util.*; @@ -19,8 +20,70 @@ public class SQLFetchOps extends SQLOps implements FetchOperations { } @Override - public ServerProfile getServerProfile(UUID serverUUID) { - return null; + public ServerProfile getServerProfile(UUID serverUUID) throws DBException { + try { + ServerProfile profile = new ServerProfile(serverUUID); + + profile.setPlayers(getPlayers(serverUUID)); + profile.setTps(tpsTable.getTPSData(serverUUID)); + Optional allTimePeak = tpsTable.getAllTimePeak(serverUUID); + if (allTimePeak.isPresent()) { + TPS peak = allTimePeak.get(); + profile.setAllTimePeak(peak.getDate()); + profile.setAllTimePeakPlayers(peak.getPlayers()); + } + Optional lastPeak = tpsTable.getPeakPlayerCount(serverUUID, MiscUtils.getTime() - (TimeAmount.DAY.ms() * 2L)); + if (lastPeak.isPresent()) { + TPS peak = lastPeak.get(); + profile.setLastPeakDate(peak.getDate()); + profile.setLastPeakPlayers(peak.getPlayers()); + } + + profile.setCommandUsage(commandUseTable.getCommandUse(serverUUID)); + profile.setServerWorldtimes(worldTimesTable.getWorldTimesOfServer(serverUUID)); + + return profile; + } catch (SQLException e) { + throw ErrorUtil.getExceptionFor(e); + } + } + + @Override + public List getPlayers(UUID serverUUID) throws DBException { + try { + List serverUserInfo = userInfoTable.getServerUserInfo(serverUUID); + Map timesKicked = usersTable.getAllTimesKicked(); + Map> actions = actionsTable.getServerActions(serverUUID); + Map> geoInfo = ipsTable.getAllGeoInfo(); + + Map> sessions = sessionsTable.getSessionInfoOfServer(serverUUID); + Map>> map = new HashMap<>(); + map.put(serverUUID, sessions); + killsTable.addKillsToSessions(map); + worldTimesTable.addWorldTimesToSessions(map); + + List players = new ArrayList<>(); + + for (UserInfo userInfo : serverUserInfo) { + UUID uuid = userInfo.getUuid(); + PlayerProfile profile = new PlayerProfile(uuid, userInfo.getName(), userInfo.getRegistered()); + profile.setTimesKicked(timesKicked.getOrDefault(uuid, 0)); + if (userInfo.isBanned()) { + profile.bannedOnServer(serverUUID); + } + if (userInfo.isOpped()) { + profile.oppedOnServer(serverUUID); + } + profile.setActions(actions.getOrDefault(uuid, new ArrayList<>())); + profile.setGeoInformation(geoInfo.getOrDefault(uuid, new ArrayList<>())); + profile.setSessions(serverUUID, sessions.getOrDefault(uuid, new ArrayList<>())); + + players.add(profile); + } + return players; + } catch (SQLException e) { + throw ErrorUtil.getExceptionFor(e); + } } @Override diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLRemoveOps.java b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLRemoveOps.java index b3905dfce..1f6aee27d 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLRemoveOps.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLRemoveOps.java @@ -28,6 +28,7 @@ public class SQLRemoveOps extends SQLOps implements RemoveOperations { try { Log.logDebug("Database", "Removing Account: " + uuid); Benchmark.start("Database", "Remove Account"); + String webUser = usersTable.getPlayerName(uuid); for (Table t : db.getAllTablesInRemoveOrder()) { if (!(t instanceof UserIDTable)) { @@ -37,6 +38,8 @@ public class SQLRemoveOps extends SQLOps implements RemoveOperations { UserIDTable table = (UserIDTable) t; table.removeUser(uuid); } + + securityTable.removeUser(webUser); } catch (SQLException e) { throw ErrorUtil.getFatalExceptionFor(e); } finally { diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/move/Version8TransferTable.java b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/move/Version8TransferTable.java index 4fd88394a..6fb9a502a 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/move/Version8TransferTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/move/Version8TransferTable.java @@ -24,8 +24,8 @@ public class Version8TransferTable extends Table { private final int serverID; - public Version8TransferTable(SQLDB db, boolean usingMySQL) throws SQLException { - super("", db, usingMySQL); + public Version8TransferTable(SQLDB db) throws SQLException { + super("", db); Optional serverID = db.getServerTable().getServerID(Plan.getServerUUID()); if (!serverID.isPresent()) { throw new IllegalStateException("Server UUID was not registered, try rebooting the plugin.");