Remove legacy conversion & properly configure the new core data loading.

This commit is contained in:
Brianna 2020-09-05 09:59:13 -05:00
parent 1605df3cae
commit 8f558e4976
5 changed files with 24 additions and 419 deletions

View File

@ -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<Hopper> 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<String> blockLoc = row.get("block").asStringList();
List<Location> blocks = new ArrayList<>();
if (blockLoc != null) {
for (String string : blockLoc) {
blocks.add(Methods.unserializeLocation(string));
}
}
Filter filter = new Filter();
List<ItemStack> whiteList = row.get("whitelist").asItemStackList();
List<ItemStack> blackList = row.get("blacklist").asItemStackList();
List<ItemStack> voidList = row.get("void").asItemStackList();
List<ItemStack> autoSellWhiteList = row.get("autosell-whitelist").asItemStackList();
List<ItemStack> 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

View File

@ -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<StorageRow> 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();
}

View File

@ -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<ItemStack> 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<Location> 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<ItemStack> asItemStackList() {
List<ItemStack> list = new ArrayList<>();
if (object == null) return list;
String obj = (String) object;
if (obj.equals("[]")) return list;
List<String> sers = new ArrayList<>(Arrays.asList(obj.split(";;")));
for (String ser : sers) {
list.add(Serializers.deserialize(ser));
}
return list;
}
public List<String> asStringList() {
List<String> 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(";;")));
}
}

View File

@ -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<String, StorageItem> items;
public StorageRow(String key, Map<String, StorageItem> items) {
this.key = key;
this.items = items;
}
public String getKey() {
return key;
}
public Map<String, StorageItem> 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);
}
}

View File

@ -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<String, Object> toSave = new HashMap<>();
private Map<String, Object> lastSave = null;
public StorageYaml(EpicHoppers plugin) {
super(plugin);
}
@Override
public boolean containsGroup(String group) {
return dataFile.contains("data." + group);
}
@Override
public List<StorageRow> getRowsByGroup(String group) {
List<StorageRow> rows = new ArrayList<>();
ConfigurationSection currentSection = dataFile.getConfigurationSection("data." + group);
for (String key : currentSection.getKeys(false)) {
Map<String, StorageItem> 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<String, Object> nextSave = new HashMap<>(toSave);
this.makeBackup();
this.save();
toSave.clear();
lastSave.clear();
lastSave.putAll(nextSave);
}
@Override
public void save() {
try {
for (Map.Entry<String, Object> 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<String, Object> 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<File> 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();
}
}
}