JIRA-76: SimpleStats Manager/Repository creation
This commit is contained in:
parent
644f946f69
commit
77dad4b475
@ -0,0 +1,64 @@
|
||||
package mineplex.core.simpleStats;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import mineplex.core.MiniPlugin;
|
||||
import mineplex.core.common.util.NautHashMap;
|
||||
import mineplex.core.updater.UpdateType;
|
||||
import mineplex.core.updater.event.UpdateEvent;
|
||||
|
||||
public class SimpleStats extends MiniPlugin
|
||||
{
|
||||
private static Object _transferLock = new Object();
|
||||
|
||||
private SimpleStatsRepository _repository = new SimpleStatsRepository();
|
||||
private NautHashMap<String, String> _entries = new NautHashMap<String, String>();
|
||||
|
||||
public SimpleStats(JavaPlugin plugin)
|
||||
{
|
||||
super("SimpleStats", plugin);
|
||||
|
||||
_repository.initialize();
|
||||
}
|
||||
|
||||
public NautHashMap<String, String> get_entries() {
|
||||
return _entries;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void storeStatsUpdate(final UpdateEvent updateEvent)
|
||||
{
|
||||
if (updateEvent.getType() != UpdateType.SLOW)
|
||||
return;
|
||||
|
||||
synchronized (_transferLock)
|
||||
{
|
||||
Bukkit.getScheduler().runTaskAsynchronously(GetPlugin(), new Runnable()
|
||||
{
|
||||
public void run()
|
||||
{
|
||||
_entries = (_repository.retrieveStatRecords());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public void store(String statName, String statValue)
|
||||
{
|
||||
final String statNameFinal = statName;
|
||||
final String statValueFinal = statValue;
|
||||
|
||||
synchronized (_transferLock)
|
||||
{
|
||||
Bukkit.getScheduler().runTaskAsynchronously(GetPlugin(), new Runnable()
|
||||
{
|
||||
public void run()
|
||||
{
|
||||
_repository.storeStatValue(statNameFinal, statValueFinal);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,158 @@
|
||||
package mineplex.core.simpleStats;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import mineplex.core.common.util.NautHashMap;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class SimpleStatsRepository
|
||||
{
|
||||
private static Object _connectionLock = new Object();
|
||||
|
||||
private String _connectionString = "jdbc:mysql://sqlstats.mineplex.com:3306/Queue?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 statName.*, statValue.* FROM simpleStats;";
|
||||
private static String STORE_STATS_RECORD = "INSERT INTO simpleStats (statName,statValue) VALUES(";
|
||||
|
||||
private Connection _connection = null;
|
||||
|
||||
public void initialize()
|
||||
{
|
||||
PreparedStatement preparedStatement = null;
|
||||
|
||||
try
|
||||
{
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
|
||||
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
|
||||
|
||||
// Create table
|
||||
preparedStatement = _connection.prepareStatement(CREATE_STATS_TABLE);
|
||||
preparedStatement.execute();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
exception.printStackTrace();
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (preparedStatement != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
preparedStatement.close();
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public NautHashMap<String, String> retrieveStatRecords()
|
||||
{
|
||||
ResultSet resultSet = null;
|
||||
PreparedStatement preparedStatement = null;
|
||||
NautHashMap<String, String> statRecords = new NautHashMap<String, String>();
|
||||
|
||||
try
|
||||
{
|
||||
synchronized (_connectionLock)
|
||||
{
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
exception.printStackTrace();
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (preparedStatement != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
preparedStatement.close();
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
if (resultSet != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
resultSet.close();
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return statRecords;
|
||||
}
|
||||
|
||||
public void storeStatValue(String statName, String statValue)
|
||||
{
|
||||
PreparedStatement preparedStatement = null;
|
||||
|
||||
try
|
||||
{
|
||||
synchronized (_connectionLock)
|
||||
{
|
||||
if (_connection.isClosed())
|
||||
{
|
||||
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
|
||||
}
|
||||
|
||||
preparedStatement = _connection.prepareStatement(STORE_STATS_RECORD + statName + "," + statValue + ");");
|
||||
|
||||
preparedStatement.executeUpdate();
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
exception.printStackTrace();
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (preparedStatement != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
preparedStatement.close();
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user