diff --git a/src/main/java/com/songoda/ultimatemoderation/UltimateModeration.java b/src/main/java/com/songoda/ultimatemoderation/UltimateModeration.java index ee4235b..5aadbde 100644 --- a/src/main/java/com/songoda/ultimatemoderation/UltimateModeration.java +++ b/src/main/java/com/songoda/ultimatemoderation/UltimateModeration.java @@ -24,9 +24,6 @@ import com.songoda.ultimatemoderation.punish.template.Template; import com.songoda.ultimatemoderation.punish.template.TemplateManager; import com.songoda.ultimatemoderation.settings.Settings; import com.songoda.ultimatemoderation.staffchat.StaffChatManager; -import com.songoda.ultimatemoderation.storage.Storage; -import com.songoda.ultimatemoderation.storage.StorageRow; -import com.songoda.ultimatemoderation.storage.types.StorageYaml; import com.songoda.ultimatemoderation.tasks.SlowModeTask; import com.songoda.ultimatemoderation.tickets.Ticket; import com.songoda.ultimatemoderation.tickets.TicketManager; @@ -139,108 +136,6 @@ public class UltimateModeration extends SongodaPlugin { return; } - 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); - console.sendMessage("[" + getDescription().getName() + "] " + ChatColor.RED + "Conversion process starting DO NOT turn off your server... " + - "UltimateModeration hasn't fully loaded yet so its best users don't interact with the plugin until conversion completes."); - if (storage.containsGroup("templates")) { - for (StorageRow row : storage.getRowsByGroup("templates")) { - Template template = new Template(PunishmentType.valueOf(row.get("type").asString()), - row.get("duration").asLong(), - row.get("reason").asString(), - UUID.fromString(row.get("creator").asString()), - row.get("name").asString()); - dataManager.createTemplate(template); - } - } - - if (storage.containsGroup("punishments")) { - for (StorageRow row : storage.getRowsByGroup("punishments")) { - AppliedPunishment appliedPunishment = new AppliedPunishment(PunishmentType.valueOf(row.get("type").asString()), - row.get("duration").asLong(), - row.get("reason").asString(), - UUID.fromString(row.get("victim").asString()), - row.get("punisher").asObject() == null ? null : UUID.fromString(row.get("punisher").asString()), - row.get("expiration").asLong()); - dataManager.createAppliedPunishment(appliedPunishment); - } - } - - if (storage.containsGroup("notes")) { - for (StorageRow row : storage.getRowsByGroup("notes")) { - PunishmentNote note = new PunishmentNote(row.get("note").asString(), - UUID.fromString(row.get("author").asString()), - UUID.fromString(row.get("subject").asString()), - row.get("creation").asLong()); - dataManager.createNote(note); - } - } - - Map tickets = new HashMap<>(); - if (storage.containsGroup("tickets")) { - for (StorageRow row : storage.getRowsByGroup("tickets")) { - - int id = Integer.parseInt(row.get("id").asString()); - Ticket ticket = new Ticket( - UUID.fromString(row.get("player").asString()), - row.get("subject").asString(), - row.get("type").asString()); - ticket.setId(id); - ticket.setLocation(Methods.unserializeLocation(row.get("location").asString())); - ticket.setStatus(TicketStatus.valueOf(row.get("status").asString())); - tickets.put(id, ticket); - } - } - - if (storage.containsGroup("ticketresponses")) { - for (StorageRow row : storage.getRowsByGroup("ticketresponses")) { - int id = row.get("ticketid").asInt(); - TicketResponse ticketResponse = new TicketResponse( - UUID.fromString(row.get("author").asString()), - row.get("message").asString(), - Long.parseLong(row.get("posted").asString())); - - tickets.get(id).addResponse(ticketResponse); - ticketResponse.setTicketId(id); - } - } - for (Ticket ticket : tickets.values()) - dataManager.createTicket(ticket); - } - dataFile.delete(); - - final boolean convrted = converted; - getDataManager().queueAsync(() -> { - if (convrted) - console.sendMessage("[" + getDescription().getName() + "] " + ChatColor.GREEN + "Conversion complete :)"); - // Load data from DB - this.dataManager.getTemplates((templates) -> { - for (Template template : templates) { - this.templateManager.addTemplate(template); - } - }); - this.dataManager.getAppliedPunishments((appliedPunishments) -> { - for (AppliedPunishment punishment : appliedPunishments) - this.punishmentManager.getPlayer(punishment.getVictim()).addPunishment(punishment); - }); - this.dataManager.getNotes((notes) -> { - for (PunishmentNote note : notes) - this.punishmentManager.getPlayer(note.getSubject()).addNotes(note); - }); - this.dataManager.getTickets((tickets) -> { - for (Ticket ticket : tickets.values()) - this.ticketManager.addTicket(ticket); - }); - }, "create"); - }, 20); - // Register Listeners guiManager.init(); PluginManager pluginManager = Bukkit.getPluginManager(); @@ -263,6 +158,30 @@ public class UltimateModeration extends SongodaPlugin { SlowModeTask.startTask(this); } + @Override + public void onDataLoad() { + getDataManager().queueAsync(() -> { + // Load data from DB + this.dataManager.getTemplates((templates) -> { + for (Template template : templates) { + this.templateManager.addTemplate(template); + } + }); + this.dataManager.getAppliedPunishments((appliedPunishments) -> { + for (AppliedPunishment punishment : appliedPunishments) + this.punishmentManager.getPlayer(punishment.getVictim()).addPunishment(punishment); + }); + this.dataManager.getNotes((notes) -> { + for (PunishmentNote note : notes) + this.punishmentManager.getPlayer(note.getSubject()).addNotes(note); + }); + this.dataManager.getTickets((tickets) -> { + for (Ticket ticket : tickets.values()) + this.ticketManager.addTicket(ticket); + }); + }, "create"); + } + @Override public void onConfigReload() { this.setLocale(getConfig().getString("System.Language Mode"), true); diff --git a/src/main/java/com/songoda/ultimatemoderation/storage/Storage.java b/src/main/java/com/songoda/ultimatemoderation/storage/Storage.java deleted file mode 100644 index c1dc54a..0000000 --- a/src/main/java/com/songoda/ultimatemoderation/storage/Storage.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.songoda.ultimatemoderation.storage; - -import com.songoda.core.configuration.Config; -import com.songoda.ultimatemoderation.UltimateModeration; - -import java.util.List; - -public abstract class Storage { - - protected final UltimateModeration plugin; - protected final Config dataFile; - - public Storage(UltimateModeration 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(UltimateModeration instance) { - } - - public abstract void doSave(); - - public abstract void save(); - - public abstract void makeBackup(); - - public abstract void closeConnection(); - -} diff --git a/src/main/java/com/songoda/ultimatemoderation/storage/StorageItem.java b/src/main/java/com/songoda/ultimatemoderation/storage/StorageItem.java deleted file mode 100644 index a5a87c9..0000000 --- a/src/main/java/com/songoda/ultimatemoderation/storage/StorageItem.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.songoda.ultimatemoderation.storage; - -import org.bukkit.Material; - -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) { - String object = ""; - for (Material m : material) { - object += m.name() + ";"; - } - this.key = key; - this.object = object; - } - - public String getKey() { - return key; - } - - public String asString() { - if (object == null) return null; - return (String) object; - } - - public long asLong() { - if (object == null) return -1; - if (object instanceof Integer) { - return (long) ((int) object); - } - return (long) object; - } - - public boolean asBoolean() { - if (object == null) return false; - return (boolean) object; - } - - public int asInt() { - if (object == null) return 0; - return (int) object; - } - - public Object asObject() { - return object; - } - -} diff --git a/src/main/java/com/songoda/ultimatemoderation/storage/StorageRow.java b/src/main/java/com/songoda/ultimatemoderation/storage/StorageRow.java deleted file mode 100644 index 134b01f..0000000 --- a/src/main/java/com/songoda/ultimatemoderation/storage/StorageRow.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.songoda.ultimatemoderation.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/ultimatemoderation/storage/types/StorageYaml.java b/src/main/java/com/songoda/ultimatemoderation/storage/types/StorageYaml.java deleted file mode 100644 index 373ab26..0000000 --- a/src/main/java/com/songoda/ultimatemoderation/storage/types/StorageYaml.java +++ /dev/null @@ -1,159 +0,0 @@ -package com.songoda.ultimatemoderation.storage.types; - -import com.songoda.ultimatemoderation.UltimateModeration; -import com.songoda.ultimatemoderation.storage.Storage; -import com.songoda.ultimatemoderation.storage.StorageItem; -import com.songoda.ultimatemoderation.storage.StorageRow; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.MemorySection; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.ArrayDeque; -import java.util.ArrayList; -import java.util.Deque; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; - -public class StorageYaml extends Storage { - - private final Map toSave = new HashMap<>(); - private Map lastSave = null; - - public StorageYaml(UltimateModeration 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(); - } - } -}