From 51f42e9c37d5eb75f0905eb172bcf46fbe1e6616 Mon Sep 17 00:00:00 2001 From: "songoda-projects-overview[bot]" <111250264+songoda-projects-overview[bot]@users.noreply.github.com> Date: Mon, 15 Aug 2022 00:49:08 +0200 Subject: [PATCH 1/4] Bump SongodaCore version to `2.6.15-DEV` --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index e10fa5f..e30547a 100644 --- a/pom.xml +++ b/pom.xml @@ -122,7 +122,7 @@ com.songoda SongodaCore - 2.6.13 + 2.6.15-DEV compile From 350267c77c4cc5d0a1c7a7448a881c707c80b9fb Mon Sep 17 00:00:00 2001 From: "songoda-projects-overview[bot]" <111250264+songoda-projects-overview[bot]@users.noreply.github.com> Date: Mon, 5 Sep 2022 22:18:45 +0200 Subject: [PATCH 2/4] Bump SongodaCore version to `2.6.16` --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index e30547a..cb4e303 100644 --- a/pom.xml +++ b/pom.xml @@ -122,7 +122,7 @@ com.songoda SongodaCore - 2.6.15-DEV + 2.6.16 compile From 6cc23d1157ae0d3175a11741b606cbee289c430d Mon Sep 17 00:00:00 2001 From: BuildTools Date: Mon, 5 Sep 2022 16:50:19 -0400 Subject: [PATCH 3/4] Migrate to new storage format. --- pom.xml | 20 +- .../java/com/songoda/epicheads/EpicHeads.java | 183 ++++++++------ .../epicheads/commands/CommandAdd.java | 4 +- .../epicheads/database/DataManager.java | 235 ++++++++++++++++++ .../migrations/_1_InitialMigration.java | 46 ++++ .../com/songoda/epicheads/gui/GUIHeads.java | 1 + .../java/com/songoda/epicheads/head/Head.java | 23 +- .../epicheads/listeners/ItemListeners.java | 9 + .../epicheads/listeners/LoginListeners.java | 20 +- .../songoda/epicheads/utils/ItemEconomy.java | 2 +- 10 files changed, 448 insertions(+), 95 deletions(-) create mode 100644 src/main/java/com/songoda/epicheads/database/DataManager.java create mode 100644 src/main/java/com/songoda/epicheads/database/migrations/_1_InitialMigration.java diff --git a/pom.xml b/pom.xml index cb4e303..6d2663c 100644 --- a/pom.xml +++ b/pom.xml @@ -48,7 +48,7 @@ org.apache.maven.plugins maven-shade-plugin - 3.3.0-SNAPSHOT + 3.3.0 @@ -93,13 +93,6 @@ - - - apache.snapshots - https://repository.apache.org/snapshots/ - - - public @@ -114,8 +107,15 @@ org.spigotmc - spigot - 1.18 + spigot-api + 1.19-R0.1-SNAPSHOT + provided + + + + com.googlecode.json-simple + json-simple + 1.1.1 provided diff --git a/src/main/java/com/songoda/epicheads/EpicHeads.java b/src/main/java/com/songoda/epicheads/EpicHeads.java index 80d5f5d..2a29f70 100644 --- a/src/main/java/com/songoda/epicheads/EpicHeads.java +++ b/src/main/java/com/songoda/epicheads/EpicHeads.java @@ -5,20 +5,16 @@ 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.PluginHook; import com.songoda.core.hooks.economies.Economy; -import com.songoda.epicheads.commands.CommandAdd; -import com.songoda.epicheads.commands.CommandBase64; -import com.songoda.epicheads.commands.CommandEpicHeads; -import com.songoda.epicheads.commands.CommandGive; -import com.songoda.epicheads.commands.CommandGiveToken; -import com.songoda.epicheads.commands.CommandHelp; -import com.songoda.epicheads.commands.CommandReload; -import com.songoda.epicheads.commands.CommandSearch; -import com.songoda.epicheads.commands.CommandSettings; -import com.songoda.epicheads.commands.CommandUrl; +import com.songoda.epicheads.commands.*; +import com.songoda.epicheads.database.DataManager; +import com.songoda.epicheads.database.migrations._1_InitialMigration; import com.songoda.epicheads.head.Category; import com.songoda.epicheads.head.Head; import com.songoda.epicheads.head.HeadManager; @@ -32,21 +28,17 @@ import com.songoda.epicheads.utils.storage.Storage; import com.songoda.epicheads.utils.storage.StorageRow; import com.songoda.epicheads.utils.storage.types.StorageYaml; import org.bukkit.Bukkit; +import org.bukkit.ChatColor; import org.bukkit.plugin.PluginManager; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; +import java.io.*; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.UUID; @@ -60,7 +52,8 @@ public class EpicHeads extends SongodaPlugin { private CommandManager commandManager; private PluginHook itemEconomyHook; - private Storage storage; + private DatabaseConnector databaseConnector; + private DataManager dataManager; public static EpicHeads getInstance() { return INSTANCE; @@ -74,8 +67,8 @@ public class EpicHeads extends SongodaPlugin { @Override public void onPluginDisable() { - this.storage.closeConnection(); - this.saveToFile(); + shutdownDataManager(this.dataManager); + this.databaseConnector.closeConnection(); } @Override @@ -117,8 +110,6 @@ public class EpicHeads extends SongodaPlugin { new CommandUrl(this) ); - this.storage = new StorageYaml(this); - // Register Listeners guiManager.init(); PluginManager pluginManager = Bukkit.getPluginManager(); @@ -133,32 +124,99 @@ public class EpicHeads extends SongodaPlugin { loadHeads(); int timeout = Settings.AUTOSAVE.getInt() * 60 * 20; - Bukkit.getScheduler().runTaskTimerAsynchronously(this, this::saveToFile, timeout, timeout); + Bukkit.getScheduler().runTaskTimerAsynchronously(this, () -> dataManager.saveAllPlayers(), timeout, timeout); } @Override public void onDataLoad() { - // Adding in favorites. - if (storage.containsGroup("players")) { - for (StorageRow row : storage.getRowsByGroup("players")) { - if (row.get("uuid").asObject() == null) { - continue; + // 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("players")) { + console.sendMessage("[" + getDescription().getName() + "] " + ChatColor.RED + + "Conversion process starting. Do NOT turn off your server. " + + "EpicHeads hasn't fully loaded yet, so make sure users don't" + + "interact with the plugin until the conversion process is complete."); + + List players = new ArrayList<>(); + for (StorageRow row : storage.getRowsByGroup("players")) { + if (row.get("uuid").asObject() == null) { + continue; + } + + players.add(new EPlayer( + UUID.fromString(row.get("uuid").asString()), + (List) row.get("favorites").asObject())); + } + dataManager.migratePlayers(players); } - EPlayer player = new EPlayer( - UUID.fromString(row.get("uuid").asString()), - (List) row.get("favorites").asObject()); + if (storage.containsGroup("local")) { + for (StorageRow row : storage.getRowsByGroup("local")) { + String tagStr = row.get("category").asString(); - this.playerManager.addPlayer(player); + Optional tagOptional = headManager.getCategories().stream() + .filter(t -> t.getName().equalsIgnoreCase(tagStr)).findFirst(); + + Category category = tagOptional.orElseGet(() -> new Category(tagStr)); + + Head head = new Head(row.get("id").asInt(), + row.get("name").asString(), + row.get("url").asString(), + category, + true, + null, + (byte) 0); + + dataManager.createLocalHead(head); + } + + if (storage.containsGroup("disabled")) { + List ids = new ArrayList<>(); + for (StorageRow row : storage.getRowsByGroup("disabled")) { + ids.add(row.get("id").asInt()); + } + + dataManager.migrateDisabledHead(ids); + } + } + + dataFile.delete(); } - } - // Save data initially so that if the person reloads again fast they don't lose all their data. - this.saveToFile(); - } + final boolean finalConverted = converted; + dataManager.queueAsync(() -> { + if (finalConverted) { + console.sendMessage("[" + getDescription().getName() + "] " + ChatColor.GREEN + "Conversion complete :)"); + } - private void saveToFile() { - storage.doSave(); + this.dataManager.getLocalHeads((heads) -> { + this.headManager.addLocalHeads(heads); + getLogger().info("Loaded " + headManager.getHeads().size() + " heads"); + }); + + this.dataManager.getDisabledHeads((ids) -> { + for (int id : ids) { + headManager.disableHead(new Head(id, false)); + } + }); + }, "create"); + }); } private void downloadHeads() { @@ -212,44 +270,6 @@ public class EpicHeads extends SongodaPlugin { headManager.addHead(head); } - if (storage.containsGroup("local")) { - for (StorageRow row : storage.getRowsByGroup("local")) { - String tagStr = row.get("category").asString(); - - Optional tagOptional = headManager.getCategories().stream() - .filter(t -> t.getName().equalsIgnoreCase(tagStr)).findFirst(); - - Category category = tagOptional.orElseGet(() -> new Category(tagStr)); - - Head head = new Head(row.get("id").asInt(), - row.get("name").asString(), - row.get("url").asString(), - category, - true, - null, - (byte) 0); - - if (!tagOptional.isPresent()) - headManager.addCategory(category); - headManager.addLocalHead(head); - } - } - - if (storage.containsGroup("disabled")) { - for (StorageRow row : storage.getRowsByGroup("disabled")) { - headManager.disableHead(new Head(row.get("id").asInt(), false)); - } - } - - // convert disabled heads - if (config.contains("Main.Disabled Global Heads")) { - for (int id : config.getIntegerList("Main.Disabled Global Heads")) { - EpicHeads.getInstance().getHeadManager().disableHead(new Head(id, false)); - } - config.set("Main.Disabled Global Heads", null); - } - - getLogger().info("Loaded " + headManager.getHeads().size() + " heads"); } catch (IOException | ParseException ex) { getLogger().warning(() -> { if (ex instanceof ParseException) { @@ -276,12 +296,9 @@ public class EpicHeads extends SongodaPlugin { @Override public void onConfigReload() { - saveToFile(); - this.setLocale(getConfig().getString("System.Language Mode"), true); this.locale.reloadMessages(); - saveToFile(); downloadHeads(); loadHeads(); } @@ -302,4 +319,12 @@ public class EpicHeads extends SongodaPlugin { public PlayerManager getPlayerManager() { return playerManager; } + + public DatabaseConnector getDatabaseConnector() { + return databaseConnector; + } + + public DataManager getDataManager() { + return dataManager; + } } diff --git a/src/main/java/com/songoda/epicheads/commands/CommandAdd.java b/src/main/java/com/songoda/epicheads/commands/CommandAdd.java index afd8d12..d35da39 100644 --- a/src/main/java/com/songoda/epicheads/commands/CommandAdd.java +++ b/src/main/java/com/songoda/epicheads/commands/CommandAdd.java @@ -38,7 +38,9 @@ public class CommandAdd extends AbstractCommand { Category category = categories.isEmpty() ? new Category(categoryStr) : categories.get(0); - headManager.addLocalHead(new Head(headManager.getNextLocalId(), name, url, category, true, null, (byte) 0)); + Head head = new Head(headManager.getNextLocalId(), name, url, category, true, null, (byte) 0); + headManager.addLocalHead(head); + plugin.getDataManager().createLocalHead(head); plugin.getLocale().getMessage("command.add.success") .processPlaceholder("name", name).sendPrefixedMessage(sender); diff --git a/src/main/java/com/songoda/epicheads/database/DataManager.java b/src/main/java/com/songoda/epicheads/database/DataManager.java new file mode 100644 index 0000000..6f85426 --- /dev/null +++ b/src/main/java/com/songoda/epicheads/database/DataManager.java @@ -0,0 +1,235 @@ +package com.songoda.epicheads.database; + +import com.google.common.reflect.TypeToken; +import com.google.gson.Gson; +import com.songoda.core.database.DataManagerAbstract; +import com.songoda.core.database.DatabaseConnector; +import com.songoda.epicheads.EpicHeads; +import com.songoda.epicheads.head.Category; +import com.songoda.epicheads.head.Head; +import com.songoda.epicheads.players.EPlayer; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; + +import java.sql.*; +import java.util.*; +import java.util.function.Consumer; + +public class DataManager extends DataManagerAbstract { + + public DataManager(DatabaseConnector connector, Plugin plugin) { + super(connector, plugin); + } + + public void updatePlayer(EPlayer ePlayer) { + Gson gson = new Gson(); + this.runAsync(() -> { + try (Connection connection = this.databaseConnector.getConnection()) { + String updatePlayer = "UPDATE " + this.getTablePrefix() + "players SET favorites = ? WHERE uuid = ?"; + try (PreparedStatement statement = connection.prepareStatement(updatePlayer)) { + statement.setString(1, gson.toJson(ePlayer.getFavorites())); + statement.setString(2, ePlayer.getUuid().toString()); + statement.executeUpdate(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + }); + } + + public void getPlayer(Player player, Consumer callback) { + Gson gson = new Gson(); + this.runAsync(() -> { + try (Connection connection = this.databaseConnector.getConnection()) { + String insertPlayer = "REPLACE INTO " + this.getTablePrefix() + "players (uuid, favorites) VALUES (?, ?)"; + String selectPlayers = "SELECT * FROM " + this.getTablePrefix() + "players WHERE uuid = ?"; + + try (PreparedStatement insert = connection.prepareStatement(insertPlayer); + PreparedStatement statement = connection.prepareStatement(selectPlayers)) { + insert.setString(1, player.getUniqueId().toString()); + insert.setString(2, gson.toJson(new ArrayList<>())); + insert.execute(); + + statement.setString(1, player.getUniqueId().toString()); + ResultSet result = statement.executeQuery(); + if (result.next()) { + UUID uuid = UUID.fromString(result.getString("uuid")); + List favorites = gson.fromJson(result.getString("favorites"), new TypeToken>() { + }.getType()); + + EPlayer ePlayer = new EPlayer(uuid, favorites); + this.sync(() -> callback.accept(ePlayer)); + } + } + } catch (SQLException e) { + e.printStackTrace(); + } + }); + } + + public void migratePlayers(List players) { + Gson gson = new Gson(); + this.runAsync(() -> { + try (Connection connection = this.databaseConnector.getConnection()) { + String insertPlayer = "REPLACE INTO " + this.getTablePrefix() + "players (uuid, favorites) VALUES (?, ?)"; + try (PreparedStatement insert = connection.prepareStatement(insertPlayer)) { + for (EPlayer player : players) { + insert.setString(1, player.getUuid().toString()); + insert.setString(2, gson.toJson(player.getFavorites())); + insert.addBatch(); + } + insert.executeBatch(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + }); + } + + public void createLocalHead(Head head) { + this.runAsync(() -> { + try (Connection connection = this.databaseConnector.getConnection()) { + String createHead = "INSERT INTO " + this.getTablePrefix() + "local_heads (category, name, url) VALUES (?, ?, ?)"; + try (PreparedStatement statement = connection.prepareStatement(createHead)) { + statement.setString(1, head.getCategory().getName()); + statement.setString(2, head.getName()); + statement.setString(3, head.getURL()); + statement.executeUpdate(); + } + + int furnaceId = this.lastInsertedId(connection, "local_heads"); + head.setId(furnaceId); + } catch (SQLException e) { + e.printStackTrace(); + } + }); + } + + public void getLocalHeads(Consumer> callback) { + this.runAsync(() -> { + try (Connection connection = this.databaseConnector.getConnection()) { + List heads = new ArrayList<>(); + + try (Statement statement = connection.createStatement()) { + String selectFurnaces = "SELECT * FROM " + this.getTablePrefix() + "local_heads"; + ResultSet result = statement.executeQuery(selectFurnaces); + while (result.next()) { + int id = result.getInt("id"); + + String categoryString = result.getString("category"); + Optional tagOptional = EpicHeads.getInstance().getHeadManager().getCategories().stream() + .filter(t -> t.getName().equalsIgnoreCase(categoryString)).findFirst(); + + Category category = tagOptional.orElseGet(() -> new Category(categoryString)); + + String name = result.getString("name"); + String url = result.getString("url"); + + Head head = new Head(id, + name, + url, + category, + true, + null, + (byte) 0); + + if (!tagOptional.isPresent()) + EpicHeads.getInstance().getHeadManager().addCategory(category); + + heads.add(head); + } + } + + this.sync(() -> callback.accept(heads)); + } catch (SQLException e) { + e.printStackTrace(); + } + }); + } + + public void updateLocalHead(Head head) { + this.runAsync(() -> { + try (Connection connection = this.databaseConnector.getConnection()) { + String updateHead = "UPDATE " + this.getTablePrefix() + "local_heads SET name = ?, url = ? WHERE id = ?"; + try (PreparedStatement statement = connection.prepareStatement(updateHead)) { + statement.setString(1, head.getName()); + statement.setString(2, head.getURL()); + statement.setInt(3, head.getId()); + statement.executeUpdate(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + }); + } + + public void disableHead(Head head) { + this.runAsync(() -> { + try (Connection connection = this.databaseConnector.getConnection()) { + String updateHead = "INSERT INTO " + this.getTablePrefix() + "disabled_heads (id) VALUES (?)"; + try (PreparedStatement statement = connection.prepareStatement(updateHead)) { + statement.setInt(1, head.getId()); + statement.executeUpdate(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + }); + } + + public void migrateDisabledHead(List heads) { + this.runAsync(() -> { + try (Connection connection = this.databaseConnector.getConnection()) { + String updateHead = "INSERT INTO " + this.getTablePrefix() + "disabled_heads (id) VALUES (?)"; + try (PreparedStatement statement = connection.prepareStatement(updateHead)) { + for (int head : heads) { + statement.setInt(1, head); + statement.addBatch(); + } + + statement.executeBatch(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + }); + } + + public void getDisabledHeads(Consumer> callback) { + this.runAsync(() -> { + try (Connection connection = this.databaseConnector.getConnection()) { + List heads = new ArrayList<>(); + + try (Statement statement = connection.createStatement()) { + String selectFurnaces = "SELECT * FROM " + this.getTablePrefix() + "disabled_heads"; + ResultSet result = statement.executeQuery(selectFurnaces); + while (result.next()) { + int id = result.getInt("id"); + heads.add(id); + } + } + + this.sync(() -> callback.accept(heads)); + } catch (SQLException e) { + e.printStackTrace(); + } + }); + } + + public void saveAllPlayers() { + Gson gson = new Gson(); + try (Connection connection = this.databaseConnector.getConnection()) { + String updatePlayer = "UPDATE " + this.getTablePrefix() + "players SET favorites = ? WHERE uuid = ?"; + try (PreparedStatement update = connection.prepareStatement(updatePlayer)) { + for (EPlayer player : EpicHeads.getInstance().getPlayerManager().getPlayers()) { + update.setString(1, gson.toJson(player.getFavorites())); + update.setString(2, player.getUuid().toString()); + update.addBatch(); + } + update.executeBatch(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/com/songoda/epicheads/database/migrations/_1_InitialMigration.java b/src/main/java/com/songoda/epicheads/database/migrations/_1_InitialMigration.java new file mode 100644 index 0000000..ac53232 --- /dev/null +++ b/src/main/java/com/songoda/epicheads/database/migrations/_1_InitialMigration.java @@ -0,0 +1,46 @@ +package com.songoda.epicheads.database.migrations; + +import com.songoda.core.database.DataMigration; +import com.songoda.core.database.MySQLConnector; +import com.songoda.epicheads.EpicHeads; + +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 = EpicHeads.getInstance().getDatabaseConnector() instanceof MySQLConnector ? " AUTO_INCREMENT" : ""; + + // Create player profiles + try (Statement statement = connection.createStatement()) { + statement.execute("CREATE TABLE " + tablePrefix + "players (" + + "uuid VARCHAR(36) PRIMARY KEY, " + + "favorites MEDIUMTEXT NOT NULL" + + ")"); + } + + // Create local heads table + try (Statement statement = connection.createStatement()) { + statement.execute("CREATE TABLE " + tablePrefix + "local_heads (" + + "id INTEGER PRIMARY KEY" + autoIncrement + ", " + + "category VARCHAR(48) NOT NULL, " + + "name VARCHAR(64) NOT NULL," + + "url VARCHAR(256) " + + ")"); + } + + // Create disabled heads table + try (Statement statement = connection.createStatement()) { + statement.execute("CREATE TABLE " + tablePrefix + "disabled_heads (" + + "id INTEGER PRIMARY KEY" + + ")"); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/songoda/epicheads/gui/GUIHeads.java b/src/main/java/com/songoda/epicheads/gui/GUIHeads.java index 2c83cfc..a7cf359 100644 --- a/src/main/java/com/songoda/epicheads/gui/GUIHeads.java +++ b/src/main/java/com/songoda/epicheads/gui/GUIHeads.java @@ -189,6 +189,7 @@ public class GUIHeads extends Gui { setButton(i + 9, item, (event) -> { if (event.clickType == ClickType.MIDDLE && player.hasPermission("epicheads.delete")) { plugin.getHeadManager().disableHead(head); + plugin.getDataManager().disableHead(head); heads.remove(head); showPage(); return; diff --git a/src/main/java/com/songoda/epicheads/head/Head.java b/src/main/java/com/songoda/epicheads/head/Head.java index 0025db5..cf956cf 100644 --- a/src/main/java/com/songoda/epicheads/head/Head.java +++ b/src/main/java/com/songoda/epicheads/head/Head.java @@ -14,7 +14,7 @@ import java.util.Objects; public class Head { - private final int id; + private int id; private String name = null; private String URL = null; private String pack = null; @@ -23,6 +23,15 @@ public class Head { private Category category; + public Head(String name, String URL, Category category, boolean local, String pack, byte staffPicked) { + this.name = name; + this.URL = URL; + this.category = category; + this.pack = pack; + this.staffPicked = staffPicked; + this.local = local; + } + public Head(int id, String name, String URL, Category category, boolean local, String pack, byte staffPicked) { this.id = id; this.name = name; @@ -42,6 +51,10 @@ public class Head { return id; } + public void setId(int id) { + this.id = id; + } + public String getName() { return name; } @@ -50,6 +63,14 @@ public class Head { return pack; } + public void setName(String name) { + this.name = name; + } + + public void setURL(String URL) { + this.URL = URL; + } + public String getURL() { if (URL == null) return "d23eaefbd581159384274cdbbd576ced82eb72423f2ea887124f9ed33a6872c"; diff --git a/src/main/java/com/songoda/epicheads/listeners/ItemListeners.java b/src/main/java/com/songoda/epicheads/listeners/ItemListeners.java index 53cd955..a688e4d 100644 --- a/src/main/java/com/songoda/epicheads/listeners/ItemListeners.java +++ b/src/main/java/com/songoda/epicheads/listeners/ItemListeners.java @@ -4,9 +4,11 @@ import com.songoda.core.compatibility.CompatibleMaterial; import com.songoda.core.utils.ItemUtils; import com.songoda.epicheads.EpicHeads; import com.songoda.epicheads.head.Head; +import com.songoda.epicheads.utils.ItemEconomy; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.entity.ItemSpawnEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -53,4 +55,11 @@ public class ItemListeners implements Listener { } } + @EventHandler + public void onPlace(BlockPlaceEvent event) { + if (ItemEconomy.isItem(event.getItemInHand())) { + event.setCancelled(true); + } + } + } diff --git a/src/main/java/com/songoda/epicheads/listeners/LoginListeners.java b/src/main/java/com/songoda/epicheads/listeners/LoginListeners.java index a70b3d3..2d03548 100644 --- a/src/main/java/com/songoda/epicheads/listeners/LoginListeners.java +++ b/src/main/java/com/songoda/epicheads/listeners/LoginListeners.java @@ -8,7 +8,9 @@ import com.songoda.epicheads.head.HeadManager; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerLoginEvent; +import org.bukkit.event.player.PlayerQuitEvent; import java.util.Optional; @@ -49,12 +51,24 @@ public class LoginListeners implements Listener { if (optional.isPresent()) { Head head = optional.get(); - id = head.getId(); - headManager.removeLocalHead(head); + head.setURL(url); + plugin.getDataManager().updateLocalHead(head); + return; } - headManager.addLocalHeads(new Head(id, player.getName(), url, tag, true, null, (byte) 0)); + Head head = new Head(id, player.getName(), url, tag, true, null, (byte) 0); + headManager.addLocalHead(head); + plugin.getDataManager().createLocalHead(head); + } + @EventHandler + public void onJoin(PlayerJoinEvent event) { + plugin.getDataManager().getPlayer(event.getPlayer(), ePlayer -> plugin.getPlayerManager().addPlayer(ePlayer)); + } + + @EventHandler + public void onQuit(PlayerQuitEvent event) { + plugin.getDataManager().updatePlayer(plugin.getPlayerManager().getPlayer(event.getPlayer())); } } diff --git a/src/main/java/com/songoda/epicheads/utils/ItemEconomy.java b/src/main/java/com/songoda/epicheads/utils/ItemEconomy.java index ff57a3a..be4799a 100644 --- a/src/main/java/com/songoda/epicheads/utils/ItemEconomy.java +++ b/src/main/java/com/songoda/epicheads/utils/ItemEconomy.java @@ -9,7 +9,7 @@ import org.bukkit.inventory.ItemStack; public class ItemEconomy extends Economy { - public boolean isItem(ItemStack itemStack) { + public static boolean isItem(ItemStack itemStack) { if (itemStack == null || itemStack.getType() == Material.AIR) return false; if (CompatibleMaterial.getMaterial(itemStack) == CompatibleMaterial.PLAYER_HEAD) From a4e67f6e24b0f9724ad17b4a1017216bb681b5d5 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Mon, 5 Sep 2022 16:55:49 -0400 Subject: [PATCH 4/4] Version 3.4.0 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 6d2663c..852d694 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ com.songoda EpicHeads - 3.3.2 + 3.4.0 clean install