Merge branch 'feature/database-pool'

This commit is contained in:
CoderTim 2014-11-01 01:59:07 -04:00
commit f3770933b6
33 changed files with 567 additions and 1253 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,10 +5,12 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import mineplex.core.database.DBPool;
import mineplex.core.database.RepositoryBase;
import mineplex.core.database.ResultSetCallable;
import mineplex.core.database.column.ColumnVarChar;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
public class RankBenefitsGiver9000Repository extends RepositoryBase
@ -18,9 +20,9 @@ public class RankBenefitsGiver9000Repository extends RepositoryBase
private static String INSERT_BENEFIT = "INSERT INTO rankBenefits (uuid, benefit) VALUES (?, ?);";
private static String RETRIEVE_BENEFITS = "SELECT benefit FROM rankBenefits WHERE uuid = ?;";
public RankBenefitsGiver9000Repository(JavaPlugin plugin)
public RankBenefitsGiver9000Repository(Plugin plugin)
{
super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh");
super(plugin, DBPool.ACCOUNT);
}
@Override

View File

@ -5,6 +5,7 @@ import java.util.List;
import java.util.UUID;
import org.bukkit.craftbukkit.libs.com.google.gson.reflect.TypeToken;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.account.repository.token.LoginToken;
@ -12,6 +13,7 @@ import mineplex.core.account.repository.token.RankUpdateToken;
import mineplex.core.common.Rank;
import mineplex.core.common.util.Callback;
import mineplex.core.common.util.UUIDFetcher;
import mineplex.core.database.DBPool;
import mineplex.core.database.DatabaseRunnable;
import mineplex.core.database.RepositoryBase;
import mineplex.core.database.column.ColumnBoolean;
@ -32,9 +34,9 @@ public class AccountRepository extends RepositoryBase
private String _webAddress;
public AccountRepository(JavaPlugin plugin, String webAddress)
public AccountRepository(Plugin plugin, String webAddress)
{
super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh");
super(plugin, DBPool.ACCOUNT);
_webAddress = webAddress;
}

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)
@ -71,15 +62,9 @@ public class AntiHackRepository
{
PreparedStatement preparedStatement = null;
synchronized (_connectionLock)
try (Connection connection = DBPool.STATS_MINEPLEX.getConnection())
{
try
{
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = _connection.prepareStatement(UPDATE_PLAYER_OFFENSES);
preparedStatement = connection.prepareStatement(UPDATE_PLAYER_OFFENSES);
preparedStatement.setString(1, player.getName());
preparedStatement.setString(2, motd);
@ -111,7 +96,6 @@ public class AntiHackRepository
}
}
}
}
}).start();
}
}

View File

@ -1,51 +1,33 @@
package mineplex.core.database;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbcp2.BasicDataSource;
public class DBPool
{
private static final DBPool _instance = new 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");
public static DBPool getInstance()
private static DataSource openDataSource(String url, String username, String password)
{
return _instance;
}
BasicDataSource source = new BasicDataSource();
source.addConnectionProperty("autoReconnect", "true");
source.setDefaultAutoCommit(true);
source.setEnableAutoCommitOnReturn(true);
source.setDefaultTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
source.setDriverClassName("com.mysql.jdbc.Driver");
source.setUrl(url);
source.setUsername(username);
source.setPassword(password);
source.setMaxTotal(-1);
source.setMaxIdle(3);
source.setTimeBetweenEvictionRunsMillis(180 * 1000);
source.setSoftMinEvictableIdleTimeMillis(180 * 1000);
private BasicDataSource _source = null;
public synchronized BasicDataSource source()
{
if (_source == null || _source.isClosed())
{
_source = new BasicDataSource();
_source.addConnectionProperty("autoReconnect", "true");
_source.setDefaultAutoCommit(true);
_source.setEnableAutoCommitOnReturn(true);
_source.setDefaultTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
_source.setDriverClassName("com.mysql.jdbc.Driver");
_source.setUrl("jdbc:mysql://db.mineplex.com/Account");
_source.setUsername("root");
_source.setPassword("tAbechAk3wR7tuTh");
}
return _source;
}
public synchronized Connection getConnection() throws SQLException
{
return source().getConnection();
}
public synchronized void close() throws SQLException
{
if (_source != null)
{
_source.close();
_source = null;
}
return source;
}
}

View File

@ -1,57 +1,44 @@
package mineplex.core.database;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import mineplex.core.common.util.NautHashMap;
import mineplex.core.database.column.Column;
import mineplex.core.logger.Logger;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
public abstract class RepositoryBase implements Listener
{
protected static Object _connectionLock = new Object();
private Connection _connection = null;
private static Object _queueLock = new Object();
private NautHashMap<DatabaseRunnable, String> _failedQueue = new NautHashMap<DatabaseRunnable, String>();
private String _connectionString;
private String _userName;
private String _password;
private final Plugin _plugin;
private final DataSource _dataSource;
protected JavaPlugin Plugin;
public RepositoryBase(JavaPlugin plugin, String connectionString, String username, String password)
public RepositoryBase(Plugin plugin, DataSource dataSource)
{
Plugin = plugin;
_connectionString = connectionString;
_userName = username;
_password = password;
_plugin = plugin;
_dataSource = dataSource;
Bukkit.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable()
{
public void run()
{
synchronized (_connectionLock)
{
initialize();
update();
}
}
});
plugin.getServer().getPluginManager().registerEvents(this, plugin);
@ -61,19 +48,9 @@ public abstract class RepositoryBase implements Listener
protected abstract void update();
protected Connection getConnection()
protected Connection getConnection() throws SQLException
{
try
{
if (_connection == null || !_connection.isValid(1))
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
catch (SQLException e)
{
e.printStackTrace();
}
return _connection;
return getDataSource().getConnection();
}
protected int executeUpdate(String query, Column<?>... columns)
@ -82,12 +59,9 @@ public abstract class RepositoryBase implements Listener
int affectedRows = 0;
try
try (Connection connection = getConnection())
{
if (_connection == null || !_connection.isValid(1))
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = _connection.prepareStatement(query);
preparedStatement = connection.prepareStatement(query);
for (int i = 0; i < columns.length; i++)
{
@ -157,12 +131,9 @@ public abstract class RepositoryBase implements Listener
{
PreparedStatement preparedStatement = null;
try
try (Connection connection = getConnection())
{
if (_connection == null || !_connection.isValid(1))
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = _connection.prepareStatement(query);
preparedStatement = connection.prepareStatement(query);
executeQuery(preparedStatement, callable, columns);
}
@ -283,4 +254,14 @@ public abstract class RepositoryBase implements Listener
}
}
}
public Plugin getPlugin()
{
return _plugin;
}
public DataSource getDataSource()
{
return _dataSource;
}
}

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

@ -2,10 +2,12 @@ package mineplex.core.donation.repository;
import java.util.UUID;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.common.util.Callback;
import mineplex.core.common.util.UUIDFetcher;
import mineplex.core.database.DBPool;
import mineplex.core.database.DatabaseRunnable;
import mineplex.core.database.RepositoryBase;
import mineplex.core.database.column.ColumnInt;
@ -28,9 +30,9 @@ public class DonationRepository extends RepositoryBase
private String _webAddress;
public DonationRepository(JavaPlugin plugin, String webAddress)
public DonationRepository(Plugin plugin, String webAddress)
{
super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh");
super(plugin, DBPool.ACCOUNT);
_webAddress = webAddress;
}

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

@ -5,9 +5,11 @@ import java.sql.SQLException;
import java.util.UUID;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.common.util.NautHashMap;
import mineplex.core.database.DBPool;
import mineplex.core.database.RepositoryBase;
import mineplex.core.database.ResultSetCallable;
import mineplex.core.database.column.ColumnVarChar;
@ -26,9 +28,9 @@ public class FriendRepository extends RepositoryBase
// Not mutual, need to drop accountFriend to recreate with constraint.
// On add record need to check for a reverse uuidsource/uuidtarget and set mutual
public FriendRepository(JavaPlugin plugin)
public FriendRepository(Plugin plugin)
{
super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh");
super(plugin, DBPool.ACCOUNT);
}
@Override

View File

@ -5,8 +5,10 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
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.ResultSetCallable;
import mineplex.core.database.column.ColumnInt;
@ -29,9 +31,9 @@ public class InventoryRepository extends RepositoryBase
private static String INSERT_CLIENT_INVENTORY = "INSERT INTO accountInventory (accountId, itemId, count) SELECT accounts.id, ?, ? FROM accounts WHERE accounts.uuid = ? ON DUPLICATE KEY UPDATE count=count + VALUES(count);";
private static String RETRIEVE_CLIENT_INVENTORY = "SELECT items.name, ic.name as category, count FROM accountInventory AS ai INNER JOIN items ON items.id = ai.itemId INNER JOIN itemCategories AS ic ON ic.id = items.categoryId INNER JOIN accounts ON accounts.id = ai.accountId WHERE accounts.uuid = ?;";
public InventoryRepository(JavaPlugin plugin)
public InventoryRepository(Plugin plugin)
{
super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh");
super(plugin, DBPool.ACCOUNT);
}
@Override

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,16 +55,9 @@ public class LoggerRepository
{
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(INSERT_LOG);
preparedStatement = connection.prepareStatement(INSERT_LOG);
for (String msg : message)
{
@ -83,7 +68,6 @@ public class LoggerRepository
preparedStatement.executeBatch();
}
}
catch (Exception exception)
{
exception.printStackTrace();

View File

@ -106,21 +106,6 @@ public class NpcManager extends MiniPlugin
private final Set<UUID> _npcDeletingPlayers = new HashSet<>();
private Connection _connection;
protected Connection getConnection()
{
try
{
if (_connection == null || !_connection.isValid(1))
_connection = DriverManager.getConnection("jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh");
}
catch (SQLException e)
{
e.printStackTrace();
}
return _connection;
}
public NpcManager(JavaPlugin plugin, Creature creature)
{
super("NpcManager", plugin);
@ -276,9 +261,7 @@ public class NpcManager extends MiniPlugin
public Entity addNpc(Player player, EntityType entityType, double radius, boolean adult, String name, String entityMeta) throws SQLException
{
Connection connection = getConnection();
//try (Connection connection = DBPool.getInstance().getConnection())
try (Connection connection = DBPool.ACCOUNT.getConnection())
{
String helmet = itemStackToYaml(player.getInventory().getHelmet());
String chestplate = itemStackToYaml(player.getInventory().getChestplate());
@ -393,9 +376,7 @@ public class NpcManager extends MiniPlugin
if (npc != null)
{
Connection connection = getConnection();
try// (Connection connection = DBPool.getInstance().getConnection())
try (Connection connection = DBPool.ACCOUNT.getConnection())
{
npc.getDatabaseRecord().attach(DSL.using(connection).configuration());
npc.getDatabaseRecord().delete();
@ -502,9 +483,7 @@ public class NpcManager extends MiniPlugin
{
String serverType = getServerName();
Connection connection = getConnection();
//try (Connection connection = DBPool.getInstance().getConnection())
try (Connection connection = DBPool.ACCOUNT.getConnection())
{
Result<NpcsRecord> result = DSL.using(connection)
.selectFrom(Tables.npcs)
@ -530,9 +509,7 @@ public class NpcManager extends MiniPlugin
{
String serverType = getServerName();
Connection connection = getConnection();
//try (Connection connection = DBPool.getInstance().getConnection())
try (Connection connection = DBPool.ACCOUNT.getConnection())
{
DSL.using(connection)
.delete(Tables.npcs)

View File

@ -6,14 +6,10 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import mineplex.core.database.DBPool;
public class PlayerTrackerRepository
{
private static Object _connectionLock = new Object();
private String _connectionString = "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
private String _userName = "root";
private String _password = "tAbechAk3wR7tuTh";
private String _serverName = "";
private boolean _us = true;
@ -22,8 +18,6 @@ public class PlayerTrackerRepository
private static String INSERT_PLAYERMAP = "INSERT INTO playerMap (playerName, serverName, us) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE serverName = VALUES(serverName), us = VALUES(us);";
private static String DELETE_PLAYERMAP = "DELETE FROM playerMap WHERE playerName = ? AND serverName = ? AND us = ?;";
private Connection _connection = null;
public void initialize(String serverName, boolean us)
{
_serverName = serverName;
@ -31,14 +25,10 @@ public class PlayerTrackerRepository
PreparedStatement preparedStatement = null;
try
try (Connection connection = DBPool.ACCOUNT.getConnection())
{
Class.forName("com.mysql.jdbc.Driver");
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
// Create table
preparedStatement = _connection.prepareStatement(CREATE_PLAYERMAP_TABLE);
preparedStatement = connection.prepareStatement(CREATE_PLAYERMAP_TABLE);
preparedStatement.execute();
}
catch (Exception exception)
@ -67,16 +57,9 @@ public class PlayerTrackerRepository
PreparedStatement preparedStatement = null;
String server = "N/A";
try
try (Connection connection = DBPool.ACCOUNT.getConnection())
{
synchronized (_connectionLock)
{
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = _connection.prepareStatement(RETRIEVE_PLAYERMAP);
preparedStatement = connection.prepareStatement(RETRIEVE_PLAYERMAP);
preparedStatement.setString(1, playerName);
preparedStatement.setBoolean(2, _us);
@ -87,7 +70,6 @@ public class PlayerTrackerRepository
server = resultSet.getString(1);
}
}
}
catch (Exception exception)
{
exception.printStackTrace();
@ -126,23 +108,15 @@ public class PlayerTrackerRepository
{
PreparedStatement preparedStatement = null;
try
try (Connection connection = DBPool.ACCOUNT.getConnection())
{
synchronized (_connectionLock)
{
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = _connection.prepareStatement(DELETE_PLAYERMAP);
preparedStatement = connection.prepareStatement(DELETE_PLAYERMAP);
preparedStatement.setString(1, playerName);
preparedStatement.setString(2, _serverName);
preparedStatement.setBoolean(3, _us);
preparedStatement.executeUpdate();
}
}
catch (Exception exception)
{
exception.printStackTrace();
@ -167,23 +141,15 @@ public class PlayerTrackerRepository
{
PreparedStatement preparedStatement = null;
try
try (Connection connection = DBPool.ACCOUNT.getConnection())
{
synchronized (_connectionLock)
{
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = _connection.prepareStatement(INSERT_PLAYERMAP);
preparedStatement = connection.prepareStatement(INSERT_PLAYERMAP);
preparedStatement.setString(1, playerName);
preparedStatement.setString(2, _serverName);
preparedStatement.setBoolean(3, _us);
preparedStatement.executeUpdate();
}
}
catch (Exception exception)
{
exception.printStackTrace();

View File

@ -36,28 +36,13 @@ public class PreferencesManager extends MiniClientPlugin<UserPreferences>
{
super("Preferences", plugin);
setupConfigValues();
_repository = new PreferencesRepository(plugin);
_repository = new PreferencesRepository(plugin, plugin.getConfig().getString("preferences.connectionurl"));
_shop = new PreferencesShop(this, clientManager, donationManager);
AddCommand(new PreferencesCommand(this));
}
private void setupConfigValues()
{
try
{
GetPlugin().getConfig().addDefault("preferences.connectionurl", "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10");
GetPlugin().getConfig().set("preferences.connectionurl", GetPlugin().getConfig().getString("preferences.connectionurl"));
GetPlugin().saveConfig();
}
catch (Exception e)
{
e.printStackTrace();
}
}
@Override
protected UserPreferences AddPlayer(String player)
{

View File

@ -1,14 +1,17 @@
package mineplex.core.preferences;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map.Entry;
import java.util.UUID;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.common.util.NautHashMap;
import mineplex.core.database.DBPool;
import mineplex.core.database.RepositoryBase;
import mineplex.core.database.ResultSetCallable;
import mineplex.core.database.column.ColumnVarChar;
@ -20,9 +23,9 @@ public class PreferencesRepository extends RepositoryBase
private static String RETRIEVE_ACCOUNT_PREFERENCES = "SELECT games, visibility, showChat, friendChat, privateMessaging, partyRequests, invisibility, forcefield, showMacReports, ignoreVelocity, pendingFriendRequests FROM accountPreferences WHERE uuid = ?;";
private static String UPDATE_ACCOUNT_PREFERENCES = "UPDATE accountPreferences SET games = ?, visibility = ?, showChat = ?, friendChat = ?, privateMessaging = ?, partyRequests = ?, invisibility = ?, forcefield = ?, showMacReports = ?, ignoreVelocity = ?, pendingFriendRequests = ? WHERE uuid=?;";
public PreferencesRepository(JavaPlugin plugin, String connectionString)
public PreferencesRepository(Plugin plugin)
{
super(plugin, connectionString, "root", "tAbechAk3wR7tuTh");
super(plugin, DBPool.ACCOUNT);
}
@Override
@ -40,9 +43,9 @@ public class PreferencesRepository extends RepositoryBase
{
PreparedStatement preparedStatement = null;
try
try (Connection connection = getConnection())
{
preparedStatement = getConnection().prepareStatement(UPDATE_ACCOUNT_PREFERENCES);
preparedStatement = connection.prepareStatement(UPDATE_ACCOUNT_PREFERENCES);
for (Entry<String, UserPreferences> entry : preferences.entrySet())
{

View File

@ -1,8 +1,13 @@
package mineplex.core.reward;
import java.sql.Connection;
import java.sql.SQLException;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.database.DBPool;
import mineplex.core.database.RepositoryBase;
import mineplex.database.Tables;
import org.jooq.DSLContext;
@ -13,9 +18,9 @@ import org.jooq.impl.DSL;
*/
public class RewardRepository extends RepositoryBase
{
public RewardRepository(JavaPlugin plugin)
public RewardRepository(Plugin plugin)
{
super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh");
super(plugin, DBPool.ACCOUNT);
}
@Override
@ -32,12 +37,11 @@ public class RewardRepository extends RepositoryBase
public void logReward(Player player, String type, String rarity, String reward)
{
DSLContext context;
synchronized (this)
try
{
context = DSL.using(getConnection());
}
try (Connection connection = getConnection())
{
DSLContext context = DSL.using(connection);
context.insertInto(Tables.rewardLog)
.set(Tables.rewardLog.accountId, DSL.select(Tables.accounts.id)
@ -50,3 +54,9 @@ public class RewardRepository extends RepositoryBase
.execute();
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}

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,16 +53,9 @@ public class SimpleStatsRepository
PreparedStatement preparedStatement = null;
NautHashMap<String, String> statRecords = new NautHashMap<String, String>();
try
try (Connection connection = DBPool.STATS_MINEPLEX.getConnection())
{
synchronized (_connectionLock)
{
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = _connection.prepareStatement(RETRIEVE_STATS_RECORDS);
preparedStatement = connection.prepareStatement(RETRIEVE_STATS_RECORDS);
resultSet = preparedStatement.executeQuery();
@ -81,7 +64,6 @@ public class SimpleStatsRepository
statRecords.put(resultSet.getString(1), resultSet.getString(2));
}
}
}
catch (Exception exception)
{
exception.printStackTrace();
@ -120,22 +102,14 @@ 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 = connection.prepareStatement(STORE_STATS_RECORD);
preparedStatement.setString(1, statName);
preparedStatement.setString(2, statValue);
preparedStatement.executeUpdate();
}
}
catch (Exception exception)
{
exception.printStackTrace();
@ -162,16 +136,9 @@ public class SimpleStatsRepository
PreparedStatement preparedStatement = null;
NautHashMap<String, String> statRecords = new NautHashMap<String, String>();
try
try (Connection connection = DBPool.STATS_MINEPLEX.getConnection())
{
synchronized (_connectionLock)
{
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = _connection.prepareStatement(RETRIEVE_STAT_RECORD);
preparedStatement = connection.prepareStatement(RETRIEVE_STAT_RECORD);
preparedStatement.setString(1, statName);
resultSet = preparedStatement.executeQuery();
@ -181,7 +148,6 @@ public class SimpleStatsRepository
statRecords.put(resultSet.getString(1), resultSet.getString(2));
}
}
}
catch (Exception exception)
{
exception.printStackTrace();

View File

@ -130,7 +130,7 @@ public class StatsManager extends MiniClientPlugin<PlayerStats>
});
}
public PlayerStats getOfflinePlayerStats(String playerName)
public PlayerStats getOfflinePlayerStats(String playerName) throws SQLException
{
return _repository.loadOfflinePlayerStats(playerName);
}

View File

@ -1,5 +1,6 @@
package mineplex.core.stats;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@ -7,9 +8,11 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.common.util.NautHashMap;
import mineplex.core.database.DBPool;
import mineplex.core.database.RepositoryBase;
import mineplex.core.database.ResultSetCallable;
import mineplex.core.database.column.ColumnVarChar;
@ -36,9 +39,9 @@ public class StatsRepository extends RepositoryBase
private static String RETRIEVE_STATS = "SELECT id, name FROM stats;";
private static String INSERT_STAT = "INSERT INTO stats (name) VALUES (?);";
public StatsRepository(JavaPlugin plugin)
public StatsRepository(Plugin plugin)
{
super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh");
super(plugin, DBPool.ACCOUNT);
}
@Override
@ -85,9 +88,9 @@ public class StatsRepository extends RepositoryBase
{
System.out.println("saving stats.");
try
try (Connection connection = getConnection())
{
DSLContext context = DSL.using(getConnection());
DSLContext context = DSL.using(connection);
List<Update> updates = new ArrayList<>();
List<Insert> inserts = new ArrayList<>();
@ -134,16 +137,13 @@ public class StatsRepository extends RepositoryBase
}
}
public PlayerStats loadOfflinePlayerStats(String playerName)
public PlayerStats loadOfflinePlayerStats(String playerName) throws SQLException
{
try (Connection connection = getConnection())
{
PlayerStats playerStats = null;
DSLContext context;
synchronized (this)
{
context = DSL.using(getConnection());
}
DSLContext context = DSL.using(connection);
Result<Record2<String, Integer>> result = context.select(Tables.stats.name, Tables.accountStats.value).from(Tables.accountStats)
.join(Tables.stats)
@ -153,7 +153,6 @@ public class StatsRepository extends RepositoryBase
.where(Tables.accounts.name.eq(playerName)))
).fetch();
if (result.isNotEmpty())
{
playerStats = new PlayerStats();
@ -165,6 +164,7 @@ public class StatsRepository extends RepositoryBase
return playerStats;
}
}
public PlayerStats loadClientInformation(String uuid)
{

View File

@ -1,5 +1,7 @@
package mineplex.core.stats.command;
import java.sql.SQLException;
import org.bukkit.entity.Player;
import mineplex.core.achievement.AchievementManager;
@ -38,6 +40,8 @@ public class TimeCommand extends CommandBase<StatsManager>
{
@Override
public void run()
{
try
{
final PlayerStats stats = Plugin.getOfflinePlayerStats(args[0]);
@ -57,7 +61,11 @@ public class TimeCommand extends CommandBase<StatsManager>
}
}
});
}
catch (SQLException e)
{
e.printStackTrace();
}
}
});
}

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

@ -15,6 +15,8 @@
<orderEntry type="module" module-name="Mineplex.Minecraft.Game.Core" />
<orderEntry type="library" name="craftbukkit" level="project" />
<orderEntry type="module" module-name="Mineplex.ServerData" />
<orderEntry type="library" name="commons-dbcp2" level="project" />
<orderEntry type="library" name="commons-pool2" level="project" />
</component>
</module>

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,16 +68,9 @@ public class HubRepository
PreparedStatement preparedStatement = null;
HashMap<String, String> newsEntries = new HashMap<String, String>();
try
try (Connection connection = DBPool.MINEPLEX.getConnection())
{
synchronized (_connectionLock)
{
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = _connection.prepareStatement(RETRIEVE_NEWS_ENTRIES);
preparedStatement = connection.prepareStatement(RETRIEVE_NEWS_ENTRIES);
resultSet = preparedStatement.executeQuery();
@ -96,7 +79,6 @@ public class HubRepository
newsEntries.put(resultSet.getString(2), resultSet.getString(1));
}
}
}
catch (Exception exception)
{
exception.printStackTrace();
@ -136,22 +118,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 = connection.prepareStatement(SET_NEWS_ENTRY);
preparedStatement.setString(1, newsEntry);
preparedStatement.setInt(2, newsPosition);
result = preparedStatement.executeUpdate();
}
}
catch (Exception exception)
{
exception.printStackTrace();
@ -180,16 +154,9 @@ public class HubRepository
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
try
try (Connection connection = DBPool.MINEPLEX.getConnection())
{
synchronized (_connectionLock)
{
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = _connection.prepareStatement(RETRIEVE_MAX_NEWS_POSITION);
preparedStatement = connection.prepareStatement(RETRIEVE_MAX_NEWS_POSITION);
resultSet = preparedStatement.executeQuery();
while (resultSet.next())
@ -197,7 +164,6 @@ public class HubRepository
result = Integer.parseInt(resultSet.getString(1));
}
}
}
catch (Exception exception)
{
exception.printStackTrace();
@ -226,22 +192,14 @@ public class HubRepository
int maxPos = retrieveMaxNewsPosition();
PreparedStatement preparedStatement = null;
try
try (Connection connection = DBPool.MINEPLEX.getConnection())
{
synchronized (_connectionLock)
{
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = _connection.prepareStatement(ADD_NEWS_ENTRY);
preparedStatement = connection.prepareStatement(ADD_NEWS_ENTRY);
preparedStatement.setString(1, newsEntry);
preparedStatement.setInt(2, maxPos + 1);
result = preparedStatement.executeUpdate();
}
}
catch (Exception exception)
{
exception.printStackTrace();
@ -270,17 +228,10 @@ public class HubRepository
int maxPos = retrieveMaxNewsPosition();
PreparedStatement preparedStatement = null;
try
try (Connection connection = DBPool.MINEPLEX.getConnection())
{
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 = connection.prepareStatement(DELETE_RECALC_NEWS_ENTRY);
preparedStatement = connection.prepareStatement(DELETE_NEWS_ENTRY);
preparedStatement.setInt(1, newsPosition);
result = preparedStatement.executeUpdate();
@ -288,13 +239,12 @@ public class HubRepository
{
preparedStatement.close();
preparedStatement = _connection.prepareStatement(RECALC_NEWS_POSITIONS);
preparedStatement = connection.prepareStatement(RECALC_NEWS_POSITIONS);
preparedStatement.setInt(1, newsPosition);
result = preparedStatement.executeUpdate();
}
}
}
catch (Exception exception)
{
exception.printStackTrace();

View File

@ -8,6 +8,7 @@ import java.util.UUID;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.database.DBPool;
import mineplex.core.database.RepositoryBase;
import mineplex.core.database.ResultSetCallable;
import mineplex.core.database.column.ColumnInt;
@ -28,7 +29,7 @@ public class PollRepository extends RepositoryBase
public PollRepository(JavaPlugin plugin)
{
super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh");
super(plugin, DBPool.ACCOUNT);
}
@Override

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,16 +62,9 @@ public class QueueRepository
{
PreparedStatement preparedStatement = null;
try
try (Connection connection = DBPool.QUEUE.getConnection())
{
synchronized (_connectionLock)
{
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = _connection.prepareStatement(DELETE_QUEUE_RECORD);
preparedStatement = connection.prepareStatement(DELETE_QUEUE_RECORD);
preparedStatement.setInt(1, matchStatus.Id);
@ -88,7 +73,6 @@ public class QueueRepository
System.out.println("Error deleting queue record.");
}
}
}
catch (Exception exception)
{
exception.printStackTrace();
@ -113,16 +97,9 @@ public class QueueRepository
{
PreparedStatement preparedStatement = null;
try
try (Connection connection = DBPool.QUEUE.getConnection())
{
synchronized (_connectionLock)
{
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = _connection.prepareStatement(SAVE_STATE_VALUE);
preparedStatement = connection.prepareStatement(SAVE_STATE_VALUE);
preparedStatement.setString(1, matchStatus.State);
preparedStatement.setInt(2, matchStatus.Id);
@ -131,7 +108,6 @@ public class QueueRepository
System.out.println("Error updating state.");
}
}
}
catch (Exception exception)
{
exception.printStackTrace();
@ -158,16 +134,9 @@ 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 = connection.prepareStatement(INSERT_ACCOUNT, Statement.RETURN_GENERATED_KEYS);
preparedStatement.setString(1, playerList);
preparedStatement.setString(2, gameType);
preparedStatement.setInt(3, elo);
@ -182,7 +151,6 @@ public class QueueRepository
matchStatus.Id = resultSet.getInt(1);
}
}
}
catch (Exception exception)
{
exception.printStackTrace();
@ -223,16 +191,9 @@ 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 = connection.prepareStatement(RETRIEVE_MATCH_STATUS);
preparedStatement.setInt(1, id);
resultSet = preparedStatement.executeQuery();
@ -246,7 +207,6 @@ public class QueueRepository
matchStatus.AssignedMatch = resultSet.getInt(2);
}
}
}
catch (Exception exception)
{
exception.printStackTrace();
@ -286,16 +246,9 @@ public class QueueRepository
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
try
try (Connection connection = DBPool.QUEUE.getConnection())
{
synchronized (_connectionLock)
{
if (_connection.isClosed())
{
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
preparedStatement = _connection.prepareStatement(RETRIEVE_OTHER_MATCH_STATUS);
preparedStatement = connection.prepareStatement(RETRIEVE_OTHER_MATCH_STATUS);
preparedStatement.setInt(1, matchStatus.AssignedMatch);
preparedStatement.setInt(2, matchStatus.Id);
@ -310,7 +263,6 @@ public class QueueRepository
matchStatus.OtherStatuses.add(resultSet.getString(1));
}
}
}
catch (Exception exception)
{
exception.printStackTrace();

View File

@ -15,6 +15,8 @@
<orderEntry type="library" name="jooq" level="project" />
<orderEntry type="module" module-name="Mineplex.Database" />
<orderEntry type="module" module-name="Mineplex.ServerData" />
<orderEntry type="library" name="commons-dbcp2" level="project" />
<orderEntry type="library" name="commons-pool2" level="project" />
</component>
</module>

View File

@ -1,6 +1,7 @@
package nautilus.game.arcade;
import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
@ -996,9 +997,16 @@ public class ArcadeManager extends MiniPlugin implements IRelation
{
@Override
public void run()
{
try
{
getArcadeRepository().saveBasicStats(game.GetType(), IsTournamentServer(), (int) (System.currentTimeMillis() - game.getGameLiveTime()), data);
}
catch (SQLException e)
{
e.printStackTrace();
}
}
});
}
@ -1020,9 +1028,16 @@ public class ArcadeManager extends MiniPlugin implements IRelation
{
@Override
public void run()
{
try
{
getArcadeRepository().saveLeaderboardStats(0, type.ordinal(), data);
}
catch (SQLException e)
{
e.printStackTrace();
}
}
});
}
}

View File

@ -1,12 +1,16 @@
package nautilus.game.arcade;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.database.DBPool;
import mineplex.core.database.RepositoryBase;
import mineplex.database.Tables;
import mineplex.database.tables.records.GamesRecord;
@ -18,9 +22,9 @@ public class ArcadeRepository extends RepositoryBase
{
private final String serverName;
public ArcadeRepository(JavaPlugin plugin)
public ArcadeRepository(Plugin plugin)
{
super(plugin, "jdbc:mysql://db.mineplex.com:3306/Account?autoReconnect=true&failOverReadOnly=false&maxReconnects=10", "root", "tAbechAk3wR7tuTh");
super(plugin, DBPool.ACCOUNT);
serverName = plugin.getConfig().getString("serverstatus.name");
}
@ -37,14 +41,11 @@ public class ArcadeRepository extends RepositoryBase
}
public void saveBasicStats(GameType type, boolean tournament, int duration, Map<UUID, Boolean> players)
public void saveBasicStats(GameType type, boolean tournament, int duration, Map<UUID, Boolean> players) throws SQLException
{
DSLContext context;
synchronized (this)
try (Connection connection = getConnection())
{
context = DSL.using(getConnection());
}
DSLContext context = DSL.using(connection);
GamesRecord record = context.newRecord(Tables.games);
record.setDuration(duration);
@ -70,16 +71,14 @@ public class ArcadeRepository extends RepositoryBase
context.batch(queryList).execute();
}
public void saveLeaderboardStats(int tournamentId, int gameId, Map<UUID, Boolean> players)
{
DSLContext context;
synchronized (this)
{
context = DSL.using(getConnection());
}
public void saveLeaderboardStats(int tournamentId, int gameId, Map<UUID, Boolean> players) throws SQLException
{
try (Connection connection = getConnection())
{
DSLContext context = DSL.using(connection);
List<Query> queryList = new ArrayList<>(players.size());
for (Map.Entry<UUID, Boolean> entry : players.entrySet())
@ -105,3 +104,4 @@ public class ArcadeRepository extends RepositoryBase
context.batch(queryList).execute();
}
}
}