Mineplex2018-withcommit/Plugins/Mineplex.ChestConverter/src/mineplex/chestConverter/ChestConverterRepository.java

317 lines
7.7 KiB
Java
Raw Normal View History

package mineplex.chestConverter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map.Entry;
public class ChestConverterRepository
{
private String _connectionString = "jdbc:mysql://db.mineplex.com:3306/Account";
private String _userName = "root";
private String _password = "tAbechAk3wR7tuTh";
private static String RETRIEVE_CHESTS = "SELECT A.uuid, count FROM accountInventory INNER JOIN accounts AS A ON A.id = accountInventory.accountId WHERE itemId = 56 AND count > 0 ORDER BY accountInventory.id LIMIT ?;";
private static String RETRIEVE_KEYS = "SELECT A.uuid, count FROM accountInventory INNER JOIN accounts AS A ON A.id = accountInventory.accountId WHERE itemId = 67 AND count > 0 ORDER BY accountInventory.id LIMIT ?;";
private static String DELETE_CHESTS = "DELETE FROM accountInventory WHERE itemId = 56 ORDER BY accountInventory.id LIMIT ?";
private static String DELETE_KEYS = "DELETE FROM accountInventory WHERE itemId = 67 ORDER BY accountInventory.id LIMIT ?";
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 UPDATE_CLIENT_INVENTORY = "UPDATE accountInventory AS AI INNER JOIN accounts ON AI.accountId = accounts.id SET AI.count = AI.count + ? WHERE accounts.uuid = ? AND AI.itemId = ?;";
private static Connection _connection;
public ChestConverterRepository()
{
PreparedStatement preparedStatement = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
public HashMap<String, Integer> retrieveChestInventoryBatch(int count)
{
HashMap<String, Integer> playerList = new HashMap<String, Integer>();
PreparedStatement preparedStatement = null;
try
{
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = _connection.prepareStatement(RETRIEVE_CHESTS);
preparedStatement.setInt(1, count);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next())
{
playerList.put(resultSet.getString(1), resultSet.getInt(2));
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return playerList;
}
public HashMap<String, Integer> retrieveKeyInventoryBatch(int count)
{
HashMap<String, Integer> playerList = new HashMap<String, Integer>();
PreparedStatement preparedStatement = null;
try
{
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = _connection.prepareStatement(RETRIEVE_KEYS);
preparedStatement.setInt(1, count);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next())
{
playerList.put(resultSet.getString(1), resultSet.getInt(2));
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return playerList;
}
public void incrementClient(HashMap<String, Integer> playerList, boolean chest)
{
PreparedStatement preparedStatement = null;
try
{
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = _connection.prepareStatement(UPDATE_CLIENT_INVENTORY);
for (Entry<String, Integer> entry : playerList.entrySet())
{
preparedStatement.setInt(1, entry.getValue());
preparedStatement.setString(2, entry.getKey());
preparedStatement.setInt(3, chest ? 690 : 692);
preparedStatement.addBatch();
if (chest)
{
if (entry.getValue() > 20)
{
preparedStatement.setInt(1, 1);
preparedStatement.setString(2, entry.getKey());
preparedStatement.setInt(3, 692);
preparedStatement.addBatch();
}
if (entry.getValue() > 50)
{
preparedStatement.setInt(1, 1);
preparedStatement.setString(2, entry.getKey());
preparedStatement.setInt(3, 691);
preparedStatement.addBatch();
}
}
}
int[] rowsAffected = preparedStatement.executeBatch();
int i = 0;
preparedStatement.close();
preparedStatement = _connection.prepareStatement(INSERT_CLIENT_INVENTORY);
for (Entry<String, Integer> entry : playerList.entrySet())
{
if (rowsAffected[i] < 1)
{
preparedStatement.setInt(1, chest ? 690 : 692);
preparedStatement.setInt(2, entry.getValue());
preparedStatement.setString(3, entry.getKey());
preparedStatement.addBatch();
if (chest)
{
if (entry.getValue() > 20)
{
preparedStatement.setInt(1, 692);
preparedStatement.setInt(2, 1);
preparedStatement.setString(3, entry.getKey());
preparedStatement.addBatch();
}
if (entry.getValue() > 50)
{
preparedStatement.setInt(1, 691);
preparedStatement.setInt(2, 1);
preparedStatement.setString(3, entry.getKey());
preparedStatement.addBatch();
}
}
}
i++;
}
preparedStatement.executeBatch();
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
public void deleteChests(int count)
{
PreparedStatement preparedStatement = null;
try
{
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = _connection.prepareStatement(DELETE_CHESTS);
preparedStatement.setInt(1, count);
preparedStatement.executeUpdate();
System.out.println("Deleting " + count + " inventory records.");
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
public void deleteKeys(int count)
{
PreparedStatement preparedStatement = null;
try
{
if (_connection == null || _connection.isClosed())
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
preparedStatement = _connection.prepareStatement(DELETE_KEYS);
preparedStatement.setInt(1, count);
preparedStatement.executeUpdate();
System.out.println("Deleting " + count + " inventory records.");
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
}