From e8b96653fbf02d601d86aa9e40f0a0ebd9c68735 Mon Sep 17 00:00:00 2001 From: Fernando Pettinelli Date: Sat, 5 Dec 2020 01:06:32 -0300 Subject: [PATCH] Added SQLite support + conversion. --- .../songoda/epicfurnaces/EpicFurnaces.java | 186 ++++++----- .../epicfurnaces/boost/BoostManager.java | 13 +- .../epicfurnaces/commands/CommandBoost.java | 1 + .../epicfurnaces/database/DataManager.java | 290 ++++++++++++++++++ .../migrations/_1_InitialMigration.java | 63 ++++ .../songoda/epicfurnaces/furnace/Furnace.java | 25 +- .../epicfurnaces/furnace/FurnaceBuilder.java | 5 + .../epicfurnaces/furnace/FurnaceManager.java | 7 + .../songoda/epicfurnaces/gui/GUIOverview.java | 15 +- .../listeners/BlockListeners.java | 2 + .../listeners/EntityListeners.java | 1 + 11 files changed, 516 insertions(+), 92 deletions(-) create mode 100644 src/main/java/com/songoda/epicfurnaces/database/DataManager.java create mode 100644 src/main/java/com/songoda/epicfurnaces/database/migrations/_1_InitialMigration.java diff --git a/src/main/java/com/songoda/epicfurnaces/EpicFurnaces.java b/src/main/java/com/songoda/epicfurnaces/EpicFurnaces.java index 58435c9..eaa4565 100644 --- a/src/main/java/com/songoda/epicfurnaces/EpicFurnaces.java +++ b/src/main/java/com/songoda/epicfurnaces/EpicFurnaces.java @@ -5,6 +5,9 @@ import com.songoda.core.SongodaPlugin; import com.songoda.core.commands.CommandManager; 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.SQLiteConnector; import com.songoda.core.gui.GuiManager; import com.songoda.core.hooks.EconomyManager; import com.songoda.core.hooks.HologramManager; @@ -19,6 +22,8 @@ import com.songoda.epicfurnaces.commands.CommandReload; import com.songoda.epicfurnaces.commands.CommandRemote; import com.songoda.epicfurnaces.commands.CommandSettings; import com.songoda.epicfurnaces.compatibility.FabledSkyBlockLoader; +import com.songoda.epicfurnaces.database.DataManager; +import com.songoda.epicfurnaces.database.migrations._1_InitialMigration; import com.songoda.epicfurnaces.furnace.Furnace; import com.songoda.epicfurnaces.furnace.FurnaceBuilder; import com.songoda.epicfurnaces.furnace.FurnaceManager; @@ -37,6 +42,7 @@ import com.songoda.epicfurnaces.tasks.FurnaceTask; import com.songoda.epicfurnaces.tasks.HologramTask; import com.songoda.epicfurnaces.utils.Methods; import org.bukkit.Bukkit; +import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.BlockState; @@ -56,7 +62,7 @@ import java.util.Map; import java.util.UUID; import java.util.stream.Collectors; -public class EpicFurnaces extends SongodaPlugin { +public class EpicFurnaces extends SongodaPlugin { private static EpicFurnaces INSTANCE; @@ -72,7 +78,8 @@ public class EpicFurnaces extends SongodaPlugin { private BlacklistHandler blacklistHandler; - private Storage storage; + private DatabaseConnector databaseConnector; + private DataManager dataManager; public static EpicFurnaces getInstance() { return INSTANCE; @@ -85,8 +92,7 @@ public class EpicFurnaces extends SongodaPlugin { @Override public void onPluginDisable() { - saveToFile(); - this.storage.closeConnection(); + this.databaseConnector.closeConnection(); HologramManager.removeAllHolograms(); } @@ -130,9 +136,95 @@ public class EpicFurnaces extends SongodaPlugin { this.boostManager = new BoostManager(); this.blacklistHandler = new BlacklistHandler(); - // Load from file - dataFile.load(); - this.storage = new StorageYaml(this); + // Database stuff. + this.databaseConnector = new SQLiteConnector(this); + this.getLogger().info("Data handler connected using SQLite."); + + this.dataManager = new DataManager(this.databaseConnector, this); + DataMigrationManager dataMigrationManager = new DataMigrationManager(this.databaseConnector, this.dataManager, + new _1_InitialMigration()); + dataMigrationManager.runMigrations(); + + Bukkit.getScheduler().runTaskAsynchronously(this, () -> { + // Legacy data! Yay! + File folder = getDataFolder(); + File dataFile = new File(folder, "data.yml"); + + boolean converted = false; + if (dataFile.exists()) { + converted = true; + Storage storage = new StorageYaml(this); + if (storage.containsGroup("charged")) { + console.sendMessage("[" + getDescription().getName() + "] " + ChatColor.RED + + "Conversion process starting. Do NOT turn off your server." + + "EpicFurnaces hasn't fully loaded yet, so make sure users don't" + + "interact with the plugin until the conversion process is complete."); + + List furnaces = new ArrayList<>(); + for (StorageRow row : storage.getRowsByGroup("charged")) { + Location location = Methods.unserializeLocation(row.getKey()); + if (location == null) continue; + + if (row.get("level").asInt() == 0) continue; + + String placedByStr = row.get("placedby").asString(); + UUID placedBy = placedByStr == null ? null : UUID.fromString(placedByStr); + + List list = row.get("accesslist").asStringList(); + if (!list.isEmpty()) { + for (String uuid : new ArrayList<>(list)) + if (uuid.contains(":")) { + list = new ArrayList<>(); + break; + } + } + List usableList = list.stream().map(UUID::fromString).collect(Collectors.toList()); + + Map toLevel = new HashMap<>(); + List toLevelCompiled = row.get("tolevelnew").asStringList(); + for (String line : toLevelCompiled) { + String[] split = line.split(":"); + toLevel.put(CompatibleMaterial.getMaterial(split[0]), Integer.parseInt(split[1])); + } + + furnaces.add(new FurnaceBuilder(location) + .setLevel(levelManager.getLevel(row.get("level").asInt())) + .setNickname(row.get("nickname").asString()) + .setUses(row.get("uses").asInt()) + .setToLevel(toLevel) + .setAccessList(usableList) + .setPlacedBy(placedBy).build()); + } + dataManager.createFurnaces(furnaces); + } + + // Adding in Boosts + if (storage.containsGroup("boosts")) { + for (StorageRow row : storage.getRowsByGroup("boosts")) { + if (row.get("uuid").asObject() == null) + continue; + + dataManager.createBoost(new BoostData( + row.get("amount").asInt(), + Long.parseLong(row.getKey()), + UUID.fromString(row.get("uuid").asString()))); + } + } + dataFile.delete(); + } + + final boolean finalConverted = converted; + dataManager.queueAsync(() -> { + if (finalConverted) { + console.sendMessage("[" + getDescription().getName() + "] " + ChatColor.GREEN + "Conversion complete :)"); + } + + this.dataManager.getFurnaces((furnaces) -> { + this.furnaceManager.addFurnaces(furnaces.values()); + this.dataManager.getBoosts((boosts) -> this.boostManager.addBoosts(boosts)); + }); + }, "create"); + }); setupRecipies(); @@ -148,81 +240,18 @@ public class EpicFurnaces extends SongodaPlugin { pluginManager.registerEvents(new InteractListeners(this, guiManager), this); pluginManager.registerEvents(new InventoryListeners(this), this); pluginManager.registerEvents(new EntityListeners(this), this); - - // Start auto save - int saveInterval = Settings.AUTOSAVE.getInt() * 60 * 20; - Bukkit.getScheduler().runTaskTimerAsynchronously(this, this::saveToFile, saveInterval, saveInterval); } @Override public void onDataLoad() { - /* - * Register furnaces into FurnaceManger from configuration - */ - if (storage.containsGroup("charged")) { - for (StorageRow row : storage.getRowsByGroup("charged")) { - Location location = Methods.unserializeLocation(row.getKey()); - if (location == null) continue; - - if (row.get("level").asInt() == 0) continue; - - String placedByStr = row.get("placedBy").asString(); - UUID placedBy = placedByStr == null ? null : UUID.fromString(placedByStr); - - List list = row.get("accesslist").asStringList(); - if (!list.isEmpty()) { - for (String uuid : new ArrayList<>(list)) - if (uuid.contains(":")) { - list = new ArrayList<>(); - break; - } - } - List usableList = list.stream().map(UUID::fromString).collect(Collectors.toList()); - - Map toLevel = new HashMap<>(); - List toLevelCompiled = row.get("tolevelnew").asStringList(); - for (String line : toLevelCompiled) { - String[] split = line.split(":"); - toLevel.put(CompatibleMaterial.getMaterial(split[0]), Integer.parseInt(split[1])); - } - - Furnace furnace = new FurnaceBuilder(location) - .setLevel(levelManager.getLevel(row.get("level").asInt())) - .setNickname(row.get("nickname").asString()) - .setUses(row.get("uses").asInt()) - .setToLevel(toLevel) - .setAccessList(usableList) - .setPlacedBy(placedBy).build(); - - furnaceManager.addFurnace(furnace); - } - } - - // Adding in Boosts - if (storage.containsGroup("boosts")) { - for (StorageRow row : storage.getRowsByGroup("boosts")) { - if (row.getItems().get("uuid").asObject() != null) - continue; - - BoostData boostData = new BoostData( - row.get("amount").asInt(), - Long.parseLong(row.getKey()), - UUID.fromString(row.get("uuid").asString())); - - this.boostManager.addBoostToPlayer(boostData); - } - } - // Register Hologram Plugin + if (Settings.HOLOGRAMS.getBoolean()) { for (Furnace furnace : getFurnaceManager().getFurnaces().values()) { if (furnace.getLocation() == null || furnace.getLocation().getWorld() == null) continue; } } - - // Save data initially so that if the person reloads again fast they don't lose all their data. - this.saveToFile(); } @Override @@ -340,13 +369,6 @@ public class EpicFurnaces extends SongodaPlugin { } } - /* - * Saves registered furnaces to file. - */ - private void saveToFile() { - storage.doSave(); - } - private void setupRecipies() { File config = new File(getDataFolder(), "Furnace Recipes.yml"); if (!config.exists()) { @@ -438,4 +460,12 @@ public class EpicFurnaces extends SongodaPlugin { public LevelManager getLevelManager() { return levelManager; } + + public DatabaseConnector getDatabaseConnector() { + return databaseConnector; + } + + public DataManager getDataManager() { + return dataManager; + } } \ No newline at end of file diff --git a/src/main/java/com/songoda/epicfurnaces/boost/BoostManager.java b/src/main/java/com/songoda/epicfurnaces/boost/BoostManager.java index c478ca0..0366b98 100644 --- a/src/main/java/com/songoda/epicfurnaces/boost/BoostManager.java +++ b/src/main/java/com/songoda/epicfurnaces/boost/BoostManager.java @@ -1,9 +1,8 @@ package com.songoda.epicfurnaces.boost; -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; -import java.util.UUID; +import com.songoda.epicfurnaces.EpicFurnaces; + +import java.util.*; public class BoostManager { @@ -13,6 +12,10 @@ public class BoostManager { this.registeredBoosts.add(data); } + public void addBoosts(List boosts) { + registeredBoosts.addAll(boosts); + } + public void removeBoostFromPlayer(BoostData data) { this.registeredBoosts.remove(data); } @@ -27,10 +30,12 @@ public class BoostManager { if (boostData.getPlayer().toString().equals(player.toString())) { if (System.currentTimeMillis() >= boostData.getEndTime()) { removeBoostFromPlayer(boostData); + EpicFurnaces.getInstance().getDataManager().deleteBoost(boostData); } return boostData; } } return null; } + } diff --git a/src/main/java/com/songoda/epicfurnaces/commands/CommandBoost.java b/src/main/java/com/songoda/epicfurnaces/commands/CommandBoost.java index 470f7d4..4fb06f5 100644 --- a/src/main/java/com/songoda/epicfurnaces/commands/CommandBoost.java +++ b/src/main/java/com/songoda/epicfurnaces/commands/CommandBoost.java @@ -51,6 +51,7 @@ public class CommandBoost extends AbstractCommand { BoostData boostData = new BoostData(Integer.parseInt(args[1]), duration == 0L ? Long.MAX_VALUE : System.currentTimeMillis() + duration, player.getUniqueId()); instance.getBoostManager().addBoostToPlayer(boostData); + instance.getDataManager().createBoost(boostData); instance.getLocale().newMessage("&7Successfully boosted &6" + Bukkit.getPlayer(args[0]).getName() + "'s &7furnace reward amounts &6" + args[1] + "x" + (duration == 0L ? "" : (" for " + Methods.makeReadable(duration))) + "&7.").sendPrefixedMessage(sender); return ReturnType.SUCCESS; diff --git a/src/main/java/com/songoda/epicfurnaces/database/DataManager.java b/src/main/java/com/songoda/epicfurnaces/database/DataManager.java new file mode 100644 index 0000000..aa9ce85 --- /dev/null +++ b/src/main/java/com/songoda/epicfurnaces/database/DataManager.java @@ -0,0 +1,290 @@ +package com.songoda.epicfurnaces.database; + +import com.songoda.core.compatibility.CompatibleMaterial; +import com.songoda.core.database.DataManagerAbstract; +import com.songoda.core.database.DatabaseConnector; +import com.songoda.epicfurnaces.EpicFurnaces; +import com.songoda.epicfurnaces.boost.BoostData; +import com.songoda.epicfurnaces.furnace.Furnace; +import com.songoda.epicfurnaces.furnace.FurnaceBuilder; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.plugin.Plugin; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.Statement; +import java.util.*; +import java.util.function.Consumer; + +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 -> { + 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(); + } + })); + } + + public void getBoosts(Consumer> callback) { + List boosts = new ArrayList<>(); + this.async(() -> this.databaseConnector.connect(connection -> { + try (Statement statement = connection.createStatement()) { + String selectBoostedPlayers = "SELECT * FROM " + this.getTablePrefix() + "boosted_players"; + ResultSet result = statement.executeQuery(selectBoostedPlayers); + while (result.next()) { + UUID player = UUID.fromString(result.getString("player")); + int multiplier = result.getInt("multiplier"); + long endTime = result.getLong("end_time"); + boosts.add(new BoostData(multiplier, endTime, player)); + } + } + + this.sync(() -> callback.accept(boosts)); + })); + } + + 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)) { + statement.setLong(1, boostData.getEndTime()); + statement.executeUpdate(); + } + })); + } + + public void createFurnaces(List furnaces) { + for (Furnace furnace : furnaces) { + createFurnace(furnace); + } + } + + 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()); + + 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.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(); + } + }), "create"); + } + + public void updateFurnace(Furnace furnace) { + this.async(() -> 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()); + statement.setString(2, furnace.getNickname()); + statement.setInt(3, furnace.getUses()); + statement.setInt(4, furnace.getId()); + statement.executeUpdate(); + } + })); + } + + public void deleteFurnace(Furnace furnace) { + this.async(() -> 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(); + } + + 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(); + } + })); + } + + public void createAccessPlayer(Furnace furnace, UUID uuid) { + this.async(() -> 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()); + statement.setString(2, uuid.toString()); + statement.executeUpdate(); + } + })); + } + + // 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 -> { + String deleteAccessPlayer = "DELETE FROM " + this.getTablePrefix() + "access_list WHERE furnace_id = ? AND uuid = ?"; + try (PreparedStatement statement = connection.prepareStatement(deleteAccessPlayer)) { + statement.setInt(1, furnace.getId()); + statement.setString(2, uuid.toString()); + statement.executeUpdate(); + } + })); + } + + public void updateAccessPlayers(Furnace furnace) { + this.async(() -> 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()) { + statement.setInt(1, furnace.getId()); + statement.setString(2, uuid.toString()); + statement.addBatch(); + } + statement.executeBatch(); + } + })); + } + + public void updateLevelupItems(Furnace furnace, CompatibleMaterial material, int amount) { + this.async(() -> 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(); + } + + 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(); + } + })); + } + + public void getFurnaces(Consumer> callback) { + this.async(() -> this.databaseConnector.connect(connection -> { + 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")); + + 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); + } + } + + 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.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"); + + Furnace furnace = furnaces.get(id); + if (furnace == null) { + break; + } + + furnace.addToLevel(material, amount); + } + } + this.sync(() -> callback.accept(furnaces)); + })); + } +} diff --git a/src/main/java/com/songoda/epicfurnaces/database/migrations/_1_InitialMigration.java b/src/main/java/com/songoda/epicfurnaces/database/migrations/_1_InitialMigration.java new file mode 100644 index 0000000..796007e --- /dev/null +++ b/src/main/java/com/songoda/epicfurnaces/database/migrations/_1_InitialMigration.java @@ -0,0 +1,63 @@ +package com.songoda.epicfurnaces.database.migrations; + +import com.songoda.core.database.DataMigration; +import com.songoda.core.database.MySQLConnector; +import com.songoda.epicfurnaces.EpicFurnaces; + +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; + +public class _1_InitialMigration extends DataMigration { + + public _1_InitialMigration() { + super(1); + } + + @Override + public void migrate(Connection connection, String tablePrefix) throws SQLException { + String autoIncrement = EpicFurnaces.getInstance().getDatabaseConnector() instanceof MySQLConnector ? " AUTO_INCREMENT" : ""; + + // Create furnaces table. + try (Statement statement = connection.createStatement()) { + statement.execute("CREATE TABLE " + tablePrefix + "active_furnaces (" + + "id INTEGER PRIMARY KEY" + autoIncrement + ", " + + "level INTEGER NOT NULL, " + + "uses INTEGER NOT NULL," + + "placed_by VARCHAR(36), " + + "nickname TEXT, " + + "world TEXT NOT NULL, " + + "x DOUBLE NOT NULL, " + + "y DOUBLE NOT NULL, " + + "z DOUBLE NOT NULL " + + ")"); + } + + // Create access lists. + try (Statement statement = connection.createStatement()) { + statement.execute("CREATE TABLE " + tablePrefix + "access_list (" + + "furnace_id INTEGER NOT NULL, " + + "uuid VARCHAR(36)" + + ")"); + } + + // Create items to level up. + try (Statement statement = connection.createStatement()) { + statement.execute("CREATE TABLE " + tablePrefix + "to_level_new (" + + "furnace_id INTEGER NOT NULL, " + + "item TEXT NOT NULL," + + "amount INT NOT NULL " + + ")"); + } + + // Create player boosts + try (Statement statement = connection.createStatement()) { + statement.execute("CREATE TABLE " + tablePrefix + "boosted_players (" + + "player VARCHAR(36) NOT NULL, " + + "multiplier INTEGER NOT NULL," + + "end_time BIGINT NOT NULL " + + ")"); + } + + } +} diff --git a/src/main/java/com/songoda/epicfurnaces/furnace/Furnace.java b/src/main/java/com/songoda/epicfurnaces/furnace/Furnace.java index 40ba62f..1fe7dcc 100644 --- a/src/main/java/com/songoda/epicfurnaces/furnace/Furnace.java +++ b/src/main/java/com/songoda/epicfurnaces/furnace/Furnace.java @@ -41,6 +41,9 @@ public class Furnace { private final EpicFurnaces plugin = EpicFurnaces.getInstance(); + // Identifier for database use. + private int id; + private final Location location; private Level level = plugin.getLevelManager().getLowestLevel(); private String nickname = null; @@ -70,12 +73,14 @@ public class Furnace { if (!block.getType().name().contains("FURNACE") && !block.getType().name().contains("SMOKER")) return; this.uses++; + plugin.getDataManager().updateFurnace(this); CompatibleMaterial material = CompatibleMaterial.getMaterial(event.getResult()); int needed = -1; if (level.getMaterials().containsKey(material)) { - addToLevel(material, 1); + int amount = addToLevel(material, 1); + plugin.getDataManager().updateLevelupItems(this, material, amount); needed = level.getMaterials().get(material) - getToLevel(material); } @@ -161,6 +166,7 @@ public class Furnace { private void upgradeFinal(Player player) { levelUp(); syncName(); + plugin.getDataManager().updateFurnace(this); if (plugin.getLevelManager().getHighestLevel() != level) { plugin.getLocale().getMessage("event.upgrade.success") .processPlaceholder("level", level.getLevel()).sendPrefixedMessage(player); @@ -352,12 +358,15 @@ public class Furnace { return Collections.unmodifiableMap(toLevel); } - public void addToLevel(CompatibleMaterial material, int amount) { + public int addToLevel(CompatibleMaterial material, int amount) { if (this.toLevel.containsKey(material)) { - this.toLevel.put(material, this.toLevel.get(material) + amount); - return; + int newAmount = this.toLevel.get(material) + amount; + this.toLevel.put(material, newAmount); + return newAmount; } + this.toLevel.put(material, amount); + return amount; } public int getRadiusOverheatLast() { @@ -375,4 +384,12 @@ public class Furnace { public void setRadiusFuelshareLast(int radiusFuelshareLast) { this.radiusFuelshareLast = radiusFuelshareLast; } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } } diff --git a/src/main/java/com/songoda/epicfurnaces/furnace/FurnaceBuilder.java b/src/main/java/com/songoda/epicfurnaces/furnace/FurnaceBuilder.java index cf4aca2..85cd06a 100644 --- a/src/main/java/com/songoda/epicfurnaces/furnace/FurnaceBuilder.java +++ b/src/main/java/com/songoda/epicfurnaces/furnace/FurnaceBuilder.java @@ -50,6 +50,11 @@ public class FurnaceBuilder { return this; } + public FurnaceBuilder setId(int id) { + this.furnace.setId(id); + return this; + } + public Furnace build() { return this.furnace; } diff --git a/src/main/java/com/songoda/epicfurnaces/furnace/FurnaceManager.java b/src/main/java/com/songoda/epicfurnaces/furnace/FurnaceManager.java index 1fc3e92..6420795 100644 --- a/src/main/java/com/songoda/epicfurnaces/furnace/FurnaceManager.java +++ b/src/main/java/com/songoda/epicfurnaces/furnace/FurnaceManager.java @@ -3,6 +3,7 @@ package com.songoda.epicfurnaces.furnace; import org.bukkit.Location; import org.bukkit.block.Block; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -16,6 +17,12 @@ public class FurnaceManager { return registeredFurnaces.put(roundLocation(furnace.getLocation()), furnace); } + public void addFurnaces(Collection furnaces) { + for (Furnace furnace : furnaces) { + addFurnace(furnace); + } + } + public Furnace removeFurnace(Location location) { return registeredFurnaces.remove(location); } diff --git a/src/main/java/com/songoda/epicfurnaces/gui/GUIOverview.java b/src/main/java/com/songoda/epicfurnaces/gui/GUIOverview.java index a85e031..9508cc3 100644 --- a/src/main/java/com/songoda/epicfurnaces/gui/GUIOverview.java +++ b/src/main/java/com/songoda/epicfurnaces/gui/GUIOverview.java @@ -51,11 +51,11 @@ public class GUIOverview extends Gui { setDefaultItem(glass1); - GuiUtils.mirrorFill(this, 0, 0, true, true, glass2); - GuiUtils.mirrorFill(this, 0, 1, true, true, glass2); - GuiUtils.mirrorFill(this, 0, 2, true, true, glass3); - GuiUtils.mirrorFill(this, 1, 0, false, true, glass2); - GuiUtils.mirrorFill(this, 1, 1, false, true, glass3); + mirrorFill(0, 0, true, true, glass2); + mirrorFill(0, 1, true, true, glass2); + mirrorFill(0, 2, true, true, glass3); + mirrorFill(1, 0, false, true, glass2); + mirrorFill(1, 1, false, true, glass3); Level level = furnace.getLevel(); Level nextLevel = plugin.getLevelManager().getHighestLevel().getLevel() > level.getLevel() ? plugin.getLevelManager().getLevel(level.getLevel() + 1) : null; @@ -149,13 +149,16 @@ public class GUIOverview extends Gui { } } + plugin.getDataManager().updateFurnace(furnace); furnace.setNickname(promptEvent.getMessage()); plugin.getLocale().getMessage("event.remote.nicknamesuccess").sendPrefixedMessage(player); }).setOnClose(this::constructGUI); }).setAction(4, ClickType.RIGHT, (event) -> { - if (!furnace.isOnAccessList(player)) + if (!furnace.isOnAccessList(player)) { furnace.addToAccessList(player); + plugin.getDataManager().createAccessPlayer(furnace, player.getUniqueId()); + } constructGUI(); }); } diff --git a/src/main/java/com/songoda/epicfurnaces/listeners/BlockListeners.java b/src/main/java/com/songoda/epicfurnaces/listeners/BlockListeners.java index 0f2029c..79d0ebc 100644 --- a/src/main/java/com/songoda/epicfurnaces/listeners/BlockListeners.java +++ b/src/main/java/com/songoda/epicfurnaces/listeners/BlockListeners.java @@ -63,6 +63,7 @@ public class BlockListeners implements Listener { : new FurnaceBuilder(location).setPlacedBy(event.getPlayer().getUniqueId()).build(); plugin.getFurnaceManager().addFurnace(furnace); + plugin.getDataManager().createFurnace(furnace); plugin.updateHologram(furnace); } @@ -92,5 +93,6 @@ public class BlockListeners implements Listener { event.getBlock().getLocation().getWorld().dropItemNaturally(event.getBlock().getLocation(), item); } plugin.getFurnaceManager().removeFurnace(block.getLocation()); + plugin.getDataManager().deleteFurnace(furnace); } } \ No newline at end of file diff --git a/src/main/java/com/songoda/epicfurnaces/listeners/EntityListeners.java b/src/main/java/com/songoda/epicfurnaces/listeners/EntityListeners.java index d38068a..8a82934 100644 --- a/src/main/java/com/songoda/epicfurnaces/listeners/EntityListeners.java +++ b/src/main/java/com/songoda/epicfurnaces/listeners/EntityListeners.java @@ -28,6 +28,7 @@ public class EntityListeners implements Listener { Furnace furnace = furnaceManager.getFurnace(block); if (furnace == null) continue; furnaceManager.removeFurnace(block.getLocation()); + plugin.getDataManager().deleteFurnace(furnace); plugin.clearHologram(furnace); } }