From 8f558e4976d8decaf0053c38f693a8b57869513d Mon Sep 17 00:00:00 2001 From: Brianna Date: Sat, 5 Sep 2020 09:59:13 -0500 Subject: [PATCH] Remove legacy conversion & properly configure the new core data loading. --- .../com/songoda/epichoppers/EpicHoppers.java | 131 +++------------- .../songoda/epichoppers/storage/Storage.java | 42 ----- .../epichoppers/storage/StorageItem.java | 94 ----------- .../epichoppers/storage/StorageRow.java | 28 ---- .../storage/types/StorageYaml.java | 148 ------------------ 5 files changed, 24 insertions(+), 419 deletions(-) delete mode 100644 src/main/java/com/songoda/epichoppers/storage/Storage.java delete mode 100644 src/main/java/com/songoda/epichoppers/storage/StorageItem.java delete mode 100644 src/main/java/com/songoda/epichoppers/storage/StorageRow.java delete mode 100644 src/main/java/com/songoda/epichoppers/storage/types/StorageYaml.java diff --git a/src/main/java/com/songoda/epichoppers/EpicHoppers.java b/src/main/java/com/songoda/epichoppers/EpicHoppers.java index 4d36017..ba43412 100644 --- a/src/main/java/com/songoda/epichoppers/EpicHoppers.java +++ b/src/main/java/com/songoda/epichoppers/EpicHoppers.java @@ -13,31 +13,34 @@ import com.songoda.core.hooks.EconomyManager; import com.songoda.core.locale.Locale; import com.songoda.core.nms.NmsManager; import com.songoda.core.nms.nbt.NBTItem; -import com.songoda.epichoppers.boost.BoostData; import com.songoda.epichoppers.boost.BoostManager; -import com.songoda.epichoppers.commands.*; +import com.songoda.epichoppers.commands.CommandBoost; +import com.songoda.epichoppers.commands.CommandGive; +import com.songoda.epichoppers.commands.CommandReload; +import com.songoda.epichoppers.commands.CommandSettings; import com.songoda.epichoppers.database.DataManager; import com.songoda.epichoppers.database.migrations._1_InitialMigration; import com.songoda.epichoppers.handlers.TeleportHandler; -import com.songoda.epichoppers.hopper.*; +import com.songoda.epichoppers.hopper.HopperManager; import com.songoda.epichoppers.hopper.levels.Level; import com.songoda.epichoppers.hopper.levels.LevelManager; -import com.songoda.epichoppers.hopper.levels.modules.*; import com.songoda.epichoppers.hopper.levels.modules.Module; -import com.songoda.epichoppers.listeners.*; +import com.songoda.epichoppers.hopper.levels.modules.ModuleAutoCrafting; +import com.songoda.epichoppers.hopper.levels.modules.ModuleAutoSell; +import com.songoda.epichoppers.hopper.levels.modules.ModuleBlockBreak; +import com.songoda.epichoppers.hopper.levels.modules.ModuleSuction; +import com.songoda.epichoppers.listeners.BlockListeners; +import com.songoda.epichoppers.listeners.EntityListeners; +import com.songoda.epichoppers.listeners.HopperListeners; +import com.songoda.epichoppers.listeners.InteractListeners; +import com.songoda.epichoppers.listeners.InventoryListeners; import com.songoda.epichoppers.player.PlayerDataManager; import com.songoda.epichoppers.settings.Settings; -import com.songoda.epichoppers.storage.Storage; -import com.songoda.epichoppers.storage.StorageRow; -import com.songoda.epichoppers.storage.types.StorageYaml; import com.songoda.epichoppers.tasks.HopTask; import com.songoda.epichoppers.utils.Methods; -import com.songoda.epichoppers.utils.TeleportTrigger; import com.songoda.skyblock.SkyBlock; import com.songoda.skyblock.permission.BasicPermission; import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.inventory.ItemStack; @@ -46,7 +49,10 @@ import org.bukkit.plugin.PluginManager; import java.io.File; import java.lang.reflect.InvocationTargetException; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; public class EpicHoppers extends SongodaPlugin { @@ -122,100 +128,6 @@ public class EpicHoppers extends SongodaPlugin { dataMigrationManager.runMigrations(); this.loadLevelManager(); - Bukkit.getScheduler().runTaskLaterAsynchronously(this, () -> { - // Legacy Data - 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("sync")) { - console.sendMessage("[" + getDescription().getName() + "] " + ChatColor.RED + "Conversion process starting DO NOT turn off your server... " + - "EpicHoppers hasn't fully loaded yet so its best users don't interact with the plugin until conversion completes."); - List hoppers = new ArrayList<>(); - for (StorageRow row : storage.getRowsByGroup("sync")) { - Location location = Methods.unserializeLocation(row.getKey()); - if (location == null) continue; - - int levelVal = row.get("level").asInt(); - Level level = levelManager.isLevel(levelVal) ? levelManager.getLevel(levelVal) : levelManager.getLowestLevel(); - - String playerStr = row.get("player").asString(); - String placedByStr = row.get("placedby").asString(); - UUID lastPlayer = playerStr == null ? null : UUID.fromString(row.get("player").asString()); - UUID placedBy = placedByStr == null ? null : UUID.fromString(placedByStr); - - List blockLoc = row.get("block").asStringList(); - List blocks = new ArrayList<>(); - if (blockLoc != null) { - for (String string : blockLoc) { - blocks.add(Methods.unserializeLocation(string)); - } - } - - Filter filter = new Filter(); - - List whiteList = row.get("whitelist").asItemStackList(); - List blackList = row.get("blacklist").asItemStackList(); - List voidList = row.get("void").asItemStackList(); - - List autoSellWhiteList = row.get("autosell-whitelist").asItemStackList(); - List autoSellBlackList = row.get("autosell-blacklist").asItemStackList(); - - String blackLoc = row.get("black").asString(); - Location black = blackLoc == null ? null : Methods.unserializeLocation(blackLoc); - - filter.setWhiteList(whiteList); - filter.setBlackList(blackList); - filter.setVoidList(voidList); - - filter.setAutoSellWhiteList(autoSellWhiteList); - filter.setAutoSellBlackList(autoSellBlackList); - - filter.setEndPoint(black); - - TeleportTrigger teleportTrigger = TeleportTrigger.valueOf(row.get("teleporttrigger").asString() == null ? "DISABLED" : row.get("teleporttrigger").asString()); - - hoppers.add(new HopperBuilder(location) - .setLevel(level) - .setLastPlayerOpened(lastPlayer) - .setPlacedBy(placedBy) - .addLinkedBlocks(LinkType.REGULAR, blocks.toArray(new Location[0])) - .setFilter(filter) - .setTeleportTrigger(teleportTrigger) - .build()); - } - dataManager.createHoppers(hoppers); - } - - // 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 convrted = converted; - getDataManager().queueAsync(() -> { - if (convrted) - console.sendMessage("[" + getDescription().getName() + "] " + ChatColor.GREEN + "Conversion complete :)"); - // Load data from DB - this.dataManager.getHoppers((hoppers) -> { - this.hopperManager.addHoppers(hoppers.values()); - this.dataManager.getBoosts((boosts) -> this.boostManager.addBoosts(boosts)); - }); - }, "create"); - }, 20); new HopTask(this); this.teleportHandler = new TeleportHandler(this); @@ -238,7 +150,7 @@ public class EpicHoppers extends SongodaPlugin { // Start auto save int saveInterval = Settings.AUTOSAVE.getInt() * 60 * 20; Bukkit.getScheduler().runTaskTimerAsynchronously(this, this::saveModules, saveInterval, saveInterval); - + // Hotfix for EH loading before FSB Bukkit.getScheduler().runTask(this, () -> { if (pluginManager.isPluginEnabled("FabledSkyBlock")) { @@ -254,6 +166,11 @@ public class EpicHoppers extends SongodaPlugin { @Override public void onDataLoad() { + // Load data from DB + this.dataManager.getHoppers((hoppers) -> { + this.hopperManager.addHoppers(hoppers.values()); + this.dataManager.getBoosts((boosts) -> this.boostManager.addBoosts(boosts)); + }); } @Override diff --git a/src/main/java/com/songoda/epichoppers/storage/Storage.java b/src/main/java/com/songoda/epichoppers/storage/Storage.java deleted file mode 100644 index 1588e90..0000000 --- a/src/main/java/com/songoda/epichoppers/storage/Storage.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.songoda.epichoppers.storage; - -import com.songoda.core.configuration.Config; -import com.songoda.epichoppers.EpicHoppers; -import com.songoda.epichoppers.boost.BoostData; -import com.songoda.epichoppers.hopper.Hopper; -import com.songoda.epichoppers.settings.Settings; -import com.songoda.epichoppers.utils.Methods; - -import java.util.ArrayList; -import java.util.List; - -public abstract class Storage { - - protected final EpicHoppers plugin; - protected final Config dataFile; - - public Storage(EpicHoppers plugin) { - this.plugin = plugin; - this.dataFile = new Config(plugin, "data.yml"); - this.dataFile.load(); - } - - public abstract boolean containsGroup(String group); - - public abstract List getRowsByGroup(String group); - - public abstract void prepareSaveItem(String group, StorageItem... items); - - public void updateData(EpicHoppers instance) { - // We're not saving data anymore. - } - - public abstract void doSave(); - - public abstract void save(); - - public abstract void makeBackup(); - - public abstract void closeConnection(); - -} diff --git a/src/main/java/com/songoda/epichoppers/storage/StorageItem.java b/src/main/java/com/songoda/epichoppers/storage/StorageItem.java deleted file mode 100644 index c8bde0f..0000000 --- a/src/main/java/com/songoda/epichoppers/storage/StorageItem.java +++ /dev/null @@ -1,94 +0,0 @@ -package com.songoda.epichoppers.storage; - -import com.songoda.epichoppers.utils.Methods; -import com.songoda.epichoppers.utils.Serializers; -import org.bukkit.Location; -import org.bukkit.inventory.ItemStack; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public class StorageItem { - - private final Object object; - private String key = null; - - public StorageItem(Object object) { - this.object = object; - } - - public StorageItem(String key, Object object) { - this.key = key; - this.object = object; - } - - public StorageItem(String key, List material) { - StringBuilder object = new StringBuilder(); - for (ItemStack m : material) { - object.append(Serializers.serialize(m)); - object.append(";;"); - } - this.key = key; - this.object = object.toString(); - } - - public StorageItem(String key, boolean type, List blocks) { - StringBuilder object = new StringBuilder(); - for (Location location : blocks) { - object.append(Methods.serializeLocation(location)); - object.append(";;"); - } - this.key = key; - this.object = object.toString(); - } - - public String getKey() { - return key; - } - - public String asString() { - if (object == null) return null; - return (String) object; - } - - public boolean asBoolean() { - if (object == null) return false; - if (object instanceof Integer) return (Integer) object == 1; - return (boolean) object; - } - - public int asInt() { - if (object == null) return 0; - return (int) object; - } - - public Object asObject() { - if (object == null) return null; - if (object instanceof Boolean) return (Boolean) object ? 1 : 0; - return object; - } - - public List asItemStackList() { - List list = new ArrayList<>(); - if (object == null) return list; - String obj = (String) object; - if (obj.equals("[]")) return list; - List sers = new ArrayList<>(Arrays.asList(obj.split(";;"))); - for (String ser : sers) { - list.add(Serializers.deserialize(ser)); - } - return list; - } - - public List asStringList() { - List list = new ArrayList<>(); - if (object == null) return list; - String obj = (String) object; - if (!((String) object).contains(";;")) { - list.add(obj); - return list; - } - return new ArrayList<>(Arrays.asList(obj.split(";;"))); - } -} diff --git a/src/main/java/com/songoda/epichoppers/storage/StorageRow.java b/src/main/java/com/songoda/epichoppers/storage/StorageRow.java deleted file mode 100644 index 26eff82..0000000 --- a/src/main/java/com/songoda/epichoppers/storage/StorageRow.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.songoda.epichoppers.storage; - -import java.util.Collections; -import java.util.Map; - -public class StorageRow { - private final String key; - - private final Map items; - - public StorageRow(String key, Map items) { - this.key = key; - this.items = items; - } - - public String getKey() { - return key; - } - - public Map getItems() { - return Collections.unmodifiableMap(items); - } - - public StorageItem get(String key) { - if (!items.containsKey(key) || items.get(key).asObject().toString().equals("")) return new StorageItem(null); - return items.get(key); - } -} diff --git a/src/main/java/com/songoda/epichoppers/storage/types/StorageYaml.java b/src/main/java/com/songoda/epichoppers/storage/types/StorageYaml.java deleted file mode 100644 index e0bd839..0000000 --- a/src/main/java/com/songoda/epichoppers/storage/types/StorageYaml.java +++ /dev/null @@ -1,148 +0,0 @@ -package com.songoda.epichoppers.storage.types; - -import com.songoda.epichoppers.EpicHoppers; -import com.songoda.epichoppers.storage.Storage; -import com.songoda.epichoppers.storage.StorageItem; -import com.songoda.epichoppers.storage.StorageRow; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.MemorySection; - -import java.io.*; -import java.util.*; - -public class StorageYaml extends Storage { - - private final Map toSave = new HashMap<>(); - private Map lastSave = null; - - public StorageYaml(EpicHoppers plugin) { - super(plugin); - } - - @Override - public boolean containsGroup(String group) { - return dataFile.contains("data." + group); - } - - @Override - public List getRowsByGroup(String group) { - List rows = new ArrayList<>(); - ConfigurationSection currentSection = dataFile.getConfigurationSection("data." + group); - for (String key : currentSection.getKeys(false)) { - - Map items = new HashMap<>(); - ConfigurationSection currentSection2 = dataFile.getConfigurationSection("data." + group + "." + key); - for (String key2 : currentSection2.getKeys(false)) { - String path = "data." + group + "." + key + "." + key2; - items.put(key2, new StorageItem(dataFile.get(path) instanceof MemorySection - ? convertToInLineList(path) : dataFile.get(path))); - } - if (items.isEmpty()) continue; - StorageRow row = new StorageRow(key, items); - rows.add(row); - } - return rows; - } - - private String convertToInLineList(String path) { - StringBuilder converted = new StringBuilder(); - for (String key : dataFile.getConfigurationSection(path).getKeys(false)) { - converted.append(key).append(":").append(dataFile.getInt(path + "." + key)).append(";"); - } - return converted.toString(); - } - - @Override - public void prepareSaveItem(String group, StorageItem... items) { - for (StorageItem item : items) { - if (item == null || item.asObject() == null) continue; - toSave.put("data." + group + "." + items[0].asString() + "." + item.getKey(), item.asObject()); - } - } - - @Override - public void doSave() { - this.updateData(plugin); - - if (lastSave == null) - lastSave = new HashMap<>(toSave); - - if (toSave.isEmpty()) return; - Map nextSave = new HashMap<>(toSave); - - this.makeBackup(); - this.save(); - - toSave.clear(); - lastSave.clear(); - lastSave.putAll(nextSave); - } - - @Override - public void save() { - try { - for (Map.Entry entry : lastSave.entrySet()) { - if (toSave.containsKey(entry.getKey())) { - Object newValue = toSave.get(entry.getKey()); - if (!entry.getValue().equals(newValue)) { - dataFile.set(entry.getKey(), newValue); - } - toSave.remove(entry.getKey()); - } else { - dataFile.set(entry.getKey(), null); - } - } - - for (Map.Entry entry : toSave.entrySet()) { - dataFile.set(entry.getKey(), entry.getValue()); - } - - dataFile.save(); - } catch (NullPointerException e) { - e.printStackTrace(); - } - } - - @Override - public void makeBackup() { - File data = new File(plugin.getDataFolder(), "data.yml"); - File dataClone = new File(plugin.getDataFolder(), "data-backup-" + System.currentTimeMillis() + ".yml"); - try { - if (data.exists()) - copyFile(data, dataClone); - } catch (IOException e) { - e.printStackTrace(); - } - Deque backups = new ArrayDeque<>(); - for (File file : Objects.requireNonNull(plugin.getDataFolder().listFiles())) { - if (file.getName().toLowerCase().contains("data-backup")) { - backups.add(file); - } - } - if (backups.size() > 3) { - backups.getFirst().delete(); - } - } - - @Override - public void closeConnection() { - dataFile.save(); - } - - private static void copyFile(File source, File dest) throws IOException { - InputStream is = null; - OutputStream os = null; - try { - is = new FileInputStream(source); - os = new FileOutputStream(dest); - byte[] buffer = new byte[1024]; - int length; - while ((length = is.read(buffer)) > 0) { - os.write(buffer, 0, length); - } - } finally { - is.close(); - os.close(); - } - } -}