Added data saving
This commit is contained in:
parent
de7cc235b4
commit
7dffb45132
7 changed files with 163 additions and 7 deletions
|
@ -2,6 +2,8 @@ package nl.sbdeveloper.showapi;
|
||||||
|
|
||||||
import com.samjakob.spigui.SpiGUI;
|
import com.samjakob.spigui.SpiGUI;
|
||||||
import nl.sbdeveloper.showapi.commands.ShowCMD;
|
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.bukkit.plugin.java.JavaPlugin;
|
||||||
import org.inventivetalent.apihelper.APIManager;
|
import org.inventivetalent.apihelper.APIManager;
|
||||||
|
|
||||||
|
@ -10,6 +12,7 @@ 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 SpiGUI spiGUI;
|
private static SpiGUI spiGUI;
|
||||||
|
private static YamlFile data;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLoad() {
|
public void onLoad() {
|
||||||
|
@ -20,6 +23,10 @@ public final class ShowAPIPlugin extends JavaPlugin {
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
instance = this;
|
instance = this;
|
||||||
|
|
||||||
|
data = new YamlFile("data");
|
||||||
|
data.loadDefaults();
|
||||||
|
DataSaving.load();
|
||||||
|
|
||||||
APIManager.initAPI(ShowAPI.class);
|
APIManager.initAPI(ShowAPI.class);
|
||||||
|
|
||||||
spiGUI = new SpiGUI(this);
|
spiGUI = new SpiGUI(this);
|
||||||
|
@ -30,6 +37,9 @@ public final class ShowAPIPlugin extends JavaPlugin {
|
||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
instance = null;
|
instance = null;
|
||||||
|
|
||||||
|
DataSaving.save();
|
||||||
|
|
||||||
APIManager.disableAPI(ShowAPI.class);
|
APIManager.disableAPI(ShowAPI.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,4 +50,8 @@ public final class ShowAPIPlugin extends JavaPlugin {
|
||||||
public static SpiGUI getSpiGUI() {
|
public static SpiGUI getSpiGUI() {
|
||||||
return spiGUI;
|
return spiGUI;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static YamlFile getData() {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,19 +3,33 @@ package nl.sbdeveloper.showapi.api;
|
||||||
import nl.sbdeveloper.showapi.ShowAPIPlugin;
|
import nl.sbdeveloper.showapi.ShowAPIPlugin;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A cue point of a show
|
* A cue point of a show
|
||||||
*/
|
*/
|
||||||
public class ShowCue {
|
public class ShowCue {
|
||||||
|
private final UUID cueID;
|
||||||
private final int timeSeconds;
|
private final int timeSeconds;
|
||||||
private final TriggerData data;
|
private final TriggerData data;
|
||||||
private int taskID;
|
private int taskID;
|
||||||
|
|
||||||
public ShowCue(int timeSeconds, TriggerData data) {
|
public ShowCue(int timeSeconds, TriggerData data) {
|
||||||
|
this.cueID = UUID.randomUUID();
|
||||||
this.timeSeconds = timeSeconds;
|
this.timeSeconds = timeSeconds;
|
||||||
this.data = data;
|
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() {
|
public int getTimeSeconds() {
|
||||||
return timeSeconds;
|
return timeSeconds;
|
||||||
}
|
}
|
||||||
|
|
40
src/main/java/nl/sbdeveloper/showapi/data/DataSaving.java
Normal file
40
src/main/java/nl/sbdeveloper/showapi/data/DataSaving.java
Normal file
|
@ -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<ShowCue> 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<String, List<ShowCue>> 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,7 +1,9 @@
|
||||||
package nl.sbdeveloper.showapi.data;
|
package nl.sbdeveloper.showapi.data;
|
||||||
|
|
||||||
|
import nl.sbdeveloper.showapi.ShowAPIPlugin;
|
||||||
import nl.sbdeveloper.showapi.api.ShowCue;
|
import nl.sbdeveloper.showapi.api.ShowCue;
|
||||||
import nl.sbdeveloper.showapi.api.TriggerData;
|
import nl.sbdeveloper.showapi.api.TriggerData;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -12,10 +14,14 @@ public class Shows {
|
||||||
|
|
||||||
public static void create(String name) {
|
public static void create(String name) {
|
||||||
showsMap.put(name, new ArrayList<>());
|
showsMap.put(name, new ArrayList<>());
|
||||||
|
Bukkit.getScheduler().runTaskAsynchronously(ShowAPIPlugin.getInstance(), DataSaving::save);
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
ShowAPIPlugin.getData().saveFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean exists(String name) {
|
public static boolean exists(String name) {
|
||||||
|
@ -27,9 +33,19 @@ public class Shows {
|
||||||
return showsMap.get(name);
|
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) {
|
public static void addPoint(String name, int sec, TriggerData data) {
|
||||||
if (!exists(name)) return;
|
if (!exists(name)) return;
|
||||||
getPoints(name).add(new ShowCue(sec, data));
|
getPoints(name).add(new ShowCue(sec, data));
|
||||||
|
Bukkit.getScheduler().runTaskAsynchronously(ShowAPIPlugin.getInstance(), DataSaving::save);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void startShow(String name) {
|
public static void startShow(String name) {
|
||||||
|
@ -41,4 +57,8 @@ public class Shows {
|
||||||
if (!exists(name)) return;
|
if (!exists(name)) return;
|
||||||
getPoints(name).forEach(ShowCue::cancel);
|
getPoints(name).forEach(ShowCue::cancel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static HashMap<String, List<ShowCue>> getShowsMap() {
|
||||||
|
return showsMap;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,19 +10,15 @@ import org.bukkit.ChatColor;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class ShowCueGUI {
|
public class ShowCueGUI {
|
||||||
public static void openGUI(String name, Player p) {
|
public static void openGUI(String name, Player p) {
|
||||||
List<ShowCue> points = Shows.getPoints(name);
|
SGMenu menu = ShowAPIPlugin.getSpiGUI().create(ChatColor.DARK_AQUA + "Show Cue Manager:", MainUtil.pointsToRow(Shows.getPoints(name).size()));
|
||||||
|
|
||||||
SGMenu menu = ShowAPIPlugin.getSpiGUI().create(ChatColor.DARK_AQUA + "Show Cue Manager:", MainUtil.pointsToRow(points.size()));
|
|
||||||
menu.setAutomaticPaginationEnabled(true);
|
menu.setAutomaticPaginationEnabled(true);
|
||||||
|
|
||||||
for (ShowCue cue : points) {
|
for (ShowCue cue : Shows.getPoints(name)) {
|
||||||
SGButton button = new SGButton(MainUtil.pointToItem(cue))
|
SGButton button = new SGButton(MainUtil.pointToItem(cue))
|
||||||
.withListener((InventoryClickEvent e) -> {
|
.withListener((InventoryClickEvent e) -> {
|
||||||
points.remove(cue);
|
Shows.removePoint(name, cue);
|
||||||
|
|
||||||
openGUI(name, p); //Refresh
|
openGUI(name, p); //Refresh
|
||||||
});
|
});
|
||||||
|
|
71
src/main/java/nl/sbdeveloper/showapi/utils/YamlFile.java
Normal file
71
src/main/java/nl/sbdeveloper/showapi/utils/YamlFile.java
Normal file
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
1
src/main/resources/data.yml
Normal file
1
src/main/resources/data.yml
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Shows: {}
|
Loading…
Add table
Reference in a new issue