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> callback) { - this.runAsync(() -> this.databaseConnector.connect(connection -> { - Map furnaces = new HashMap<>(); + this.runAsync(() -> { + try (Connection connection = this.databaseConnector.getConnection()) { + Map furnaces = new HashMap<>(); - try (Statement statement = connection.createStatement()) { - String selectFurnaces = "SELECT * FROM " + this.getTablePrefix() + "active_furnaces"; - ResultSet result = statement.executeQuery(selectFurnaces); - while (result.next()) { - World world = Bukkit.getWorld(result.getString("world")); + try (Statement statement = connection.createStatement()) { + String selectFurnaces = "SELECT * FROM " + this.getTablePrefix() + "active_furnaces"; + ResultSet result = statement.executeQuery(selectFurnaces); + while (result.next()) { + World world = Bukkit.getWorld(result.getString("world")); - if (world == null) { - continue; + if (world == null) { + continue; + } + + int id = result.getInt("id"); + int level = result.getInt("level"); + int uses = result.getInt("uses"); + + String placedByStr = result.getString("placed_by"); + UUID placedBy = placedByStr == null ? null : UUID.fromString(result.getString("placed_by")); + + String nickname = result.getString("nickname"); + + int x = result.getInt("x"); + int y = result.getInt("y"); + int z = result.getInt("z"); + Location location = new Location(world, x, y, z); + + Furnace furnace = new FurnaceBuilder(location) + .setId(id) + .setLevel(EpicFurnaces.getInstance().getLevelManager().getLevel(level)) + .setUses(uses) + .setPlacedBy(placedBy) + .setNickname(nickname) + .build(); + + furnaces.put(id, furnace); } - - int id = result.getInt("id"); - int level = result.getInt("level"); - int uses = result.getInt("uses"); - - String placedByStr = result.getString("placed_by"); - UUID placedBy = placedByStr == null ? null : UUID.fromString(result.getString("placed_by")); - - String nickname = result.getString("nickname"); - - int x = result.getInt("x"); - int y = result.getInt("y"); - int z = result.getInt("z"); - Location location = new Location(world, x, y, z); - - Furnace furnace = new FurnaceBuilder(location) - .setId(id) - .setLevel(EpicFurnaces.getInstance().getLevelManager().getLevel(level)) - .setUses(uses) - .setPlacedBy(placedBy) - .setNickname(nickname) - .build(); - - furnaces.put(id, furnace); } - } - try (Statement statement = connection.createStatement()) { - String selectAccessList = "SELECT * FROM " + this.getTablePrefix() + "access_list"; - ResultSet result = statement.executeQuery(selectAccessList); - while (result.next()) { - int id = result.getInt("furnace_id"); - UUID uuid = UUID.fromString(result.getString("uuid")); + try (Statement statement = connection.createStatement()) { + String selectAccessList = "SELECT * FROM " + this.getTablePrefix() + "access_list"; + ResultSet result = statement.executeQuery(selectAccessList); + while (result.next()) { + int id = result.getInt("furnace_id"); + UUID uuid = UUID.fromString(result.getString("uuid")); - Furnace furnace = furnaces.get(id); - if (furnace == null) { - break; + Furnace furnace = furnaces.get(id); + if (furnace == null) { + break; + } + + furnace.addToAccessList(uuid); } - - furnace.addToAccessList(uuid); } - } - try (Statement statement = connection.createStatement()) { - String selectLevelupItems = "SELECT * FROM " + this.getTablePrefix() + "to_level_new"; - ResultSet result = statement.executeQuery(selectLevelupItems); - while (result.next()) { - int id = result.getInt("furnace_id"); - CompatibleMaterial material = CompatibleMaterial.getMaterial(result.getString("item")); - int amount = result.getInt("amount"); + try (Statement statement = connection.createStatement()) { + String selectLevelupItems = "SELECT * FROM " + this.getTablePrefix() + "to_level_new"; + ResultSet result = statement.executeQuery(selectLevelupItems); + while (result.next()) { + int id = result.getInt("furnace_id"); + CompatibleMaterial material = CompatibleMaterial.getMaterial(result.getString("item")); + int amount = result.getInt("amount"); - Furnace furnace = furnaces.get(id); - if (furnace == null) { - break; + Furnace furnace = furnaces.get(id); + if (furnace == null) { + break; + } + + furnace.addToLevel(material, amount); } - - furnace.addToLevel(material, amount); } + this.sync(() -> callback.accept(furnaces)); + } catch (Exception ex) { + ex.printStackTrace(); } - this.sync(() -> callback.accept(furnaces)); - })); + }); } } diff --git a/src/main/java/com/songoda/epicfurnaces/settings/Settings.java b/src/main/java/com/songoda/epicfurnaces/settings/Settings.java index 16b9d2b..47eab46 100644 --- a/src/main/java/com/songoda/epicfurnaces/settings/Settings.java +++ b/src/main/java/com/songoda/epicfurnaces/settings/Settings.java @@ -88,6 +88,15 @@ public class Settings { "The enabled language file.", "More language files (if available) can be found in the plugins data folder."); + public static final ConfigSetting MYSQL_ENABLED = new ConfigSetting(config, "MySQL.Enabled", false, "Set to 'true' to use MySQL instead of SQLite for data storage."); + public static final ConfigSetting MYSQL_HOSTNAME = new ConfigSetting(config, "MySQL.Hostname", "localhost"); + public static final ConfigSetting MYSQL_PORT = new ConfigSetting(config, "MySQL.Port", 3306); + public static final ConfigSetting MYSQL_DATABASE = new ConfigSetting(config, "MySQL.Database", "your-database"); + public static final ConfigSetting MYSQL_USERNAME = new ConfigSetting(config, "MySQL.Username", "user"); + public static final ConfigSetting MYSQL_PASSWORD = new ConfigSetting(config, "MySQL.Password", "pass"); + public static final ConfigSetting MYSQL_USE_SSL = new ConfigSetting(config, "MySQL.Use SSL", false); + public static final ConfigSetting MYSQL_POOL_SIZE = new ConfigSetting(config, "MySQL.Pool Size", 3, "Determines the number of connections the pool is using. Increase this value if you are getting timeout errors when more players online."); + /** * In order to set dynamic economy comment correctly, this needs to be * called after EconomyManager load