Removed legacy storage system.

This commit is contained in:
Brianna 2020-09-08 15:31:23 -05:00
parent 2c9255bced
commit 9bb8033ba8
5 changed files with 24 additions and 390 deletions

View File

@ -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<Integer, Ticket> 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);

View File

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

View File

@ -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> 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;
}
}

View File

@ -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<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,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<String, Object> toSave = new HashMap<>();
private Map<String, Object> lastSave = null;
public StorageYaml(UltimateModeration 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();
}
}
}