From 180bf7f6428b51ff090b9ef9893fae8ccc05e44e Mon Sep 17 00:00:00 2001
From: ceze88 <70201650+ceze88@users.noreply.github.com>
Date: Tue, 9 Aug 2022 15:26:16 +0200
Subject: [PATCH 1/4] Updated deprecated methods, added MySQL support
---
pom.xml | 2 +-
.../com/songoda/epichoppers/EpicHoppers.java | 23 +-
.../epichoppers/commands/CommandReload.java | 2 +-
.../epichoppers/database/DataManager.java | 448 ++++++++++--------
.../epichoppers/settings/Settings.java | 10 +
5 files changed, 275 insertions(+), 210 deletions(-)
diff --git a/pom.xml b/pom.xml
index ad8a6d1..8f92b2e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -133,7 +133,7 @@
com.songoda
SongodaCore
- 2.6.13
+ 2.6.14-DEV
compile
diff --git a/src/main/java/com/songoda/epichoppers/EpicHoppers.java b/src/main/java/com/songoda/epichoppers/EpicHoppers.java
index 0f98e6e..dae3cdb 100644
--- a/src/main/java/com/songoda/epichoppers/EpicHoppers.java
+++ b/src/main/java/com/songoda/epichoppers/EpicHoppers.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;
@@ -122,8 +123,26 @@ public class EpicHoppers extends SongodaPlugin {
this.boostManager = new BoostManager();
// Database stuff, go!
- 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,
diff --git a/src/main/java/com/songoda/epichoppers/commands/CommandReload.java b/src/main/java/com/songoda/epichoppers/commands/CommandReload.java
index e3888e8..2a30d9d 100644
--- a/src/main/java/com/songoda/epichoppers/commands/CommandReload.java
+++ b/src/main/java/com/songoda/epichoppers/commands/CommandReload.java
@@ -11,7 +11,7 @@ public class CommandReload extends AbstractCommand {
private final EpicHoppers plugin;
public CommandReload(EpicHoppers plugin) {
- super(false, "reload");
+ super(CommandType.CONSOLE_OK, "reload");
this.plugin = plugin;
}
diff --git a/src/main/java/com/songoda/epichoppers/database/DataManager.java b/src/main/java/com/songoda/epichoppers/database/DataManager.java
index ad1af16..921e81f 100644
--- a/src/main/java/com/songoda/epichoppers/database/DataManager.java
+++ b/src/main/java/com/songoda/epichoppers/database/DataManager.java
@@ -21,6 +21,7 @@ import org.bukkit.util.io.BukkitObjectOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
@@ -39,21 +40,25 @@ 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)) {
+ 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) {
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()) {
@@ -62,26 +67,32 @@ 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 createLink(Hopper hopper, Location location, LinkType type) {
- this.async(() -> this.databaseConnector.connect(connection -> {
- String createLink = "INSERT INTO " + this.getTablePrefix() + "links (hopper_id, link_type, world, x, y, z) VALUES (?, ?, ?, ?, ?, ?)";
- try (PreparedStatement statement = connection.prepareStatement(createLink)) {
+ this.runAsync(() -> {
+ try (Connection connection = this.databaseConnector.getConnection()){
+ String createLink = "INSERT INTO " + this.getTablePrefix() + "links (hopper_id, link_type, world, x, y, z) VALUES (?, ?, ?, ?, ?, ?)";
+ PreparedStatement statement = connection.prepareStatement(createLink);
statement.setInt(1, hopper.getId());
statement.setString(2, type.name());
@@ -91,62 +102,73 @@ public class DataManager extends DataManagerAbstract {
statement.setInt(5, location.getBlockY());
statement.setInt(6, location.getBlockZ());
statement.executeUpdate();
+ } catch (Exception ex) {
+ ex.printStackTrace();
}
- }));
+ });
}
public void updateItems(Hopper hopper, ItemType type, List items) {
- this.async(() -> this.databaseConnector.connect(connection -> {
-
- String clearItems = "DELETE FROM " + this.getTablePrefix() + "items WHERE hopper_id = ? AND item_type = ?";
- try (PreparedStatement statement = connection.prepareStatement(clearItems)) {
- statement.setInt(1, hopper.getId());
- statement.setString(2, type.name());
- statement.executeUpdate();
- }
-
- String createItem = "INSERT INTO " + this.getTablePrefix() + "items (hopper_id, item_type, item) VALUES (?, ?, ?)";
- try (PreparedStatement statement = connection.prepareStatement(createItem)) {
- for (ItemStack item : items) {
+ this.runAsync(() -> {
+ try (Connection connection = this.databaseConnector.getConnection()){
+ String clearItems = "DELETE FROM " + this.getTablePrefix() + "items WHERE hopper_id = ? AND item_type = ?";
+ try (PreparedStatement statement = connection.prepareStatement(clearItems)) {
statement.setInt(1, hopper.getId());
statement.setString(2, type.name());
-
- try (ByteArrayOutputStream stream = new ByteArrayOutputStream(); BukkitObjectOutputStream bukkitStream = new BukkitObjectOutputStream(stream)) {
- bukkitStream.writeObject(item);
- statement.setString(3, Base64.getEncoder().encodeToString(stream.toByteArray()));
- } catch (IOException e) {
- e.printStackTrace();
- continue;
- }
- statement.addBatch();
+ statement.executeUpdate();
}
- statement.executeBatch();
+
+ String createItem = "INSERT INTO " + this.getTablePrefix() + "items (hopper_id, item_type, item) VALUES (?, ?, ?)";
+ try (PreparedStatement statement = connection.prepareStatement(createItem)) {
+ for (ItemStack item : items) {
+ statement.setInt(1, hopper.getId());
+ statement.setString(2, type.name());
+
+ try (ByteArrayOutputStream stream = new ByteArrayOutputStream(); BukkitObjectOutputStream bukkitStream = new BukkitObjectOutputStream(stream)) {
+ bukkitStream.writeObject(item);
+ statement.setString(3, Base64.getEncoder().encodeToString(stream.toByteArray()));
+ } catch (IOException e) {
+ e.printStackTrace();
+ continue;
+ }
+ statement.addBatch();
+ }
+ statement.executeBatch();
+ }
+ } catch (Exception ex) {
+ ex.printStackTrace();
}
- }));
+ });
}
public void deleteLink(Hopper hopper, Location location) {
- this.async(() -> this.databaseConnector.connect(connection -> {
- String deleteLink = "DELETE FROM " + this.getTablePrefix() + "links WHERE hopper_id = ? AND world = ? AND x = ? AND y = ? AND z = ?";
- try (PreparedStatement statement = connection.prepareStatement(deleteLink)) {
+ this.runAsync(() -> {
+ try (Connection connection = this.databaseConnector.getConnection()){
+ String deleteLink = "DELETE FROM " + this.getTablePrefix() + "links WHERE hopper_id = ? AND world = ? AND x = ? AND y = ? AND z = ?";
+ PreparedStatement statement = connection.prepareStatement(deleteLink);
statement.setInt(1, hopper.getId());
statement.setString(2, location.getWorld().getName());
statement.setInt(3, location.getBlockX());
statement.setInt(4, location.getBlockY());
statement.setInt(5, location.getBlockZ());
statement.executeUpdate();
+ } catch (Exception ex) {
+ ex.printStackTrace();
}
- }));
+ });
}
public void deleteLinks(Hopper hopper) {
- this.async(() -> this.databaseConnector.connect(connection -> {
- String deleteHopperLinks = "DELETE FROM " + this.getTablePrefix() + "links WHERE hopper_id = ?";
- try (PreparedStatement statement = connection.prepareStatement(deleteHopperLinks)) {
+ this.runAsync(() -> {
+ try (Connection connection = this.databaseConnector.getConnection()){
+ String deleteHopperLinks = "DELETE FROM " + this.getTablePrefix() + "links WHERE hopper_id = ?";
+ PreparedStatement statement = connection.prepareStatement(deleteHopperLinks);
statement.setInt(1, hopper.getId());
statement.executeUpdate();
+ } catch (Exception ex) {
+ ex.printStackTrace();
}
- }));
+ });
}
public void createHoppers(List hoppers) {
@@ -155,217 +177,231 @@ public class DataManager extends DataManagerAbstract {
}
public void createHopper(Hopper hopper) {
- this.queueAsync(() -> this.databaseConnector.connect(connection -> {
- String createHopper = "INSERT INTO " + this.getTablePrefix() + "placed_hoppers (level, placed_by, last_opened_by, teleport_trigger, world, x, y, z) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
- try (PreparedStatement statement = connection.prepareStatement(createHopper)) {
- statement.setInt(1, hopper.getLevel().getLevel());
+ this.runAsync(() -> {
+ try (Connection connection = this.databaseConnector.getConnection()){
+ String createHopper = "INSERT INTO " + this.getTablePrefix() + "placed_hoppers (level, placed_by, last_opened_by, teleport_trigger, world, x, y, z) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
+ try (PreparedStatement statement = connection.prepareStatement(createHopper)) {
+ statement.setInt(1, hopper.getLevel().getLevel());
- statement.setString(2,
- hopper.getPlacedBy() == null ? null : hopper.getPlacedBy().toString());
+ statement.setString(2,
+ hopper.getPlacedBy() == null ? null : hopper.getPlacedBy().toString());
- statement.setString(3,
- hopper.getLastPlayerOpened() == null ? null : hopper.getLastPlayerOpened().toString());
+ statement.setString(3,
+ hopper.getLastPlayerOpened() == null ? null : hopper.getLastPlayerOpened().toString());
- statement.setString(4, hopper.getTeleportTrigger().name());
+ statement.setString(4, hopper.getTeleportTrigger().name());
- statement.setString(5, hopper.getWorld().getName());
- statement.setInt(6, hopper.getX());
- statement.setInt(7, hopper.getY());
- statement.setInt(8, hopper.getZ());
- statement.executeUpdate();
- }
+ statement.setString(5, hopper.getWorld().getName());
+ statement.setInt(6, hopper.getX());
+ statement.setInt(7, hopper.getY());
+ statement.setInt(8, hopper.getZ());
+ statement.executeUpdate();
+ }
- int hopperId = this.lastInsertedId(connection, "placed_hoppers");
- hopper.setId(hopperId);
+ int hopperId = this.lastInsertedId(connection, "placed_hoppers");
+ hopper.setId(hopperId);
- Map items = new HashMap<>();
- Filter filter = hopper.getFilter();
+ Map items = new HashMap<>();
+ Filter filter = hopper.getFilter();
- for (ItemStack item : filter.getWhiteList())
- items.put(item, ItemType.WHITELIST);
+ for (ItemStack item : filter.getWhiteList())
+ items.put(item, ItemType.WHITELIST);
- for (ItemStack item : filter.getBlackList())
- items.put(item, ItemType.BLACKLIST);
+ for (ItemStack item : filter.getBlackList())
+ items.put(item, ItemType.BLACKLIST);
- for (ItemStack item : filter.getVoidList())
- items.put(item, ItemType.VOID);
+ for (ItemStack item : filter.getVoidList())
+ items.put(item, ItemType.VOID);
- for (ItemStack item : filter.getAutoSellWhiteList())
- items.put(item, ItemType.AUTO_SELL_WHITELIST);
+ for (ItemStack item : filter.getAutoSellWhiteList())
+ items.put(item, ItemType.AUTO_SELL_WHITELIST);
- for (ItemStack item : filter.getAutoSellBlackList())
- items.put(item, ItemType.AUTO_SELL_BLACKLIST);
+ for (ItemStack item : filter.getAutoSellBlackList())
+ items.put(item, ItemType.AUTO_SELL_BLACKLIST);
- String createItem = "INSERT INTO " + this.getTablePrefix() + "items (hopper_id, item_type, item) VALUES (?, ?, ?)";
- try (PreparedStatement statement = connection.prepareStatement(createItem)) {
- for (Map.Entry entry : items.entrySet()) {
- statement.setInt(1, hopper.getId());
- statement.setString(2, entry.getValue().name());
+ String createItem = "INSERT INTO " + this.getTablePrefix() + "items (hopper_id, item_type, item) VALUES (?, ?, ?)";
+ try (PreparedStatement statement = connection.prepareStatement(createItem)) {
+ for (Map.Entry entry : items.entrySet()) {
+ statement.setInt(1, hopper.getId());
+ statement.setString(2, entry.getValue().name());
- try (ByteArrayOutputStream stream = new ByteArrayOutputStream(); BukkitObjectOutputStream bukkitStream = new BukkitObjectOutputStream(stream)) {
- bukkitStream.writeObject(entry.getKey());
- statement.setString(3, Base64.getEncoder().encodeToString(stream.toByteArray()));
- } catch (IOException e) {
- e.printStackTrace();
- continue;
+ try (ByteArrayOutputStream stream = new ByteArrayOutputStream(); BukkitObjectOutputStream bukkitStream = new BukkitObjectOutputStream(stream)) {
+ bukkitStream.writeObject(entry.getKey());
+ statement.setString(3, Base64.getEncoder().encodeToString(stream.toByteArray()));
+ } catch (IOException e) {
+ e.printStackTrace();
+ continue;
+ }
+ statement.addBatch();
}
- statement.addBatch();
+ statement.executeBatch();
}
- statement.executeBatch();
- }
- Map links = new HashMap<>();
+ Map links = new HashMap<>();
- for (Location location : hopper.getLinkedBlocks())
- links.put(location, LinkType.REGULAR);
+ for (Location location : hopper.getLinkedBlocks())
+ links.put(location, LinkType.REGULAR);
- if (filter.getEndPoint() != null)
- links.put(filter.getEndPoint(), LinkType.REJECT);
+ if (filter.getEndPoint() != null)
+ links.put(filter.getEndPoint(), LinkType.REJECT);
- String createLink = "INSERT INTO " + this.getTablePrefix() + "links (hopper_id, link_type, world, x, y, z) VALUES (?, ?, ?, ?, ?, ?)";
- try (PreparedStatement statement = connection.prepareStatement(createLink)) {
- for (Map.Entry entry : links.entrySet()) {
- statement.setInt(1, hopper.getId());
+ String createLink = "INSERT INTO " + this.getTablePrefix() + "links (hopper_id, link_type, world, x, y, z) VALUES (?, ?, ?, ?, ?, ?)";
+ try (PreparedStatement statement = connection.prepareStatement(createLink)) {
+ for (Map.Entry entry : links.entrySet()) {
+ statement.setInt(1, hopper.getId());
- statement.setString(2, entry.getValue().name());
+ statement.setString(2, entry.getValue().name());
- Location location = entry.getKey();
- statement.setString(3, location.getWorld().getName());
- statement.setInt(4, location.getBlockX());
- statement.setInt(5, location.getBlockY());
- statement.setInt(6, location.getBlockZ());
- statement.addBatch();
+ Location location = entry.getKey();
+ statement.setString(3, location.getWorld().getName());
+ statement.setInt(4, location.getBlockX());
+ statement.setInt(5, location.getBlockY());
+ statement.setInt(6, location.getBlockZ());
+ statement.addBatch();
+ }
+ statement.executeBatch();
}
- statement.executeBatch();
+ } catch (Exception ex) {
+ ex.printStackTrace();
}
- }), "create");
+ });
}
public void updateHopper(Hopper hopper) {
- this.async(() -> this.databaseConnector.connect(connection -> {
- String updateHopper = "UPDATE " + this.getTablePrefix() + "placed_hoppers SET level = ?, placed_by = ?, last_opened_by = ?, teleport_trigger = ? WHERE id = ?";
- try (PreparedStatement statement = connection.prepareStatement(updateHopper)) {
+ this.runAsync(() -> {
+ try (Connection connection = this.databaseConnector.getConnection()){
+ String updateHopper = "UPDATE " + this.getTablePrefix() + "placed_hoppers SET level = ?, placed_by = ?, last_opened_by = ?, teleport_trigger = ? WHERE id = ?";
+ PreparedStatement statement = connection.prepareStatement(updateHopper);
statement.setInt(1, hopper.getLevel().getLevel());
statement.setString(2, hopper.getPlacedBy().toString());
statement.setString(3, hopper.getLastPlayerOpened().toString());
statement.setString(4, hopper.getTeleportTrigger().name());
statement.setInt(5, hopper.getId());
statement.executeUpdate();
+ } catch (Exception ex) {
+ ex.printStackTrace();
}
- }));
+ });
}
public void deleteHopper(Hopper hopper) {
- this.async(() -> this.databaseConnector.connect(connection -> {
- String deleteHopper = "DELETE FROM " + this.getTablePrefix() + "placed_hoppers WHERE id = ?";
- try (PreparedStatement statement = connection.prepareStatement(deleteHopper)) {
- statement.setInt(1, hopper.getId());
- statement.executeUpdate();
- }
+ this.runAsync(() -> {
+ try (Connection connection = this.databaseConnector.getConnection()){
+ String deleteHopper = "DELETE FROM " + this.getTablePrefix() + "placed_hoppers WHERE id = ?";
+ try (PreparedStatement statement = connection.prepareStatement(deleteHopper)) {
+ statement.setInt(1, hopper.getId());
+ statement.executeUpdate();
+ }
- String deleteHopperLinks = "DELETE FROM " + this.getTablePrefix() + "links WHERE hopper_id = ?";
- try (PreparedStatement statement = connection.prepareStatement(deleteHopperLinks)) {
- statement.setInt(1, hopper.getId());
- statement.executeUpdate();
- }
+ String deleteHopperLinks = "DELETE FROM " + this.getTablePrefix() + "links WHERE hopper_id = ?";
+ try (PreparedStatement statement = connection.prepareStatement(deleteHopperLinks)) {
+ statement.setInt(1, hopper.getId());
+ statement.executeUpdate();
+ }
- String deleteItems = "DELETE FROM " + this.getTablePrefix() + "items WHERE hopper_id = ?";
- try (PreparedStatement statement = connection.prepareStatement(deleteItems)) {
- statement.setInt(1, hopper.getId());
- statement.executeUpdate();
+ String deleteItems = "DELETE FROM " + this.getTablePrefix() + "items WHERE hopper_id = ?";
+ try (PreparedStatement statement = connection.prepareStatement(deleteItems)) {
+ statement.setInt(1, hopper.getId());
+ statement.executeUpdate();
+ }
+ } catch (Exception ex) {
+ ex.printStackTrace();
}
- }));
+ });
}
public void getHoppers(Consumer