From 97e66d65f1c86ec534f89718182cbdbb92d5dfcc Mon Sep 17 00:00:00 2001 From: stijnb1234 Date: Mon, 25 Jan 2021 13:30:02 +0100 Subject: [PATCH] Fixed ms and tick delays, and moved to another gui util --- pom.xml | 15 +- .../java/nl/sbdeveloper/showapi/ShowAPI.java | 78 ++- .../nl/sbdeveloper/showapi/ShowAPIPlugin.java | 13 +- .../nl/sbdeveloper/showapi/api/ShowCue.java | 44 +- .../{TriggerData.java => TriggerTask.java} | 4 +- .../sbdeveloper/showapi/api/TriggerType.java | 6 +- .../showapi/api/triggers/AnimaTrigger.java | 4 +- .../showapi/api/triggers/CommandTrigger.java | 6 +- .../showapi/api/triggers/FireworkTrigger.java | 4 +- .../showapi/api/triggers/LaserTrigger.java | 4 +- .../showapi/api/triggers/ParticleTrigger.java | 4 +- .../showapi/api/triggers/SpotTrigger.java | 4 +- .../sbdeveloper/showapi/commands/ShowCMD.java | 16 +- .../sbdeveloper/showapi/data/DataSaving.java | 29 +- .../nl/sbdeveloper/showapi/data/Shows.java | 24 +- .../sbdeveloper/showapi/gui/ShowCueGUI.java | 70 +- .../sbdeveloper/showapi/utils/Inventory.java | 53 ++ .../showapi/utils/ItemBuilder.java | 601 ++++++++++++++++++ .../sbdeveloper/showapi/utils/MainUtil.java | 21 +- .../sbdeveloper/showapi/utils/TimeUtil.java | 116 ++-- 20 files changed, 919 insertions(+), 197 deletions(-) rename src/main/java/nl/sbdeveloper/showapi/api/{TriggerData.java => TriggerTask.java} (90%) create mode 100644 src/main/java/nl/sbdeveloper/showapi/utils/Inventory.java create mode 100644 src/main/java/nl/sbdeveloper/showapi/utils/ItemBuilder.java diff --git a/pom.xml b/pom.xml index cc749a2..db77073 100644 --- a/pom.xml +++ b/pom.xml @@ -54,10 +54,6 @@ org.inventivetalent.apihelper nl.sbdeveloper.showapi.helpers.apihelper - - com.samjakob.spigui - nl.sbdeveloper.showapi.helpers.guihelper - @@ -144,10 +140,15 @@ compile - com.samjakob - SpiGUI - v1.1 + fr.minuskube.inv + smart-invs + 1.2.7 compile + + com.github.cryptomorin + XSeries + 7.8.0 + diff --git a/src/main/java/nl/sbdeveloper/showapi/ShowAPI.java b/src/main/java/nl/sbdeveloper/showapi/ShowAPI.java index 8c29f90..e369cb9 100644 --- a/src/main/java/nl/sbdeveloper/showapi/ShowAPI.java +++ b/src/main/java/nl/sbdeveloper/showapi/ShowAPI.java @@ -137,26 +137,47 @@ public class ShowAPI implements API, Listener { SpotRunnable spot = spots.get(name); new BukkitRunnable() { + boolean fired = false; Location oldLoc = spot.posLoc; @Override public void run() { - if (oldLoc.getX() > posLoc.getX()) { //De x gaat omhoog - oldLoc = oldLoc.add(0.01, 0, 0); + if (oldLoc.getBlockX() != posLoc.getBlockX()) { + if (oldLoc.getX() > posLoc.getX()) { //De x gaat omhoog + oldLoc = oldLoc.add(0.01, 0, 0); + } else { + oldLoc = oldLoc.add(-0.01, 0, 0); + } + fired = true; } else { - oldLoc = oldLoc.add(-0.01, 0, 0); + fired = false; } - if (oldLoc.getY() > posLoc.getY()) { //De y gaat omhoog - oldLoc = oldLoc.add(0, 0.01, 0); + if (oldLoc.getBlockY() != posLoc.getBlockY()) { + if (oldLoc.getY() > posLoc.getY()) { //De y gaat omhoog + oldLoc = oldLoc.add(0, 0.01, 0); + } else { + oldLoc = oldLoc.add(0, -0.01, 0); + } + fired = true; } else { - oldLoc = oldLoc.add(0, -0.01, 0); + fired = false; } - if (oldLoc.getZ() > posLoc.getZ()) { //De z gaat omhoog - oldLoc = oldLoc.add(0, 0, 0.01); + if (oldLoc.getBlockZ() != posLoc.getBlockZ()) { + if (oldLoc.getZ() > posLoc.getZ()) { //De z gaat omhoog + oldLoc = oldLoc.add(0, 0, 0.01); + } else { + oldLoc = oldLoc.add(0, 0, -0.01); + } + fired = true; } else { - oldLoc = oldLoc.add(0, 0, -0.01); + fired = false; + } + + if (!fired) { + cancel(); + return; } spot.changePositionLocation(oldLoc); @@ -250,26 +271,47 @@ public class ShowAPI implements API, Listener { LaserRunnable laser = lasers.get(name); new BukkitRunnable() { + boolean fired = false; Location oldLoc = laser.posLoc; @Override public void run() { - if (oldLoc.getX() > posLoc.getX()) { //De x gaat omhoog - oldLoc = oldLoc.add(0.01, 0, 0); + if (oldLoc.getBlockX() != posLoc.getBlockX()) { + if (oldLoc.getX() > posLoc.getX()) { //De x gaat omhoog + oldLoc = oldLoc.add(0.01, 0, 0); + } else { + oldLoc = oldLoc.add(-0.01, 0, 0); + } + fired = true; } else { - oldLoc = oldLoc.add(-0.01, 0, 0); + fired = false; } - if (oldLoc.getY() > posLoc.getY()) { //De y gaat omhoog - oldLoc = oldLoc.add(0, 0.01, 0); + if (oldLoc.getBlockY() != posLoc.getBlockY()) { + if (oldLoc.getY() > posLoc.getY()) { //De y gaat omhoog + oldLoc = oldLoc.add(0, 0.01, 0); + } else { + oldLoc = oldLoc.add(0, -0.01, 0); + } + fired = true; } else { - oldLoc = oldLoc.add(0, -0.01, 0); + fired = false; } - if (oldLoc.getZ() > posLoc.getZ()) { //De z gaat omhoog - oldLoc = oldLoc.add(0, 0, 0.01); + if (oldLoc.getBlockZ() != posLoc.getBlockZ()) { + if (oldLoc.getZ() > posLoc.getZ()) { //De z gaat omhoog + oldLoc = oldLoc.add(0, 0, 0.01); + } else { + oldLoc = oldLoc.add(0, 0, -0.01); + } + fired = true; } else { - oldLoc = oldLoc.add(0, 0, -0.01); + fired = false; + } + + if (!fired) { + cancel(); + return; } laser.changePositionLocation(oldLoc); diff --git a/src/main/java/nl/sbdeveloper/showapi/ShowAPIPlugin.java b/src/main/java/nl/sbdeveloper/showapi/ShowAPIPlugin.java index c3d1130..882fa04 100644 --- a/src/main/java/nl/sbdeveloper/showapi/ShowAPIPlugin.java +++ b/src/main/java/nl/sbdeveloper/showapi/ShowAPIPlugin.java @@ -1,8 +1,9 @@ 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.data.Shows; +import nl.sbdeveloper.showapi.utils.Inventory; import nl.sbdeveloper.showapi.utils.YamlFile; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; @@ -12,7 +13,6 @@ 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 @@ -29,8 +29,7 @@ public final class ShowAPIPlugin extends JavaPlugin { APIManager.initAPI(ShowAPI.class); - spiGUI = new SpiGUI(this); - spiGUI.setEnableAutomaticPagination(true); + Inventory.init(); getCommand("mctpshow").setExecutor(new ShowCMD()); @@ -43,6 +42,8 @@ public final class ShowAPIPlugin extends JavaPlugin { DataSaving.save(); + Shows.getShowsMap().values().forEach(show -> show.forEach(showCue -> showCue.getTask().remove())); + APIManager.disableAPI(ShowAPI.class); } @@ -50,10 +51,6 @@ public final class ShowAPIPlugin extends JavaPlugin { return instance; } - 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 70c50f6..a18a8bb 100644 --- a/src/main/java/nl/sbdeveloper/showapi/api/ShowCue.java +++ b/src/main/java/nl/sbdeveloper/showapi/api/ShowCue.java @@ -1,8 +1,5 @@ package nl.sbdeveloper.showapi.api; -import nl.sbdeveloper.showapi.ShowAPIPlugin; -import org.bukkit.Bukkit; - import java.util.UUID; /** @@ -10,30 +7,29 @@ import java.util.UUID; */ public class ShowCue { private final UUID cueID; - private final int ticks; - private final TriggerData data; - private int taskID; + private final Long time; + private final TriggerTask data; /** * Create a new cue point * - * @param ticks The starttime in ticks + * @param time The starttime (milli) * @param data The data */ - public ShowCue(int ticks, TriggerData data) { - this(UUID.randomUUID(), ticks, data); + public ShowCue(Long time, TriggerTask data) { + this(UUID.randomUUID(), time, data); } /** * Load an exisiting cue point * * @param uuid The UUID - * @param ticks The starttime in ticks + * @param time The starttime (milli) * @param data The data */ - public ShowCue(UUID uuid, int ticks, TriggerData data) { + public ShowCue(UUID uuid, Long time, TriggerTask data) { this.cueID = uuid; - this.ticks = ticks; + this.time = time; this.data = data; } @@ -47,12 +43,12 @@ public class ShowCue { } /** - * Get the time in seconds + * Get the time (milli) * - * @return The time in seconds + * @return The time (milli) */ - public int getTicks() { - return ticks; + public Long getTime() { + return time; } /** @@ -60,21 +56,7 @@ public class ShowCue { * * @return The data */ - public TriggerData getData() { + public TriggerTask getTask() { return data; } - - /** - * Start this cue point - */ - public void runAtTime() { - this.taskID = Bukkit.getScheduler().runTaskLater(ShowAPIPlugin.getInstance(), data::trigger, ticks).getTaskId(); - } - - /** - * Cancel this cue point - */ - public void cancel() { - Bukkit.getScheduler().cancelTask(taskID); - } } \ No newline at end of file diff --git a/src/main/java/nl/sbdeveloper/showapi/api/TriggerData.java b/src/main/java/nl/sbdeveloper/showapi/api/TriggerTask.java similarity index 90% rename from src/main/java/nl/sbdeveloper/showapi/api/TriggerData.java rename to src/main/java/nl/sbdeveloper/showapi/api/TriggerTask.java index 58ab73e..683c52a 100644 --- a/src/main/java/nl/sbdeveloper/showapi/api/TriggerData.java +++ b/src/main/java/nl/sbdeveloper/showapi/api/TriggerTask.java @@ -1,13 +1,13 @@ package nl.sbdeveloper.showapi.api; -public abstract class TriggerData { +public abstract class TriggerTask { private final TriggerType type; private final String[] dataString; /** * Create a new trigger */ - public TriggerData(TriggerType type, String[] dataString) { + public TriggerTask(TriggerType type, String[] dataString) { this.type = type; this.dataString = dataString; } diff --git a/src/main/java/nl/sbdeveloper/showapi/api/TriggerType.java b/src/main/java/nl/sbdeveloper/showapi/api/TriggerType.java index b8b8252..d26835e 100644 --- a/src/main/java/nl/sbdeveloper/showapi/api/TriggerType.java +++ b/src/main/java/nl/sbdeveloper/showapi/api/TriggerType.java @@ -10,15 +10,15 @@ public enum TriggerType { ANIMA(AnimaTrigger.class, 2), PARTICLE(ParticleTrigger.class, 7); - private final Class trigger; + private final Class trigger; private final int minArgs; - TriggerType(Class trigger, int minArgs) { + TriggerType(Class trigger, int minArgs) { this.trigger = trigger; this.minArgs = minArgs; } - public Class getTrigger() { + public Class getTrigger() { return trigger; } diff --git a/src/main/java/nl/sbdeveloper/showapi/api/triggers/AnimaTrigger.java b/src/main/java/nl/sbdeveloper/showapi/api/triggers/AnimaTrigger.java index d785bb3..2b2a8c2 100644 --- a/src/main/java/nl/sbdeveloper/showapi/api/triggers/AnimaTrigger.java +++ b/src/main/java/nl/sbdeveloper/showapi/api/triggers/AnimaTrigger.java @@ -1,10 +1,10 @@ package nl.sbdeveloper.showapi.api.triggers; -import nl.sbdeveloper.showapi.api.TriggerData; +import nl.sbdeveloper.showapi.api.TriggerTask; import nl.sbdeveloper.showapi.api.TriggerType; import org.bukkit.Bukkit; -public class AnimaTrigger extends TriggerData { +public class AnimaTrigger extends TriggerTask { private final String name; public AnimaTrigger(String[] data) { diff --git a/src/main/java/nl/sbdeveloper/showapi/api/triggers/CommandTrigger.java b/src/main/java/nl/sbdeveloper/showapi/api/triggers/CommandTrigger.java index 39ff234..473549e 100644 --- a/src/main/java/nl/sbdeveloper/showapi/api/triggers/CommandTrigger.java +++ b/src/main/java/nl/sbdeveloper/showapi/api/triggers/CommandTrigger.java @@ -1,10 +1,10 @@ package nl.sbdeveloper.showapi.api.triggers; -import nl.sbdeveloper.showapi.api.TriggerData; +import nl.sbdeveloper.showapi.api.TriggerTask; import nl.sbdeveloper.showapi.api.TriggerType; import org.bukkit.Bukkit; -public class CommandTrigger extends TriggerData { +public class CommandTrigger extends TriggerTask { private final String command; public CommandTrigger(String[] data) { @@ -15,7 +15,7 @@ public class CommandTrigger extends TriggerData { @Override public void trigger() { - Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command); + Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), command); } @Override diff --git a/src/main/java/nl/sbdeveloper/showapi/api/triggers/FireworkTrigger.java b/src/main/java/nl/sbdeveloper/showapi/api/triggers/FireworkTrigger.java index 5e2984f..6d8daab 100644 --- a/src/main/java/nl/sbdeveloper/showapi/api/triggers/FireworkTrigger.java +++ b/src/main/java/nl/sbdeveloper/showapi/api/triggers/FireworkTrigger.java @@ -1,7 +1,7 @@ package nl.sbdeveloper.showapi.api.triggers; import nl.sbdeveloper.showapi.ShowAPI; -import nl.sbdeveloper.showapi.api.TriggerData; +import nl.sbdeveloper.showapi.api.TriggerTask; import nl.sbdeveloper.showapi.api.TriggerType; import nl.sbdeveloper.showapi.utils.Color; import org.bukkit.Bukkit; @@ -9,7 +9,7 @@ import org.bukkit.FireworkEffect; import org.bukkit.Location; import org.bukkit.World; -public class FireworkTrigger extends TriggerData { +public class FireworkTrigger extends TriggerTask { private ShowAPI.Fireworks.Firework fw; private Location spawnLoc; diff --git a/src/main/java/nl/sbdeveloper/showapi/api/triggers/LaserTrigger.java b/src/main/java/nl/sbdeveloper/showapi/api/triggers/LaserTrigger.java index b00916d..0b156e2 100644 --- a/src/main/java/nl/sbdeveloper/showapi/api/triggers/LaserTrigger.java +++ b/src/main/java/nl/sbdeveloper/showapi/api/triggers/LaserTrigger.java @@ -1,13 +1,13 @@ package nl.sbdeveloper.showapi.api.triggers; import nl.sbdeveloper.showapi.ShowAPI; -import nl.sbdeveloper.showapi.api.TriggerData; +import nl.sbdeveloper.showapi.api.TriggerTask; import nl.sbdeveloper.showapi.api.TriggerType; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; -public class LaserTrigger extends TriggerData { +public class LaserTrigger extends TriggerTask { private final String name; private Location newLocation; diff --git a/src/main/java/nl/sbdeveloper/showapi/api/triggers/ParticleTrigger.java b/src/main/java/nl/sbdeveloper/showapi/api/triggers/ParticleTrigger.java index c900b24..584b541 100644 --- a/src/main/java/nl/sbdeveloper/showapi/api/triggers/ParticleTrigger.java +++ b/src/main/java/nl/sbdeveloper/showapi/api/triggers/ParticleTrigger.java @@ -1,13 +1,13 @@ package nl.sbdeveloper.showapi.api.triggers; -import nl.sbdeveloper.showapi.api.TriggerData; +import nl.sbdeveloper.showapi.api.TriggerTask; import nl.sbdeveloper.showapi.api.TriggerType; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Particle; import org.bukkit.World; -public class ParticleTrigger extends TriggerData { +public class ParticleTrigger extends TriggerTask { private Particle type; private Location spawnLoc; private int count; diff --git a/src/main/java/nl/sbdeveloper/showapi/api/triggers/SpotTrigger.java b/src/main/java/nl/sbdeveloper/showapi/api/triggers/SpotTrigger.java index 8e87f0c..88ac173 100644 --- a/src/main/java/nl/sbdeveloper/showapi/api/triggers/SpotTrigger.java +++ b/src/main/java/nl/sbdeveloper/showapi/api/triggers/SpotTrigger.java @@ -1,13 +1,13 @@ package nl.sbdeveloper.showapi.api.triggers; import nl.sbdeveloper.showapi.ShowAPI; -import nl.sbdeveloper.showapi.api.TriggerData; +import nl.sbdeveloper.showapi.api.TriggerTask; import nl.sbdeveloper.showapi.api.TriggerType; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; -public class SpotTrigger extends TriggerData { +public class SpotTrigger extends TriggerTask { private final String name; private Location newLocation; diff --git a/src/main/java/nl/sbdeveloper/showapi/commands/ShowCMD.java b/src/main/java/nl/sbdeveloper/showapi/commands/ShowCMD.java index 640ea7d..f79a5b6 100644 --- a/src/main/java/nl/sbdeveloper/showapi/commands/ShowCMD.java +++ b/src/main/java/nl/sbdeveloper/showapi/commands/ShowCMD.java @@ -1,6 +1,6 @@ package nl.sbdeveloper.showapi.commands; -import nl.sbdeveloper.showapi.api.TriggerData; +import nl.sbdeveloper.showapi.api.TriggerTask; import nl.sbdeveloper.showapi.data.Shows; import nl.sbdeveloper.showapi.gui.ShowCueGUI; import nl.sbdeveloper.showapi.utils.MainUtil; @@ -59,20 +59,26 @@ public class ShowCMD implements CommandExecutor { return false; } - int ticks = TimeUtil.parseTicks(args[2]); + Long time; + try { + time = TimeUtil.toMilis(args[2]); + } catch (Exception e) { + sender.sendMessage(ChatColor.RED + "Heeft een correcte tijd mee."); + return false; + } StringBuilder builder = new StringBuilder(); for (int i = 3; i < args.length; i++) { builder.append(args[i]).append(" "); } - TriggerData data = MainUtil.parseData(builder.toString().trim()); + TriggerTask data = MainUtil.parseData(builder.toString().trim()); if (data == null) { sender.sendMessage(ChatColor.RED + "Je hebt niet genoeg informatie meegeven voor de trigger."); return false; } - Shows.addPoint(name, ticks, data); + Shows.addPoint(name, time, data); sender.sendMessage(ChatColor.GREEN + "De show " + ChatColor.WHITE + name + ChatColor.GREEN + " bevat nu een extra punt!"); return true; @@ -112,7 +118,7 @@ public class ShowCMD implements CommandExecutor { return false; } - ShowCueGUI.openGUI(name, p); + new ShowCueGUI(p, name); return true; } } diff --git a/src/main/java/nl/sbdeveloper/showapi/data/DataSaving.java b/src/main/java/nl/sbdeveloper/showapi/data/DataSaving.java index 4ecf12f..9098850 100644 --- a/src/main/java/nl/sbdeveloper/showapi/data/DataSaving.java +++ b/src/main/java/nl/sbdeveloper/showapi/data/DataSaving.java @@ -2,7 +2,7 @@ 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.api.TriggerTask; import nl.sbdeveloper.showapi.utils.MainUtil; import java.util.ArrayList; @@ -12,7 +12,7 @@ import java.util.UUID; public class DataSaving { public static void load() { - boolean newSystem = ShowAPIPlugin.getData().getFile().contains("NewSystem"); + boolean newSystem = ShowAPIPlugin.getData().getFile().contains("NewestSystem"); for (String name : ShowAPIPlugin.getData().getFile().getConfigurationSection("Shows").getKeys(false)) { List cues = new ArrayList<>(); @@ -20,31 +20,34 @@ public class DataSaving { 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")); + TriggerTask data = MainUtil.parseData(ShowAPIPlugin.getData().getFile().getString("Shows." + name + "." + id + ".Type") + " " + ShowAPIPlugin.getData().getFile().getString("Shows." + name + "." + id + ".Data")); - int ticks; - if (!newSystem) ticks = ShowAPIPlugin.getData().getFile().getInt("Shows." + name + "." + id + ".Time") * 20; - else ticks = ShowAPIPlugin.getData().getFile().getInt("Shows." + name + "." + id + ".Time"); + long time; + if (!newSystem) time = Math.round(ShowAPIPlugin.getData().getFile().getInt("Shows." + name + "." + id + ".Time") * 50); + else time = ShowAPIPlugin.getData().getFile().getLong("Shows." + name + "." + id + ".Time"); if (!newSystem) { - ShowAPIPlugin.getData().getFile().set("Shows." + name + "." + id + ".Time", ticks); - ShowAPIPlugin.getData().getFile().set("NewSystem", true); - ShowAPIPlugin.getData().saveFile(); + ShowAPIPlugin.getData().getFile().set("Shows." + name + "." + id + ".Time", time); } - cues.add(new ShowCue(cueID, ShowAPIPlugin.getData().getFile().getInt("Shows." + name + "." + id + ".Time"), data)); + cues.add(new ShowCue(cueID, ShowAPIPlugin.getData().getFile().getLong("Shows." + name + "." + id + ".Time"), data)); } Shows.getShowsMap().put(name, cues); } + + if (!newSystem) { + ShowAPIPlugin.getData().getFile().set("NewestSystem", true); + ShowAPIPlugin.getData().saveFile(); + } } 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.getTicks()); - 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().getFile().set("Shows." + entry.getKey() + "." + cue.getCueID().toString() + ".Time", cue.getTime()); + ShowAPIPlugin.getData().getFile().set("Shows." + entry.getKey() + "." + cue.getCueID().toString() + ".Type", cue.getTask().getType().name()); + ShowAPIPlugin.getData().getFile().set("Shows." + entry.getKey() + "." + cue.getCueID().toString() + ".Data", cue.getTask().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 fa13cf8..6be86b4 100644 --- a/src/main/java/nl/sbdeveloper/showapi/data/Shows.java +++ b/src/main/java/nl/sbdeveloper/showapi/data/Shows.java @@ -2,15 +2,19 @@ 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.api.TriggerTask; import org.bukkit.Bukkit; 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 { 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<>()); @@ -33,16 +37,16 @@ public class Shows { return showsMap.get(name); } - public static void addPoint(String name, int ticks, TriggerData data) { + public static void addPoint(String name, Long time, TriggerTask data) { if (!exists(name)) return; - getPoints(name).add(new ShowCue(ticks, data)); + getPoints(name).add(new ShowCue(time, data)); Bukkit.getScheduler().runTaskAsynchronously(ShowAPIPlugin.getInstance(), DataSaving::save); } public static void removePoint(String name, ShowCue point) { if (!exists(name)) return; - point.getData().remove(); + point.getTask().remove(); showsMap.get(name).remove(point); ShowAPIPlugin.getData().getFile().set("Shows." + name + "." + point.getCueID(), null); @@ -51,12 +55,20 @@ public class Shows { public static void startShow(String name) { if (!exists(name)) return; - getPoints(name).forEach(ShowCue::runAtTime); + ScheduledExecutorService showTimer = Executors.newSingleThreadScheduledExecutor(); + Bukkit.getLogger().info("Scheduled show " + name); + for (ShowCue point : getPoints(name)) { + Bukkit.getLogger().info("Point " + point.getTask().getDataString() + " on " + point.getTime()); + showTimer.schedule(() -> Bukkit.getScheduler().runTask(ShowAPIPlugin.getInstance(), () -> point.getTask().trigger()), point.getTime(), TimeUnit.MILLISECONDS); + } + showTimers.put(name, showTimer); } public static void cancelShow(String name) { if (!exists(name)) return; - getPoints(name).forEach(ShowCue::cancel); + if (!showTimers.containsKey(name)) return; + ScheduledExecutorService showTimer = showTimers.get(name); + showTimer.shutdownNow(); } public static HashMap> getShowsMap() { diff --git a/src/main/java/nl/sbdeveloper/showapi/gui/ShowCueGUI.java b/src/main/java/nl/sbdeveloper/showapi/gui/ShowCueGUI.java index 1faa42f..a355193 100644 --- a/src/main/java/nl/sbdeveloper/showapi/gui/ShowCueGUI.java +++ b/src/main/java/nl/sbdeveloper/showapi/gui/ShowCueGUI.java @@ -1,30 +1,68 @@ package nl.sbdeveloper.showapi.gui; -import com.samjakob.spigui.SGMenu; -import com.samjakob.spigui.buttons.SGButton; -import nl.sbdeveloper.showapi.ShowAPIPlugin; +import fr.minuskube.inv.ClickableItem; +import fr.minuskube.inv.content.InventoryContents; +import fr.minuskube.inv.content.Pagination; +import fr.minuskube.inv.content.SlotIterator; import nl.sbdeveloper.showapi.api.ShowCue; import nl.sbdeveloper.showapi.data.Shows; +import nl.sbdeveloper.showapi.utils.Inventory; +import nl.sbdeveloper.showapi.utils.ItemBuilder; import nl.sbdeveloper.showapi.utils.MainUtil; import org.bukkit.ChatColor; +import org.bukkit.Material; import org.bukkit.entity.Player; -import org.bukkit.event.inventory.InventoryClickEvent; -public class ShowCueGUI { - public static void openGUI(String name, Player p) { - SGMenu menu = ShowAPIPlugin.getSpiGUI().create(ChatColor.DARK_AQUA + "Show Cue Manager:", 5); - menu.setAutomaticPaginationEnabled(true); +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; - for (ShowCue cue : Shows.getPoints(name)) { - SGButton button = new SGButton(MainUtil.pointToItem(cue)) - .withListener((InventoryClickEvent e) -> { - Shows.removePoint(name, cue); //Remove the point - openGUI(name, p); //Refresh - }); +import static nl.sbdeveloper.showapi.utils.MainUtil.__; - menu.addButton(button); +public class ShowCueGUI extends Inventory { + private final String showName; + + public ShowCueGUI(Player p, String name) { + super(5, ChatColor.DARK_AQUA + "Show Cue Manager:"); + this.showName = name; + open(p); + } + + @Override + public void init(Player player, InventoryContents contents) { + Pagination pagination = contents.pagination(); + + List items = new ArrayList<>(); + for (ShowCue cue : Shows.getPoints(showName).stream().sorted(Comparator.comparing(ShowCue::getTime)).collect(Collectors.toList())) { + items.add(ClickableItem.of(MainUtil.pointToItem(cue), e -> { + Shows.removePoint(showName, cue); + open(player, pagination.getPage()); + })); } - p.openInventory(menu.getInventory()); + ClickableItem[] itemsArray = new ClickableItem[items.size()]; + itemsArray = items.toArray(itemsArray); + pagination.setItems(itemsArray); + pagination.setItemsPerPage(45); + + pagination.addToIterator(contents.newIterator(SlotIterator.Type.HORIZONTAL, 0, 0)); + + contents.set(5, 0, ClickableItem.of(new ItemBuilder(Material.PLAYER_HEAD, 1) + .setName(__("&7Vorige pagina")) + .setLore(__("&eGa naar de vorige pagina.")) + .setSkullTexture("http://textures.minecraft.net/texture/6e8c3ce2aee6cf2faade7db37bbae73a36627ac1473fef75b410a0af97659f") + .toItemStack(), e -> open(player, pagination.previous().getPage()))); + + contents.set(5, 4, ClickableItem.of(new ItemBuilder(Material.BARRIER, 1) + .setName(__("&7Sluiten")) + .setLore(__("&eSluit dit menu.")) + .toItemStack(), e -> player.closeInventory())); + + contents.set(5, 8, ClickableItem.of(new ItemBuilder(Material.PLAYER_HEAD, 1) + .setName(__("&7Volgende pagina")) + .setLore(__("&eGa naar de volgende pagina.")) + .setSkullTexture("http://textures.minecraft.net/texture/6e8cd53664d9307b6869b9abbae2b7737ab762bb18bb34f31c5ca8f3edb63b6") + .toItemStack(), e -> open(player, pagination.next().getPage()))); } } diff --git a/src/main/java/nl/sbdeveloper/showapi/utils/Inventory.java b/src/main/java/nl/sbdeveloper/showapi/utils/Inventory.java new file mode 100644 index 0000000..aaaf15e --- /dev/null +++ b/src/main/java/nl/sbdeveloper/showapi/utils/Inventory.java @@ -0,0 +1,53 @@ +package nl.sbdeveloper.showapi.utils; + +import fr.minuskube.inv.InventoryManager; +import fr.minuskube.inv.SmartInventory; +import fr.minuskube.inv.content.InventoryContents; +import fr.minuskube.inv.content.InventoryProvider; +import nl.sbdeveloper.showapi.ShowAPIPlugin; +import org.bukkit.entity.Player; + +import static nl.sbdeveloper.showapi.utils.MainUtil.__; + +public abstract class Inventory implements InventoryProvider { + /* + This file is part of FrogRacing. + Copyright (c) 2018-2021 FrogNetwork - All Rights Reserved + Unauthorized copying of this file, via any medium is strictly prohibited + Proprietary and confidential + Written by Stijn Bannink , March 2020 + */ + + private static InventoryManager manager; + protected SmartInventory inventory; + + public Inventory(int rows, String title) { + this(rows, title, true); //Standaard sluitbaar! + } + + public Inventory(int rows, String title, boolean closeable) { + this.inventory = SmartInventory.builder().id(title).provider(this).manager(manager).size(rows, 9).closeable(closeable).title(__("&8" + title)).build(); + } + + @Override + public void update(Player player, InventoryContents contents) { + // Niet altijd nodig, daarom staat hij hier alvast. + } + + protected void open(Player player) { + this.inventory.open(player); + } + + protected void open(Player player, int page) { + this.inventory.open(player, page); + } + + public void close(Player player) { + this.inventory.close(player); + } + + public static void init() { + manager = new InventoryManager(ShowAPIPlugin.getInstance()); + manager.init(); + } +} diff --git a/src/main/java/nl/sbdeveloper/showapi/utils/ItemBuilder.java b/src/main/java/nl/sbdeveloper/showapi/utils/ItemBuilder.java new file mode 100644 index 0000000..561a7b1 --- /dev/null +++ b/src/main/java/nl/sbdeveloper/showapi/utils/ItemBuilder.java @@ -0,0 +1,601 @@ +package nl.sbdeveloper.showapi.utils; + +import com.cryptomorin.xseries.SkullUtils; +import org.bukkit.Bukkit; +import org.bukkit.Color; +import org.bukkit.Material; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemFlag; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.inventory.meta.PotionMeta; +import org.bukkit.inventory.meta.SkullMeta; +import org.bukkit.material.MaterialData; +import org.bukkit.potion.PotionType; + +import java.util.*; +import java.util.function.Function; + +/** + * @author Blutkrone + */ +public class ItemBuilder { + /* + This file is part of FrogRacing. + Copyright (c) 2018-2021 FrogNetwork - All Rights Reserved + Unauthorized copying of this file, via any medium is strictly prohibited + Proprietary and confidential + Written by Stijn Bannink , March 2020 + */ + + private final ItemStack is; + private static boolean loaded = false; + private static boolean usePotionSetColor = false; + + /** */ + private static void init() { + try { + Class.forName("org.bukkit.Color"); + usePotionSetColor = true; + } catch (Exception e) { + usePotionSetColor = false; + } + + loaded = true; + } + + /** + * Create a new ItemBuilder from scratch. + * + * @param m The material to create the ItemBuilder with. + */ + public ItemBuilder(Material m) { + this(m, 1); + } + + /** + * Create a new ItemBuilder over an existing itemstack. + * + * @param is The itemstack to create the ItemBuilder over. + */ + public ItemBuilder(ItemStack is) { + this.is = is; + } + + /** + * Create a new ItemBuilder from scratch. + * + * @param m The material of the item. + * @param amount The evaluate of the item. + */ + public ItemBuilder(Material m, int amount) { + this(m, amount, (short) 0); + } + + /** + * Create a new ItemBuilder from scratch. + * + * @param m The material of the item. + * @param amount The evaluate of the item. + * @param durability The durability of the item. + */ + public ItemBuilder(Material m, int amount, short durability) { + this(m, amount, durability, null); + } + + /** + * Create a new ItemBuilder from scratch. + * + * @param m The material of the item. + * @param amount The evaluate of the item. + * @param durability The durability of the item. + */ + public ItemBuilder(Material m, int amount, short durability, Byte data) { + if (!loaded) init(); + + if (data != null && data > 0 && data <= 15) { + is = new ItemStack(m, amount); + setDurability(durability); + + // Ensure that we only allow applying data on valid objects. + if (is.getType().name().contains("STAINED_GLASS")) { + setData(GlassColor.values()[data]); + } else if (is.getType().name().contains("WOOL")) { + setData(WoolColor.values()[data]); + } else if (is.getType().name().contains("DYE") || DyeColor.is(is.getType())) { + setData(DyeColor.values()[data]); + } else + Bukkit.getLogger().warning("Unable to assign " + m + " a data value!"); + } else { + is = new ItemStack(m, amount); + setDurability(durability); + } + } + + public static ItemBuilder create(ItemStack from) { + return new ItemBuilder(from); + } + + public ItemBuilder setData(GlassColor color) { + if (is.getType().name().contains("STAINED_GLASS")) { + color.apply(this); + } else + throw new IllegalArgumentException("Cannot apply " + color.getClass().getSimpleName() + " on " + is.getType()); + return this; + } + + public ItemBuilder setData(WoolColor color) { + if (is.getType().name().contains("WOOL")) { + color.apply(this); + } else + throw new IllegalArgumentException("Cannot apply " + color.getClass().getSimpleName() + " on " + is.getType()); + return this; + } + + public ItemBuilder setData(DyeColor color) { + if (is.getType().name().contains("DYE") || DyeColor.is(is.getType())) { + color.apply(this); + } else + throw new IllegalArgumentException("Cannot apply " + color.getClass().getSimpleName() + " on " + is.getType()); + return this; + } + + public ItemBuilder setData(PotionColor color) { + if (is.getType().name().contains("POTION")) { + color.apply(this); + } else + throw new IllegalArgumentException("Cannot apply " + color.getClass().getSimpleName() + " on " + is.getType()); + return this; + } + + /** + * Clone the ItemBuilder into a new one. + * + * @return The cloned instance. + */ + public ItemBuilder clone() { + return new ItemBuilder(is.clone()); + } + + /** + * Change the durability of the item. + * + * @param dur The durability to set it to. + */ + public ItemBuilder setDurability(short dur) { + if (is.getType() == Material.AIR) return this; + ItemMeta meta = is.getItemMeta(); + if (meta instanceof org.bukkit.inventory.meta.Damageable) { + ((org.bukkit.inventory.meta.Damageable) meta).setDamage(dur); + is.setItemMeta(meta); + } + return this; + } + + /** + * Change the durability of the item. + * + * @param data The durability to set it to. + */ + @Deprecated + public ItemBuilder setData(byte data) { + if (is.getType() == Material.AIR) return this; + is.setData(new MaterialData(is.getType(), data)); + return this; + } + + /** + * Change the unbreakable of the item. + * + * @param unbreakable The unbreakable to set it to. + */ + public ItemBuilder setUnbreakable(boolean unbreakable) { + if (is.getType() == Material.AIR) return this; + ItemMeta meta = is.getItemMeta(); + meta.setUnbreakable(unbreakable); + is.setItemMeta(meta); + return this; + } + + /** + * Change the evaluate of the item. + * + * @param amount The evaluate to set it to. + */ + public ItemBuilder setAmount(int amount) { + if (is.getType() == Material.AIR) return this; + is.setAmount(amount); + return this; + } + + /** + * Add an item flag + * + * @param flags item flagS + */ + public ItemBuilder addFlags(ItemFlag... flags) { + if (is.getType() == Material.AIR) return this; + ItemMeta im = is.getItemMeta(); + im.addItemFlags(flags); + is.setItemMeta(im); + return this; + } + + /** + * Remove an item flag + * + * @param flags item flagS + */ + public ItemBuilder removeFlags(ItemFlag... flags) { + if (is.getType() == Material.AIR) return this; + ItemMeta im = is.getItemMeta(); + im.removeItemFlags(flags); + is.setItemMeta(im); + return this; + } + + /** + * Set the displayname of the item. + * + * @param name The name to change it to. + */ + public ItemBuilder setName(String name) { + if (is.getType() == Material.AIR || name == null || name.isEmpty()) return this; + ItemMeta im = is.getItemMeta(); + im.setDisplayName(name); + is.setItemMeta(im); + return this; + } + + /** + * Add an unsafe enchantment. + * + * @param ench The enchantment to add. + * @param level The level to put the enchant on. + */ + public ItemBuilder addUnsafeEnchantment(Enchantment ench, int level) { + if (is.getType() == Material.AIR) return this; + is.addUnsafeEnchantment(ench, level); + return this; + } + + /** + * Remove a certain enchant from the item. + * + * @param ench The enchantment to remove + */ + public ItemBuilder removeEnchantment(Enchantment ench) { + if (is.getType() == Material.AIR) return this; + is.removeEnchantment(ench); + return this; + } + + public ItemBuilder glow() { + if (is.getType() == Material.AIR) return this; + is.addUnsafeEnchantment(Enchantment.LURE, 0); + ItemMeta im = is.getItemMeta(); + im.addItemFlags(ItemFlag.HIDE_ENCHANTS); + is.setItemMeta(im); + return this; + } + + /** + * Set the skull getOwner for the item. Works on skulls only. + * + * @param owner The name of the skull's getOwner. + */ + public ItemBuilder setSkullOwner(String owner) { + if (is.getType() == Material.AIR) return this; + try { + SkullMeta im = (SkullMeta) is.getItemMeta(); + im.setOwner(owner); + is.setItemMeta(im); + } catch (ClassCastException expected) { + } + return this; + } + + public ItemBuilder setSkullTexture(String url) { + if (is.getType() == Material.AIR) return this; + is.setItemMeta(SkullUtils.applySkin(is.getItemMeta(), url)); + return this; + } + + /** + * Add an enchant to the item. + * + * @param ench The enchant to add + * @param level The level + */ + public ItemBuilder addEnchant(Enchantment ench, int level) { + if (is.getType() == Material.AIR) return this; + ItemMeta im = is.getItemMeta(); + im.addEnchant(ench, level, true); + is.setItemMeta(im); + return this; + } + + /** + * Add multiple enchants at once. + * + * @param enchantments The enchants to add. + */ + public ItemBuilder addEnchantments(Map enchantments) { + if (is.getType() == Material.AIR) return this; + is.addEnchantments(enchantments); + return this; + } + + /** + * Sets infinity durability on the item by setting the durability to Short.MAX_VALUE. + */ + public ItemBuilder setInfinityDurability() { + if (is.getType() == Material.AIR) return this; + is.setDurability(Short.MAX_VALUE); + return this; + } + + /** + * Re-sets the lore. + * + * @param lore The lore to set it to. + */ + public ItemBuilder setLore(String... lore) { + if (is.getType() == Material.AIR || lore == null) return this; + ItemMeta im = is.getItemMeta(); + im.setLore(Arrays.asList(lore)); + is.setItemMeta(im); + return this; + } + + /** + * Re-sets the lore. + * + * @param lore The lore to set it to. + */ + public ItemBuilder setLore(List lore) { + if (is.getType() == Material.AIR) return this; + ItemMeta im = is.getItemMeta(); + im.setLore(lore); + is.setItemMeta(im); + return this; + } + + /** + * Remove a lore line. + * + * @param line The lore to remove. + */ + public ItemBuilder removeLoreLine(String line) { + if (is.getType() == Material.AIR) return this; + ItemMeta im = is.getItemMeta(); + List lore = new ArrayList<>(im.getLore()); + if (!lore.contains(line)) return this; + lore.remove(line); + im.setLore(lore); + is.setItemMeta(im); + return this; + } + + /** + * Remove a lore line. + * + * @param index The index of the lore line to remove. + */ + public ItemBuilder removeLoreLine(int index) { + if (is.getType() == Material.AIR) return this; + ItemMeta im = is.getItemMeta(); + List lore = new ArrayList<>(im.getLore()); + if (index < 0 || index > lore.size()) return this; + lore.remove(index); + im.setLore(lore); + is.setItemMeta(im); + return this; + } + + /** + * Add a lore line. + * + * @param line The lore line to add. + */ + public ItemBuilder addLoreLine(String line) { + if (is.getType() == Material.AIR) return this; + ItemMeta im = is.getItemMeta(); + List lore = new ArrayList<>(); + if (im.hasLore()) lore = new ArrayList<>(im.getLore()); + lore.add(line); + im.setLore(lore); + is.setItemMeta(im); + return this; + } + + public ItemBuilder addLoreLine(String... lines) { + if (is.getType() == Material.AIR) return this; + for (String line : lines) { + addLoreLine(line); + } + return this; + } + + public ItemBuilder addLoreLine(Collection lines) { + if (is.getType() == Material.AIR) return this; + for (String line : lines) { + addLoreLine(line); + } + return this; + } + + /** + * Add a lore line. + * + * @param line The lore line to add. + * @param pos The index of where to put it. + */ + public ItemBuilder addLoreLine(String line, int pos) { + if (is.getType() == Material.AIR) return this; + ItemMeta im = is.getItemMeta(); + List lore = new ArrayList<>(im.getLore()); + lore.set(pos, line); + im.setLore(lore); + is.setItemMeta(im); + return this; + } + + /** + * Direct access to the item meta + * + * @param modifier function to modify the meta. + */ + public ItemBuilder writeMeta( + Function modifier + ) { + if (is.getType() == Material.AIR) return this; + is.setItemMeta(modifier.apply(is.getItemMeta())); + return this; + } + + /** + * Retrieves the itemstack from the ItemBuilder. + * + * @return The itemstack created/modified by the ItemBuilder instance. + */ + public ItemStack toItemStack() { + return is; + } + + public enum GlassColor { + WHITE, + ORANGE, + MAGENTA, + LIGHT_BLUE, + YELLOW, + LIME, + PINK, + GRAY, + LIGHT_GRAY, + CYAN, + PURPLE, + BLUE, + BROWN, + GREEN, + RED, + BLACK; + + private void apply(ItemBuilder builder) { + boolean panel = builder.is.getType().name().contains("STAINED_GLASS_PANE"); + builder.is.setType(Material.valueOf(name() + (panel ? "_STAINED_GLASS_PANE" : "STAINED_GLASS"))); + } + } + + public enum WoolColor { + WHITE, + ORANGE, + MAGENTA, + LIGHT_BLUE, + YELLOW, + LIME, + PINK, + GRAY, + LIGHT_GRAY, + CYAN, + PURPLE, + BLUE, + BROWN, + GREEN, + RED, + BLACK; + + private void apply(ItemBuilder builder) { + builder.is.setType(Material.valueOf(name() + "_WOOL")); + } + } + + public enum DyeColor { + BLACK(0, "INK_SACK"), + RED(1, "ROSE_RED"), + GREEN(2, "CACTUS_GREEN"), + BROWN(3, "COCOA_BEANS"), + BLUE(4, "LAPIS_LAZULI"), + PURPLE(5, "PURPLE_DYE"), + CYAN(6, "CYAN_DYE"), + LIGHT_GRAY(7, "LIGHT_GRAY_DYE"), + GRAY(8, "GRAY_DYE"), + PINK(9, "PINK_DYE"), + LIME(10, "LIME_DYE"), + YELLOW(11, "DANDELION_YELLOW"), + LIGHT_BLUE(12, "LIGHT_BLUE_DYE"), + MAGENTA(13, "MAGENTA_DYE"), + ORANGE(14, "ORANGE_DYE"), + WHITE(15, "BONE_MEAL"); + + private final String coded; + + DyeColor(int data, String coded) { + this.coded = coded; + } + + static boolean is(Material mat) { + if (mat.name().contains("DYE")) return true; + if (mat.name().contains("INK_SAC")) return true; + + for (DyeColor dyeColor : values()) { + if (dyeColor.name().equalsIgnoreCase(mat.name())) + return true; + } + + return false; + } + + private void apply(ItemBuilder builder) { + builder.is.setType(Material.valueOf(coded)); + } + } + + public enum PotionColor { + PINK("REGEN", 0xF442E2), + CYAN("SPEED", 0x42f1f4), + GOLD("FIRE_RESISTANCE", 0xf4b942), + DARK_GREEN("POISON", 0x365b0e), + RED("INSTANT_HEAL", 0xe5251b), + DARK_BLUE("NIGHT_VISION", 0x092366), + DARK_GRAY("WEAKNESS", 0x2e2f33), + DARK_RED("STRENGTH", 0x4f0a01), + GRAY("SLOWNESS", 0x939393), + LIGHT_GREEN("JUMP", 0x42f4b0), + BROWN("INSTANT_DAMAGE", 0xa54126), + BLUE("WATER_BREATHING", 0x0e59ef), // TEAL + LIGHT_GRAY("INVISIBILITY", 0xc4c4c4), + GREEN("LUCK", 0x1f890f), + BLACK(null, 0x161616), + LIGHT_BROWN("TURTLE_MASTER", 0xad581b), + SILVER("SLOW_FALLING", 0xd8ccc3); + + private final String potionType; + private final int rgb; + + PotionColor(String potionType, int rgb) { + this.potionType = potionType; + this.rgb = rgb; + } + + private void apply(ItemBuilder builder) { + PotionMeta meta = (PotionMeta) builder.is.getItemMeta(); + if (usePotionSetColor) { + meta.setColor(Color.fromRGB(rgb)); + } else if (potionType != null) { + try { + meta.setBasePotionData(new org.bukkit.potion.PotionData(PotionType.valueOf(potionType))); + } catch (Exception e) { + Bukkit.getLogger().severe("Unknown Potion Color: " + this + ", " + + "the underlying " + potionType + " handle isn't available in this version!"); + } + } + builder.is.setItemMeta(meta); + } + } + + public enum FireworkColor { + + } +} + diff --git a/src/main/java/nl/sbdeveloper/showapi/utils/MainUtil.java b/src/main/java/nl/sbdeveloper/showapi/utils/MainUtil.java index 0ab1d49..a5d31f2 100644 --- a/src/main/java/nl/sbdeveloper/showapi/utils/MainUtil.java +++ b/src/main/java/nl/sbdeveloper/showapi/utils/MainUtil.java @@ -1,8 +1,7 @@ package nl.sbdeveloper.showapi.utils; -import com.samjakob.spigui.item.ItemBuilder; import nl.sbdeveloper.showapi.api.ShowCue; -import nl.sbdeveloper.showapi.api.TriggerData; +import nl.sbdeveloper.showapi.api.TriggerTask; import nl.sbdeveloper.showapi.api.TriggerType; import org.apache.commons.lang.StringUtils; import org.bukkit.ChatColor; @@ -17,24 +16,28 @@ import java.util.Arrays; import java.util.List; public class MainUtil { + public static String __(String in) { + return ChatColor.translateAlternateColorCodes('&', in); + } + public static ItemStack pointToItem(ShowCue point) { ItemBuilder builder = new ItemBuilder(Material.NOTE_BLOCK); - builder.name(ChatColor.ITALIC + "TimeCode: " + TimeUtil.showTime(point.getTicks())); + builder.setName(ChatColor.ITALIC + "TimeCode: " + TimeUtil.makeReadable(point.getTime())); List lores = new ArrayList<>(); - lores.add(ChatColor.GREEN + "Type: " + ChatColor.AQUA + StringUtils.capitalize(point.getData().getType().name())); + lores.add(ChatColor.GREEN + "Type: " + ChatColor.AQUA + StringUtils.capitalize(point.getTask().getType().name())); lores.add(ChatColor.GREEN + "Data:"); - for (String str : ChatPaginator.paginate(point.getData().getDataString(), 20).getLines()) { + for (String str : ChatPaginator.paginate(point.getTask().getDataString(), 20).getLines()) { lores.add(ChatColor.AQUA + ChatColor.stripColor(str)); } lores.add(""); lores.add(ChatColor.RED + ChatColor.BOLD.toString() + "Click to remove!"); - builder.lore(lores); - return builder.build(); + builder.setLore(lores); + return builder.toItemStack(); } - public static TriggerData parseData(String data) { + public static TriggerTask parseData(String data) { String[] dataSplitter = data.split(" "); String[] dataSplitterNew = Arrays.copyOfRange(dataSplitter, 1, dataSplitter.length); @@ -46,7 +49,7 @@ public class MainUtil { } try { - Constructor ctor = type.getTrigger().getConstructor(String[].class); + Constructor ctor = type.getTrigger().getConstructor(String[].class); if (dataSplitter.length < type.getMinArgs()) return null; return ctor.newInstance(new Object[] { dataSplitterNew }); } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) { diff --git a/src/main/java/nl/sbdeveloper/showapi/utils/TimeUtil.java b/src/main/java/nl/sbdeveloper/showapi/utils/TimeUtil.java index c8468f0..5ac05d4 100644 --- a/src/main/java/nl/sbdeveloper/showapi/utils/TimeUtil.java +++ b/src/main/java/nl/sbdeveloper/showapi/utils/TimeUtil.java @@ -1,75 +1,59 @@ package nl.sbdeveloper.showapi.utils; import java.time.LocalTime; -import java.time.format.DateTimeParseException; -import java.util.regex.Matcher; -import java.util.regex.Pattern; +import java.time.format.DateTimeFormatter; +import java.util.concurrent.TimeUnit; +/** + * Source from: + * https://github.com/Mindgamesnl/OpenAudioMc/blob/master/plugin/src/main/java/com/craftmend/openaudiomc/spigot/modules/show/util/TimeParser.java + */ public class TimeUtil { - private static final int s = 1000; - private static final int m = s * 60; - private static final int h = m * 60; - private static final int d = h * 24; - private static final int w = d * 7; - private static final int y = (int)(d * 365.25); - public static String showTime(int seconds) { - LocalTime timeOfDay = LocalTime.ofSecondOfDay(seconds / 20); - return timeOfDay.toString(); - } + public static Long toMilis(String input) { + long time = 0L; - public static int parseTicks(String str) { - try { - LocalTime localTime = LocalTime.parse(str); - return localTime.toSecondOfDay(); - } catch (DateTimeParseException ex) { - Pattern pattern = Pattern.compile("^(-?(?:\\d+)?\\.?\\d+) *(ticks?|tick?|t|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)?$"); - Matcher matcher = pattern.matcher(str); - - if (!matcher.find()) return 0; - - float n = Float.parseFloat(matcher.group(1)); - switch (matcher.group(2).toLowerCase()) { - case "years": - case "year": - case "yrs": - case "yr": - case "y": - return (int)(n * y) / 20000; - case "weeks": - case "week": - case "w": - return (int)(n * w) / 20000; - case "days": - case "day": - case "d": - return (int)(n * d) / 20000; - case "hours": - case "hour": - case "hrs": - case "hr": - case "h": - return (int)(n * h) / 20000; - case "minutes": - case "minute": - case "mins": - case "min": - case "m": - return (int)(n * m) / 20000; - case "seconds": - case "second": - case "secs": - case "sec": - case "s": - return (int)(n * s) / 20000; - case "ticks": - case "tick": - case "ts": - case "t": - return (int) n / 20000; - default: - return 0; - } + // ITS A TIMECODE + if (input.contains(":")) { + return LocalTime.parse(input, DateTimeFormatter.ofPattern("HH:mm:ss")).toSecondOfDay() * 1000L; } + + input = input.toLowerCase() + "-"; + + String[] milisSplit = input.split("ms"); + if (isValid(milisSplit)) { + time += Long.parseLong(milisSplit[0]); + return time; + } + + String[] secondsSplit = input.split("s"); + if (isValid(secondsSplit)) { + time += Math.round(Double.parseDouble(secondsSplit[0]) * 1000); + return time; + } + + String[] minutesSplit = input.split("m"); + if (isValid(minutesSplit)) { + time += Math.round(Double.parseDouble(minutesSplit[0]) * 60000); + return time; + } + + String[] tickSplit = input.split("t"); + if (isValid(tickSplit)) { + time += Math.round(Integer.parseInt(tickSplit[0]) * 50); + return time; + } + + return time; } -} + + private static boolean isValid(String[] array) { + return array.length > 1 && array[0].length() > 0; + } + + public static Object 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))); + } +} \ No newline at end of file