diff --git a/src/main/java/nl/sbdeveloper/showapi/ShowAPIPlugin.java b/src/main/java/nl/sbdeveloper/showapi/ShowAPIPlugin.java index c13b3e3..3513ffb 100644 --- a/src/main/java/nl/sbdeveloper/showapi/ShowAPIPlugin.java +++ b/src/main/java/nl/sbdeveloper/showapi/ShowAPIPlugin.java @@ -4,22 +4,25 @@ import co.aikar.commands.PaperCommandManager; import com.github.fierioziy.particlenativeapi.api.ParticleNativeAPI; import com.github.fierioziy.particlenativeapi.api.utils.ParticleException; import com.github.fierioziy.particlenativeapi.core.ParticleNativeCore; +import nl.sbdeveloper.showapi.api.TriggerType; import nl.sbdeveloper.showapi.commands.ShowCMD; +import nl.sbdeveloper.showapi.data.DataConversion; import nl.sbdeveloper.showapi.data.DataSaving; import nl.sbdeveloper.showapi.data.Shows; import nl.sbdeveloper.showapi.utils.Inventory; -import nl.sbdeveloper.showapi.utils.YamlFile; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; import org.inventivetalent.apihelper.APIManager; +import java.util.Arrays; +import java.util.stream.Collectors; + public final class ShowAPIPlugin extends JavaPlugin { private static ShowAPIPlugin instance; private final ShowAPI showAPI = new ShowAPI(); - + private static PaperCommandManager commandManager; - private static YamlFile data; private static ParticleNativeAPI particleAPI; @Override @@ -31,8 +34,7 @@ public final class ShowAPIPlugin extends JavaPlugin { public void onEnable() { instance = this; - data = new YamlFile("data"); - data.loadDefaults(); + DataConversion.handle(); APIManager.initAPI(ShowAPI.class); @@ -41,6 +43,9 @@ public final class ShowAPIPlugin extends JavaPlugin { 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 { particleAPI = ParticleNativeCore.loadAPI(this); } catch (ParticleException ex) { @@ -73,10 +78,6 @@ public final class ShowAPIPlugin extends JavaPlugin { return commandManager; } - public static YamlFile getData() { - return data; - } - public static ParticleNativeAPI getParticleAPI() { return particleAPI; } diff --git a/src/main/java/nl/sbdeveloper/showapi/data/DataConversion.java b/src/main/java/nl/sbdeveloper/showapi/data/DataConversion.java new file mode 100644 index 0000000..323d182 --- /dev/null +++ b/src/main/java/nl/sbdeveloper/showapi/data/DataConversion.java @@ -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(); + } +} diff --git a/src/main/java/nl/sbdeveloper/showapi/data/DataSaving.java b/src/main/java/nl/sbdeveloper/showapi/data/DataSaving.java index 9098850..4d4289a 100644 --- a/src/main/java/nl/sbdeveloper/showapi/data/DataSaving.java +++ b/src/main/java/nl/sbdeveloper/showapi/data/DataSaving.java @@ -4,52 +4,47 @@ import nl.sbdeveloper.showapi.ShowAPIPlugin; import nl.sbdeveloper.showapi.api.ShowCue; import nl.sbdeveloper.showapi.api.TriggerTask; 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.util.List; -import java.util.Map; -import java.util.UUID; +import java.io.File; +import java.util.*; public class DataSaving { + private static final Map files = new HashMap<>(); + + public static Map getFiles() { + return files; + } + 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 cues = new ArrayList<>(); - - for (String id : ShowAPIPlugin.getData().getFile().getConfigurationSection("Shows." + name).getKeys(false)) { + for (String id : showConfig.getFile().getKeys(false)) { 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")); - - 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)); + cues.add(new ShowCue(cueID, time, data)); } - - Shows.getShowsMap().put(name, cues); - } - - if (!newSystem) { - ShowAPIPlugin.getData().getFile().set("NewestSystem", true); - ShowAPIPlugin.getData().saveFile(); + Shows.getShowsMap().put(showID, cues); } } public static void save() { for (Map.Entry> entry : Shows.getShowsMap().entrySet()) { + YamlFile file = files.get(entry.getKey()); for (ShowCue cue : entry.getValue()) { - ShowAPIPlugin.getData().getFile().set("Shows." + entry.getKey() + "." + cue.getCueID().toString() + ".Time", cue.getTime()); - ShowAPIPlugin.getData().getFile().set("Shows." + entry.getKey() + "." + 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() + ".Time", cue.getTime()); + file.getFile().set(cue.getCueID().toString() + ".Type", cue.getTask().getType().name()); + file.getFile().set(cue.getCueID().toString() + ".Data", cue.getTask().getDataString()); } - ShowAPIPlugin.getData().saveFile(); + file.saveFile(); } } } diff --git a/src/main/java/nl/sbdeveloper/showapi/data/Shows.java b/src/main/java/nl/sbdeveloper/showapi/data/Shows.java index 6be86b4..b7742c7 100644 --- a/src/main/java/nl/sbdeveloper/showapi/data/Shows.java +++ b/src/main/java/nl/sbdeveloper/showapi/data/Shows.java @@ -3,8 +3,10 @@ package nl.sbdeveloper.showapi.data; import nl.sbdeveloper.showapi.ShowAPIPlugin; import nl.sbdeveloper.showapi.api.ShowCue; import nl.sbdeveloper.showapi.api.TriggerTask; +import nl.sbdeveloper.showapi.utils.YamlFile; import org.bukkit.Bukkit; +import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -24,8 +26,8 @@ public class Shows { public static void delete(String name) { showsMap.remove(name); - ShowAPIPlugin.getData().getFile().set("Shows." + name, null); - ShowAPIPlugin.getData().saveFile(); + File data = new File(ShowAPIPlugin.getInstance().getDataFolder(), "data/" + name + ".yml"); + data.delete(); } public static boolean exists(String name) { @@ -49,8 +51,10 @@ public class Shows { point.getTask().remove(); showsMap.get(name).remove(point); - ShowAPIPlugin.getData().getFile().set("Shows." + name + "." + point.getCueID(), null); - ShowAPIPlugin.getData().saveFile(); + YamlFile data = DataSaving.getFiles().get(name); + + data.getFile().set(point.getCueID().toString(), null); + data.saveFile(); } public static void startShow(String name) { diff --git a/src/main/resources/data.yml b/src/main/resources/data.yml deleted file mode 100644 index 08dfca3..0000000 --- a/src/main/resources/data.yml +++ /dev/null @@ -1 +0,0 @@ -Shows: {} \ No newline at end of file