Moved data to seperate files for every show

This commit is contained in:
stijnb1234 2021-02-02 11:33:58 +01:00
parent 609045e3d9
commit 2105904ba2
5 changed files with 89 additions and 44 deletions

View file

@ -4,22 +4,25 @@ import co.aikar.commands.PaperCommandManager;
import com.github.fierioziy.particlenativeapi.api.ParticleNativeAPI; import com.github.fierioziy.particlenativeapi.api.ParticleNativeAPI;
import com.github.fierioziy.particlenativeapi.api.utils.ParticleException; import com.github.fierioziy.particlenativeapi.api.utils.ParticleException;
import com.github.fierioziy.particlenativeapi.core.ParticleNativeCore; import com.github.fierioziy.particlenativeapi.core.ParticleNativeCore;
import nl.sbdeveloper.showapi.api.TriggerType;
import nl.sbdeveloper.showapi.commands.ShowCMD; import nl.sbdeveloper.showapi.commands.ShowCMD;
import nl.sbdeveloper.showapi.data.DataConversion;
import nl.sbdeveloper.showapi.data.DataSaving; import nl.sbdeveloper.showapi.data.DataSaving;
import nl.sbdeveloper.showapi.data.Shows; import nl.sbdeveloper.showapi.data.Shows;
import nl.sbdeveloper.showapi.utils.Inventory; import nl.sbdeveloper.showapi.utils.Inventory;
import nl.sbdeveloper.showapi.utils.YamlFile;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.inventivetalent.apihelper.APIManager; import org.inventivetalent.apihelper.APIManager;
import java.util.Arrays;
import java.util.stream.Collectors;
public final class ShowAPIPlugin extends JavaPlugin { public final class ShowAPIPlugin extends JavaPlugin {
private static ShowAPIPlugin instance; private static ShowAPIPlugin instance;
private final ShowAPI showAPI = new ShowAPI(); private final ShowAPI showAPI = new ShowAPI();
private static PaperCommandManager commandManager; private static PaperCommandManager commandManager;
private static YamlFile data;
private static ParticleNativeAPI particleAPI; private static ParticleNativeAPI particleAPI;
@Override @Override
@ -31,8 +34,7 @@ public final class ShowAPIPlugin extends JavaPlugin {
public void onEnable() { public void onEnable() {
instance = this; instance = this;
data = new YamlFile("data"); DataConversion.handle();
data.loadDefaults();
APIManager.initAPI(ShowAPI.class); APIManager.initAPI(ShowAPI.class);
@ -41,6 +43,9 @@ public final class ShowAPIPlugin extends JavaPlugin {
commandManager.registerCommand(new ShowCMD()); commandManager.registerCommand(new ShowCMD());
commandManager.getCommandCompletions().registerCompletion("showname", c -> Shows.getShowsMap().keySet());
commandManager.getCommandCompletions().registerStaticCompletion("showtype", Arrays.stream(TriggerType.values()).map(Enum::name).collect(Collectors.toList()));
try { try {
particleAPI = ParticleNativeCore.loadAPI(this); particleAPI = ParticleNativeCore.loadAPI(this);
} catch (ParticleException ex) { } catch (ParticleException ex) {
@ -73,10 +78,6 @@ public final class ShowAPIPlugin extends JavaPlugin {
return commandManager; return commandManager;
} }
public static YamlFile getData() {
return data;
}
public static ParticleNativeAPI getParticleAPI() { public static ParticleNativeAPI getParticleAPI() {
return particleAPI; return particleAPI;
} }

View file

@ -0,0 +1,46 @@
package nl.sbdeveloper.showapi.data;
import nl.sbdeveloper.showapi.ShowAPIPlugin;
import nl.sbdeveloper.showapi.api.TriggerTask;
import nl.sbdeveloper.showapi.utils.MainUtil;
import nl.sbdeveloper.showapi.utils.YamlFile;
import java.io.File;
public class DataConversion {
public static void handle() {
if (isOldSystem()) convert();
}
private static boolean isOldSystem() {
File dataFolder = ShowAPIPlugin.getInstance().getDataFolder();
File dataFile = new File(dataFolder, "data.yml");
return dataFile.exists();
}
private static void convert() {
File dataFolder = new File(ShowAPIPlugin.getInstance().getDataFolder(), "data");
if(!dataFolder.exists()) {
dataFolder.mkdirs();
}
YamlFile dataFile = new YamlFile("data");
for (String name : dataFile.getFile().getConfigurationSection("Shows").getKeys(false)) {
//STEP 1: Convert to new system.
YamlFile showFile = new YamlFile("data/" + name);
for (String id : dataFile.getFile().getConfigurationSection("Shows." + name).getKeys(false)) {
TriggerTask data = MainUtil.parseData(dataFile.getFile().getString("Shows." + name + "." + id + ".Type") + " " + dataFile.getFile().getString("Shows." + name + "." + id + ".Data"));
long time = dataFile.getFile().getLong("Shows." + name + "." + id + ".Time");
showFile.getFile().set(id + ".Time", time);
showFile.getFile().set(id + ".Type", data.getType().name());
showFile.getFile().set(id + ".Data", data.getDataString());
}
showFile.saveFile();
}
//STEP 2: Remove old storage.
File data = new File(ShowAPIPlugin.getInstance().getDataFolder(), "data.yml");
data.delete();
}
}

View file

@ -4,52 +4,47 @@ import nl.sbdeveloper.showapi.ShowAPIPlugin;
import nl.sbdeveloper.showapi.api.ShowCue; import nl.sbdeveloper.showapi.api.ShowCue;
import nl.sbdeveloper.showapi.api.TriggerTask; import nl.sbdeveloper.showapi.api.TriggerTask;
import nl.sbdeveloper.showapi.utils.MainUtil; import nl.sbdeveloper.showapi.utils.MainUtil;
import nl.sbdeveloper.showapi.utils.YamlFile;
import org.bukkit.craftbukkit.libs.org.apache.commons.io.FilenameUtils;
import java.util.ArrayList; import java.io.File;
import java.util.List; import java.util.*;
import java.util.Map;
import java.util.UUID;
public class DataSaving { public class DataSaving {
private static final Map<String, YamlFile> files = new HashMap<>();
public static Map<String, YamlFile> getFiles() {
return files;
}
public static void load() { public static void load() {
boolean newSystem = ShowAPIPlugin.getData().getFile().contains("NewestSystem"); File showsFolder = new File(ShowAPIPlugin.getInstance().getDataFolder(), "data");
for (File showFile : showsFolder.listFiles()) {
String showID = FilenameUtils.removeExtension(showFile.getName());
YamlFile showConfig = new YamlFile("data/" + showID);
files.put(showID, showConfig);
for (String name : ShowAPIPlugin.getData().getFile().getConfigurationSection("Shows").getKeys(false)) {
List<ShowCue> cues = new ArrayList<>(); List<ShowCue> cues = new ArrayList<>();
for (String id : showConfig.getFile().getKeys(false)) {
for (String id : ShowAPIPlugin.getData().getFile().getConfigurationSection("Shows." + name).getKeys(false)) {
UUID cueID = UUID.fromString(id); UUID cueID = UUID.fromString(id);
TriggerTask data = MainUtil.parseData(showConfig.getFile().getString(id + ".Type") + " " + showConfig.getFile().getString(id + ".Data"));
long time = showConfig.getFile().getLong(id + ".Time");
TriggerTask data = MainUtil.parseData(ShowAPIPlugin.getData().getFile().getString("Shows." + name + "." + id + ".Type") + " " + ShowAPIPlugin.getData().getFile().getString("Shows." + name + "." + id + ".Data")); cues.add(new ShowCue(cueID, time, data));
long time;
if (!newSystem) time = Math.round(ShowAPIPlugin.getData().getFile().getInt("Shows." + name + "." + id + ".Time") * 50);
else time = ShowAPIPlugin.getData().getFile().getLong("Shows." + name + "." + id + ".Time");
if (!newSystem) {
ShowAPIPlugin.getData().getFile().set("Shows." + name + "." + id + ".Time", time);
}
cues.add(new ShowCue(cueID, ShowAPIPlugin.getData().getFile().getLong("Shows." + name + "." + id + ".Time"), data));
} }
Shows.getShowsMap().put(showID, cues);
Shows.getShowsMap().put(name, cues);
}
if (!newSystem) {
ShowAPIPlugin.getData().getFile().set("NewestSystem", true);
ShowAPIPlugin.getData().saveFile();
} }
} }
public static void save() { public static void save() {
for (Map.Entry<String, List<ShowCue>> entry : Shows.getShowsMap().entrySet()) { for (Map.Entry<String, List<ShowCue>> entry : Shows.getShowsMap().entrySet()) {
YamlFile file = files.get(entry.getKey());
for (ShowCue cue : entry.getValue()) { for (ShowCue cue : entry.getValue()) {
ShowAPIPlugin.getData().getFile().set("Shows." + entry.getKey() + "." + cue.getCueID().toString() + ".Time", cue.getTime()); file.getFile().set(cue.getCueID().toString() + ".Time", cue.getTime());
ShowAPIPlugin.getData().getFile().set("Shows." + entry.getKey() + "." + cue.getCueID().toString() + ".Type", cue.getTask().getType().name()); file.getFile().set(cue.getCueID().toString() + ".Type", cue.getTask().getType().name());
ShowAPIPlugin.getData().getFile().set("Shows." + entry.getKey() + "." + cue.getCueID().toString() + ".Data", cue.getTask().getDataString()); file.getFile().set(cue.getCueID().toString() + ".Data", cue.getTask().getDataString());
} }
ShowAPIPlugin.getData().saveFile(); file.saveFile();
} }
} }
} }

View file

@ -3,8 +3,10 @@ package nl.sbdeveloper.showapi.data;
import nl.sbdeveloper.showapi.ShowAPIPlugin; import nl.sbdeveloper.showapi.ShowAPIPlugin;
import nl.sbdeveloper.showapi.api.ShowCue; import nl.sbdeveloper.showapi.api.ShowCue;
import nl.sbdeveloper.showapi.api.TriggerTask; import nl.sbdeveloper.showapi.api.TriggerTask;
import nl.sbdeveloper.showapi.utils.YamlFile;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -24,8 +26,8 @@ public class Shows {
public static void delete(String name) { public static void delete(String name) {
showsMap.remove(name); showsMap.remove(name);
ShowAPIPlugin.getData().getFile().set("Shows." + name, null); File data = new File(ShowAPIPlugin.getInstance().getDataFolder(), "data/" + name + ".yml");
ShowAPIPlugin.getData().saveFile(); data.delete();
} }
public static boolean exists(String name) { public static boolean exists(String name) {
@ -49,8 +51,10 @@ public class Shows {
point.getTask().remove(); point.getTask().remove();
showsMap.get(name).remove(point); showsMap.get(name).remove(point);
ShowAPIPlugin.getData().getFile().set("Shows." + name + "." + point.getCueID(), null); YamlFile data = DataSaving.getFiles().get(name);
ShowAPIPlugin.getData().saveFile();
data.getFile().set(point.getCueID().toString(), null);
data.saveFile();
} }
public static void startShow(String name) { public static void startShow(String name) {

View file

@ -1 +0,0 @@
Shows: {}