Removed some redundant code & fixed IllegalPluginAccessError (#482)

This commit is contained in:
Rsl1122 2018-01-14 21:41:51 +02:00
parent 15c6e17173
commit 0aed988e22
7 changed files with 107 additions and 141 deletions

View File

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

View File

@ -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<PlayerProfile> getPlayers(UUID serverUUID) throws DBException;
PlayerProfile getPlayerProfile(UUID uuid) throws DBException;
Set<UUID> getSavedUUIDs() throws DBException;

View File

@ -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<TPS> allTimePeak = tpsTable.getAllTimePeak(serverUUID);
if (allTimePeak.isPresent()) {
TPS peak = allTimePeak.get();
profile.setAllTimePeak(peak.getDate());
profile.setAllTimePeakPlayers(peak.getPlayers());
}
Optional<TPS> 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<PlayerProfile> getPlayers(UUID serverUUID) throws SQLException {
List<UserInfo> serverUserInfo = userInfoTable.getServerUserInfo(serverUUID);
Map<UUID, Integer> timesKicked = usersTable.getAllTimesKicked();
Map<UUID, List<Action>> actions = actionsTable.getServerActions(serverUUID);
Map<UUID, List<GeoInfo>> geoInfo = ipsTable.getAllGeoInfo();
Map<UUID, List<Session>> sessions = sessionsTable.getSessionInfoOfServer(serverUUID);
Map<UUID, Map<UUID, List<Session>>> map = new HashMap<>();
map.put(serverUUID, sessions);
killsTable.addKillsToSessions(map);
worldTimesTable.addWorldTimesToSessions(map);
List<PlayerProfile> 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;
/**

View File

@ -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() {

View File

@ -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<TPS> allTimePeak = tpsTable.getAllTimePeak(serverUUID);
if (allTimePeak.isPresent()) {
TPS peak = allTimePeak.get();
profile.setAllTimePeak(peak.getDate());
profile.setAllTimePeakPlayers(peak.getPlayers());
}
Optional<TPS> 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<PlayerProfile> getPlayers(UUID serverUUID) throws DBException {
try {
List<UserInfo> serverUserInfo = userInfoTable.getServerUserInfo(serverUUID);
Map<UUID, Integer> timesKicked = usersTable.getAllTimesKicked();
Map<UUID, List<Action>> actions = actionsTable.getServerActions(serverUUID);
Map<UUID, List<GeoInfo>> geoInfo = ipsTable.getAllGeoInfo();
Map<UUID, List<Session>> sessions = sessionsTable.getSessionInfoOfServer(serverUUID);
Map<UUID, Map<UUID, List<Session>>> map = new HashMap<>();
map.put(serverUUID, sessions);
killsTable.addKillsToSessions(map);
worldTimesTable.addWorldTimesToSessions(map);
List<PlayerProfile> 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

View File

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

View File

@ -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<Integer> serverID = db.getServerTable().getServerID(Plan.getServerUUID());
if (!serverID.isPresent()) {
throw new IllegalStateException("Server UUID was not registered, try rebooting the plugin.");