Dateien nach "src/main/java/craftersland/eco/bridge/database" hochladen
This commit is contained in:
@@ -0,0 +1,179 @@
|
||||
package net.craftersland.eco.bridge.database;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import net.craftersland.eco.bridge.Eco;
|
||||
|
||||
public class EcoMysqlHandler {
|
||||
|
||||
private Eco eco;
|
||||
|
||||
public EcoMysqlHandler(Eco eco) {
|
||||
this.eco = eco;
|
||||
}
|
||||
|
||||
public String[] getData(Player player) {
|
||||
if (!hasAccount(player)) {
|
||||
createAccount(player);
|
||||
}
|
||||
PreparedStatement preparedUpdateStatement = null;
|
||||
ResultSet result = null;
|
||||
Connection conn = eco.getMysqlSetup().getConnection();
|
||||
if (conn != null) {
|
||||
try {
|
||||
String sql = "SELECT * FROM `" + eco.getConfigHandler().getString("database.mysql.dataTableName") + "` WHERE `player_uuid` = ? LIMIT 1";
|
||||
preparedUpdateStatement = conn.prepareStatement(sql);
|
||||
preparedUpdateStatement.setString(1, player.getUniqueId().toString());
|
||||
|
||||
result = preparedUpdateStatement.executeQuery();
|
||||
while (result.next()) {
|
||||
String[] data = {"" + result.getDouble("money"), result.getString("sync_complete"), result.getString("last_seen")};
|
||||
return data;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
Eco.log.warning("Error: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (result != null) {
|
||||
result.close();
|
||||
}
|
||||
if (preparedUpdateStatement != null) {
|
||||
preparedUpdateStatement.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setSyncStatus(Player player, String syncStatus) {
|
||||
PreparedStatement preparedUpdateStatement = null;
|
||||
Connection conn = eco.getMysqlSetup().getConnection();
|
||||
if (conn != null) {
|
||||
try {
|
||||
String data = "UPDATE `" + eco.getConfigHandler().getString("database.mysql.dataTableName") + "` " + "SET `sync_complete` = ?" + ", `last_seen` = ?" + " WHERE `player_uuid` = ?";
|
||||
preparedUpdateStatement = conn.prepareStatement(data);
|
||||
preparedUpdateStatement.setString(1, syncStatus);
|
||||
preparedUpdateStatement.setString(2, String.valueOf(System.currentTimeMillis()));
|
||||
preparedUpdateStatement.setString(3, player.getUniqueId().toString());
|
||||
|
||||
preparedUpdateStatement.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
Eco.log.warning("Error: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (preparedUpdateStatement != null) {
|
||||
preparedUpdateStatement.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setData(Player player, Double money, String syncComplete) {
|
||||
if (!hasAccount(player)) {
|
||||
createAccount(player);
|
||||
}
|
||||
PreparedStatement preparedUpdateStatement = null;
|
||||
Connection conn = eco.getMysqlSetup().getConnection();
|
||||
if (conn != null) {
|
||||
try {
|
||||
String data = "UPDATE `" + eco.getConfigHandler().getString("database.mysql.dataTableName") + "` " + "SET `player_name` = ?" + ", `money` = ?" + ", `sync_complete` = ?" + ", `last_seen` = ?" + " WHERE `player_uuid` = ?";
|
||||
preparedUpdateStatement = conn.prepareStatement(data);
|
||||
preparedUpdateStatement.setString(1, player.getName());
|
||||
preparedUpdateStatement.setDouble(2, money);
|
||||
preparedUpdateStatement.setString(3, syncComplete);
|
||||
preparedUpdateStatement.setString(4, String.valueOf(System.currentTimeMillis()));
|
||||
preparedUpdateStatement.setString(5, player.getUniqueId().toString());
|
||||
|
||||
preparedUpdateStatement.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
Eco.log.warning("Error: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (preparedUpdateStatement != null) {
|
||||
preparedUpdateStatement.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void createAccount(Player player) {
|
||||
PreparedStatement preparedStatement = null;
|
||||
Connection conn = eco.getMysqlSetup().getConnection();
|
||||
if (conn != null) {
|
||||
try {
|
||||
String sql = "INSERT INTO `" + eco.getConfigHandler().getString("database.mysql.dataTableName") + "`(`player_uuid`, `player_name`, `money`, `last_seen`, `sync_complete`) " + "VALUES(?, ?, ?, ?, ?)";
|
||||
preparedStatement = conn.prepareStatement(sql);
|
||||
|
||||
preparedStatement.setString(1, player.getUniqueId().toString());
|
||||
preparedStatement.setString(2, player.getName());
|
||||
preparedStatement.setDouble(3, 0.0);
|
||||
preparedStatement.setString(4, String.valueOf(System.currentTimeMillis()));
|
||||
preparedStatement.setString(5, "true");
|
||||
|
||||
preparedStatement.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
Eco.log.warning("Error: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (preparedStatement != null) {
|
||||
preparedStatement.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasAccount(Player player) {
|
||||
PreparedStatement preparedUpdateStatement = null;
|
||||
ResultSet result = null;
|
||||
Connection conn = eco.getMysqlSetup().getConnection();
|
||||
if (conn != null) {
|
||||
try {
|
||||
String sql = "SELECT `player_uuid` FROM `" + eco.getConfigHandler().getString("database.mysql.dataTableName") + "` WHERE `player_uuid` = ? LIMIT 1";
|
||||
preparedUpdateStatement = conn.prepareStatement(sql);
|
||||
preparedUpdateStatement.setString(1, player.getUniqueId().toString());
|
||||
|
||||
result = preparedUpdateStatement.executeQuery();
|
||||
while (result.next()) {
|
||||
return true;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
Eco.log.warning("Error: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (result != null) {
|
||||
result.close();
|
||||
}
|
||||
if (preparedUpdateStatement != null) {
|
||||
preparedUpdateStatement.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
208
src/main/java/craftersland/eco/bridge/database/MysqlSetup.java
Normal file
208
src/main/java/craftersland/eco/bridge/database/MysqlSetup.java
Normal file
@@ -0,0 +1,208 @@
|
||||
package net.craftersland.eco.bridge.database;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import net.craftersland.eco.bridge.Eco;
|
||||
|
||||
public class MysqlSetup {
|
||||
|
||||
private Connection conn = null;
|
||||
private Eco eco;
|
||||
|
||||
public MysqlSetup(Eco eco) {
|
||||
this.eco = eco;
|
||||
connectToDatabase();
|
||||
setupDatabase();
|
||||
updateTables();
|
||||
databaseMaintenanceTask();
|
||||
}
|
||||
|
||||
public void connectToDatabase() {
|
||||
Eco.log.info("Connecting to the database...");
|
||||
try {
|
||||
//Load Drivers
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("user", eco.getConfigHandler().getString("database.mysql.user"));
|
||||
properties.setProperty("password", eco.getConfigHandler().getString("database.mysql.password"));
|
||||
properties.setProperty("autoReconnect", "true");
|
||||
properties.setProperty("verifyServerCertificate", "false");
|
||||
properties.setProperty("useSSL", eco.getConfigHandler().getString("database.mysql.sslEnabled"));
|
||||
properties.setProperty("requireSSL", eco.getConfigHandler().getString("database.mysql.sslEnabled"));
|
||||
|
||||
//Connect to database
|
||||
conn = DriverManager.getConnection("jdbc:mysql://" + eco.getConfigHandler().getString("database.mysql.host") + ":" + eco.getConfigHandler().getString("database.mysql.port") + "/" + eco.getConfigHandler().getString("database.mysql.databaseName"), properties);
|
||||
|
||||
} catch (ClassNotFoundException e) {
|
||||
Eco.log.severe("Could not locate drivers for mysql! Error: " + e.getMessage());
|
||||
return;
|
||||
} catch (SQLException e) {
|
||||
Eco.log.severe("Could not connect to mysql database! Error: " + e.getMessage());
|
||||
return;
|
||||
}
|
||||
Eco.log.info("Database connection successful!");
|
||||
}
|
||||
|
||||
public void setupDatabase() {
|
||||
if (conn == null) return;
|
||||
//Create tables if needed
|
||||
PreparedStatement query = null;
|
||||
try {
|
||||
String data = "CREATE TABLE IF NOT EXISTS `" + eco.getConfigHandler().getString("database.mysql.dataTableName") + "` (id int(10) AUTO_INCREMENT, player_uuid varchar(50) NOT NULL UNIQUE, player_name varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, money double(30,2) NOT NULL, sync_complete varchar(5) NOT NULL, last_seen varchar(30) NOT NULL, PRIMARY KEY(id));";
|
||||
query = conn.prepareStatement(data);
|
||||
query.execute();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
Eco.log.severe("Error creating tables! Error: " + e.getMessage());
|
||||
} finally {
|
||||
try {
|
||||
if (query != null) {
|
||||
query.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Connection getConnection() {
|
||||
checkConnection();
|
||||
return conn;
|
||||
}
|
||||
|
||||
public void checkConnection() {
|
||||
try {
|
||||
if (conn == null) {
|
||||
Eco.log.warning("Connection failed. Reconnecting...");
|
||||
reConnect();
|
||||
}
|
||||
if (!conn.isValid(3)) {
|
||||
Eco.log.warning("Connection is idle or terminated. Reconnecting...");
|
||||
reConnect();
|
||||
}
|
||||
if (conn.isClosed() == true) {
|
||||
Eco.log.warning("Connection is closed. Reconnecting...");
|
||||
reConnect();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Eco.log.severe("Could not reconnect to Database! Error: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean reConnect() {
|
||||
try {
|
||||
long start = 0;
|
||||
long end = 0;
|
||||
|
||||
start = System.currentTimeMillis();
|
||||
Eco.log.info("Attempting to establish a connection to the MySQL server!");
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("user", eco.getConfigHandler().getString("database.mysql.user"));
|
||||
properties.setProperty("password", eco.getConfigHandler().getString("database.mysql.password"));
|
||||
properties.setProperty("autoReconnect", "true");
|
||||
properties.setProperty("verifyServerCertificate", "false");
|
||||
properties.setProperty("useSSL", eco.getConfigHandler().getString("database.mysql.sslEnabled"));
|
||||
properties.setProperty("requireSSL", eco.getConfigHandler().getString("database.mysql.sslEnabled"));
|
||||
//properties.setProperty("useUnicode", "true");
|
||||
//properties.setProperty("characterEncoding", "utf8");
|
||||
//properties.setProperty("characterSetResults", "utf8");
|
||||
//properties.setProperty("connectionCollation", "utf8mb4_unicode_ci");
|
||||
conn = DriverManager.getConnection("jdbc:mysql://" + eco.getConfigHandler().getString("database.mysql.host") + ":" + eco.getConfigHandler().getString("database.mysql.port") + "/" + eco.getConfigHandler().getString("database.mysql.databaseName"), properties);
|
||||
end = System.currentTimeMillis();
|
||||
Eco.log.info("Connection to MySQL server established in " + ((end - start)) + " ms!");
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
Eco.log.severe("Error re-connecting to the database! Error: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void closeConnection() {
|
||||
try {
|
||||
Eco.log.info("Closing database connection...");
|
||||
conn.close();
|
||||
conn = null;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void updateTables() {
|
||||
if (conn != null) {
|
||||
DatabaseMetaData md = null;
|
||||
ResultSet rs1 = null;
|
||||
PreparedStatement query1 = null;
|
||||
try {
|
||||
md = conn.getMetaData();
|
||||
rs1 = md.getColumns(null, null, eco.getConfigHandler().getString("database.mysql.dataTableName"), "sync_complete");
|
||||
if (rs1.next()) {
|
||||
|
||||
} else {
|
||||
String data = "ALTER TABLE `" + eco.getConfigHandler().getString("database.mysql.dataTableName") + "` ADD sync_complete varchar(5) NOT NULL DEFAULT 'true';";
|
||||
query1 = conn.prepareStatement(data);
|
||||
query1.execute();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (query1 != null) {
|
||||
query1.close();
|
||||
}
|
||||
if (rs1 != null) {
|
||||
rs1.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void databaseMaintenanceTask() {
|
||||
if (eco.getConfigHandler().getBoolean("database.removeOldAccounts.enabled") == true) {
|
||||
Bukkit.getScheduler().runTaskLaterAsynchronously(eco, new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (conn != null) {
|
||||
long inactivityDays = Long.parseLong(eco.getConfigHandler().getString("database.removeOldAccounts.inactivity"));
|
||||
long inactivityMils = inactivityDays * 24 * 60 * 60 * 1000;
|
||||
long curentTime = System.currentTimeMillis();
|
||||
long inactiveTime = curentTime - inactivityMils;
|
||||
Eco.log.info("Database maintenance task started...");
|
||||
PreparedStatement preparedStatement = null;
|
||||
try {
|
||||
String sql = "DELETE FROM `" + eco.getConfigHandler().getString("database.mysql.dataTableName") + "` WHERE `last_seen` < ?";
|
||||
preparedStatement = conn.prepareStatement(sql);
|
||||
preparedStatement.setString(1, String.valueOf(inactiveTime));
|
||||
preparedStatement.execute();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (preparedStatement != null) {
|
||||
preparedStatement.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
Eco.log.info("Database maintenance complete!");
|
||||
}
|
||||
}
|
||||
|
||||
}, 100 * 20L);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user