Everything but Mineplexer using database pool

This commit is contained in:
CoderTim 2014-10-29 18:35:59 -04:00
parent 5244f65d8f
commit 4fc291ea80
14 changed files with 208 additions and 833 deletions

View File

@ -8,6 +8,9 @@
<fileset dir="../Mineplex.Core.Common/bin">
<include name="**/*.class"/>
</fileset>
<fileset dir="../Mineplex.Database/bin">
<include name="**/*.class"/>
</fileset>
<fileset dir="../Mineplex.EnjinTranslator/bin">
<include name="**/*.class"/>

View File

@ -5,6 +5,7 @@ import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import mineplex.core.database.DBPool;
import mineplex.core.logger.Logger;
import org.bukkit.craftbukkit.v1_7_R4.entity.CraftPlayer;
@ -12,15 +13,8 @@ import org.bukkit.entity.Player;
public class AntiHackRepository
{
private static Object _connectionLock = new Object();
private String _serverName;
private static Connection _connection;
private String _connectionString = "jdbc:mysql://sqlstats.mineplex.com:3306/Mineplex?autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
private String _userName = "root";
private String _password = "tAbechAk3wR7tuTh";
private static String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS AntiHack_Kick_Log (id INT NOT NULL AUTO_INCREMENT, updated LONG, playerName VARCHAR(256), motd VARCHAR(56), gameType VARCHAR(56), map VARCHAR(256), serverName VARCHAR(256), report VARCHAR(256), ping VARCHAR(25), PRIMARY KEY (id));";
private static String UPDATE_PLAYER_OFFENSES = "INSERT INTO AntiHack_Kick_Log (updated, playerName, motd, gameType, map, serverName, report, ping) VALUES (now(), ?, ?, ?, ?, ?, ?, ?);";
@ -33,13 +27,10 @@ public class AntiHackRepository
{
PreparedStatement preparedStatement = null;
try
try (Connection connection = DBPool.STATS_MINEPLEX.getConnection())
{
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
// Create table
preparedStatement = _connection.prepareStatement(CREATE_TABLE);
preparedStatement = connection.prepareStatement(CREATE_TABLE);
preparedStatement.execute();
}
catch (Exception exception)
@ -70,47 +61,40 @@ public class AntiHackRepository
public void run()
{
PreparedStatement preparedStatement = null;
synchronized (_connectionLock)
{
try
{
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = _connection.prepareStatement(UPDATE_PLAYER_OFFENSES);
preparedStatement.setString(1, player.getName());
preparedStatement.setString(2, motd);
preparedStatement.setString(3, game);
preparedStatement.setString(4, map);
preparedStatement.setString(5, _serverName);
preparedStatement.setString(6, report);
preparedStatement.setString(7, ((CraftPlayer)player).getHandle().ping + "ms");
preparedStatement.execute();
}
catch (Exception exception)
{
exception.printStackTrace();
Logger.Instance.log(exception);
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
try (Connection connection = DBPool.STATS_MINEPLEX.getConnection())
{
preparedStatement = connection.prepareStatement(UPDATE_PLAYER_OFFENSES);
preparedStatement.setString(1, player.getName());
preparedStatement.setString(2, motd);
preparedStatement.setString(3, game);
preparedStatement.setString(4, map);
preparedStatement.setString(5, _serverName);
preparedStatement.setString(6, report);
preparedStatement.setString(7, ((CraftPlayer)player).getHandle().ping + "ms");
preparedStatement.execute();
}
catch (Exception exception)
{
exception.printStackTrace();
Logger.Instance.log(exception);
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
}).start();
}

View File

@ -9,6 +9,9 @@ import org.apache.commons.dbcp2.BasicDataSource;
public class DBPool
{
public static final DataSource ACCOUNT = openDataSource("jdbc:mysql://db.mineplex.com/Account", "root", "tAbechAk3wR7tuTh");
public static final DataSource QUEUE = openDataSource("jdbc:mysql://db.mineplex.com/Queue", "root", "tAbechAk3wR7tuTh");
public static final DataSource MINEPLEX = openDataSource("jdbc:mysql://db.mineplex.com:3306/Mineplex", "root", "tAbechAk3wR7tuTh");
public static final DataSource STATS_MINEPLEX = openDataSource("jdbc:mysql://sqlstats.mineplex.com:3306/Mineplex", "root", "tAbechAk3wR7tuTh");
private static DataSource openDataSource(String url, String username, String password)
{

View File

@ -1,417 +0,0 @@
package mineplex.core.database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import mineplex.core.common.util.NautHashMap;
import mineplex.core.database.column.Column;
public class Table
{
private static Connection _connection;
private String _connectionString = "jdbc:mysql://db.mineplex.com:3306/Mineplex?autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
private String _userName = "root";
private String _password = "tAbechAk3wR7tuTh";
private String _name;
private List<Column<?>> _primaryKeys;
private NautHashMap<String, Column<?>> _columns = new NautHashMap<String, Column<?>>();
private Column<?> _index;
public Table(String name, List<Column<?>> primaryKeys, List<Column<?>> columns, Column<?> index)
{
_name = name;
_primaryKeys = primaryKeys;
for (Column<?> column : columns)
{
_columns.put(column.Name, column);
}
_index = index;
}
public void initialize()
{
if (!doesTableExist())
{
create();
}
}
/*
private void updateSchema()
{
PreparedStatement getTableColumns = null;
ResultSet resultSet = null;
try
{
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
getTableColumns = _connection.prepareStatement("SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '`" + _name + "`' AND table_schema = 'Mineplex';");
resultSet = getTableColumns.executeQuery();
HashSet<String> columnExists = new HashSet<String>();
while (resultSet.next())
{
columnExists.add(resultSet.getString("COLUMN_NAME"));
}
}
catch (Exception exception)
{
System.out.println("Error updating table `" + _name + "`.");
exception.printStackTrace();
}
finally
{
if (getTableColumns != null)
{
try
{
getTableColumns.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (resultSet != null)
{
try
{
resultSet.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
*/
private void create()
{
PreparedStatement createStatement = null;
try
{
StringBuilder columnBuilder = new StringBuilder();
for (Iterator<Column<?>> columnIterator = _columns.values().iterator(); columnIterator.hasNext();)
{
Column<?> column = columnIterator.next();
columnBuilder.append(column.getCreateString());
if (columnIterator.hasNext())
{
columnBuilder.append(", ");
}
}
StringBuilder primaryKey = new StringBuilder();
for (Column<?> column : _primaryKeys)
{
primaryKey.append(column.Name);
if (!column.equals(_primaryKeys.get(_primaryKeys.size() - 1)))
{
primaryKey.append(", ");
}
}
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
createStatement = _connection.prepareStatement("CREATE TABLE IF NOT EXISTS `" + _name + "` (" + columnBuilder.toString() + ", PRIMARY KEY (" + primaryKey.toString() + "), INDEX (" + _index.Name + "));");
createStatement.execute();
}
catch (Exception exception)
{
System.out.println("Error creating table `" + _name + "`.");
exception.printStackTrace();
}
finally
{
if (createStatement != null)
{
try
{
createStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
private boolean doesTableExist()
{
PreparedStatement checkIfTableExistsStatement = null;
try
{
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
checkIfTableExistsStatement = _connection.prepareStatement("SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = 'Mineplex' AND table_name LIKE '`" + _name + "`'");
if (checkIfTableExistsStatement.executeQuery().next())
return true;
}
catch (Exception exception)
{
System.out.println("Error updating table `" + _name + "`.");
exception.printStackTrace();
}
finally
{
if (checkIfTableExistsStatement != null)
{
try
{
checkIfTableExistsStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return false;
}
public void insert(List<Column<?>> columns)
{
StringBuilder temporaryBuilder = new StringBuilder();
StringBuilder questionBuilder = new StringBuilder();
StringBuilder updateBuilder = new StringBuilder();
for (Column<?> column : columns)
{
temporaryBuilder.append(column.Name);
questionBuilder.append("'" + column.Value + "'");
updateBuilder.append(column.Name + " = VALUES(" + column.Name + ")");
if (!column.equals(columns.get(columns.size() - 1)))
{
temporaryBuilder.append(", ");
questionBuilder.append(", ");
updateBuilder.append(", ");
}
}
PreparedStatement preparedStatement = null;
try
{
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = _connection.prepareStatement("INSERT INTO `" + _name + "` (" + temporaryBuilder.toString() + ") VALUES (" + questionBuilder.toString() + ") ON DUPLICATE KEY UPDATE " + updateBuilder.toString() + ";", Statement.RETURN_GENERATED_KEYS);
preparedStatement.execute();
}
catch (Exception exception)
{
System.out.println("Error updating table `" + _name + "`.");
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
public boolean update(List<Column<?>> columns, Column<?> whereColumn)
{
List<Column<?>> whereColumnList = new ArrayList<Column<?>>();
whereColumnList.add(whereColumn);
return update(columns, whereColumnList);
}
public boolean update(List<Column<?>> columns, List<Column<?>> whereColumns)
{
String updateStatement = buildUpdateStatement(columns, whereColumns);
PreparedStatement preparedStatement = null;
try
{
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = _connection.prepareStatement(updateStatement);
if (preparedStatement.executeUpdate() != 0)
return true;
}
catch (Exception exception)
{
System.out.println("Error updating table `" + _name + "`.");
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return false;
}
public List<Row> retrieve(List<Column<?>> columns)
{
StringBuilder temporaryBuilder = new StringBuilder();
for (Iterator<Column<?>> columnIterator = _columns.values().iterator(); columnIterator.hasNext();)
{
Column<?> column = columnIterator.next();
temporaryBuilder.append(column.Name);
if (columnIterator.hasNext())
temporaryBuilder.append(", ");
}
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
List<Row> rows = new ArrayList<Row>();
try
{
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = _connection.prepareStatement("Select " + temporaryBuilder.toString() + " FROM `" + _name + "` " + buildWhereString(columns) + ";");
resultSet = preparedStatement.executeQuery();
while (resultSet.next())
{
Row row = new Row();
for (Column<?> column : columns)
{
column.getValue(resultSet);
row.Columns.put(column.Name, column);
}
rows.add(row);
}
}
catch (Exception exception)
{
System.out.println("Error updating table `" + _name + "`.");
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return rows;
}
private String buildUpdateStatement(List<Column<?>> columns, List<Column<?>> whereColumns)
{
StringBuilder setBuilder = new StringBuilder();
if (columns.size() > 0)
setBuilder.append("SET ");
for (Column<?> column : columns)
{
setBuilder.append(column.Name + " = '" + column.Value + "'");
if (!column.equals(columns.get(columns.size() - 1)))
setBuilder.append(", ");
}
return "UPDATE `" + _name + "` " + setBuilder.toString() + " " + buildWhereString(whereColumns) + ";";
}
private String buildWhereString(List<Column<?>> columns)
{
StringBuilder whereBuilder = new StringBuilder();
if (columns.size() > 0)
{
whereBuilder.append("WHERE ");
}
for (Column<?> column : columns)
{
whereBuilder.append(column.Name + " = '" + column.Value + "'");
if (!column.equals(columns.get(columns.size() - 1)))
whereBuilder.append(" AND ");
}
return whereBuilder.toString();
}
public Column<?> getColumn(String columnName)
{
return _columns.get(columnName);
}
public Row createRow()
{
Row row = new Row();
for (Column<?> column : _columns.values())
{
row.Columns.put(column.Name, column);
}
return row;
}
}

View File

@ -22,27 +22,11 @@ public class EloManager extends MiniPlugin
{
super("Elo Rating", plugin);
setupConfigValues(plugin);
_repository = new EloRepository(plugin.getConfig().getString("elo.connectionurl"));
_repository = new EloRepository();
_ratingSystem = new EloRatingSystem(new KFactor(0, 1200, 25), new KFactor(1201, 1600, 20), new KFactor(1601, 2000, 15), new KFactor(2001, 2500, 10));
_playerElos = new NautHashMap<String, NautHashMap<String, Integer>>();
}
private void setupConfigValues(JavaPlugin plugin)
{
try
{
plugin.getConfig().addDefault("elo.connectionurl", "jdbc:mysql://sqlstats.mineplex.com:3306/Mineplex?autoReconnect=true&failOverReadOnly=false&maxReconnects=10");
plugin.getConfig().set("elo.connectionurl", plugin.getConfig().getString("elo.connectionurl"));
plugin.saveConfig();
}
catch (Exception e)
{
e.printStackTrace();
}
}
@EventHandler
public void retrievePlayersElos(final RetrieveClientInformationEvent event)
{

View File

@ -7,24 +7,18 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;
import mineplex.core.common.util.C;
import mineplex.core.common.util.NautHashMap;
import mineplex.core.database.DBPool;
public class EloRepository
{
private String _connectionString;
private String _userName = "root";
private String _password = "tAbechAk3wR7tuTh";
private static String CREATE_ELO_TABLE = "CREATE TABLE IF NOT EXISTS eloRating (id INT NOT NULL AUTO_INCREMENT, uuid VARCHAR(256), gameType VARCHAR(256), elo INT, PRIMARY KEY (id), UNIQUE INDEX uuid_gameType_index (uuid, gameType));";
private static String INSERT_ELO = "INSERT INTO eloRating (uuid, gameType, elo) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE elo=VALUES(elo);";
private static String RETRIEVE_ELO = "SELECT gameType, elo FROM eloRating WHERE uuid = ?;";
private Connection _connection = null;
public EloRepository(String connectionUrl)
public EloRepository()
{
_connectionString = connectionUrl;
initialize();
}
@ -32,12 +26,10 @@ public class EloRepository
{
PreparedStatement preparedStatement = null;
try
try (Connection connection = DBPool.STATS_MINEPLEX.getConnection())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
// Create table
preparedStatement = _connection.prepareStatement(CREATE_ELO_TABLE);
preparedStatement = connection.prepareStatement(CREATE_ELO_TABLE);
preparedStatement.execute();
}
catch (Exception exception)
@ -66,14 +58,9 @@ public class EloRepository
int affectedRows = 0;
try
try (Connection connection = DBPool.STATS_MINEPLEX.getConnection())
{
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = _connection.prepareStatement(INSERT_ELO);
preparedStatement = connection.prepareStatement(INSERT_ELO);
preparedStatement.setString(1, uuid);
preparedStatement.setString(2, gameType);
@ -118,14 +105,9 @@ public class EloRepository
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
try
try (Connection connection = DBPool.STATS_MINEPLEX.getConnection())
{
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = _connection.prepareStatement(RETRIEVE_ELO);
preparedStatement = connection.prepareStatement(RETRIEVE_ELO);
preparedStatement.setString(1, uuid.toString());
resultSet = preparedStatement.executeQuery();

View File

@ -21,9 +21,7 @@ public class Logger
public Logger(JavaPlugin plugin)
{
setupConfigValues(plugin);
_repository = new LoggerRepository(plugin.getConfig().getString("log.connectionurl"), plugin.getConfig().getString("serverstatus.name"));
_repository = new LoggerRepository(plugin.getConfig().getString("serverstatus.name"));
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler()
{
@ -36,20 +34,6 @@ public class Logger
});
}
private void setupConfigValues(JavaPlugin plugin)
{
try
{
plugin.getConfig().addDefault("log.connectionurl", "jdbc:mysql://sqlstats.mineplex.com:3306/Mineplex?autoReconnect=true&failOverReadOnly=false&maxReconnects=10");
plugin.getConfig().set("log.connectionurl", plugin.getConfig().getString("log.connectionurl"));
plugin.saveConfig();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void log(final String message)
{
System.out.println(message);

View File

@ -5,23 +5,17 @@ import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import mineplex.core.database.DBPool;
public class LoggerRepository
{
private static Object _connectionLock = new Object();
private String _connectionString;
private String _userName = "root";
private String _password = "tAbechAk3wR7tuTh";
private static String CREATE_LOG_TABLE = "CREATE TABLE IF NOT EXISTS errorLog (id INT NOT NULL AUTO_INCREMENT, server VARCHAR(256), message VARCHAR(256), date LONG, PRIMARY KEY (id));";
private static String INSERT_LOG = "INSERT INTO errorLog (server, message, date) VALUES (?, ?, now());";
private Connection _connection = null;
private String _serverName;
public LoggerRepository(String connectionUrl, String serverName)
public LoggerRepository(String serverName)
{
_connectionString = connectionUrl;
_serverName = serverName;
initialize();
@ -31,12 +25,10 @@ public class LoggerRepository
{
PreparedStatement preparedStatement = null;
try
try (Connection connection = DBPool.STATS_MINEPLEX.getConnection())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
// Create table
preparedStatement = _connection.prepareStatement(CREATE_LOG_TABLE);
preparedStatement = connection.prepareStatement(CREATE_LOG_TABLE);
preparedStatement.execute();
}
catch (Exception exception)
@ -63,26 +55,18 @@ public class LoggerRepository
{
PreparedStatement preparedStatement = null;
try
try (Connection connection = DBPool.STATS_MINEPLEX.getConnection())
{
synchronized (_connectionLock)
preparedStatement = connection.prepareStatement(INSERT_LOG);
for (String msg : message)
{
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = _connection.prepareStatement(INSERT_LOG);
for (String msg : message)
{
preparedStatement.setString(1, _serverName);
preparedStatement.setString(2, msg.substring(0, Math.min(257, msg.length())));
preparedStatement.addBatch();
}
preparedStatement.executeBatch();
preparedStatement.setString(1, _serverName);
preparedStatement.setString(2, msg.substring(0, Math.min(257, msg.length())));
preparedStatement.addBatch();
}
preparedStatement.executeBatch();
}
catch (Exception exception)
{

View File

@ -6,35 +6,25 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import mineplex.core.common.util.C;
import mineplex.core.common.util.NautHashMap;
import mineplex.core.database.DBPool;
public class SimpleStatsRepository
{
private static Object _connectionLock = new Object();
private String _connectionString = "jdbc:mysql://sqlstats.mineplex.com:3306/Mineplex?autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
private String _userName = "root";
private String _password = "tAbechAk3wR7tuTh"; //try to obfuscate this in the future!
private static String CREATE_STATS_TABLE = "CREATE TABLE IF NOT EXISTS simpleStats (id INT NOT NULL AUTO_INCREMENT, statName VARCHAR(64), statValue VARCHAR(64), PRIMARY KEY (id));";
private static String RETRIEVE_STATS_RECORDS = "SELECT simpleStats.statName, simpleStats.statValue FROM simpleStats;";
private static String STORE_STATS_RECORD = "INSERT INTO simpleStats (statName,statValue) VALUES(?,?);";
private static String RETRIEVE_STAT_RECORD = "SELECT simpleStats.statName, simpleStats.statValue FROM simpleStats WHERE statName = '?';";
private Connection _connection = null;
public void initialize()
{
PreparedStatement preparedStatement = null;
try
try (Connection connection = DBPool.STATS_MINEPLEX.getConnection())
{
Class.forName("com.mysql.jdbc.Driver");
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
// Create table
preparedStatement = _connection.prepareStatement(CREATE_STATS_TABLE);
preparedStatement = connection.prepareStatement(CREATE_STATS_TABLE);
preparedStatement.execute();
}
catch (Exception exception)
@ -63,23 +53,15 @@ public class SimpleStatsRepository
PreparedStatement preparedStatement = null;
NautHashMap<String, String> statRecords = new NautHashMap<String, String>();
try
try (Connection connection = DBPool.STATS_MINEPLEX.getConnection())
{
synchronized (_connectionLock)
preparedStatement = connection.prepareStatement(RETRIEVE_STATS_RECORDS);
resultSet = preparedStatement.executeQuery();
while (resultSet.next())
{
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = _connection.prepareStatement(RETRIEVE_STATS_RECORDS);
resultSet = preparedStatement.executeQuery();
while (resultSet.next())
{
statRecords.put(resultSet.getString(1), resultSet.getString(2));
}
statRecords.put(resultSet.getString(1), resultSet.getString(2));
}
}
catch (Exception exception)
@ -120,21 +102,13 @@ public class SimpleStatsRepository
{
PreparedStatement preparedStatement = null;
try
try (Connection connection = DBPool.STATS_MINEPLEX.getConnection())
{
synchronized (_connectionLock)
{
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = _connection.prepareStatement(STORE_STATS_RECORD);
preparedStatement.setString(1, statName);
preparedStatement.setString(2, statValue);
preparedStatement.executeUpdate();
}
preparedStatement = connection.prepareStatement(STORE_STATS_RECORD);
preparedStatement.setString(1, statName);
preparedStatement.setString(2, statValue);
preparedStatement.executeUpdate();
}
catch (Exception exception)
{
@ -162,24 +136,16 @@ public class SimpleStatsRepository
PreparedStatement preparedStatement = null;
NautHashMap<String, String> statRecords = new NautHashMap<String, String>();
try
try (Connection connection = DBPool.STATS_MINEPLEX.getConnection())
{
synchronized (_connectionLock)
preparedStatement = connection.prepareStatement(RETRIEVE_STAT_RECORD);
preparedStatement.setString(1, statName);
resultSet = preparedStatement.executeQuery();
while (resultSet.next())
{
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = _connection.prepareStatement(RETRIEVE_STAT_RECORD);
preparedStatement.setString(1, statName);
resultSet = preparedStatement.executeQuery();
while (resultSet.next())
{
statRecords.put(resultSet.getString(1), resultSet.getString(2));
}
statRecords.put(resultSet.getString(1), resultSet.getString(2));
}
}
catch (Exception exception)

View File

@ -5,5 +5,6 @@
<classpathentry kind="var" path="REPO_DIR/Plugins/Libraries/craftbukkit.jar" sourcepath="/REPO_DIR/GitHubLibraries/CraftBukkit/src"/>
<classpathentry combineaccessrules="false" kind="src" path="/Mineplex.Core"/>
<classpathentry combineaccessrules="false" kind="src" path="/Mineplex.Core.Common"/>
<classpathentry combineaccessrules="false" kind="src" path="/Mineplex.Database"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@ -1,7 +1,9 @@
package mineplex.enjinTranslator;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.database.DBPool;
import mineplex.core.database.RepositoryBase;
import mineplex.core.database.column.ColumnInt;
import mineplex.core.database.column.ColumnVarChar;
@ -10,9 +12,9 @@ public class TempRepository extends RepositoryBase
{
private static String INSERT_CLIENT_INVENTORY = "INSERT INTO accountInventory (accountId, itemId, count) SELECT accounts.id, 5, ? FROM accounts WHERE accounts.name = ? ON DUPLICATE KEY UPDATE count=count + VALUES(count);";
public TempRepository(JavaPlugin plugin)
public TempRepository(Plugin plugin)
{
super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh");
super(plugin, DBPool.ACCOUNT);
}
public void addGemBooster(String name, int amount)

View File

@ -11,17 +11,13 @@ import java.util.HashMap;
import java.util.List;
import mineplex.core.common.util.NautHashMap;
import mineplex.core.database.DBPool;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
public class HubRepository
{
private static Object _connectionLock = new Object();
private String _connectionString = "jdbc:mysql://db.mineplex.com:3306/Mineplex?autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
private String _userName = "root";
private String _password = "tAbechAk3wR7tuTh";
private boolean _us = true;
private static String CREATE_NEWS_TABLE = "CREATE TABLE IF NOT EXISTS newsList (id INT NOT NULL AUTO_INCREMENT, newsString VARCHAR(256), newsPosition INT, PRIMARY KEY (id));";
@ -34,22 +30,16 @@ public class HubRepository
private static String RECALC_NEWS_POSITIONS = "UPDATE newsList SET newsPosition = newsPosition - 1 WHERE newsPosition > ?;";
//private static String DELETE_RECALC_NEWS_ENTRY = "SET @pos = ?;SET @max = (SELECT MAX(newsPosition) AS newsPosition FROM newsList);DELETE FROM newsList WHERE newsPosition = @pos;UPDATE newsList SET newsPosition = IF(@max <> @pos, newsPosition - 1, newsPosition) WHERE newsPosition > @pos;";
private Connection _connection = null;
public void initialize(boolean us)
{
_us = us;
PreparedStatement preparedStatement = null;
try
try (Connection connection = DBPool.MINEPLEX.getConnection())
{
Class.forName("com.mysql.jdbc.Driver");
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
// Create table
preparedStatement = _connection.prepareStatement(CREATE_NEWS_TABLE);
preparedStatement = connection.prepareStatement(CREATE_NEWS_TABLE);
preparedStatement.execute();
}
catch (Exception exception)
@ -78,23 +68,15 @@ public class HubRepository
PreparedStatement preparedStatement = null;
HashMap<String, String> newsEntries = new HashMap<String, String>();
try
try (Connection connection = DBPool.MINEPLEX.getConnection())
{
synchronized (_connectionLock)
preparedStatement = connection.prepareStatement(RETRIEVE_NEWS_ENTRIES);
resultSet = preparedStatement.executeQuery();
while (resultSet.next())
{
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = _connection.prepareStatement(RETRIEVE_NEWS_ENTRIES);
resultSet = preparedStatement.executeQuery();
while (resultSet.next())
{
newsEntries.put(resultSet.getString(2), resultSet.getString(1));
}
newsEntries.put(resultSet.getString(2), resultSet.getString(1));
}
}
catch (Exception exception)
@ -135,22 +117,14 @@ public class HubRepository
{
int result = 0;
PreparedStatement preparedStatement = null;
try
try (Connection connection = DBPool.MINEPLEX.getConnection())
{
synchronized (_connectionLock)
{
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = _connection.prepareStatement(SET_NEWS_ENTRY);
preparedStatement.setString(1, newsEntry);
preparedStatement.setInt(2, newsPosition);
result = preparedStatement.executeUpdate();
}
preparedStatement = connection.prepareStatement(SET_NEWS_ENTRY);
preparedStatement.setString(1, newsEntry);
preparedStatement.setInt(2, newsPosition);
result = preparedStatement.executeUpdate();
}
catch (Exception exception)
{
@ -179,23 +153,15 @@ public class HubRepository
int result = 0;
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
try
try (Connection connection = DBPool.MINEPLEX.getConnection())
{
synchronized (_connectionLock)
preparedStatement = connection.prepareStatement(RETRIEVE_MAX_NEWS_POSITION);
resultSet = preparedStatement.executeQuery();
while (resultSet.next())
{
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = _connection.prepareStatement(RETRIEVE_MAX_NEWS_POSITION);
resultSet = preparedStatement.executeQuery();
while (resultSet.next())
{
result = Integer.parseInt(resultSet.getString(1));
}
result = Integer.parseInt(resultSet.getString(1));
}
}
catch (Exception exception)
@ -225,22 +191,14 @@ public class HubRepository
int result = 0;
int maxPos = retrieveMaxNewsPosition();
PreparedStatement preparedStatement = null;
try
{
synchronized (_connectionLock)
{
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = _connection.prepareStatement(ADD_NEWS_ENTRY);
preparedStatement.setString(1, newsEntry);
preparedStatement.setInt(2, maxPos + 1);
result = preparedStatement.executeUpdate();
}
try (Connection connection = DBPool.MINEPLEX.getConnection())
{
preparedStatement = connection.prepareStatement(ADD_NEWS_ENTRY);
preparedStatement.setString(1, newsEntry);
preparedStatement.setInt(2, maxPos + 1);
result = preparedStatement.executeUpdate();
}
catch (Exception exception)
{
@ -269,30 +227,22 @@ public class HubRepository
int result = 0;
int maxPos = retrieveMaxNewsPosition();
PreparedStatement preparedStatement = null;
try
{
synchronized (_connectionLock)
{
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
//preparedStatement = _connection.prepareStatement(DELETE_RECALC_NEWS_ENTRY);
preparedStatement = _connection.prepareStatement(DELETE_NEWS_ENTRY);
preparedStatement.setInt(1, newsPosition);
result = preparedStatement.executeUpdate();
if (result != 0 && maxPos != newsPosition)
{
preparedStatement.close();
preparedStatement = _connection.prepareStatement(RECALC_NEWS_POSITIONS);
preparedStatement.setInt(1, newsPosition);
result = preparedStatement.executeUpdate();
}
try (Connection connection = DBPool.MINEPLEX.getConnection())
{
//preparedStatement = connection.prepareStatement(DELETE_RECALC_NEWS_ENTRY);
preparedStatement = connection.prepareStatement(DELETE_NEWS_ENTRY);
preparedStatement.setInt(1, newsPosition);
result = preparedStatement.executeUpdate();
if (result != 0 && maxPos != newsPosition)
{
preparedStatement.close();
preparedStatement = connection.prepareStatement(RECALC_NEWS_POSITIONS);
preparedStatement.setInt(1, newsPosition);
result = preparedStatement.executeUpdate();
}
}
catch (Exception exception)

View File

@ -41,16 +41,13 @@ public class QueueManager extends MiniPlugin
_eloManager = eloManager;
_partyManager = partyManager;
_repository = new QueueRepository(plugin.getConfig().getString("queue.connectionurl"), plugin.getConfig().getBoolean("queue.us"));
_repository = new QueueRepository(plugin.getConfig().getBoolean("queue.us"));
}
private void setupConfigValues()
{
try
{
GetPlugin().getConfig().addDefault("queue.connectionurl", "jdbc:mysql://db.mineplex.com:3306/Queue?autoReconnect=true&failOverReadOnly=false&maxReconnects=10");
GetPlugin().getConfig().set("queue.connectionurl", GetPlugin().getConfig().getString("queue.connectionurl"));
GetPlugin().getConfig().addDefault("queue.us", true);
GetPlugin().getConfig().set("queue.us", GetPlugin().getConfig().getBoolean("queue.us"));
}

View File

@ -7,14 +7,11 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import mineplex.core.common.util.C;
import mineplex.core.database.DBPool;
public class QueueRepository
{
private static Object _connectionLock = new Object();
private String _connectionString;
private String _userName = "root";
private String _password = "tAbechAk3wR7tuTh";
private boolean _us = true;
private static String CREATE_ELO_QUEUE_TABLE = "CREATE TABLE IF NOT EXISTS playerQueue (id INT NOT NULL AUTO_INCREMENT, playerList VARCHAR(256), gameType VARCHAR(256), playerCount INT, elo INT, state VARCHAR(256), time LONG, assignedMatch INT, US BOOLEAN NOT NULL DEFAULT '1', PRIMARY KEY (id), UNIQUE INDEX name_gametype (playerList, gameType));";
@ -24,11 +21,8 @@ public class QueueRepository
private static String RETRIEVE_MATCH_STATUS = "SELECT state, assignedMatch FROM playerQueue WHERE id = ?;";
private static String RETRIEVE_OTHER_MATCH_STATUS = "SELECT state, playerCount FROM playerQueue WHERE assignedMatch = ? AND id != ? ORDER BY id DESC;";
private Connection _connection = null;
public QueueRepository(String connectionUrl, boolean us)
public QueueRepository(boolean us)
{
_connectionString = connectionUrl;
_us = us;
initialize();
@ -38,12 +32,10 @@ public class QueueRepository
{
PreparedStatement preparedStatement = null;
try
try (Connection connection = DBPool.QUEUE.getConnection())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
// Create table
preparedStatement = _connection.prepareStatement(CREATE_ELO_QUEUE_TABLE);
preparedStatement = connection.prepareStatement(CREATE_ELO_QUEUE_TABLE);
preparedStatement.execute();
}
catch (Exception exception)
@ -70,23 +62,15 @@ public class QueueRepository
{
PreparedStatement preparedStatement = null;
try
try (Connection connection = DBPool.QUEUE.getConnection())
{
synchronized (_connectionLock)
preparedStatement = connection.prepareStatement(DELETE_QUEUE_RECORD);
preparedStatement.setInt(1, matchStatus.Id);
if (preparedStatement.executeUpdate() == 0)
{
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = _connection.prepareStatement(DELETE_QUEUE_RECORD);
preparedStatement.setInt(1, matchStatus.Id);
if (preparedStatement.executeUpdate() == 0)
{
System.out.println("Error deleting queue record.");
}
System.out.println("Error deleting queue record.");
}
}
catch (Exception exception)
@ -113,23 +97,15 @@ public class QueueRepository
{
PreparedStatement preparedStatement = null;
try
try (Connection connection = DBPool.QUEUE.getConnection())
{
synchronized (_connectionLock)
preparedStatement = connection.prepareStatement(SAVE_STATE_VALUE);
preparedStatement.setString(1, matchStatus.State);
preparedStatement.setInt(2, matchStatus.Id);
if (preparedStatement.executeUpdate() == 0)
{
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = _connection.prepareStatement(SAVE_STATE_VALUE);
preparedStatement.setString(1, matchStatus.State);
preparedStatement.setInt(2, matchStatus.Id);
if (preparedStatement.executeUpdate() == 0)
{
System.out.println("Error updating state.");
}
System.out.println("Error updating state.");
}
}
catch (Exception exception)
@ -158,29 +134,21 @@ public class QueueRepository
PreparedStatement preparedStatement = null;
PlayerMatchStatus matchStatus = new PlayerMatchStatus();
try
try (Connection connection = DBPool.QUEUE.getConnection())
{
synchronized (_connectionLock)
{
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = _connection.prepareStatement(INSERT_ACCOUNT, Statement.RETURN_GENERATED_KEYS);
preparedStatement.setString(1, playerList);
preparedStatement.setString(2, gameType);
preparedStatement.setInt(3, elo);
//preparedStatement.setBoolean(4, _us);
preparedStatement.setInt(4, playerCount);
preparedStatement.executeUpdate();
resultSet = preparedStatement.getGeneratedKeys();
preparedStatement = connection.prepareStatement(INSERT_ACCOUNT, Statement.RETURN_GENERATED_KEYS);
preparedStatement.setString(1, playerList);
preparedStatement.setString(2, gameType);
preparedStatement.setInt(3, elo);
//preparedStatement.setBoolean(4, _us);
preparedStatement.setInt(4, playerCount);
while (resultSet.next())
{
matchStatus.Id = resultSet.getInt(1);
}
preparedStatement.executeUpdate();
resultSet = preparedStatement.getGeneratedKeys();
while (resultSet.next())
{
matchStatus.Id = resultSet.getInt(1);
}
}
catch (Exception exception)
@ -223,28 +191,20 @@ public class QueueRepository
PreparedStatement preparedStatement = null;
PlayerMatchStatus matchStatus = null;
try
try (Connection connection = DBPool.QUEUE.getConnection())
{
synchronized (_connectionLock)
{
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = _connection.prepareStatement(RETRIEVE_MATCH_STATUS);
preparedStatement.setInt(1, id);
resultSet = preparedStatement.executeQuery();
preparedStatement = connection.prepareStatement(RETRIEVE_MATCH_STATUS);
preparedStatement.setInt(1, id);
while (resultSet.next())
{
matchStatus = new PlayerMatchStatus();
matchStatus.Id = id;
matchStatus.State = resultSet.getString(1);
matchStatus.AssignedMatch = resultSet.getInt(2);
}
resultSet = preparedStatement.executeQuery();
while (resultSet.next())
{
matchStatus = new PlayerMatchStatus();
matchStatus.Id = id;
matchStatus.State = resultSet.getString(1);
matchStatus.AssignedMatch = resultSet.getInt(2);
}
}
catch (Exception exception)
@ -286,29 +246,21 @@ public class QueueRepository
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
try
try (Connection connection = DBPool.QUEUE.getConnection())
{
synchronized (_connectionLock)
preparedStatement = connection.prepareStatement(RETRIEVE_OTHER_MATCH_STATUS);
preparedStatement.setInt(1, matchStatus.AssignedMatch);
preparedStatement.setInt(2, matchStatus.Id);
resultSet = preparedStatement.executeQuery();
matchStatus.OtherStatuses.clear();
while (resultSet.next())
{
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = _connection.prepareStatement(RETRIEVE_OTHER_MATCH_STATUS);
preparedStatement.setInt(1, matchStatus.AssignedMatch);
preparedStatement.setInt(2, matchStatus.Id);
resultSet = preparedStatement.executeQuery();
matchStatus.OtherStatuses.clear();
while (resultSet.next())
{
int playerCount = resultSet.getInt(2);
for (int i = 0; i < playerCount; i++)
matchStatus.OtherStatuses.add(resultSet.getString(1));
}
int playerCount = resultSet.getInt(2);
for (int i = 0; i < playerCount; i++)
matchStatus.OtherStatuses.add(resultSet.getString(1));
}
}
catch (Exception exception)