From 3963d17c710483a8e8ccede3695dd148194d4674 Mon Sep 17 00:00:00 2001 From: Stijn Bannink Date: Thu, 14 Sep 2023 15:25:23 +0200 Subject: [PATCH] Small API changes --- .../showcontrol/ShowControlPlugin.java | 11 ++- .../api/{ShowAPI.java => SCAPI.java} | 83 ++++++++++++++++--- .../showcontrol/commands/ShowCMD.java | 27 +++--- .../showcontrol/data/DataStorage.java | 8 +- .../sbdevelopment/showcontrol/data/Shows.java | 77 ----------------- .../showcontrol/gui/ShowCueGUI.java | 6 +- .../showcontrol/utils/TimeUtil.java | 2 +- 7 files changed, 99 insertions(+), 115 deletions(-) rename src/main/java/tech/sbdevelopment/showcontrol/api/{ShowAPI.java => SCAPI.java} (54%) delete mode 100644 src/main/java/tech/sbdevelopment/showcontrol/data/Shows.java diff --git a/src/main/java/tech/sbdevelopment/showcontrol/ShowControlPlugin.java b/src/main/java/tech/sbdevelopment/showcontrol/ShowControlPlugin.java index ae5c435..7ea0055 100644 --- a/src/main/java/tech/sbdevelopment/showcontrol/ShowControlPlugin.java +++ b/src/main/java/tech/sbdevelopment/showcontrol/ShowControlPlugin.java @@ -1,10 +1,9 @@ package tech.sbdevelopment.showcontrol; import co.aikar.commands.PaperCommandManager; -import tech.sbdevelopment.showcontrol.api.ShowAPI; import tech.sbdevelopment.showcontrol.commands.ShowCMD; import tech.sbdevelopment.showcontrol.data.DataStorage; -import tech.sbdevelopment.showcontrol.data.Shows; +import tech.sbdevelopment.showcontrol.api.SCAPI; import tech.sbdevelopment.showcontrol.utils.inventories.Inventory; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; @@ -25,14 +24,14 @@ public final class ShowControlPlugin extends JavaPlugin { final PaperCommandManager commandManager = new PaperCommandManager(this); commandManager.enableUnstableAPI("help"); commandManager.registerCommand(new ShowCMD()); - commandManager.getCommandCompletions().registerCompletion("showname", c -> Shows.getShowsMap().keySet()); - commandManager.getCommandCompletions().registerCompletion("showtype", c -> ShowAPI.getTriggers().keySet()); + commandManager.getCommandCompletions().registerCompletion("showname", c -> SCAPI.getShowsMap().keySet()); + commandManager.getCommandCompletions().registerCompletion("showtype", c -> SCAPI.getTriggers().keySet()); getLogger().info("Loading GUI manageer..."); Inventory.init(this); getLogger().info("Loading default triggers..."); - ShowAPI.index(ShowControlPlugin.class, "tech.sbdevelopment.showcontrol.api.triggers.impl"); + SCAPI.index(ShowControlPlugin.class, "tech.sbdevelopment.showcontrol.api.triggers.impl"); Bukkit.getScheduler().runTaskLater(this, DataStorage::load, 1L); //Load 1 tick later, because of multi world @@ -44,7 +43,7 @@ public final class ShowControlPlugin extends JavaPlugin { public void onDisable() { getLogger().info("Saving data..."); DataStorage.save(); - Shows.getShowsMap().values().forEach(show -> show.forEach(showCue -> showCue.getData().remove())); + SCAPI.getShowsMap().values().forEach(show -> show.forEach(showCue -> showCue.getData().remove())); getLogger().info("Plugin disabled!"); instance = null; diff --git a/src/main/java/tech/sbdevelopment/showcontrol/api/ShowAPI.java b/src/main/java/tech/sbdevelopment/showcontrol/api/SCAPI.java similarity index 54% rename from src/main/java/tech/sbdevelopment/showcontrol/api/ShowAPI.java rename to src/main/java/tech/sbdevelopment/showcontrol/api/SCAPI.java index c4efb77..ecf7d24 100644 --- a/src/main/java/tech/sbdevelopment/showcontrol/api/ShowAPI.java +++ b/src/main/java/tech/sbdevelopment/showcontrol/api/SCAPI.java @@ -1,26 +1,34 @@ package tech.sbdevelopment.showcontrol.api; import lombok.Getter; -import tech.sbdevelopment.showcontrol.ShowControlPlugin; -import tech.sbdevelopment.showcontrol.api.exceptions.InvalidTriggerException; -import tech.sbdevelopment.showcontrol.api.exceptions.TooFewArgumentsException; -import tech.sbdevelopment.showcontrol.api.triggers.Trigger; -import tech.sbdevelopment.showcontrol.api.triggers.TriggerIdentifier; import org.reflections.Reflections; import org.reflections.scanners.Scanners; import org.reflections.util.ClasspathHelper; import org.reflections.util.ConfigurationBuilder; import org.reflections.util.FilterBuilder; +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.triggers.Trigger; +import tech.sbdevelopment.showcontrol.api.triggers.TriggerIdentifier; +import tech.sbdevelopment.showcontrol.data.DataStorage; +import tech.sbdevelopment.showcontrol.utils.YamlFile; +import org.bukkit.Bukkit; +import java.io.File; import java.lang.reflect.Constructor; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; +import java.util.*; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; -public class ShowAPI { +public class SCAPI { @Getter private static final Map> triggers = new HashMap<>(); + @Getter + private static final HashMap> showsMap = new HashMap<>(); + private static final HashMap showTimers = new HashMap<>(); public static void index(Class clazz, String... packages) { ShowControlPlugin.getInstance().getLogger().info("Indexing triggers for starting point " + clazz.getSimpleName() + "..."); @@ -59,4 +67,59 @@ public class ShowAPI { throw new TooFewArgumentsException(identifier.argDesc()); return ctor.newInstance(new Object[]{dataSplitterNew}); } + + public static void create(String name) { + showsMap.put(name, new ArrayList<>()); + DataStorage.save(); + } + + public static void delete(String name) { + showsMap.remove(name); + + File data = new File(ShowControlPlugin.getInstance().getDataFolder(), "data/" + name + ".yml"); + data.delete(); + } + + public static boolean exists(String name) { + return showsMap.containsKey(name); + } + + public static List getPoints(String name) { + if (!exists(name)) return new ArrayList<>(); + return showsMap.get(name); + } + + public static void addPoint(String name, Long time, Trigger data) { + if (!exists(name)) return; + getPoints(name).add(new ShowCuePoint(time, data)); + DataStorage.save(); + } + + 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(); + } + + 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); + } + + public static void cancelShow(String name) { + if (!exists(name)) return; + if (!showTimers.containsKey(name)) return; + ScheduledExecutorService showTimer = showTimers.get(name); + showTimer.shutdownNow(); + } } diff --git a/src/main/java/tech/sbdevelopment/showcontrol/commands/ShowCMD.java b/src/main/java/tech/sbdevelopment/showcontrol/commands/ShowCMD.java index e2bc666..3cb1c3c 100644 --- a/src/main/java/tech/sbdevelopment/showcontrol/commands/ShowCMD.java +++ b/src/main/java/tech/sbdevelopment/showcontrol/commands/ShowCMD.java @@ -3,10 +3,9 @@ package tech.sbdevelopment.showcontrol.commands; import co.aikar.commands.BaseCommand; import co.aikar.commands.annotation.*; import tech.sbdevelopment.showcontrol.api.exceptions.InvalidTriggerException; -import tech.sbdevelopment.showcontrol.api.ShowAPI; import tech.sbdevelopment.showcontrol.api.exceptions.TooFewArgumentsException; import tech.sbdevelopment.showcontrol.api.triggers.Trigger; -import tech.sbdevelopment.showcontrol.data.Shows; +import tech.sbdevelopment.showcontrol.api.SCAPI; import tech.sbdevelopment.showcontrol.gui.ShowCueGUI; import tech.sbdevelopment.showcontrol.utils.TimeUtil; import org.bukkit.ChatColor; @@ -19,12 +18,12 @@ public class ShowCMD extends BaseCommand { @Subcommand("create") @Description("") public void onCreate(CommandSender sender, @Single String name) { - if (Shows.exists(name)) { + if (SCAPI.exists(name)) { sender.sendMessage(ChatColor.RED + "That show already exists."); return; } - Shows.create(name); + SCAPI.create(name); sender.sendMessage(ChatColor.GREEN + "The show " + ChatColor.WHITE + name + ChatColor.GREEN + " has been created!"); } @@ -33,12 +32,12 @@ public class ShowCMD extends BaseCommand { @Description("") @CommandCompletion("@showname") public void onDelete(CommandSender sender, @Single String name) { - if (!Shows.exists(name)) { + if (!SCAPI.exists(name)) { sender.sendMessage(ChatColor.RED + "That show doesn't exists."); return; } - Shows.delete(name); + SCAPI.delete(name); sender.sendMessage(ChatColor.GREEN + "The show " + ChatColor.WHITE + name + ChatColor.GREEN + " has been removed!"); } @@ -47,7 +46,7 @@ public class ShowCMD extends BaseCommand { @Description("") @CommandCompletion("@showname @empty @showtype @empty") public void onAdd(CommandSender sender, String name, String time, String args) { - if (!Shows.exists(name)) { + if (!SCAPI.exists(name)) { sender.sendMessage(ChatColor.RED + "That show doesn't exists."); return; } @@ -62,7 +61,7 @@ public class ShowCMD extends BaseCommand { Trigger data; try { - data = ShowAPI.getTrigger(args); + data = SCAPI.getTrigger(args); } catch (ReflectiveOperationException e) { e.printStackTrace(); sender.sendMessage(ChatColor.RED + "Something went wrong! Please ask a server admin."); @@ -78,7 +77,7 @@ public class ShowCMD extends BaseCommand { return; } - Shows.addPoint(name, timeMilli, data); + SCAPI.addPoint(name, timeMilli, data); sender.sendMessage(ChatColor.GREEN + "The show " + ChatColor.WHITE + name + ChatColor.GREEN + " now contains an extra point!"); } @@ -86,12 +85,12 @@ public class ShowCMD extends BaseCommand { @Description("") @CommandCompletion("@showname") public void onStart(CommandSender sender, @Single String name) { - if (!Shows.exists(name)) { + if (!SCAPI.exists(name)) { sender.sendMessage(ChatColor.RED + "That show doesn't exists."); return; } - Shows.startShow(name); + SCAPI.startShow(name); sender.sendMessage(ChatColor.GREEN + "The show " + ChatColor.WHITE + name + ChatColor.GREEN + " has been started!"); } @@ -100,12 +99,12 @@ public class ShowCMD extends BaseCommand { @Description("") @CommandCompletion("@showname") public void onCancel(CommandSender sender, @Single String name) { - if (!Shows.exists(name)) { + if (!SCAPI.exists(name)) { sender.sendMessage(ChatColor.RED + "That show doesn't exists."); return; } - Shows.cancelShow(name); + SCAPI.cancelShow(name); sender.sendMessage(ChatColor.GREEN + "The show " + ChatColor.WHITE + name + ChatColor.GREEN + " has been stopped!"); } @@ -114,7 +113,7 @@ public class ShowCMD extends BaseCommand { @Description("") @CommandCompletion("@showname") public void onGUI(Player sender, @Single String name) { - if (!Shows.exists(name)) { + if (!SCAPI.exists(name)) { sender.sendMessage(ChatColor.RED + "That show doesn't exists."); return; } diff --git a/src/main/java/tech/sbdevelopment/showcontrol/data/DataStorage.java b/src/main/java/tech/sbdevelopment/showcontrol/data/DataStorage.java index c1c6c96..2f4479d 100644 --- a/src/main/java/tech/sbdevelopment/showcontrol/data/DataStorage.java +++ b/src/main/java/tech/sbdevelopment/showcontrol/data/DataStorage.java @@ -1,8 +1,8 @@ package tech.sbdevelopment.showcontrol.data; import tech.sbdevelopment.showcontrol.ShowControlPlugin; +import tech.sbdevelopment.showcontrol.api.SCAPI; import tech.sbdevelopment.showcontrol.api.exceptions.InvalidTriggerException; -import tech.sbdevelopment.showcontrol.api.ShowAPI; import tech.sbdevelopment.showcontrol.api.points.ShowCuePoint; import tech.sbdevelopment.showcontrol.api.exceptions.TooFewArgumentsException; import tech.sbdevelopment.showcontrol.api.triggers.Trigger; @@ -36,7 +36,7 @@ public class DataStorage { UUID cueID = UUID.fromString(id); Trigger data; try { - data = ShowAPI.getTrigger(showConfig.getFile().getString(id + ".Type") + " " + showConfig.getFile().getString(id + ".Data")); + data = SCAPI.getTrigger(showConfig.getFile().getString(id + ".Type") + " " + showConfig.getFile().getString(id + ".Data")); } catch (ReflectiveOperationException | InvalidTriggerException | TooFewArgumentsException e) { e.printStackTrace(); return; @@ -45,12 +45,12 @@ public class DataStorage { cues.add(new ShowCuePoint(cueID, time, data)); } - Shows.getShowsMap().put(showID, cues); + SCAPI.getShowsMap().put(showID, cues); } } public static void save() { - for (Map.Entry> entry : Shows.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()) { file.getFile().set(cue.getCueID().toString() + ".Time", cue.getTime()); diff --git a/src/main/java/tech/sbdevelopment/showcontrol/data/Shows.java b/src/main/java/tech/sbdevelopment/showcontrol/data/Shows.java deleted file mode 100644 index 8275d2c..0000000 --- a/src/main/java/tech/sbdevelopment/showcontrol/data/Shows.java +++ /dev/null @@ -1,77 +0,0 @@ -package tech.sbdevelopment.showcontrol.data; - -import lombok.Getter; -import tech.sbdevelopment.showcontrol.ShowControlPlugin; -import tech.sbdevelopment.showcontrol.api.points.ShowCuePoint; -import tech.sbdevelopment.showcontrol.api.triggers.Trigger; -import tech.sbdevelopment.showcontrol.utils.YamlFile; -import org.bukkit.Bukkit; - -import java.io.File; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; - -public class Shows { - @Getter - private static final HashMap> showsMap = new HashMap<>(); - private static final HashMap showTimers = new HashMap<>(); - - public static void create(String name) { - showsMap.put(name, new ArrayList<>()); - DataStorage.save(); - } - - public static void delete(String name) { - showsMap.remove(name); - - File data = new File(ShowControlPlugin.getInstance().getDataFolder(), "data/" + name + ".yml"); - data.delete(); - } - - public static boolean exists(String name) { - return showsMap.containsKey(name); - } - - public static List getPoints(String name) { - if (!exists(name)) return new ArrayList<>(); - return showsMap.get(name); - } - - public static void addPoint(String name, Long time, Trigger data) { - if (!exists(name)) return; - getPoints(name).add(new ShowCuePoint(time, data)); - DataStorage.save(); - } - - 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(); - } - - 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); - } - - public static void cancelShow(String name) { - if (!exists(name)) return; - if (!showTimers.containsKey(name)) return; - ScheduledExecutorService showTimer = showTimers.get(name); - showTimer.shutdownNow(); - } -} diff --git a/src/main/java/tech/sbdevelopment/showcontrol/gui/ShowCueGUI.java b/src/main/java/tech/sbdevelopment/showcontrol/gui/ShowCueGUI.java index dfd456b..d9c92e7 100644 --- a/src/main/java/tech/sbdevelopment/showcontrol/gui/ShowCueGUI.java +++ b/src/main/java/tech/sbdevelopment/showcontrol/gui/ShowCueGUI.java @@ -2,7 +2,7 @@ package tech.sbdevelopment.showcontrol.gui; import fr.minuskube.inv.ClickableItem; import tech.sbdevelopment.showcontrol.api.points.ShowCuePoint; -import tech.sbdevelopment.showcontrol.data.Shows; +import tech.sbdevelopment.showcontrol.api.SCAPI; import tech.sbdevelopment.showcontrol.utils.MainUtil; import tech.sbdevelopment.showcontrol.utils.inventories.PaginationInventory; import org.bukkit.ChatColor; @@ -14,9 +14,9 @@ public class ShowCueGUI extends PaginationInventory { public ShowCueGUI(Player p, String name) { super(5, ChatColor.DARK_AQUA + "Show Cue Manager:"); - Shows.getPoints(name).stream().sorted(Comparator.comparing(ShowCuePoint::getTime)) + SCAPI.getPoints(name).stream().sorted(Comparator.comparing(ShowCuePoint::getTime)) .forEach(cue -> addItem(ClickableItem.of(MainUtil.pointToItem(cue), e -> { - Shows.removePoint(name, cue); + SCAPI.removePoint(name, cue); new ShowCueGUI(p, name).open(p); }))); diff --git a/src/main/java/tech/sbdevelopment/showcontrol/utils/TimeUtil.java b/src/main/java/tech/sbdevelopment/showcontrol/utils/TimeUtil.java index e9563eb..35257a1 100644 --- a/src/main/java/tech/sbdevelopment/showcontrol/utils/TimeUtil.java +++ b/src/main/java/tech/sbdevelopment/showcontrol/utils/TimeUtil.java @@ -51,7 +51,7 @@ public class TimeUtil { return array.length > 1 && array[0].length() > 0; } - public static Object makeReadable(Long time) { + public static String makeReadable(Long time) { return String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(time), TimeUnit.MILLISECONDS.toMinutes(time) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(time)), TimeUnit.MILLISECONDS.toSeconds(time) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(time)));