diff --git a/src/main/java/nl/sbdeveloper/showapi/ShowAPIPlugin.java b/src/main/java/nl/sbdeveloper/showapi/ShowAPIPlugin.java index 415b03c..f33ddee 100644 --- a/src/main/java/nl/sbdeveloper/showapi/ShowAPIPlugin.java +++ b/src/main/java/nl/sbdeveloper/showapi/ShowAPIPlugin.java @@ -2,6 +2,8 @@ package nl.sbdeveloper.showapi; import com.samjakob.spigui.SpiGUI; import nl.sbdeveloper.showapi.commands.ShowCMD; +import nl.sbdeveloper.showapi.data.DataSaving; +import nl.sbdeveloper.showapi.utils.YamlFile; import org.bukkit.plugin.java.JavaPlugin; import org.inventivetalent.apihelper.APIManager; @@ -10,6 +12,7 @@ public final class ShowAPIPlugin extends JavaPlugin { private static ShowAPIPlugin instance; private final ShowAPI showAPI = new ShowAPI(); private static SpiGUI spiGUI; + private static YamlFile data; @Override public void onLoad() { @@ -20,6 +23,10 @@ public final class ShowAPIPlugin extends JavaPlugin { public void onEnable() { instance = this; + data = new YamlFile("data"); + data.loadDefaults(); + DataSaving.load(); + APIManager.initAPI(ShowAPI.class); spiGUI = new SpiGUI(this); @@ -30,6 +37,9 @@ public final class ShowAPIPlugin extends JavaPlugin { @Override public void onDisable() { instance = null; + + DataSaving.save(); + APIManager.disableAPI(ShowAPI.class); } @@ -40,4 +50,8 @@ public final class ShowAPIPlugin extends JavaPlugin { public static SpiGUI getSpiGUI() { return spiGUI; } + + public static YamlFile getData() { + return data; + } } diff --git a/src/main/java/nl/sbdeveloper/showapi/api/ShowCue.java b/src/main/java/nl/sbdeveloper/showapi/api/ShowCue.java index 39ff887..1655c59 100644 --- a/src/main/java/nl/sbdeveloper/showapi/api/ShowCue.java +++ b/src/main/java/nl/sbdeveloper/showapi/api/ShowCue.java @@ -3,19 +3,33 @@ package nl.sbdeveloper.showapi.api; import nl.sbdeveloper.showapi.ShowAPIPlugin; import org.bukkit.Bukkit; +import java.util.UUID; + /** * A cue point of a show */ public class ShowCue { + private final UUID cueID; private final int timeSeconds; private final TriggerData data; private int taskID; public ShowCue(int timeSeconds, TriggerData data) { + this.cueID = UUID.randomUUID(); this.timeSeconds = timeSeconds; this.data = data; } + public ShowCue(UUID uuid, int timeSeconds, TriggerData data) { + this.cueID = uuid; + this.timeSeconds = timeSeconds; + this.data = data; + } + + public UUID getCueID() { + return cueID; + } + public int getTimeSeconds() { return timeSeconds; } diff --git a/src/main/java/nl/sbdeveloper/showapi/data/DataSaving.java b/src/main/java/nl/sbdeveloper/showapi/data/DataSaving.java new file mode 100644 index 0000000..8003dda --- /dev/null +++ b/src/main/java/nl/sbdeveloper/showapi/data/DataSaving.java @@ -0,0 +1,40 @@ +package nl.sbdeveloper.showapi.data; + +import nl.sbdeveloper.showapi.ShowAPIPlugin; +import nl.sbdeveloper.showapi.api.ShowCue; +import nl.sbdeveloper.showapi.api.TriggerData; +import nl.sbdeveloper.showapi.utils.MainUtil; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +public class DataSaving { + public static void load() { + 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)) { + UUID cueID = UUID.fromString(id); + + TriggerData data = MainUtil.parseData(ShowAPIPlugin.getData().getFile().getString("Shows." + name + "." + id + ".Type") + " " + ShowAPIPlugin.getData().getFile().getString("Shows." + name + "." + id + ".Data")); + + cues.add(new ShowCue(cueID, ShowAPIPlugin.getData().getFile().getInt("Shows." + name + "." + id + ".Time"), data)); + } + + Shows.getShowsMap().put(name, cues); + } + } + + public static void save() { + for (Map.Entry> entry : Shows.getShowsMap().entrySet()) { + for (ShowCue cue : entry.getValue()) { + ShowAPIPlugin.getData().getFile().set("Shows." + entry.getKey() + "." + cue.getCueID().toString() + ".Time", cue.getTimeSeconds()); + ShowAPIPlugin.getData().getFile().set("Shows." + entry.getKey() + "." + cue.getCueID().toString() + ".Type", cue.getData().getType().name()); + ShowAPIPlugin.getData().getFile().set("Shows." + entry.getKey() + "." + cue.getCueID().toString() + ".Data", cue.getData().getDataString()); + } + ShowAPIPlugin.getData().saveFile(); + } + } +} diff --git a/src/main/java/nl/sbdeveloper/showapi/data/Shows.java b/src/main/java/nl/sbdeveloper/showapi/data/Shows.java index cbf0d42..af064a1 100644 --- a/src/main/java/nl/sbdeveloper/showapi/data/Shows.java +++ b/src/main/java/nl/sbdeveloper/showapi/data/Shows.java @@ -1,7 +1,9 @@ package nl.sbdeveloper.showapi.data; +import nl.sbdeveloper.showapi.ShowAPIPlugin; import nl.sbdeveloper.showapi.api.ShowCue; import nl.sbdeveloper.showapi.api.TriggerData; +import org.bukkit.Bukkit; import java.util.ArrayList; import java.util.HashMap; @@ -12,10 +14,14 @@ public class Shows { public static void create(String name) { showsMap.put(name, new ArrayList<>()); + Bukkit.getScheduler().runTaskAsynchronously(ShowAPIPlugin.getInstance(), DataSaving::save); } public static void delete(String name) { showsMap.remove(name); + + ShowAPIPlugin.getData().getFile().set("Shows." + name, null); + ShowAPIPlugin.getData().saveFile(); } public static boolean exists(String name) { @@ -27,9 +33,19 @@ public class Shows { return showsMap.get(name); } + public static void removePoint(String name, ShowCue point) { + if (!exists(name)) return; + + showsMap.get(name).remove(point); + + ShowAPIPlugin.getData().getFile().set("Shows." + name + "." + point.getCueID(), null); + ShowAPIPlugin.getData().saveFile(); + } + public static void addPoint(String name, int sec, TriggerData data) { if (!exists(name)) return; getPoints(name).add(new ShowCue(sec, data)); + Bukkit.getScheduler().runTaskAsynchronously(ShowAPIPlugin.getInstance(), DataSaving::save); } public static void startShow(String name) { @@ -41,4 +57,8 @@ public class Shows { if (!exists(name)) return; getPoints(name).forEach(ShowCue::cancel); } + + public static HashMap> getShowsMap() { + return showsMap; + } } diff --git a/src/main/java/nl/sbdeveloper/showapi/gui/ShowCueGUI.java b/src/main/java/nl/sbdeveloper/showapi/gui/ShowCueGUI.java index e150f29..ac38fb2 100644 --- a/src/main/java/nl/sbdeveloper/showapi/gui/ShowCueGUI.java +++ b/src/main/java/nl/sbdeveloper/showapi/gui/ShowCueGUI.java @@ -10,19 +10,15 @@ import org.bukkit.ChatColor; import org.bukkit.entity.Player; import org.bukkit.event.inventory.InventoryClickEvent; -import java.util.List; - public class ShowCueGUI { public static void openGUI(String name, Player p) { - List points = Shows.getPoints(name); - - SGMenu menu = ShowAPIPlugin.getSpiGUI().create(ChatColor.DARK_AQUA + "Show Cue Manager:", MainUtil.pointsToRow(points.size())); + SGMenu menu = ShowAPIPlugin.getSpiGUI().create(ChatColor.DARK_AQUA + "Show Cue Manager:", MainUtil.pointsToRow(Shows.getPoints(name).size())); menu.setAutomaticPaginationEnabled(true); - for (ShowCue cue : points) { + for (ShowCue cue : Shows.getPoints(name)) { SGButton button = new SGButton(MainUtil.pointToItem(cue)) .withListener((InventoryClickEvent e) -> { - points.remove(cue); + Shows.removePoint(name, cue); openGUI(name, p); //Refresh }); diff --git a/src/main/java/nl/sbdeveloper/showapi/utils/YamlFile.java b/src/main/java/nl/sbdeveloper/showapi/utils/YamlFile.java new file mode 100644 index 0000000..d3ee5ce --- /dev/null +++ b/src/main/java/nl/sbdeveloper/showapi/utils/YamlFile.java @@ -0,0 +1,71 @@ +package nl.sbdeveloper.showapi.utils; + +import nl.sbdeveloper.showapi.ShowAPIPlugin; +import org.bukkit.Bukkit; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; + +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Reader; +import java.nio.charset.StandardCharsets; +import java.util.Objects; + +public class YamlFile { + //SBYamlFile file = new SBYamlFile(this, "data"); + + private FileConfiguration fileConfiguration; + private File file; + private final String name; + + public YamlFile(String name) { + this.name = name; + + if (!ShowAPIPlugin.getInstance().getDataFolder().exists()) { + if (!ShowAPIPlugin.getInstance().getDataFolder().mkdir()) { + Bukkit.getLogger().severe("[ShowAPI] Couldn't generate the pluginfolder!"); + return; + } + } + + this.file = new File(ShowAPIPlugin.getInstance().getDataFolder(), name + ".yml"); + if (!this.file.exists()) { + try { + if (!this.file.createNewFile()) { + Bukkit.getLogger().severe("[ShowAPI] Couldn't generate the " + name + ".yml!"); + return; + } + Bukkit.getLogger().info("[ShowAPI] Generating the " + name + ".yml!"); + } catch (IOException e) { + Bukkit.getLogger().severe("[ShowAPI] Couldn't generate the " + name + ".yml!"); + return; + } + } + this.fileConfiguration = YamlConfiguration.loadConfiguration(this.file); + } + + public void loadDefaults() { + Reader defConfigStream1 = new InputStreamReader(Objects.requireNonNull(ShowAPIPlugin.getInstance().getResource(name + ".yml"), "Resource is null"), StandardCharsets.UTF_8); + YamlConfiguration defConfig1 = YamlConfiguration.loadConfiguration(defConfigStream1); + getFile().setDefaults(defConfig1); + getFile().options().copyDefaults(true); + saveFile(); + } + + public FileConfiguration getFile() { + return this.fileConfiguration; + } + + public void saveFile() { + try { + this.fileConfiguration.save(this.file); + } catch (IOException e) { + Bukkit.getLogger().severe("[ShowAPI] Couldn't save the " + name + ".yml!"); + } + } + + public void reloadConfig() { + this.fileConfiguration = YamlConfiguration.loadConfiguration(this.file); + } +} diff --git a/src/main/resources/data.yml b/src/main/resources/data.yml new file mode 100644 index 0000000..08dfca3 --- /dev/null +++ b/src/main/resources/data.yml @@ -0,0 +1 @@ +Shows: {} \ No newline at end of file