From 9b4333a643a4929bdc393136e44908601d48f207 Mon Sep 17 00:00:00 2001
From: ceze88 <70201650+ceze88@users.noreply.github.com>
Date: Sun, 14 Aug 2022 10:46:52 +0200
Subject: [PATCH] Remove deprecated methods, add MySQL support
---
pom.xml | 2 +-
.../songoda/epicfurnaces/EpicFurnaces.java | 27 +-
.../epicfurnaces/database/DataManager.java | 369 ++++++++++--------
.../epicfurnaces/settings/Settings.java | 9 +
4 files changed, 237 insertions(+), 170 deletions(-)
diff --git a/pom.xml b/pom.xml
index 22262b1..5ca18e5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -125,7 +125,7 @@
com.songoda
SongodaCore
- 2.6.13
+ 2.6.14-DEV
compile
diff --git a/src/main/java/com/songoda/epicfurnaces/EpicFurnaces.java b/src/main/java/com/songoda/epicfurnaces/EpicFurnaces.java
index 7b1a841..74be6a4 100644
--- a/src/main/java/com/songoda/epicfurnaces/EpicFurnaces.java
+++ b/src/main/java/com/songoda/epicfurnaces/EpicFurnaces.java
@@ -7,6 +7,7 @@ import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.configuration.Config;
import com.songoda.core.database.DataMigrationManager;
import com.songoda.core.database.DatabaseConnector;
+import com.songoda.core.database.MySQLConnector;
import com.songoda.core.database.SQLiteConnector;
import com.songoda.core.gui.GuiManager;
import com.songoda.core.hooks.EconomyManager;
@@ -141,8 +142,26 @@ public class EpicFurnaces extends SongodaPlugin {
this.blacklistHandler = new BlacklistHandler();
// Database stuff.
- this.databaseConnector = new SQLiteConnector(this);
- this.getLogger().info("Data handler connected using SQLite.");
+ try {
+ if (Settings.MYSQL_ENABLED.getBoolean()) {
+ String hostname = Settings.MYSQL_HOSTNAME.getString();
+ int port = Settings.MYSQL_PORT.getInt();
+ String database = Settings.MYSQL_DATABASE.getString();
+ String username = Settings.MYSQL_USERNAME.getString();
+ String password = Settings.MYSQL_PASSWORD.getString();
+ boolean useSSL = Settings.MYSQL_USE_SSL.getBoolean();
+ int poolSize = Settings.MYSQL_POOL_SIZE.getInt();
+
+ this.databaseConnector = new MySQLConnector(this, hostname, port, database, username, password, useSSL, poolSize);
+ this.getLogger().info("Data handler connected using MySQL.");
+ } else {
+ this.databaseConnector = new SQLiteConnector(this);
+ this.getLogger().info("Data handler connected using SQLite.");
+ }
+ } catch (Exception ex) {
+ this.getLogger().severe("Fatal error trying to connect to database. Please make sure all your connection settings are correct and try again. Plugin has been disabled.");
+ this.emergencyStop();
+ }
this.dataManager = new DataManager(this.databaseConnector, this);
DataMigrationManager dataMigrationManager = new DataMigrationManager(this.databaseConnector, this.dataManager,
@@ -218,7 +237,7 @@ public class EpicFurnaces extends SongodaPlugin {
}
final boolean finalConverted = converted;
- dataManager.queueAsync(() -> {
+ dataManager.runAsync(() -> {
if (finalConverted) {
console.sendMessage("[" + getDescription().getName() + "] " + ChatColor.GREEN + "Conversion complete :)");
}
@@ -227,7 +246,7 @@ public class EpicFurnaces extends SongodaPlugin {
this.furnaceManager.addFurnaces(furnaces.values());
this.dataManager.getBoosts((boosts) -> this.boostManager.addBoosts(boosts));
});
- }, "create");
+ });
});
setupRecipies();
diff --git a/src/main/java/com/songoda/epicfurnaces/database/DataManager.java b/src/main/java/com/songoda/epicfurnaces/database/DataManager.java
index 085e103..1fa0ff1 100644
--- a/src/main/java/com/songoda/epicfurnaces/database/DataManager.java
+++ b/src/main/java/com/songoda/epicfurnaces/database/DataManager.java
@@ -12,6 +12,7 @@ import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.plugin.Plugin;
+import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
@@ -63,22 +64,26 @@ public class DataManager extends DataManagerAbstract {
}
public void createBoost(BoostData boostData) {
- this.runAsync(() -> this.databaseConnector.connect(connection -> {
- String createBoostedPlayer = "INSERT INTO " + this.getTablePrefix() + "boosted_players (player, multiplier, end_time) VALUES (?, ?, ?)";
- try (PreparedStatement statement = connection.prepareStatement(createBoostedPlayer)) {
+ this.runAsync(() -> {
+ try (Connection connection = this.databaseConnector.getConnection()) {
+ String createBoostedPlayer = "INSERT INTO " + this.getTablePrefix() + "boosted_players (player, multiplier, end_time) VALUES (?, ?, ?)";
+ PreparedStatement statement = connection.prepareStatement(createBoostedPlayer);
statement.setString(1, boostData.getPlayer().toString());
statement.setInt(2, boostData.getMultiplier());
statement.setLong(3, boostData.getEndTime());
statement.executeUpdate();
+ } catch (Exception ex) {
+ ex.printStackTrace();
}
- }));
+ });
}
public void getBoosts(Consumer> callback) {
- this.runAsync(() -> this.databaseConnector.connect(connection -> {
- List boosts = new ArrayList<>();
+ this.runAsync(() -> {
+ try (Connection connection = this.databaseConnector.getConnection()) {
+ List boosts = new ArrayList<>();
- try (Statement statement = connection.createStatement()) {
+ Statement statement = connection.createStatement();
String selectBoostedPlayers = "SELECT * FROM " + this.getTablePrefix() + "boosted_players";
ResultSet result = statement.executeQuery(selectBoostedPlayers);
while (result.next()) {
@@ -87,21 +92,26 @@ public class DataManager extends DataManagerAbstract {
long endTime = result.getLong("end_time");
boosts.add(new BoostData(multiplier, endTime, player));
}
- }
- this.sync(() -> callback.accept(boosts));
- }));
+ this.sync(() -> callback.accept(boosts));
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
+ });
}
public void deleteBoost(BoostData boostData) {
- this.runAsync(() -> this.databaseConnector.connect(connection -> {
- String deleteBoost = "DELETE FROM " + this.getTablePrefix() + "boosted_players WHERE end_time = ?";
+ this.runAsync(() -> {
+ try (Connection connection = this.databaseConnector.getConnection()) {
+ String deleteBoost = "DELETE FROM " + this.getTablePrefix() + "boosted_players WHERE end_time = ?";
- try (PreparedStatement statement = connection.prepareStatement(deleteBoost)) {
+ PreparedStatement statement = connection.prepareStatement(deleteBoost);
statement.setLong(1, boostData.getEndTime());
statement.executeUpdate();
+ } catch (Exception ex) {
+ ex.printStackTrace();
}
- }));
+ });
}
public void createFurnaces(List furnaces) {
@@ -111,53 +121,58 @@ public class DataManager extends DataManagerAbstract {
}
public void createFurnace(Furnace furnace) {
- this.queueAsync(() -> this.databaseConnector.connect(connection -> {
- String createFurnace = "INSERT INTO " + this.getTablePrefix() + "active_furnaces (level, uses, nickname, placed_by, world, x, y, z) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
- try (PreparedStatement statement = connection.prepareStatement(createFurnace)) {
- statement.setInt(1, furnace.getLevel().getLevel());
- statement.setInt(2, furnace.getUses());
- statement.setString(3, furnace.getNickname());
- statement.setString(4,
- furnace.getPlacedBy() == null ? null : furnace.getPlacedBy().toString());
+ this.runAsync(() -> {
+ try (Connection connection = this.databaseConnector.getConnection()) {
+ String createFurnace = "INSERT INTO " + this.getTablePrefix() + "active_furnaces (level, uses, nickname, placed_by, world, x, y, z) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
+ try (PreparedStatement statement = connection.prepareStatement(createFurnace)) {
+ statement.setInt(1, furnace.getLevel().getLevel());
+ statement.setInt(2, furnace.getUses());
+ statement.setString(3, furnace.getNickname());
+ statement.setString(4,
+ furnace.getPlacedBy() == null ? null : furnace.getPlacedBy().toString());
- statement.setString(5, furnace.getLocation().getWorld().getName());
- statement.setInt(6, furnace.getLocation().getBlockX());
- statement.setInt(7, furnace.getLocation().getBlockY());
- statement.setInt(8, furnace.getLocation().getBlockZ());
+ statement.setString(5, furnace.getLocation().getWorld().getName());
+ statement.setInt(6, furnace.getLocation().getBlockX());
+ statement.setInt(7, furnace.getLocation().getBlockY());
+ statement.setInt(8, furnace.getLocation().getBlockZ());
- statement.executeUpdate();
- }
-
- int furnaceId = this.lastInsertedId(connection, "active_furnaces");
- furnace.setId(furnaceId);
-
- String createAccessList = "INSERT INTO " + this.getTablePrefix() + "access_list (furnace_id, uuid) VALUES (?, ?)";
- try (PreparedStatement statement = connection.prepareStatement(createAccessList)) {
- for (UUID uuid : furnace.getAccessList()) {
- statement.setInt(1, furnace.getId());
- statement.setString(2, uuid.toString());
- statement.addBatch();
+ statement.executeUpdate();
}
- statement.executeBatch();
- }
- String createNewLevel = "INSERT INTO " + this.getTablePrefix() + "to_level_new (furnace_id, item, amount) VALUES (?, ?, ?)";
- try (PreparedStatement statement = connection.prepareStatement(createNewLevel)) {
- for (Map.Entry entry : furnace.getToLevel().entrySet()) {
- statement.setInt(1, furnace.getId());
- statement.setString(2, entry.getKey().name());
- statement.setInt(3, entry.getValue());
- statement.addBatch();
+ int furnaceId = this.lastInsertedId(connection, "active_furnaces");
+ furnace.setId(furnaceId);
+
+ String createAccessList = "INSERT INTO " + this.getTablePrefix() + "access_list (furnace_id, uuid) VALUES (?, ?)";
+ try (PreparedStatement statement = connection.prepareStatement(createAccessList)) {
+ for (UUID uuid : furnace.getAccessList()) {
+ statement.setInt(1, furnace.getId());
+ statement.setString(2, uuid.toString());
+ statement.addBatch();
+ }
+ statement.executeBatch();
}
- statement.executeBatch();
+
+ String createNewLevel = "INSERT INTO " + this.getTablePrefix() + "to_level_new (furnace_id, item, amount) VALUES (?, ?, ?)";
+ try (PreparedStatement statement = connection.prepareStatement(createNewLevel)) {
+ for (Map.Entry entry : furnace.getToLevel().entrySet()) {
+ statement.setInt(1, furnace.getId());
+ statement.setString(2, entry.getKey().name());
+ statement.setInt(3, entry.getValue());
+ statement.addBatch();
+ }
+ statement.executeBatch();
+ }
+ } catch (Exception ex) {
+ ex.printStackTrace();
}
- }), "create");
+ });
}
public void updateFurnaces(Collection furnaces) {
- this.runAsync(() -> this.databaseConnector.connect(connection -> {
- String updateFurnace = "UPDATE " + this.getTablePrefix() + "active_furnaces SET level =?, nickname =?, uses =? WHERE id =?;";
- try (PreparedStatement statement = connection.prepareStatement(updateFurnace)) {
+ this.runAsync(() -> {
+ try (Connection connection = this.databaseConnector.getConnection()) {
+ String updateFurnace = "UPDATE " + this.getTablePrefix() + "active_furnaces SET level =?, nickname =?, uses =? WHERE id =?;";
+ PreparedStatement statement = connection.prepareStatement(updateFurnace);
for (Furnace furnace : furnaces) {
statement.setInt(1, furnace.getLevel().getLevel());
statement.setString(2, furnace.getNickname());
@@ -168,170 +183,194 @@ public class DataManager extends DataManagerAbstract {
}
statement.executeBatch();
+ } catch (Exception ex) {
+ ex.printStackTrace();
}
- }));
+ });
}
public void deleteFurnace(Furnace furnace) {
dequeueFurnaceForUpdate(furnace);
- this.runAsync(() -> this.databaseConnector.connect(connection -> {
- String deleteFurnace = "DELETE FROM " + this.getTablePrefix() + "active_furnaces WHERE id = ?";
- try (PreparedStatement statement = connection.prepareStatement(deleteFurnace)) {
- statement.setInt(1, furnace.getId());
- statement.executeUpdate();
- }
+ this.runAsync(() -> {
+ try (Connection connection = this.databaseConnector.getConnection()) {
+ String deleteFurnace = "DELETE FROM " + this.getTablePrefix() + "active_furnaces WHERE id = ?";
+ try (PreparedStatement statement = connection.prepareStatement(deleteFurnace)) {
+ statement.setInt(1, furnace.getId());
+ statement.executeUpdate();
+ }
- String deleteAccessList = "DELETE FROM " + this.getTablePrefix() + "access_list WHERE furnace_id = ?";
- try (PreparedStatement statement = connection.prepareStatement(deleteAccessList)) {
- statement.setInt(1, furnace.getId());
- statement.executeUpdate();
- }
+ String deleteAccessList = "DELETE FROM " + this.getTablePrefix() + "access_list WHERE furnace_id = ?";
+ try (PreparedStatement statement = connection.prepareStatement(deleteAccessList)) {
+ statement.setInt(1, furnace.getId());
+ statement.executeUpdate();
+ }
- String deleteLevelupItems = "DELETE FROM " + this.getTablePrefix() + "to_level_new WHERE furnace_id = ?";
- try (PreparedStatement statement = connection.prepareStatement(deleteLevelupItems)) {
- statement.setInt(1, furnace.getId());
- statement.executeUpdate();
+ String deleteLevelupItems = "DELETE FROM " + this.getTablePrefix() + "to_level_new WHERE furnace_id = ?";
+ try (PreparedStatement statement = connection.prepareStatement(deleteLevelupItems)) {
+ statement.setInt(1, furnace.getId());
+ statement.executeUpdate();
+ }
+ } catch (Exception ex) {
+ ex.printStackTrace();
}
- }));
+ });
}
public void createAccessPlayer(Furnace furnace, UUID uuid) {
- this.runAsync(() -> this.databaseConnector.connect(connection -> {
- String createAccessPlayer = "INSERT INTO " + this.getTablePrefix() + "access_list (furnace_id, uuid) VALUES (?, ?)";
- try (PreparedStatement statement = connection.prepareStatement(createAccessPlayer)) {
+ this.runAsync(() -> {
+ try (Connection connection = this.databaseConnector.getConnection()) {
+ String createAccessPlayer = "INSERT INTO " + this.getTablePrefix() + "access_list (furnace_id, uuid) VALUES (?, ?)";
+ PreparedStatement statement = connection.prepareStatement(createAccessPlayer);
statement.setInt(1, furnace.getId());
statement.setString(2, uuid.toString());
statement.executeUpdate();
+ } catch (Exception ex) {
+ ex.printStackTrace();
}
- }));
+ });
}
// These will be used in the future when the access list gets revamped.
// Probably by me since I already have a custom version in my server.
public void deleteAccessPlayer(Furnace furnace, UUID uuid) {
- this.runAsync(() -> this.databaseConnector.connect(connection -> {
- String deleteAccessPlayer = "DELETE FROM " + this.getTablePrefix() + "access_list WHERE furnace_id = ? AND uuid = ?";
- try (PreparedStatement statement = connection.prepareStatement(deleteAccessPlayer)) {
+ this.runAsync(() -> {
+ try (Connection connection = this.databaseConnector.getConnection()) {
+ String deleteAccessPlayer = "DELETE FROM " + this.getTablePrefix() + "access_list WHERE furnace_id = ? AND uuid = ?";
+ PreparedStatement statement = connection.prepareStatement(deleteAccessPlayer);
statement.setInt(1, furnace.getId());
statement.setString(2, uuid.toString());
statement.executeUpdate();
+ } catch (Exception ex) {
+ ex.printStackTrace();
}
- }));
+ });
}
public void updateAccessPlayers(Furnace furnace) {
- this.runAsync(() -> this.databaseConnector.connect(connection -> {
- String deletePlayers = "DELETE FROM " + this.getTablePrefix() + "access_list WHERE furnace_id = ?";
- try (PreparedStatement statement = connection.prepareStatement(deletePlayers)) {
- statement.setInt(1, furnace.getId());
- statement.executeUpdate();
- }
-
- String createAccessPlayer = "INSERT INTO " + this.getTablePrefix() + "access_list (furnace_id, uuid) VALUES (?, ?)";
- try (PreparedStatement statement = connection.prepareStatement(createAccessPlayer)) {
- for (UUID uuid : furnace.getAccessList()) {
+ this.runAsync(() -> {
+ try (Connection connection = this.databaseConnector.getConnection()) {
+ String deletePlayers = "DELETE FROM " + this.getTablePrefix() + "access_list WHERE furnace_id = ?";
+ try (PreparedStatement statement = connection.prepareStatement(deletePlayers)) {
statement.setInt(1, furnace.getId());
- statement.setString(2, uuid.toString());
- statement.addBatch();
+ statement.executeUpdate();
}
- statement.executeBatch();
+
+ String createAccessPlayer = "INSERT INTO " + this.getTablePrefix() + "access_list (furnace_id, uuid) VALUES (?, ?)";
+ try (PreparedStatement statement = connection.prepareStatement(createAccessPlayer)) {
+ for (UUID uuid : furnace.getAccessList()) {
+ statement.setInt(1, furnace.getId());
+ statement.setString(2, uuid.toString());
+ statement.addBatch();
+ }
+ statement.executeBatch();
+ }
+ } catch (Exception ex) {
+ ex.printStackTrace();
}
- }));
+ });
}
public void updateLevelupItems(Furnace furnace, CompatibleMaterial material, int amount) {
- this.runAsync(() -> this.databaseConnector.connect(connection -> {
- String deleteLevelupItem = "DELETE FROM " + this.getTablePrefix() + "to_level_new WHERE furnace_id = ? AND item = ?";
- try (PreparedStatement statement = connection.prepareStatement(deleteLevelupItem)) {
- statement.setInt(1, furnace.getId());
- statement.setString(2, material.name());
- statement.executeUpdate();
- }
+ this.runAsync(() -> {
+ try (Connection connection = this.databaseConnector.getConnection()) {
+ String deleteLevelupItem = "DELETE FROM " + this.getTablePrefix() + "to_level_new WHERE furnace_id = ? AND item = ?";
+ try (PreparedStatement statement = connection.prepareStatement(deleteLevelupItem)) {
+ statement.setInt(1, furnace.getId());
+ statement.setString(2, material.name());
+ statement.executeUpdate();
+ }
- String createLevelupItem = "INSERT INTO " + this.getTablePrefix() + "to_level_new (furnace_id, item, amount) VALUES (?, ?, ?)";
- try (PreparedStatement statement = connection.prepareStatement(createLevelupItem)) {
- statement.setInt(1, furnace.getId());
- statement.setString(2, material.name());
- statement.setInt(3, amount);
- statement.executeUpdate();
+ String createLevelupItem = "INSERT INTO " + this.getTablePrefix() + "to_level_new (furnace_id, item, amount) VALUES (?, ?, ?)";
+ try (PreparedStatement statement = connection.prepareStatement(createLevelupItem)) {
+ statement.setInt(1, furnace.getId());
+ statement.setString(2, material.name());
+ statement.setInt(3, amount);
+ statement.executeUpdate();
+ }
+ } catch (Exception ex) {
+ ex.printStackTrace();
}
- }));
+ });
}
public void getFurnaces(Consumer