Merge branch '4.0.0-BungeeCord-Support' of https://github.com/Rsl1122/Plan-PlayerAnalytics into 4.0.0-BungeeCord-Support

This commit is contained in:
Rsl1122 2017-08-24 11:54:26 +03:00
commit 0029829d7b
31 changed files with 329 additions and 282 deletions

View File

@ -31,6 +31,22 @@
<version>2.0.4</version>
<scope>compile</scope>
</dependency>
<!-- Connection Pool-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- SoftDepended Plugins-->
<dependency>
<groupId>com.djrapitops</groupId>

View File

@ -3,7 +3,9 @@ package main.java.com.djrapitops.plan.database;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.UserInfo;
import main.java.com.djrapitops.plan.database.tables.*;
import org.apache.commons.dbcp2.BasicDataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;
@ -100,6 +102,8 @@ public abstract class Database {
*/
protected ServerTable serverTable;
protected BasicDataSource dataSource;
/**
* Super constructor.
*
@ -332,8 +336,6 @@ public abstract class Database {
return serverTable;
}
public abstract void commit() throws SQLException;
public ActionsTable getActionsTable() {
return actionsTable;
}
@ -341,4 +343,9 @@ public abstract class Database {
public UserInfoTable getUserInfoTable() {
return userInfoTable;
}
public BasicDataSource getDataSource() {
return dataSource;
}
public abstract void commit(Connection connection) throws SQLException;
}

View File

@ -1,15 +1,9 @@
package main.java.com.djrapitops.plan.database.databases;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.locale.Locale;
import main.java.com.djrapitops.plan.locale.Msg;
import org.apache.commons.dbcp2.BasicDataSource;
import org.bukkit.configuration.file.FileConfiguration;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* @author Rsl1122
*/
@ -25,30 +19,32 @@ public class MySQLDB extends SQLDB {
}
/**
* Creates a new connection to the database.
*
* @return the new Connection.
* Setups the {@link BasicDataSource}
*/
@Override
public Connection getNewConnection() {
public void setupDataSource() {
FileConfiguration config = plugin.getConfig();
try {
Class.forName("com.mysql.jdbc.Driver");
dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://" + config.getString("mysql.host") + ":" + config.getString("mysql.port") + "/"
+ config.getString("mysql.database")
+ "?rewriteBatchedStatements=true";
String host = config.getString("Database.MySQL.Host");
String port = config.getString("Database.MySQL.Port");
String database = config.getString("Database.MySQL.Database");
return DriverManager.getConnection(url, config.getString("mysql.user"), config.getString("mysql.password"));
} catch (ClassNotFoundException | SQLException e) {
Log.error(Locale.get(Msg.ENABLE_FAIL_DB).parse(getConfigName(), e.getMessage()));
return null;
}
dataSource.setUrl("jdbc:mysql://" + host + ":" + port + "/" + database + "?rewriteBatchedStatements=true");
String username = config.getString("Database.MySQL.User");
String password = config.getString("Database.MySQL.Password");
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setMaxTotal(-1);
}
/**
* @return
* @return the name of the Database
*/
@Override
public String getName() {

View File

@ -1,13 +1,12 @@
package main.java.com.djrapitops.plan.database.databases;
import com.djrapitops.plugin.task.AbsRunnable;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.UserInfo;
import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.database.tables.*;
import main.java.com.djrapitops.plan.utilities.Benchmark;
import main.java.com.djrapitops.plan.utilities.MiscUtils;
import org.apache.commons.dbcp2.BasicDataSource;
import java.sql.Connection;
import java.sql.SQLException;
@ -27,8 +26,6 @@ public abstract class SQLDB extends Database {
private final boolean usingMySQL;
private Connection connection;
/**
* @param plugin
*/
@ -52,31 +49,6 @@ public abstract class SQLDB extends Database {
killsTable = new KillsTable(this, usingMySQL);
worldTable = new WorldTable(this, usingMySQL);
worldTimesTable = new WorldTimesTable(this, usingMySQL);
startConnectionPingTask();
}
/**
* Starts repeating Async task that maintains the Database connection.
*/
public void startConnectionPingTask() {
// Maintains Connection.
plugin.getRunnableFactory().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) {
connection = getNewConnection();
} finally {
MiscUtils.close(statement);
}
}
}).runTaskTimerAsynchronously(60L * 20L, 60L * 20L);
}
/**
@ -96,7 +68,9 @@ public abstract class SQLDB extends Database {
String benchName = "Init " + getConfigName();
Benchmark.start(benchName);
try {
if (!checkConnection()) {
setupDataSource();
if (!setupDatabase()) {
return false;
}
clean();
@ -118,37 +92,29 @@ public abstract class SQLDB extends Database {
* @return Is the connection usable?
* @throws SQLException
*/
public boolean checkConnection() throws SQLException {
if (connection == null || connection.isClosed()) {
connection = getNewConnection();
public boolean setupDatabase() throws SQLException {
boolean newDatabase = isNewDatabase();
if (connection == null || connection.isClosed()) {
return false;
}
if (!versionTable.createTable()) {
Log.error("Failed to create table: " + versionTable.getTableName());
return false;
}
boolean newDatabase = isNewDatabase();
if (newDatabase) {
Log.info("New Database created.");
}
if (!versionTable.createTable()) {
Log.error("Failed to create table: " + versionTable.getTableName());
return false;
}
if (!createTables()) {
return false;
}
if (newDatabase) {
Log.info("New Database created.");
setVersion(8);
}
if (newDatabase || getVersion() < 8) {
setVersion(8);
}
if (!createTables()) {
return false;
}
if (!newDatabase && getVersion() < 8) {
setVersion(8);
}
try (Statement statement = connection.createStatement()) {
statement.execute("DROP TABLE IF EXISTS plan_locations");
}
try (Statement statement = getConnection().createStatement()) {
statement.execute("DROP TABLE IF EXISTS plan_locations");
endTransaction(statement.getConnection());
}
return true;
}
@ -209,18 +175,16 @@ public abstract class SQLDB extends Database {
}
/**
* @return
* Setups the {@link BasicDataSource}
*/
public abstract Connection getNewConnection();
public abstract void setupDataSource();
/**
* @throws SQLException
*/
@Override
public void close() throws SQLException {
if (connection != null) {
connection.close();
}
dataSource.close();
setStatus("Closed");
Log.logDebug("Database"); // Log remaining Debug info if present
}
@ -240,7 +204,6 @@ public abstract class SQLDB extends Database {
@Override
public void setVersion(int version) throws SQLException {
versionTable.setVersion(version);
commit();
}
/**
@ -266,29 +229,31 @@ public abstract class SQLDB extends Database {
if (uuid == null) {
return false;
}
try {
Benchmark.start("Remove Account");
Log.debug("Database", "Removing Account: " + uuid);
checkConnection();
try {
setupDatabase();
} catch (Exception e) {
Log.toLog(this.getClass().getName(), e);
return false;
}
boolean success = true;
for (Table t : getAllTablesInRemoveOrder()) {
if (!success) {
if (!(t instanceof UserIDTable)) {
continue;
}
if (t instanceof UserIDTable) {
UserIDTable table = (UserIDTable) t;
success = table.removeUser(uuid);
UserIDTable table = (UserIDTable) t;
if (!table.removeUser(uuid)) {
throw new IllegalStateException("Removal Failed");
}
}
if (success) {
commit();
return true;
}
throw new IllegalStateException("Removal Failed");
return true;
} catch (Exception e) {
Log.toLog(this.getClass().getName(), e);
rollback(); // TODO Test case
return false;
} finally {
Benchmark.stop("Database", "Remove Account");
@ -303,7 +268,7 @@ public abstract class SQLDB extends Database {
public void clean() {
Log.info("Cleaning the database.");
try {
checkConnection();
setupDatabase();
tpsTable.clean();
Log.info("Clean complete.");
} catch (SQLException e) {
@ -316,27 +281,21 @@ public abstract class SQLDB extends Database {
*/
@Override
public boolean removeAllData() {
boolean success = true;
setStatus("Clearing all data");
try {
for (Table table : getAllTablesInRemoveOrder()) {
if (!table.removeAllData()) {
success = false;
break;
return false;
}
}
if (success) {
commit();
} else {
rollback(); // TODO Tests for this case
}
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
return true;
} finally {
setAvailable();
}
setAvailable();
return success;
}
@Override
public List<UserInfo> getUserDataForUUIDS(Collection<UUID> uuidsCol) throws SQLException {
if (uuidsCol == null || uuidsCol.isEmpty()) {
@ -346,13 +305,6 @@ public abstract class SQLDB extends Database {
return new ArrayList<>();
}
/**
* @return
*/
public Connection getConnection() {
return connection;
}
private void setStatus(String status) {
Log.debug("Database", status);
}
@ -361,14 +313,23 @@ public abstract class SQLDB extends Database {
Log.logDebug("Database");
}
public Connection getConnection() throws SQLException {
return getDataSource().getConnection();
}
/**
* Commits changes to the .db file when using SQLite Database.
* <p>
* MySQL has Auto Commit enabled.
*/
public void commit() throws SQLException {
if (!usingMySQL) {
getConnection().commit();
@Override
public void commit(Connection connection) throws SQLException {
try {
if (!usingMySQL) {
connection.commit();
}
} finally {
endTransaction(connection);
}
}
@ -377,9 +338,17 @@ public abstract class SQLDB extends Database {
* <p>
* MySQL has Auto Commit enabled.
*/
public void rollback() throws SQLException {
if (!usingMySQL) {
connection.rollback();
public void rollback(Connection connection) throws SQLException {
try {
if (!usingMySQL) {
connection.rollback();
}
} finally {
endTransaction(connection);
}
}
public void endTransaction(Connection connection) throws SQLException {
connection.close();
}
}

View File

@ -1,11 +1,10 @@
package main.java.com.djrapitops.plan.database.databases;
import main.java.com.djrapitops.plan.Plan;
import org.apache.commons.dbcp2.BasicDataSource;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Collections;
/**
* @author Rsl1122
@ -33,36 +32,28 @@ public class SQLiteDB extends SQLDB {
}
/**
* Creates a new connection to the database.
*
* @return the new Connection.
* Setups the {@link BasicDataSource}
*/
@Override
public Connection getNewConnection() {
return getNewConnection(dbName);
public void setupDataSource() {
dataSource = new BasicDataSource();
String filePath = new File(plugin.getDataFolder(), dbName + ".db").getAbsolutePath();
dataSource.setUrl("jdbc:sqlite:" + filePath);
dataSource.setEnableAutoCommitOnReturn(false);
dataSource.setDefaultAutoCommit(false);
dataSource.setConnectionInitSqls(Collections.singletonList("PRAGMA JOURNAL_MODE=WAL"));
dataSource.setMaxTotal(-1);
}
/**
* @param dbName
* @return
*/
public Connection getNewConnection(String dbName) {
try {
Class.forName("org.sqlite.JDBC");
Connection connection = DriverManager.getConnection("jdbc:sqlite:" + new File(plugin.getDataFolder(), dbName + ".db").getAbsolutePath());
connection.setAutoCommit(false);
return connection;
} catch (ClassNotFoundException | SQLException e) {
return null;
}
}
/**
* @return
* @return the name of the Database
*/
@Override
public String getName() {
return "SQLite";
}
}

View File

@ -81,6 +81,7 @@ public class ActionsTable extends UserIDTable {
statement.setString(5, action.getAdditionalInfo());
statement.execute();
} finally {
endTransaction(statement);
close(statement);
}
}
@ -111,6 +112,7 @@ public class ActionsTable extends UserIDTable {
}
return actions;
} finally {
endTransaction(statement);
close(set, statement);
}
}

View File

@ -94,8 +94,8 @@ public class CommandUseTable extends Table {
}
return commandUse;
} finally {
close(set);
close(statement);
endTransaction(statement);
close(set, statement);
Benchmark.stop("Database", "Get CommandUse");
}
}
@ -129,7 +129,6 @@ public class CommandUseTable extends Table {
}
updateCommands(updateData);
commit();
Benchmark.stop("Database", "Save Commanduse");
db.setAvailable();
}
@ -142,7 +141,7 @@ public class CommandUseTable extends Table {
"WHERE (" + columnCommand + "=?) AND (" +
columnServerID + "=" + serverTable.statementSelectServerID + ")";
statement = prepareStatement(updateStatement);
boolean commitRequired = false;
for (Map.Entry<String, Integer> entrySet : data.entrySet()) {
String key = entrySet.getKey();
Integer amount = entrySet.getValue();
@ -155,13 +154,11 @@ public class CommandUseTable extends Table {
statement.setString(2, key);
statement.setString(3, Plan.getServerUUID().toString());
statement.addBatch();
commitRequired = true;
}
if (commitRequired) {
statement.executeBatch();
}
statement.executeBatch();
} finally {
endTransaction(statement);
close(statement);
}
}
@ -175,7 +172,6 @@ public class CommandUseTable extends Table {
+ columnServerID
+ ") VALUES (?, ?, " + serverTable.statementSelectServerID + ")";
statement = prepareStatement(insertStatement);
boolean addedRows = false;
for (Map.Entry<String, Integer> entrySet : data.entrySet()) {
String key = entrySet.getKey();
Integer amount = entrySet.getValue();
@ -188,13 +184,11 @@ public class CommandUseTable extends Table {
statement.setInt(2, amount);
statement.setString(3, Plan.getServerUUID().toString());
statement.addBatch();
addedRows = true;
}
if (addedRows) {
statement.executeBatch();
}
statement.executeBatch();
} finally {
endTransaction(statement);
close(statement);
}
}
@ -211,7 +205,8 @@ public class CommandUseTable extends Table {
}
return Optional.empty();
} finally {
close(statement);
endTransaction(statement);
close(set, statement);
}
}
@ -227,7 +222,8 @@ public class CommandUseTable extends Table {
}
return Optional.empty();
} finally {
close(statement);
endTransaction(statement);
close(set, statement);
}
}
}

View File

@ -75,6 +75,7 @@ public class IPsTable extends UserIDTable {
return stringList;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -84,6 +85,7 @@ public class IPsTable extends UserIDTable {
if (ips.contains(ip)) {
return;
}
insertIp(uuid, ip, geolocation);
}
@ -102,6 +104,7 @@ public class IPsTable extends UserIDTable {
statement.setString(3, geolocation);
statement.execute();
} finally {
endTransaction(statement);
close(statement);
}
}
@ -120,6 +123,7 @@ public class IPsTable extends UserIDTable {
}
return Optional.empty();
} finally {
endTransaction(statement);
close(set, statement);
}
}

View File

@ -70,6 +70,11 @@ public class KillsTable extends UserIDTable {
Log.toLog(this.getClass().getName(), ex);
return false;
} finally {
try {
endTransaction(statement);
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
close(statement);
}
}
@ -103,6 +108,7 @@ public class KillsTable extends UserIDTable {
}
statement.executeBatch();
} finally {
endTransaction(statement);
close(statement);
}
}
@ -136,6 +142,7 @@ public class KillsTable extends UserIDTable {
session.getPlayerKills().add(new PlayerKill(victim, weapon, date));
}
} finally {
endTransaction(statement);
close(set, statement);
}
}

View File

@ -76,6 +76,7 @@ public class NicknamesTable extends UserIDTable {
}
return nicknames;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -127,6 +128,7 @@ public class NicknamesTable extends UserIDTable {
}
return nicknames;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -152,6 +154,7 @@ public class NicknamesTable extends UserIDTable {
statement.setString(3, displayName);
statement.execute();
} finally {
endTransaction(statement);
close(statement);
}
}

View File

@ -53,6 +53,12 @@ public class SecurityTable extends Table {
Log.toLog(this.getClass().getName(), ex);
return false;
} finally {
try {
endTransaction(statement);
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
close(statement);
}
}
@ -72,7 +78,8 @@ public class SecurityTable extends Table {
statement.setString(2, saltPassHash);
statement.setInt(3, permLevel);
statement.execute();
commit();
commit(statement.getConnection());
} finally {
close(statement);
}
@ -96,8 +103,8 @@ public class SecurityTable extends Table {
}
return null;
} finally {
close(set);
close(statement);
endTransaction(statement);
close(set, statement);
}
}
@ -117,8 +124,8 @@ public class SecurityTable extends Table {
}
return list;
} finally {
close(set);
close(statement);
endTransaction(statement);
close(set, statement);
}
}
}

View File

@ -86,6 +86,7 @@ public class ServerTable extends Table {
statement.setInt(5, info.getId());
statement.executeUpdate();
} finally {
endTransaction(statement);
close(statement);
}
}
@ -116,6 +117,7 @@ public class ServerTable extends Table {
statement.setBoolean(4, true);
statement.execute();
} finally {
endTransaction(statement);
close(statement);
}
}
@ -143,6 +145,7 @@ public class ServerTable extends Table {
return Optional.empty();
}
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -170,6 +173,7 @@ public class ServerTable extends Table {
return Optional.empty();
}
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -189,6 +193,7 @@ public class ServerTable extends Table {
}
return names;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -218,6 +223,7 @@ public class ServerTable extends Table {
return Optional.empty();
}
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -241,6 +247,7 @@ public class ServerTable extends Table {
}
return servers;
} finally {
endTransaction(statement);
close(set, statement);
}
}

View File

@ -111,6 +111,7 @@ public class SessionsTable extends UserIDTable {
statement.setString(6, Plan.getServerUUID().toString());
statement.execute();
} finally {
endTransaction(statement);
close(statement);
}
}
@ -139,6 +140,7 @@ public class SessionsTable extends UserIDTable {
}
return -1L;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -182,6 +184,7 @@ public class SessionsTable extends UserIDTable {
}
return sessionsByServer;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -258,6 +261,7 @@ public class SessionsTable extends UserIDTable {
}
return 0;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -303,6 +307,7 @@ public class SessionsTable extends UserIDTable {
}
return playtimes;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -343,6 +348,7 @@ public class SessionsTable extends UserIDTable {
}
return 0;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -410,6 +416,7 @@ public class SessionsTable extends UserIDTable {
}
return 0;
} finally {
endTransaction(statement);
close(set, statement);
}
}

View File

@ -87,8 +87,8 @@ public class TPSTable extends Table {
}
return data;
} finally {
close(set);
close(statement);
endTransaction(statement);
close(set, statement);
Benchmark.stop("Database", "Get TPS");
}
}
@ -135,6 +135,7 @@ public class TPSTable extends Table {
statement.setLong(1, MiscUtils.getTime() - fiveWeeks);
statement.execute();
} finally {
endTransaction(statement);
close(statement);
}
}

View File

@ -65,11 +65,7 @@ public abstract class Table {
* @return @throws SQLException
*/
protected Connection getConnection() throws SQLException {
Connection connection = db.getConnection();
if (connection == null || connection.isClosed()) {
connection = db.getNewConnection();
}
return connection;
return db.getConnection();
}
/**
@ -89,11 +85,11 @@ public abstract class Table {
Statement statement = null;
try {
statement = connection.createStatement();
return statement.execute(statementString);
boolean b = statement.execute(statementString);
commit(statement.getConnection());
return b;
} finally {
if (statement != null) {
statement.close();
}
close(statement);
}
}
@ -192,7 +188,19 @@ public abstract class Table {
*
* @throws SQLException If commit fails or there is nothing to commit.
*/
protected void commit() throws SQLException {
db.commit();
protected void commit(Connection connection) throws SQLException {
db.commit(connection);
}
protected void endTransaction(Connection connection) throws SQLException {
db.endTransaction(connection);
}
protected void endTransaction(Statement statement) throws SQLException {
if (statement == null) {
return;
}
endTransaction(statement.getConnection());
}
}

View File

@ -35,6 +35,11 @@ public abstract class UserIDTable extends Table {
Log.toLog(this.getClass().getName(), ex);
return false;
} finally {
try {
endTransaction(statement);
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
close(statement);
}
}

View File

@ -1,6 +1,7 @@
package main.java.com.djrapitops.plan.database.tables;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.*;
@ -63,6 +64,7 @@ public class UsersTable extends UserIDTable {
}
return uuids;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -82,6 +84,11 @@ public class UsersTable extends UserIDTable {
} catch (SQLException ex) {
return false;
} finally {
try {
endTransaction(statement);
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
close(statement);
}
}
@ -117,6 +124,7 @@ public class UsersTable extends UserIDTable {
}
return null;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -133,6 +141,7 @@ public class UsersTable extends UserIDTable {
}
return registerDates;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -160,6 +169,7 @@ public class UsersTable extends UserIDTable {
statement.setString(3, name);
statement.execute();
} finally {
endTransaction(statement);
close(statement);
}
}
@ -175,6 +185,7 @@ public class UsersTable extends UserIDTable {
set = statement.executeQuery();
return set.next();
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -189,6 +200,7 @@ public class UsersTable extends UserIDTable {
statement.setString(2, uuid.toString());
statement.execute();
} finally {
endTransaction(statement);
close(statement);
}
}
@ -207,6 +219,7 @@ public class UsersTable extends UserIDTable {
}
return 0;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -220,6 +233,7 @@ public class UsersTable extends UserIDTable {
statement.setString(1, uuid.toString());
statement.execute();
} finally {
endTransaction(statement);
close(statement);
}
}

View File

@ -50,8 +50,8 @@ public class VersionTable extends Table {
Log.debug("Database", "DB Schema version: " + version);
return version;
} finally {
close(set);
close(statement);
endTransaction(statement);
close(set, statement);
}
}
@ -65,9 +65,9 @@ public class VersionTable extends Table {
try {
statement = prepareStatement("INSERT INTO " + tableName + " (version) VALUES (" + version + ")");
statement.executeUpdate();
commit(statement.getConnection());
} finally {
close(statement);
}
}
}

View File

@ -68,6 +68,7 @@ public class WorldTable extends Table {
}
return worldNames;
} finally {
endTransaction(statement);
close(set, statement);
}
}
@ -94,16 +95,14 @@ public class WorldTable extends Table {
statement = prepareStatement("INSERT INTO " + tableName + " ("
+ columnWorldName
+ ") VALUES (?)");
boolean commitRequired = false;
for (String world : worlds) {
statement.setString(1, world);
statement.addBatch();
commitRequired = true;
}
if (commitRequired) {
statement.executeBatch();
}
statement.executeBatch();
} finally {
endTransaction(statement);
close(statement);
}
}

View File

@ -99,6 +99,7 @@ public class WorldTimesTable extends UserIDTable {
statement.executeBatch();
} finally {
endTransaction(statement);
close(statement);
}
}
@ -144,6 +145,7 @@ public class WorldTimesTable extends UserIDTable {
session.getWorldTimes().setGMTimesForWorld(worldName, gmTimes);
}
} finally {
endTransaction(statement);
close(set, statement);
}
}

View File

@ -1,6 +1,5 @@
package main.java.com.djrapitops.plan.systems.listeners;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.systems.processing.player.DeathProcessor;
import main.java.com.djrapitops.plan.systems.processing.player.KillProcessor;
@ -16,8 +15,6 @@ import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.projectiles.ProjectileSource;
import java.util.UUID;
/**
* Event Listener for EntityDeathEvents.
*
@ -59,9 +56,6 @@ public class PlanDeathEventListener implements Listener {
EntityDamageByEntityEvent entityDamageByEntityEvent = (EntityDamageByEntityEvent) entityDamageEvent;
Entity killerEntity = entityDamageByEntityEvent.getDamager();
UUID killerUUID = null;
String weapon = null;
if (killerEntity instanceof Player) {
Player killer = (Player) killerEntity;
Material itemInHand;
@ -75,32 +69,38 @@ public class PlanDeathEventListener implements Listener {
}
}
killerUUID = killer.getUniqueId();
weapon = normalizeMaterialName(itemInHand);
} else if (killerEntity instanceof Wolf) {
plugin.addToProcessQueue(new KillProcessor(killer.getUniqueId(), time, dead, normalizeMaterialName(itemInHand)));
return;
}
if (killerEntity instanceof Wolf) {
Wolf wolf = (Wolf) killerEntity;
if (!wolf.isTamed()) {
return;
}
AnimalTamer owner = wolf.getOwner();
if (owner instanceof Player) {
killerUUID = owner.getUniqueId();
weapon = "Wolf";
}
} else if (killerEntity instanceof Arrow) {
Arrow arrow = (Arrow) killerEntity;
ProjectileSource source = arrow.getShooter();
if (source instanceof Player) {
Player player = (Player) source;
killerUUID = player.getUniqueId();
weapon = "Bow";
if (!(owner instanceof Player)) {
return;
}
plugin.addToProcessQueue(new KillProcessor(owner.getUniqueId(), time, dead, "Wolf"));
}
if (Verify.notNull(killerUUID, weapon)) {
plugin.addToProcessQueue(new KillProcessor(killerUUID, time, dead, weapon));
if (killerEntity instanceof Arrow) {
Arrow arrow = (Arrow) killerEntity;
ProjectileSource source = arrow.getShooter();
if (!(source instanceof Player)) {
return;
}
Player player = (Player) source;
plugin.addToProcessQueue(new KillProcessor(player.getUniqueId(), time, dead, "Bow"));
}
}

View File

@ -30,7 +30,9 @@ public class ProcessingQueue extends Queue<Processor> {
* @param processor processing object.
*/
public void addToQueue(Processor processor) {
queue.offer(processor);
if (!queue.offer(processor)) {
Log.toLog("ProcessingQueue.addToQueue", new IllegalStateException("Processor was not added to Queue"));
}
}
}

View File

@ -312,7 +312,7 @@ public class WebServer {
Plan plan = Plan.getInstance();
if (!checkKey(plan, key)) {
if (!checkKey(key)) {
String error = "Server Key not given or invalid";
return PageCache.loadPage(error, () -> {
ForbiddenResponse forbidden = new ForbiddenResponse();
@ -336,8 +336,8 @@ public class WebServer {
}
}
private boolean checkKey(Plan plan, String key) {
UUID uuid = plan.getServerInfoManager().getServerUUID();
private boolean checkKey(String key) {
UUID uuid = Plan.getServerUUID();
UUID keyUUID;
try {
keyUUID = UUID.fromString(key);
@ -544,7 +544,7 @@ public class WebServer {
return usingHttps ? "https" : "http";
}
public boolean usingHttps() {
public boolean isUsingHTTPS() {
return usingHttps;
}

View File

@ -7,6 +7,7 @@ import main.java.com.djrapitops.plan.data.UserInfo;
/**
* @author Rsl1122
* @deprecated Will be removed once it's sure that it's unnecessary
*/
@Deprecated // TODO Remove once sure that this is unnecessary.
public class NewPlayerCreator {

View File

@ -198,7 +198,7 @@ public class DumpUtils {
* @param plan The Plan instance
*/
private static void addConfigurationDetails(DumpLog log, Plan plan) {
boolean usingHTTPS = plan.getUiServer().usingHttps();
boolean usingHTTPS = plan.getUiServer().isUsingHTTPS();
boolean analysisExport = Settings.ANALYSIS_EXPORT.isTrue();
boolean usingAlternativeServerIP = Settings.SHOW_ALTERNATIVE_IP.isTrue();

View File

@ -44,11 +44,7 @@ public class QueueTest {
public void setUp() throws Exception {
TestInit t = TestInit.init();
Plan plan = t.getPlanMock();
db = new SQLiteDB(plan, "debug" + MiscUtils.getTime()) {
@Override
public void startConnectionPingTask() {
}
};
db = new SQLiteDB(plan, "debug" + MiscUtils.getTime());
db.init();
when(plan.getDB()).thenReturn(db);
dataCache = new DataCache(plan) {

View File

@ -29,14 +29,12 @@ public class WorldTimesTest {
public void setUp() throws Exception {
test = new WorldTimes(worldOne, gms[0]);
time = test.getGMTimes(worldOne).getLastStateChange();
System.out.println(test);
}
@Test
public void testWorldChange() {
long changeTime = time + 1000L;
test.updateState(worldTwo, gms[0], changeTime);
System.out.println(test);
assertEquals(1000L, (long) test.getWorldPlaytime(worldOne));
assertEquals(1000L, test.getGMTimes(worldOne).getTime(gms[0]));
}
@ -45,7 +43,6 @@ public class WorldTimesTest {
public void testGMChange() {
long changeTime = time + 1000L;
test.updateState(worldOne, gms[0], changeTime);
System.out.println(test);
assertEquals(1000L, (long) test.getWorldPlaytime(worldOne));
assertEquals(1000L, test.getGMTimes(worldOne).getTime(gms[0]));
}
@ -55,11 +52,9 @@ public class WorldTimesTest {
long changeTime = time + 1000L;
long changeTime2 = changeTime + 1000L;
test.updateState(worldTwo, gms[2], changeTime);
System.out.println(test);
assertEquals(1000L, (long) test.getWorldPlaytime(worldOne));
assertEquals(1000L, test.getGMTimes(worldOne).getTime(gms[0]));
test.updateState(worldOne, gms[1], changeTime2);
System.out.println(test);
assertEquals(1000L, (long) test.getWorldPlaytime(worldOne));
assertEquals(1000L, test.getGMTimes(worldOne).getTime(gms[0]));
assertEquals(1000L, test.getGMTimes(worldTwo).getTime(gms[2]));
@ -98,7 +93,6 @@ public class WorldTimesTest {
long time1 = test.getWorldPlaytime(worldOne);
long time2 = test.getWorldPlaytime(worldTwo);
System.out.println(test);
// Tests World time calculation.
assertEquals(amount * 50, time1 + time2);
@ -156,13 +150,10 @@ public class WorldTimesTest {
// No change should occur.
test.updateState(worldOne, "ADVENTURE", time + 5000L);
System.out.println(test);
assertEquals(1000L, worldOneGMTimes.getTime("ADVENTURE"));
assertEquals(1000L, worldTwoGMTimes.getTime("CREATIVE"));
test.updateState(worldTwo, "CREATIVE", time + 5000L);
System.out.println(test);
test.updateState(worldOne, "ADVENTURE", time + 6000L);
System.out.println(test);
assertEquals(1000L, worldOneGMTimes.getTime("ADVENTURE"));
assertEquals(2000L, worldTwoGMTimes.getTime("CREATIVE"));

View File

@ -3,7 +3,6 @@ package test.java.main.java.com.djrapitops.plan.database;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.TPS;
import main.java.com.djrapitops.plan.data.WebUser;
import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.database.databases.SQLiteDB;
import main.java.com.djrapitops.plan.utilities.MiscUtils;
import main.java.com.djrapitops.plan.utilities.PassEncryptUtil;
@ -34,21 +33,18 @@ import static org.junit.Assert.assertTrue;
public class DatabaseCommitTest {
private Plan plan;
private Database db;
private SQLiteDB db;
private int rows;
@Before
public void setUp() throws Exception {
TestInit t = TestInit.init();
plan = t.getPlanMock();
db = new SQLiteDB(plan, "debug" + MiscUtils.getTime()) {
@Override
public void startConnectionPingTask() {
}
};
db = new SQLiteDB(plan, "debug" + MiscUtils.getTime());
File f = new File(plan.getDataFolder(), "Errors.txt");
rows = FileUtil.lines(f).size();
db.init();
}
/**
@ -72,9 +68,10 @@ public class DatabaseCommitTest {
@Test
public void testNoExceptionWhenCommitEmpty() throws SQLException {
db.init();
db.commit();
db.commit();
db.commit();
db.commit(db.getConnection());
db.commit(db.getConnection());
db.commit(db.getConnection());
}
@Ignore("//TODO")

View File

@ -68,6 +68,8 @@ public class DatabaseTest {
db.getServerTable().saveCurrentServerInfo(new ServerInfo(-1, t.getServerUUID(), "ServerName", ""));
File f = new File(plan.getDataFolder(), "Errors.txt");
rows = FileUtil.lines(f).size();
db.init();
}
@After
@ -105,22 +107,12 @@ public class DatabaseTest {
@Test
public void testMysqlGetConfigName() {
assertEquals("mysql", new MySQLDB(plan) {
@Override
public void startConnectionPingTask() {
}
}.getConfigName());
assertEquals("mysql", new MySQLDB(plan).getConfigName());
}
@Test
public void testMysqlGetName() {
assertEquals("MySQL", new MySQLDB(plan) {
@Override
public void startConnectionPingTask() {
}
}.getName());
assertEquals("MySQL", new MySQLDB(plan).getName());
}
@Test
@ -176,7 +168,6 @@ public class DatabaseTest {
public void testTPSSaving() throws SQLException {
db.init();
TPSTable tpsTable = db.getTpsTable();
List<TPS> expected = new ArrayList<>();
Random r = new Random();
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
@ -187,6 +178,8 @@ public class DatabaseTest {
final int entityCount = 6123;
final int chunksLoaded = 2134;
List<TPS> expected = new ArrayList<>();
expected.add(new TPS(r.nextLong(), r.nextDouble(), r.nextInt(100000000), averageCPUUsage, usedMemory, entityCount, chunksLoaded));
expected.add(new TPS(r.nextLong(), r.nextDouble(), r.nextInt(100000000), averageCPUUsage, usedMemory, entityCount, chunksLoaded));
expected.add(new TPS(r.nextLong(), r.nextDouble(), r.nextInt(100000000), averageCPUUsage, usedMemory, entityCount, chunksLoaded));

View File

@ -0,0 +1,48 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package test.java.utils;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import test.java.main.java.com.djrapitops.plan.database.DatabaseCommitTest;
import test.java.main.java.com.djrapitops.plan.database.DatabaseTest;
import java.io.File;
import java.io.IOException;
/**
* @author Fuzzlemann
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({DatabaseCommitTest.class, DatabaseTest.class})
public class DBTestSuite {
@BeforeClass
public static void setUp() throws IOException {
clean(true);
}
@AfterClass
public static void tearDown() throws IOException {
clean(false);
}
private static void clean(boolean dbOnly) throws IOException {
File testFolder = TestInit.getTestFolder();
if (!testFolder.exists() || !testFolder.isDirectory()) {
return;
}
for (File f : testFolder.listFiles()) {
if (dbOnly && !f.getName().contains(".db")) {
continue;
}
f.delete();
}
}
}

View File

@ -63,17 +63,11 @@ public class TestInit {
public static TestInit init() throws Exception {
TestInit t = new TestInit();
t.setUp(true);
t.setUp();
return t;
}
public static TestInit init(boolean clearOnStart) throws Exception {
TestInit t = new TestInit();
t.setUp(clearOnStart);
return t;
}
private void setUp(boolean clearOnStart) throws Exception {
private void setUp() throws Exception {
planMock = PowerMockito.mock(Plan.class);
StaticHolder.setInstance(Plan.class, planMock);
StaticHolder.setInstance(planMock.getClass(), planMock);
@ -82,9 +76,6 @@ public class TestInit {
when(planMock.getConfig()).thenReturn(config);
File testFolder = getTestFolder();
if (clearOnStart) {
clean(testFolder);
}
when(planMock.getDataFolder()).thenReturn(testFolder);
// Html Files
@ -123,7 +114,7 @@ public class TestInit {
}
private RunnableFactory<Plan> mockRunnableFactory() {
RunnableFactory<Plan> runnableFactory = new RunnableFactory<Plan>(planMock) {
return new RunnableFactory<Plan>(planMock) {
@Override
public IRunnable createNew(String name, final AbsRunnable runnable) {
return new IRunnable() {
@ -174,27 +165,14 @@ public class TestInit {
};
}
};
return runnableFactory;
}
private static File getTestFolder() {
static File getTestFolder() {
File testFolder = new File("temporaryTestFolder");
testFolder.mkdir();
return testFolder;
}
public static void clean() throws IOException {
clean(getTestFolder());
}
public static void clean(File testFolder) throws IOException {
if (testFolder.exists() && testFolder.isDirectory()) {
for (File f : testFolder.listFiles()) {
Files.deleteIfExists(f.toPath());
}
}
}
private Server mockServer() {
Server mockServer = PowerMockito.mock(Server.class);