From 75dc4ad1036d42bf16a4f523dec158b96a1af892 Mon Sep 17 00:00:00 2001
From: ceze88 <70201650+ceze88@users.noreply.github.com>
Date: Sun, 14 Aug 2022 17:56:50 +0200
Subject: [PATCH 1/4] Remove deprecated methods, add MySQL support
---
pom.xml | 2 +-
.../com/songoda/epicfarming/EpicFarming.java | 28 ++-
.../epicfarming/database/DataManager.java | 230 ++++++++++--------
.../epicfarming/settings/Settings.java | 9 +
4 files changed, 163 insertions(+), 106 deletions(-)
diff --git a/pom.xml b/pom.xml
index 68e167c..2475a4f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -124,7 +124,7 @@
com.songoda
SongodaCore
- 2.6.13
+ 2.6.14-DEV
compile
diff --git a/src/main/java/com/songoda/epicfarming/EpicFarming.java b/src/main/java/com/songoda/epicfarming/EpicFarming.java
index e156162..6f19137 100644
--- a/src/main/java/com/songoda/epicfarming/EpicFarming.java
+++ b/src/main/java/com/songoda/epicfarming/EpicFarming.java
@@ -8,6 +8,7 @@ import com.songoda.core.compatibility.ServerVersion;
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;
@@ -136,8 +137,27 @@ public class EpicFarming extends SongodaPlugin {
new CommandSettings(this)
);
- this.databaseConnector = new SQLiteConnector(this);
- this.getLogger().info("Data handler connected using SQLite.");
+ // Database stuff.
+ 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,
@@ -252,7 +272,7 @@ public class EpicFarming extends SongodaPlugin {
}
final boolean finalConverted = converted;
- dataManager.queueAsync(() -> {
+ dataManager.runAsync(() -> {
if (finalConverted) {
console.sendMessage("[" + getDescription().getName() + "] " + ChatColor.GREEN + "Conversion complete :)");
}
@@ -261,7 +281,7 @@ public class EpicFarming extends SongodaPlugin {
this.farmManager.addFarms(farms.values());
this.dataManager.getBoosts((boosts) -> this.boostManager.addBoosts(boosts));
});
- }, "create");
+ });
});
}
diff --git a/src/main/java/com/songoda/epicfarming/database/DataManager.java b/src/main/java/com/songoda/epicfarming/database/DataManager.java
index 350e3ec..d719c81 100644
--- a/src/main/java/com/songoda/epicfarming/database/DataManager.java
+++ b/src/main/java/com/songoda/epicfarming/database/DataManager.java
@@ -13,6 +13,7 @@ import org.bukkit.World;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
+import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
@@ -30,21 +31,26 @@ public class DataManager extends DataManagerAbstract {
}
public void createBoost(BoostData boostData) {
- this.async(() -> this.databaseConnector.connect(connection -> {
- String createBoostedPlayer = "INSERT INTO " + this.getTablePrefix() + "boosted_players (player, multiplier, end_time) VALUES (?, ?, ?)";
- try (PreparedStatement statement = connection.prepareStatement(createBoostedPlayer)) {
- statement.setString(1, boostData.getPlayer().toString());
- statement.setInt(2, boostData.getMultiplier());
- statement.setLong(3, boostData.getEndTime());
- statement.executeUpdate();
+ this.runAsync(() -> {
+ try (Connection connection = this.databaseConnector.getConnection()) {
+ String createBoostedPlayer = "INSERT INTO " + this.getTablePrefix() + "boosted_players (player, multiplier, end_time) VALUES (?, ?, ?)";
+ try (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) {
List boosts = new ArrayList<>();
- this.async(() -> this.databaseConnector.connect(connection -> {
- try (Statement statement = connection.createStatement()) {
+ this.runAsync(() -> {
+ try (Connection connection = this.databaseConnector.getConnection()) {
+ Statement statement = connection.createStatement();
String selectBoostedPlayers = "SELECT * FROM " + this.getTablePrefix() + "boosted_players";
ResultSet result = statement.executeQuery(selectBoostedPlayers);
while (result.next()) {
@@ -53,50 +59,59 @@ 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.async(() -> this.databaseConnector.connect(connection -> {
- String deleteBoost = "DELETE FROM " + this.getTablePrefix() + "boosted_players WHERE end_time = ?";
- try (PreparedStatement statement = connection.prepareStatement(deleteBoost)) {
+ this.runAsync(() -> {
+ try (Connection connection = this.databaseConnector.getConnection()) {
+ String deleteBoost = "DELETE FROM " + this.getTablePrefix() + "boosted_players WHERE end_time = ?";
+ PreparedStatement statement = connection.prepareStatement(deleteBoost);
statement.setLong(1, boostData.getEndTime());
statement.executeUpdate();
+ } catch (Exception ex) {
+ ex.printStackTrace();
}
- }));
+ });
}
public void createFarm(Farm farm) {
- this.queueAsync(() -> this.databaseConnector.connect(connection -> {
- String createFarm = "INSERT INTO " + this.getTablePrefix() + "active_farms (farm_type, level, placed_by, world, x, y, z) VALUES (?, ?, ?, ?, ?, ?, ?)";
- try (PreparedStatement statement = connection.prepareStatement(createFarm)) {
- statement.setString(1, farm.getFarmType().name());
- statement.setInt(2, farm.getLevel().getLevel());
- statement.setString(3, farm.getPlacedBy().toString());
- statement.setString(4, farm.getLocation().getWorld().getName());
- statement.setInt(5, farm.getLocation().getBlockX());
- statement.setInt(6, farm.getLocation().getBlockY());
- statement.setInt(7, farm.getLocation().getBlockZ());
+ this.runAsync(() -> {
+ try (Connection connection = this.databaseConnector.getConnection()) {
+ String createFarm = "INSERT INTO " + this.getTablePrefix() + "active_farms (farm_type, level, placed_by, world, x, y, z) VALUES (?, ?, ?, ?, ?, ?, ?)";
+ try (PreparedStatement statement = connection.prepareStatement(createFarm)) {
+ statement.setString(1, farm.getFarmType().name());
+ statement.setInt(2, farm.getLevel().getLevel());
+ statement.setString(3, farm.getPlacedBy().toString());
+ statement.setString(4, farm.getLocation().getWorld().getName());
+ statement.setInt(5, farm.getLocation().getBlockX());
+ statement.setInt(6, farm.getLocation().getBlockY());
+ statement.setInt(7, farm.getLocation().getBlockZ());
- statement.executeUpdate();
- }
-
- int farmId = this.lastInsertedId(connection, "active_farms");
- farm.setId(farmId);
-
- String createItem = "INSERT INTO " + this.getTablePrefix() + "items (farm_id, item) VALUES (?, ?)";
- try (PreparedStatement statement = connection.prepareStatement(createItem)) {
- for (ItemStack item : farm.getItems().toArray(new ItemStack[0])) {
- statement.setInt(1, farm.getId());
- statement.setBytes(2, ItemSerializer.serializeItem(item));
- statement.addBatch();
+ statement.executeUpdate();
}
- statement.executeBatch();
+
+ int farmId = this.lastInsertedId(connection, "active_farms");
+ farm.setId(farmId);
+
+ String createItem = "INSERT INTO " + this.getTablePrefix() + "items (farm_id, item) VALUES (?, ?)";
+ try (PreparedStatement statement = connection.prepareStatement(createItem)) {
+ for (ItemStack item : farm.getItems().toArray(new ItemStack[0])) {
+ statement.setInt(1, farm.getId());
+ statement.setBytes(2, ItemSerializer.serializeItem(item));
+ statement.addBatch();
+ }
+ statement.executeBatch();
+ }
+ } catch (Exception ex) {
+ ex.printStackTrace();
}
- }), "create");
+ });
}
public void createFarms(List farms) {
@@ -106,39 +121,46 @@ public class DataManager extends DataManagerAbstract {
}
public void updateFarm(Farm farm) {
- this.async(() -> this.databaseConnector.connect(connection -> {
- String updateFarm = "UPDATE " + this.getTablePrefix() + "active_farms SET level = ?, farm_type = ? WHERE id = ?";
- try (PreparedStatement statement = connection.prepareStatement(updateFarm)) {
+ this.runAsync(() -> {
+ try (Connection connection = this.databaseConnector.getConnection()) {
+ String updateFarm = "UPDATE " + this.getTablePrefix() + "active_farms SET level = ?, farm_type = ? WHERE id = ?";
+ PreparedStatement statement = connection.prepareStatement(updateFarm);
statement.setInt(1, farm.getLevel().getLevel());
statement.setString(2, farm.getFarmType().name());
statement.setInt(3, farm.getId());
statement.executeUpdate();
+ } catch (Exception ex) {
+ ex.printStackTrace();
}
- }));
+ });
}
public void deleteFarm(Farm farm) {
- this.async(() -> this.databaseConnector.connect(connection -> {
- String deleteFarm = "DELETE FROM " + this.getTablePrefix() + "active_farms WHERE id = ?";
- try (PreparedStatement statement = connection.prepareStatement(deleteFarm)) {
- statement.setInt(1, farm.getId());
- statement.executeUpdate();
- }
+ this.runAsync(() -> {
+ try (Connection connection = this.databaseConnector.getConnection()) {
+ String deleteFarm = "DELETE FROM " + this.getTablePrefix() + "active_farms WHERE id = ?";
+ try (PreparedStatement statement = connection.prepareStatement(deleteFarm)) {
+ statement.setInt(1, farm.getId());
+ statement.executeUpdate();
+ }
- String deleteItems = "DELETE FROM " + this.getTablePrefix() + "items WHERE farm_id = ?";
- try (PreparedStatement statement = connection.prepareStatement(deleteItems)) {
- statement.setInt(1, farm.getId());
- statement.executeUpdate();
+ String deleteItems = "DELETE FROM " + this.getTablePrefix() + "items WHERE farm_id = ?";
+ try (PreparedStatement statement = connection.prepareStatement(deleteItems)) {
+ statement.setInt(1, farm.getId());
+ statement.executeUpdate();
+ }
+ } catch (Exception ex) {
+ ex.printStackTrace();
}
- }));
+ });
}
public void updateItemsAsync(Farm farm) {
- this.async(() -> updateItems(farm));
+ this.runAsync(() -> updateItems(farm));
}
public void updateItems(Farm farm) {
- this.databaseConnector.connect(connection -> {
+ try (Connection connection = this.databaseConnector.getConnection()) {
String deleteItems = "DELETE FROM " + this.getTablePrefix() + "items WHERE farm_id = ?";
try (PreparedStatement statement = connection.prepareStatement(deleteItems)) {
statement.setInt(1, farm.getId());
@@ -155,62 +177,68 @@ public class DataManager extends DataManagerAbstract {
}
statement.executeBatch();
}
- });
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
farm.setNeedsToBeSaved(false);
}
public void getFarms(Consumer