v1.6: Small refactor of the API
This commit is contained in:
parent
071bf5830b
commit
a4dbdab581
11 changed files with 157 additions and 102 deletions
|
@ -19,7 +19,7 @@ The project is on our Maven repository, add the following to your `pom.xml` file
|
|||
<dependency>
|
||||
<groupId>tech.sbdevelopment</groupId>
|
||||
<artifactId>ShowControl</artifactId>
|
||||
<version>1.5</version>
|
||||
<version>1.6</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
```
|
||||
|
|
2
pom.xml
2
pom.xml
|
@ -6,7 +6,7 @@
|
|||
|
||||
<groupId>tech.sbdevelopment</groupId>
|
||||
<artifactId>ShowControl</artifactId>
|
||||
<version>1.5</version>
|
||||
<version>1.6</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>ShowControl</name>
|
||||
<url>https://sbdevelopment.tech</url>
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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<String, Trigger> defaultTriggers = new HashMap<>();
|
||||
|
||||
/**
|
||||
* A map of all triggers, used for creating new triggers.
|
||||
*/
|
||||
@Getter
|
||||
private static final Map<String, Class<? extends Trigger>> triggers = new HashMap<>();
|
||||
|
||||
/**
|
||||
* A map of all shows with their cue points.
|
||||
* A map of all shows.
|
||||
*/
|
||||
@Getter
|
||||
private static final HashMap<String, List<ShowCuePoint>> showsMap = new HashMap<>();
|
||||
/**
|
||||
* A map of all show timers.
|
||||
*/
|
||||
private static final HashMap<String, ScheduledExecutorService> showTimers = new HashMap<>();
|
||||
private static final HashMap<String, Show> 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<ShowCuePoint> 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<Show> getShow(String name) {
|
||||
return Optional.of(showsMap.get(name));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String> 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();
|
||||
}
|
||||
}
|
|
@ -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<ShowCuePoint> 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<ShowCuePoint> 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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
/**
|
||||
* This package contains all classes related to shows.
|
||||
*/
|
||||
package tech.sbdevelopment.showcontrol.api.shows;
|
|
@ -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!");
|
||||
}
|
||||
|
|
|
@ -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<String, YamlFile> files = new HashMap<>();
|
||||
|
||||
public static Map<String, YamlFile> 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<String, List<ShowCuePoint>> entry : SCAPI.getShowsMap().entrySet()) {
|
||||
for (Map.Entry<String, Show> 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());
|
||||
|
|
|
@ -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<Show> 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);
|
||||
})));
|
||||
|
||||
|
|
|
@ -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<String> 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);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue