From a4dbdab581ea330b26eef43c54b42d60bd80adee Mon Sep 17 00:00:00 2001 From: Stijn Bannink Date: Fri, 10 Nov 2023 12:11:32 +0100 Subject: [PATCH] v1.6: Small refactor of the API --- README.md | 2 +- pom.xml | 2 +- .../showcontrol/ShowControlPlugin.java | 2 +- .../sbdevelopment/showcontrol/api/SCAPI.java | 79 ++-------------- .../showcontrol/api/points/ShowCuePoint.java | 27 ++++++ .../showcontrol/api/shows/Show.java | 92 +++++++++++++++++++ .../showcontrol/api/shows/package-info.java | 4 + .../showcontrol/commands/ShowCMD.java | 6 +- .../showcontrol/data/DataStorage.java | 13 ++- .../showcontrol/gui/ShowCueGUI.java | 15 ++- .../showcontrol/utils/MainUtil.java | 17 ---- 11 files changed, 157 insertions(+), 102 deletions(-) create mode 100644 src/main/java/tech/sbdevelopment/showcontrol/api/shows/Show.java create mode 100644 src/main/java/tech/sbdevelopment/showcontrol/api/shows/package-info.java diff --git a/README.md b/README.md index 4206121..1e6158c 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ The project is on our Maven repository, add the following to your `pom.xml` file tech.sbdevelopment ShowControl - 1.5 + 1.6 provided ``` diff --git a/pom.xml b/pom.xml index acd70ba..10ad049 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ tech.sbdevelopment ShowControl - 1.5 + 1.6 jar ShowControl https://sbdevelopment.tech diff --git a/src/main/java/tech/sbdevelopment/showcontrol/ShowControlPlugin.java b/src/main/java/tech/sbdevelopment/showcontrol/ShowControlPlugin.java index 8a61674..c34358c 100644 --- a/src/main/java/tech/sbdevelopment/showcontrol/ShowControlPlugin.java +++ b/src/main/java/tech/sbdevelopment/showcontrol/ShowControlPlugin.java @@ -53,7 +53,7 @@ public final class ShowControlPlugin extends JavaPlugin { public void onDisable() { getLogger().info("Saving data..."); DataStorage.save(); - SCAPI.getShowsMap().values().forEach(show -> show.forEach(showCue -> showCue.getData().remove())); + SCAPI.getShowsMap().values().forEach(show -> show.getCuePoints().forEach(showCue -> showCue.getData().remove())); getLogger().info("Plugin disabled!"); instance = null; diff --git a/src/main/java/tech/sbdevelopment/showcontrol/api/SCAPI.java b/src/main/java/tech/sbdevelopment/showcontrol/api/SCAPI.java index 8d1652c..4160a6e 100644 --- a/src/main/java/tech/sbdevelopment/showcontrol/api/SCAPI.java +++ b/src/main/java/tech/sbdevelopment/showcontrol/api/SCAPI.java @@ -12,6 +12,7 @@ import tech.sbdevelopment.showcontrol.ShowControlPlugin; import tech.sbdevelopment.showcontrol.api.exceptions.InvalidTriggerException; import tech.sbdevelopment.showcontrol.api.exceptions.TooFewArgumentsException; import tech.sbdevelopment.showcontrol.api.points.ShowCuePoint; +import tech.sbdevelopment.showcontrol.api.shows.Show; import tech.sbdevelopment.showcontrol.api.triggers.Trigger; import tech.sbdevelopment.showcontrol.api.triggers.TriggerIdentifier; import tech.sbdevelopment.showcontrol.data.DataStorage; @@ -35,20 +36,18 @@ public class SCAPI { */ @Getter private static final Map defaultTriggers = new HashMap<>(); + /** * A map of all triggers, used for creating new triggers. */ @Getter private static final Map> triggers = new HashMap<>(); + /** - * A map of all shows with their cue points. + * A map of all shows. */ @Getter - private static final HashMap> showsMap = new HashMap<>(); - /** - * A map of all show timers. - */ - private static final HashMap showTimers = new HashMap<>(); + private static final HashMap showsMap = new HashMap<>(); /** * Index all triggers in a package. Call this method in your onEnable method. @@ -131,7 +130,7 @@ public class SCAPI { * @param name The name of the show. */ public static void create(String name) { - showsMap.put(name, new ArrayList<>()); + showsMap.put(name, new Show(name)); DataStorage.save(); } @@ -158,70 +157,12 @@ public class SCAPI { } /** - * Get the points of a show. + * Get a show by name. * * @param name The name of the show. - * @return The points of the show. + * @return An optional of the show. */ - public static List getPoints(String name) { - if (!exists(name)) return new ArrayList<>(); - return showsMap.get(name); - } - - /** - * Add a point to a show. - * - * @param name The name of the show. - * @param time The time of the point. - * @param data The data of the point. - */ - public static void addPoint(String name, Long time, Trigger data) { - if (!exists(name)) return; - getPoints(name).add(new ShowCuePoint(time, data)); - DataStorage.save(); - } - - /** - * Remove a point from a show. - * - * @param name The name of the show. - * @param point The point to remove. - */ - public static void removePoint(String name, ShowCuePoint point) { - if (!exists(name)) return; - - point.getData().remove(); - showsMap.get(name).remove(point); - - YamlFile data = DataStorage.getFiles().get(name); - - data.getFile().set(point.getCueID().toString(), null); - data.saveFile(); - } - - /** - * Start a show. - * - * @param name The name of the show. - */ - public static void startShow(String name) { - if (!exists(name)) return; - ScheduledExecutorService showTimer = Executors.newSingleThreadScheduledExecutor(); - for (ShowCuePoint point : getPoints(name)) { - showTimer.schedule(() -> Bukkit.getScheduler().runTask(ShowControlPlugin.getInstance(), () -> point.getData().trigger()), point.getTime(), TimeUnit.MILLISECONDS); - } - showTimers.put(name, showTimer); - } - - /** - * Cancel a show. - * - * @param name The name of the show. - */ - public static void cancelShow(String name) { - if (!exists(name)) return; - if (!showTimers.containsKey(name)) return; - ScheduledExecutorService showTimer = showTimers.get(name); - showTimer.shutdownNow(); + public static Optional getShow(String name) { + return Optional.of(showsMap.get(name)); } } diff --git a/src/main/java/tech/sbdevelopment/showcontrol/api/points/ShowCuePoint.java b/src/main/java/tech/sbdevelopment/showcontrol/api/points/ShowCuePoint.java index 0731ed9..a7ef9b3 100644 --- a/src/main/java/tech/sbdevelopment/showcontrol/api/points/ShowCuePoint.java +++ b/src/main/java/tech/sbdevelopment/showcontrol/api/points/ShowCuePoint.java @@ -2,10 +2,20 @@ package tech.sbdevelopment.showcontrol.api.points; import lombok.AllArgsConstructor; import lombok.Getter; +import org.bukkit.ChatColor; +import org.bukkit.inventory.ItemStack; +import org.bukkit.util.ChatPaginator; import tech.sbdevelopment.showcontrol.api.triggers.Trigger; +import tech.sbdevelopment.showcontrol.api.triggers.TriggerIdentifier; +import tech.sbdevelopment.showcontrol.utils.ItemBuilder; +import tech.sbdevelopment.showcontrol.utils.TimeUtil; +import java.util.ArrayList; +import java.util.List; import java.util.UUID; +import static tech.sbdevelopment.showcontrol.utils.MainUtil.capitalize; + /** * A cue point of a show */ @@ -36,4 +46,21 @@ public class ShowCuePoint { public ShowCuePoint(Long time, Trigger data) { this(UUID.randomUUID(), time, data); } + + public ItemStack getGUIItem() { + TriggerIdentifier identifier = data.getClass().getAnnotation(TriggerIdentifier.class); + + List lores = new ArrayList<>(); + lores.add(ChatColor.GREEN + "Type: " + ChatColor.AQUA + capitalize(data.getTriggerId())); + lores.add(ChatColor.GREEN + "Data:"); + for (String str : ChatPaginator.paginate(data.getDataString(), 20).getLines()) { + lores.add(ChatColor.AQUA + ChatColor.stripColor(str)); + } + lores.add(""); + lores.add(ChatColor.RED + ChatColor.BOLD.toString() + "Click to remove!"); + + return new ItemBuilder(identifier.item()) + .displayname(ChatColor.LIGHT_PURPLE + ChatColor.ITALIC.toString() + "TimeCode: " + TimeUtil.makeReadable(getTime())) + .lore(lores).getItemStack(); + } } \ No newline at end of file diff --git a/src/main/java/tech/sbdevelopment/showcontrol/api/shows/Show.java b/src/main/java/tech/sbdevelopment/showcontrol/api/shows/Show.java new file mode 100644 index 0000000..ebcd815 --- /dev/null +++ b/src/main/java/tech/sbdevelopment/showcontrol/api/shows/Show.java @@ -0,0 +1,92 @@ +package tech.sbdevelopment.showcontrol.api.shows; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.bukkit.Bukkit; +import tech.sbdevelopment.showcontrol.ShowControlPlugin; +import tech.sbdevelopment.showcontrol.api.points.ShowCuePoint; +import tech.sbdevelopment.showcontrol.api.triggers.Trigger; +import tech.sbdevelopment.showcontrol.data.DataStorage; +import tech.sbdevelopment.showcontrol.utils.YamlFile; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +/** + * This class represents a show. + */ +@RequiredArgsConstructor +public class Show { + /** + * The name of the show. + */ + @Getter + private final String name; + /** + * A list of all cue points in the show. + */ + @Getter + private List cuePoints = new ArrayList<>(); + /** + * The timer used to run the show. + */ + private ScheduledExecutorService timer; + + /** + * Create a new show. + * + * @param name The name of the show. + * @param cuePoints A list of all cue points in the show. + */ + public Show(String name, List cuePoints) { + this.name = name; + this.cuePoints = cuePoints; + } + + /** + * Add a point to the show. + * + * @param time The time of the point. + * @param data The data of the point. + */ + public void addPoint(Long time, Trigger data) { + cuePoints.add(new ShowCuePoint(time, data)); + DataStorage.save(); + } + + /** + * Remove a point from the show. + * + * @param point The point to remove. + */ + public void removePoint(ShowCuePoint point) { + point.getData().remove(); + cuePoints.remove(point); + + YamlFile data = DataStorage.getFiles().get(name); + + data.getFile().set(point.getCueID().toString(), null); + data.saveFile(); + } + + /** + * Start the show. + */ + public void start() { + timer = Executors.newSingleThreadScheduledExecutor(); + for (ShowCuePoint point : cuePoints) { + timer.schedule(() -> Bukkit.getScheduler().runTask(ShowControlPlugin.getInstance(), () -> point.getData().trigger()), point.getTime(), TimeUnit.MILLISECONDS); + } + } + + /** + * Cancel the show. + */ + public void cancel() { + if (timer != null) timer.shutdownNow(); + timer = null; + } +} diff --git a/src/main/java/tech/sbdevelopment/showcontrol/api/shows/package-info.java b/src/main/java/tech/sbdevelopment/showcontrol/api/shows/package-info.java new file mode 100644 index 0000000..f3ea30e --- /dev/null +++ b/src/main/java/tech/sbdevelopment/showcontrol/api/shows/package-info.java @@ -0,0 +1,4 @@ +/** + * This package contains all classes related to shows. + */ +package tech.sbdevelopment.showcontrol.api.shows; \ No newline at end of file diff --git a/src/main/java/tech/sbdevelopment/showcontrol/commands/ShowCMD.java b/src/main/java/tech/sbdevelopment/showcontrol/commands/ShowCMD.java index 408e930..3fb2fbd 100644 --- a/src/main/java/tech/sbdevelopment/showcontrol/commands/ShowCMD.java +++ b/src/main/java/tech/sbdevelopment/showcontrol/commands/ShowCMD.java @@ -85,7 +85,7 @@ public class ShowCMD extends BaseCommand { return; } - SCAPI.addPoint(name, timeMilli, data); + SCAPI.getShow(name).get().addPoint(timeMilli, data); sender.sendMessage(ChatColor.GREEN + "The show " + ChatColor.WHITE + name + ChatColor.GREEN + " now contains an extra point!"); } @@ -98,7 +98,7 @@ public class ShowCMD extends BaseCommand { return; } - SCAPI.startShow(name); + SCAPI.getShow(name).get().start(); sender.sendMessage(ChatColor.GREEN + "The show " + ChatColor.WHITE + name + ChatColor.GREEN + " has been started!"); } @@ -112,7 +112,7 @@ public class ShowCMD extends BaseCommand { return; } - SCAPI.cancelShow(name); + SCAPI.getShow(name).get().cancel(); sender.sendMessage(ChatColor.GREEN + "The show " + ChatColor.WHITE + name + ChatColor.GREEN + " has been stopped!"); } diff --git a/src/main/java/tech/sbdevelopment/showcontrol/data/DataStorage.java b/src/main/java/tech/sbdevelopment/showcontrol/data/DataStorage.java index 05b594e..7451d91 100644 --- a/src/main/java/tech/sbdevelopment/showcontrol/data/DataStorage.java +++ b/src/main/java/tech/sbdevelopment/showcontrol/data/DataStorage.java @@ -1,10 +1,12 @@ package tech.sbdevelopment.showcontrol.data; +import lombok.Getter; import tech.sbdevelopment.showcontrol.ShowControlPlugin; import tech.sbdevelopment.showcontrol.api.SCAPI; import tech.sbdevelopment.showcontrol.api.exceptions.InvalidTriggerException; import tech.sbdevelopment.showcontrol.api.exceptions.TooFewArgumentsException; import tech.sbdevelopment.showcontrol.api.points.ShowCuePoint; +import tech.sbdevelopment.showcontrol.api.shows.Show; import tech.sbdevelopment.showcontrol.api.triggers.Trigger; import tech.sbdevelopment.showcontrol.utils.YamlFile; @@ -12,12 +14,9 @@ import java.io.File; import java.util.*; public class DataStorage { + @Getter private static final Map files = new HashMap<>(); - public static Map getFiles() { - return files; - } - public static void load() { // Create data folder if not exists if (!ShowControlPlugin.getInstance().getDataFolder().exists()) @@ -45,14 +44,14 @@ public class DataStorage { cues.add(new ShowCuePoint(cueID, time, data)); } - SCAPI.getShowsMap().put(showID, cues); + SCAPI.getShowsMap().put(showID, new Show(showID, cues)); } } public static void save() { - for (Map.Entry> entry : SCAPI.getShowsMap().entrySet()) { + for (Map.Entry entry : SCAPI.getShowsMap().entrySet()) { YamlFile file = files.containsKey(entry.getKey()) ? files.get(entry.getKey()) : new YamlFile(ShowControlPlugin.getInstance(), "data/" + entry.getKey()); - for (ShowCuePoint cue : entry.getValue()) { + for (ShowCuePoint cue : entry.getValue().getCuePoints()) { file.getFile().set(cue.getCueID().toString() + ".Time", cue.getTime()); file.getFile().set(cue.getCueID().toString() + ".Type", cue.getData().getTriggerId()); file.getFile().set(cue.getCueID().toString() + ".Data", cue.getData().getDataString()); diff --git a/src/main/java/tech/sbdevelopment/showcontrol/gui/ShowCueGUI.java b/src/main/java/tech/sbdevelopment/showcontrol/gui/ShowCueGUI.java index 9be6930..ef8e3c9 100644 --- a/src/main/java/tech/sbdevelopment/showcontrol/gui/ShowCueGUI.java +++ b/src/main/java/tech/sbdevelopment/showcontrol/gui/ShowCueGUI.java @@ -5,18 +5,27 @@ import org.bukkit.ChatColor; import org.bukkit.entity.Player; import tech.sbdevelopment.showcontrol.api.SCAPI; import tech.sbdevelopment.showcontrol.api.points.ShowCuePoint; +import tech.sbdevelopment.showcontrol.api.shows.Show; import tech.sbdevelopment.showcontrol.utils.MainUtil; import tech.sbdevelopment.showcontrol.utils.inventories.PaginationInventory; import java.util.Comparator; +import java.util.Optional; public class ShowCueGUI extends PaginationInventory { public ShowCueGUI(Player p, String name) { super(5, ChatColor.DARK_AQUA + "Show Cue Manager:"); - SCAPI.getPoints(name).stream().sorted(Comparator.comparing(ShowCuePoint::getTime)) - .forEach(cue -> addItem(ClickableItem.of(MainUtil.pointToItem(cue), e -> { - SCAPI.removePoint(name, cue); + Optional showOpt = SCAPI.getShow(name); + if (showOpt.isEmpty()) { + p.sendMessage(MainUtil.__("&cShow not found!")); + return; + } + Show show = showOpt.get(); + + show.getCuePoints().stream().sorted(Comparator.comparing(ShowCuePoint::getTime)) + .forEach(cue -> addItem(ClickableItem.of(cue.getGUIItem(), e -> { + show.removePoint(cue); new ShowCueGUI(p, name).open(p); }))); diff --git a/src/main/java/tech/sbdevelopment/showcontrol/utils/MainUtil.java b/src/main/java/tech/sbdevelopment/showcontrol/utils/MainUtil.java index da5860d..263b9cf 100644 --- a/src/main/java/tech/sbdevelopment/showcontrol/utils/MainUtil.java +++ b/src/main/java/tech/sbdevelopment/showcontrol/utils/MainUtil.java @@ -14,23 +14,6 @@ public class MainUtil { return ChatColor.translateAlternateColorCodes('&', in); } - public static ItemStack pointToItem(ShowCuePoint point) { - TriggerIdentifier identifier = point.getData().getClass().getAnnotation(TriggerIdentifier.class); - - List lores = new ArrayList<>(); - lores.add(ChatColor.GREEN + "Type: " + ChatColor.AQUA + capitalize(point.getData().getTriggerId())); - lores.add(ChatColor.GREEN + "Data:"); - for (String str : ChatPaginator.paginate(point.getData().getDataString(), 20).getLines()) { - lores.add(ChatColor.AQUA + ChatColor.stripColor(str)); - } - lores.add(""); - lores.add(ChatColor.RED + ChatColor.BOLD.toString() + "Click to remove!"); - - return new ItemBuilder(identifier.item()) - .displayname(ChatColor.LIGHT_PURPLE + ChatColor.ITALIC.toString() + "TimeCode: " + TimeUtil.makeReadable(point.getTime())) - .lore(lores).getItemStack(); - } - public static String capitalize(String str) { return str.substring(0, 1).toUpperCase() + str.substring(1); }