diff --git a/src/main/java/com/songoda/epicfurnaces/EpicFurnaces.java b/src/main/java/com/songoda/epicfurnaces/EpicFurnaces.java index 4d2ea96..20c507c 100644 --- a/src/main/java/com/songoda/epicfurnaces/EpicFurnaces.java +++ b/src/main/java/com/songoda/epicfurnaces/EpicFurnaces.java @@ -66,7 +66,6 @@ import java.util.UUID; import java.util.stream.Collectors; public class EpicFurnaces extends SongodaPlugin { - private static EpicFurnaces INSTANCE; private final Config furnaceRecipeFile = new Config(this, "Furnace Recipes.yml"); @@ -94,7 +93,9 @@ public class EpicFurnaces extends SongodaPlugin { @Override public void onPluginDisable() { + shutdownDataManager(this.dataManager); this.databaseConnector.closeConnection(); + HologramManager.removeAllHolograms(); } @@ -475,4 +476,4 @@ public class EpicFurnaces extends SongodaPlugin { public DataManager getDataManager() { return dataManager; } -} \ No newline at end of file +} diff --git a/src/main/java/com/songoda/epicfurnaces/database/DataManager.java b/src/main/java/com/songoda/epicfurnaces/database/DataManager.java index aa9ce85..4c692ef 100644 --- a/src/main/java/com/songoda/epicfurnaces/database/DataManager.java +++ b/src/main/java/com/songoda/epicfurnaces/database/DataManager.java @@ -15,17 +15,23 @@ import org.bukkit.plugin.Plugin; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; import java.util.function.Consumer; +// FIXME: The DataMager is used in a way that clogs up the async thread. +// A hotfix has been applied trying to not crash the server because of it but it might just make things worse... +// The DataManager needs to update furnaces **a lot** more sanely public class DataManager extends DataManagerAbstract { - public DataManager(DatabaseConnector connector, Plugin plugin) { super(connector, plugin); } public void createBoost(BoostData boostData) { - this.async(() -> this.databaseConnector.connect(connection -> { + 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)) { statement.setString(1, boostData.getPlayer().toString()); @@ -37,8 +43,9 @@ public class DataManager extends DataManagerAbstract { } public void getBoosts(Consumer> callback) { - List boosts = new ArrayList<>(); - this.async(() -> this.databaseConnector.connect(connection -> { + this.runAsync(() -> this.databaseConnector.connect(connection -> { + List boosts = new ArrayList<>(); + try (Statement statement = connection.createStatement()) { String selectBoostedPlayers = "SELECT * FROM " + this.getTablePrefix() + "boosted_players"; ResultSet result = statement.executeQuery(selectBoostedPlayers); @@ -55,8 +62,9 @@ public class DataManager extends DataManagerAbstract { } public void deleteBoost(BoostData boostData) { - this.async(() -> this.databaseConnector.connect(connection -> { + this.runAsync(() -> this.databaseConnector.connect(connection -> { String deleteBoost = "DELETE FROM " + this.getTablePrefix() + "boosted_players WHERE end_time = ?"; + try (PreparedStatement statement = connection.prepareStatement(deleteBoost)) { statement.setLong(1, boostData.getEndTime()); statement.executeUpdate(); @@ -115,7 +123,7 @@ public class DataManager extends DataManagerAbstract { } public void updateFurnace(Furnace furnace) { - this.async(() -> this.databaseConnector.connect(connection -> { + this.runAsync(() -> this.databaseConnector.connect(connection -> { String updateHopper = "UPDATE " + this.getTablePrefix() + "active_furnaces SET level = ?, nickname = ?, uses = ? WHERE id = ?"; try (PreparedStatement statement = connection.prepareStatement(updateHopper)) { statement.setInt(1, furnace.getLevel().getLevel()); @@ -128,7 +136,7 @@ public class DataManager extends DataManagerAbstract { } public void deleteFurnace(Furnace furnace) { - this.async(() -> this.databaseConnector.connect(connection -> { + 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()); @@ -150,7 +158,7 @@ public class DataManager extends DataManagerAbstract { } public void createAccessPlayer(Furnace furnace, UUID uuid) { - this.async(() -> this.databaseConnector.connect(connection -> { + this.runAsync(() -> this.databaseConnector.connect(connection -> { String createAccessPlayer = "INSERT INTO " + this.getTablePrefix() + "access_list (furnace_id, uuid) VALUES (?, ?)"; try (PreparedStatement statement = connection.prepareStatement(createAccessPlayer)) { statement.setInt(1, furnace.getId()); @@ -163,7 +171,7 @@ public class DataManager extends DataManagerAbstract { // 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.async(() -> this.databaseConnector.connect(connection -> { + 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)) { statement.setInt(1, furnace.getId()); @@ -174,7 +182,7 @@ public class DataManager extends DataManagerAbstract { } public void updateAccessPlayers(Furnace furnace) { - this.async(() -> this.databaseConnector.connect(connection -> { + 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()); @@ -194,7 +202,7 @@ public class DataManager extends DataManagerAbstract { } public void updateLevelupItems(Furnace furnace, CompatibleMaterial material, int amount) { - this.async(() -> this.databaseConnector.connect(connection -> { + 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()); @@ -213,7 +221,7 @@ public class DataManager extends DataManagerAbstract { } public void getFurnaces(Consumer> callback) { - this.async(() -> this.databaseConnector.connect(connection -> { + this.runAsync(() -> this.databaseConnector.connect(connection -> { Map furnaces = new HashMap<>(); try (Statement statement = connection.createStatement()) {