From 5d8a0139abc2d0e752dd3fdcfdd222b0e576d6e8 Mon Sep 17 00:00:00 2001 From: stijnb1234 Date: Sat, 21 Nov 2020 13:06:26 +0100 Subject: [PATCH 1/6] Started with Malfunction GUI --- pom.xml | 19 ++++ .../themeparkplus/ThemeParkPlus.java | 14 ++- .../themeparkplus/api/PlusAPI.java | 22 +++- .../api/objects/MalfunctionReport.java | 21 ++-- .../themeparkplus/gui/MalfunctionGUI.java | 102 ++++++++++++++++++ .../listeners/FastpassListeners.java | 1 - .../listeners/MalfunctionListener.java | 34 ++++++ .../listeners/SignListeners.java | 5 + .../listeners/StatusChangeListener.java | 30 +++++- src/main/resources/config.yml | 37 ++++++- src/main/resources/messages.yml | 4 +- 11 files changed, 271 insertions(+), 18 deletions(-) create mode 100644 src/main/lombok/nl/sbdeveloper/themeparkplus/gui/MalfunctionGUI.java create mode 100644 src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/MalfunctionListener.java diff --git a/pom.xml b/pom.xml index 9734812..c63ff9b 100644 --- a/pom.xml +++ b/pom.xml @@ -55,6 +55,10 @@ org.bstats nl.sbdeveloper.themeparkplus.libs.bstats + + com.samjakob.spigui + nl.sbdeveloper.themeparkplus.libs.spigui + @@ -172,5 +176,20 @@ worldguardwrapper 1.1.9-SNAPSHOT + + com.samjakob + SpiGUI + v1.1 + + + org.spigotmc + spigot-api + + + org.bukkit + bukkit + + + diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java index fa23474..9ddb15b 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java @@ -1,5 +1,6 @@ package nl.sbdeveloper.themeparkplus; +import com.samjakob.spigui.SpiGUI; import net.milkbowl.vault.economy.Economy; import nl.sbdeveloper.themeparkplus.commands.TPPCMD; import nl.sbdeveloper.themeparkplus.commands.TPPTabComplete; @@ -32,7 +33,9 @@ public final class ThemeParkPlus extends JavaPlugin { private static Economy econ = null; private static String webhookURL; - private final int configVersion = 2; + private static SpiGUI spiGUI; + + private final int configVersion = 3; @Override public void onEnable() { @@ -140,6 +143,7 @@ public final class ThemeParkPlus extends JavaPlugin { Bukkit.getPluginManager().registerEvents(new DirectionalGateListener(), this); Bukkit.getPluginManager().registerEvents(new FastpassListeners(), this); Bukkit.getPluginManager().registerEvents(new SignListeners(), this); + Bukkit.getPluginManager().registerEvents(new MalfunctionListener(), this); Bukkit.getPluginManager().registerEvents(new WaitingTimeListener(), this); if (getSConfig().getFile().getBoolean("AntiFreerun.Enabled")) { Bukkit.getPluginManager().registerEvents(new AntiFreerunListener(), this); @@ -157,6 +161,10 @@ public final class ThemeParkPlus extends JavaPlugin { } } + Bukkit.getLogger().info("[ThemeParkPlus] Loading GUI helper..."); + spiGUI = new SpiGUI(this); + spiGUI.setEnableAutomaticPagination(false); + Bukkit.getLogger().info("[ThemeParkPlus] Loading data..."); try { data.load(); @@ -205,6 +213,10 @@ public final class ThemeParkPlus extends JavaPlugin { return webhookURL; } + public static SpiGUI getSpiGUI() { + return spiGUI; + } + private boolean setupEconomy() { if (getServer().getPluginManager().getPlugin("Vault") == null) { return false; diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/api/PlusAPI.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/api/PlusAPI.java index 22f44ed..8a9d029 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/api/PlusAPI.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/api/PlusAPI.java @@ -4,6 +4,7 @@ import de.tr7zw.changeme.nbtapi.NBTItem; import nl.iobyte.themepark.api.attraction.Attraction; import nl.sbdeveloper.themeparkplus.ThemeParkPlus; import nl.sbdeveloper.themeparkplus.api.objects.Gate; +import nl.sbdeveloper.themeparkplus.api.objects.MalfunctionReport; import nl.sbdeveloper.themeparkplus.api.objects.WaitingRow; import nl.sbdeveloper.themeparkplus.util.ConfigUtil; import nl.sbdeveloper.themeparkplus.util.XMaterial; @@ -18,8 +19,25 @@ import org.jetbrains.annotations.Nullable; import java.util.*; public class PlusAPI { - private static HashMap gates = new HashMap<>(); - private static HashMap rows = new HashMap<>(); + private static final HashMap gates = new HashMap<>(); + private static final HashMap rows = new HashMap<>(); + private static final HashMap reports = new HashMap<>(); + + public static void addReport(MalfunctionReport report) { + reports.put(report.getRideID(), report); + } + + public static void removeReport(String rideID) { + reports.remove(rideID); + } + + public static MalfunctionReport getReport(String rideID) { + return reports.get(rideID); + } + + public static boolean isReported(String rideID) { + return reports.containsKey(rideID); + } /** * Add a gate diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/api/objects/MalfunctionReport.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/api/objects/MalfunctionReport.java index ac67429..3fc8892 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/api/objects/MalfunctionReport.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/api/objects/MalfunctionReport.java @@ -1,18 +1,19 @@ package nl.sbdeveloper.themeparkplus.api.objects; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; +import lombok.*; import java.time.LocalDateTime; import java.util.UUID; -/** @deprecated Please don't use! It's not implemented yet. */ -@Getter @Setter @NoArgsConstructor @AllArgsConstructor +@Getter @NoArgsConstructor public class MalfunctionReport { - private String rideID; - private UUID reporterUUID; - private LocalDateTime reportDate; - private String reason; + @Getter private String rideID; + @Getter private UUID reporterUUID; + @Getter private final LocalDateTime reportDate = LocalDateTime.now(); + @Getter @Setter private String reason = "Unknown"; + + public MalfunctionReport(String rideID, UUID reporterUUID) { + this.rideID = rideID; + this.reporterUUID = reporterUUID; + } } diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/gui/MalfunctionGUI.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/gui/MalfunctionGUI.java new file mode 100644 index 0000000..2c3e7c9 --- /dev/null +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/gui/MalfunctionGUI.java @@ -0,0 +1,102 @@ +package nl.sbdeveloper.themeparkplus.gui; + +import com.samjakob.spigui.SGMenu; +import com.samjakob.spigui.buttons.SGButton; +import com.samjakob.spigui.item.ItemBuilder; +import nl.iobyte.themepark.api.API; +import nl.iobyte.themepark.api.attraction.Attraction; +import nl.iobyte.themepark.api.attraction.component.Status; +import nl.sbdeveloper.themeparkplus.ThemeParkPlus; +import nl.sbdeveloper.themeparkplus.api.PlusAPI; +import nl.sbdeveloper.themeparkplus.api.objects.MalfunctionReport; +import nl.sbdeveloper.themeparkplus.util.ConfigUtil; +import nl.sbdeveloper.themeparkplus.util.XMaterial; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.inventory.ItemStack; + +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +public class MalfunctionGUI { + public static void open(Attraction att, Player p) { + SGMenu menu = ThemeParkPlus.getSpiGUI().create(ConfigUtil.makecolored(ThemeParkPlus.getSConfig().getFile().getString("Malfunction.GUI.Title").replace("%ridename%", att.getId())), 1); + + SGButton report = new SGButton(getItem("Report", ConfigUtil.makecolored(att.getName()))).withListener((InventoryClickEvent e) -> { + if (!PlusAPI.isReported(att.getId())) { + MalfunctionReport malfunctionReport = new MalfunctionReport(att.getId(), p.getUniqueId()); + PlusAPI.addReport(malfunctionReport); + att.setStatus(Status.MALFUNCTION, null); //Player must be null, otherwise we will get a loop in the listener + + //TODO Send message + } else { + //TODO Already reported + } + p.closeInventory(); + }); + menu.setButton(getSlot("Report"), report); + + SGButton fix = new SGButton(getItem("Fix", PlusAPI.getReport(att.getId()))).withListener((InventoryClickEvent e) -> { + if (PlusAPI.isReported(att.getId())) { + + PlusAPI.removeReport(att.getId()); + att.setStatus(Status.CLOSED, null); //Player must be null, otherwise we will get a loop in the listener + + //TODO Send message + } else { + //TODO No malfunction + } + p.closeInventory(); + }); + menu.setButton(getSlot("Fix"), fix); + + SGButton cancel = new SGButton(getItem("Cancel")).withListener((InventoryClickEvent e) -> p.closeInventory()); + menu.setButton(getSlot("Cancel"), cancel); + + p.openInventory(menu.getInventory()); + } + + private static int getSlot(String type) { + return ThemeParkPlus.getSConfig().getFile().getInt("Malfunction.GUI.Buttons." + type + ".Slot"); + } + + private static ItemStack getItem(String type, Object... data) { //data is a string or a report + String material = ThemeParkPlus.getSConfig().getFile().getString("Malfunction.GUI.Buttons." + type + ".Material"); + String name = ThemeParkPlus.getSConfig().getFile().getString("Malfunction.GUI.Buttons." + type + ".Name"); + List lores = ThemeParkPlus.getSConfig().getFile().getStringList("Malfunction.GUI.Buttons." + type + ".Lores"); + + if (material == null || name == null || lores.isEmpty()) return null; //Something is wrong in the config + + List loresFormatted = new ArrayList<>(); + switch (type) { + case "Report": + lores.forEach(lore -> loresFormatted.add(lore.replace("%ridename%", (String) data[0]))); + break; + case "Fix": + String ridename = "this ride"; + String reporter = "-"; + String date = "-"; + String reason = "-"; + if (data[0] != null) { + ridename = API.getAttraction(((MalfunctionReport) data[0]).getRideID()).getName(); + reporter = Bukkit.getOfflinePlayer(((MalfunctionReport) data[0]).getReporterUUID()).getName(); + date = ((MalfunctionReport) data[0]).getReportDate().format(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss")); + reason = ((MalfunctionReport) data[0]).getReason(); + } + + for (String lore : lores) { + loresFormatted.add(lore.replace("%ridename%", ridename).replace("%reporter%", reporter).replace("%date%", date).replace("%reason%", reason)); + } + break; + default: + break; + } + + Optional stack = XMaterial.matchXMaterial(material); + + return stack.map(xMaterial -> new ItemBuilder(xMaterial.parseItem()).name(name).lore(loresFormatted).build()).orElse(null); + } +} diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/FastpassListeners.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/FastpassListeners.java index 121b53a..f4752a9 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/FastpassListeners.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/FastpassListeners.java @@ -46,7 +46,6 @@ public class FastpassListeners implements Listener { //Buy a ticket String attID = sign.getLine(2); if (!API.isAttraction(attID)) return; - Attraction att = API.getAttraction(attID); double price; diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/MalfunctionListener.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/MalfunctionListener.java new file mode 100644 index 0000000..c09e865 --- /dev/null +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/MalfunctionListener.java @@ -0,0 +1,34 @@ +package nl.sbdeveloper.themeparkplus.listeners; + +import nl.iobyte.themepark.api.API; +import nl.iobyte.themepark.api.attraction.Attraction; +import nl.sbdeveloper.themeparkplus.ThemeParkPlus; +import nl.sbdeveloper.themeparkplus.gui.MalfunctionGUI; +import nl.sbdeveloper.themeparkplus.util.ConfigUtil; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.block.Sign; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.EquipmentSlot; + +public class MalfunctionListener implements Listener { + @EventHandler + public void onClickSign(PlayerInteractEvent e) { + if (e.getClickedBlock() == null || e.getAction() != Action.RIGHT_CLICK_BLOCK || !(e.getClickedBlock().getState() instanceof Sign) || e.getHand() != EquipmentSlot.HAND) return; + + String mrLineOne = ChatColor.stripColor(ConfigUtil.makecolored(ThemeParkPlus.getSConfig().getFile().getString("Malfunction.Sign.Row1"))); + String mrLineTwo = ChatColor.stripColor(ConfigUtil.makecolored(ThemeParkPlus.getSConfig().getFile().getString("Malfunction.Sign.Row2"))); + + Sign sign = (Sign) e.getClickedBlock().getState(); + if (ChatColor.stripColor(sign.getLine(0)).equalsIgnoreCase(mrLineOne) && ChatColor.stripColor(sign.getLine(1)).equalsIgnoreCase(mrLineTwo)) { + String attID = sign.getLine(2); + if (!API.isAttraction(attID)) return; + Attraction att = API.getAttraction(attID); + + MalfunctionGUI.open(att, e.getPlayer()); + } + } +} diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/SignListeners.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/SignListeners.java index 30276ed..41ea2a1 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/SignListeners.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/SignListeners.java @@ -52,6 +52,8 @@ public class SignListeners implements Listener { String sLineTwo = ConfigUtil.makecolored(ThemeParkPlus.getSConfig().getFile().getString("Fastpass.ScannerSign.Row2")); String wrLineOne = ConfigUtil.makecolored(ThemeParkPlus.getSConfig().getFile().getString("WaitingRow.Sign.Row1")); String wrLineTwo = ConfigUtil.makecolored(ThemeParkPlus.getSConfig().getFile().getString("WaitingRow.Sign.Row2")); + String mrLineOne = ConfigUtil.makecolored(ThemeParkPlus.getSConfig().getFile().getString("Malfunction.Sign.Row1")); + String mrLineTwo = ConfigUtil.makecolored(ThemeParkPlus.getSConfig().getFile().getString("Malfunction.Sign.Row2")); if (lines[1].equalsIgnoreCase("Machine") && !lines[2].isEmpty() && !lines[3].isEmpty()) { e.setLine(0, mLineOne); @@ -78,6 +80,9 @@ public class SignListeners implements Listener { e.setLine(3, "0 min."); p.sendMessage(ConfigUtil.getMessage("WaitingRow.SignCreated")); + } else if (lines[1].equalsIgnoreCase("Malfunction") && !lines[2].isEmpty()) { + e.setLine(0, mrLineOne); + e.setLine(1, mrLineTwo); } else { p.sendMessage(ConfigUtil.getMessage("General.IncorrectSign")); } diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/StatusChangeListener.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/StatusChangeListener.java index 7280ca8..0025ae4 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/StatusChangeListener.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/StatusChangeListener.java @@ -1,9 +1,13 @@ package nl.sbdeveloper.themeparkplus.listeners; +import nl.iobyte.themepark.api.attraction.Attraction; import nl.iobyte.themepark.api.attraction.component.Status; import nl.iobyte.themepark.api.attraction.manager.StatusManager; import nl.iobyte.themepark.api.events.attraction.ChangeStatusEvent; +import nl.iobyte.themepark.api.events.attraction.PreChangeStatusEvent; import nl.sbdeveloper.themeparkplus.ThemeParkPlus; +import nl.sbdeveloper.themeparkplus.api.PlusAPI; +import nl.sbdeveloper.themeparkplus.api.objects.MalfunctionReport; import nl.sbdeveloper.themeparkplus.util.ConfigUtil; import nl.sbdeveloper.themeparkplus.util.DiscordWebhook; import org.bukkit.Bukkit; @@ -16,9 +20,33 @@ import java.awt.*; import java.io.IOException; /** - * Status change listener for discord webhook + * Status change listener for malfunction menu and discord webhook */ public class StatusChangeListener implements Listener { + @EventHandler + public void onStatusChange(PreChangeStatusEvent e) { + Attraction att = e.getAttraction(); + + if (!PlusAPI.isReported(att.getId()) && e.getAfter() == Status.MALFUNCTION && e.getPlayer() != null) { + if (ThemeParkPlus.getSConfig().getFile().getBoolean("Malfunction.AllowCommandReport")) { + MalfunctionReport malfunctionReport = new MalfunctionReport(att.getId(), e.getPlayer().getUniqueId()); + PlusAPI.addReport(malfunctionReport); + } else { + e.setCancelled(true); + e.getPlayer().sendMessage(ConfigUtil.getMessage("Malfunction.NoCommand")); + } + } + + if (PlusAPI.isReported(att.getId()) && e.getAfter() != Status.MALFUNCTION && e.getPlayer() != null) { + if (ThemeParkPlus.getSConfig().getFile().getBoolean("Malfunction.AllowCommandFix")) { + PlusAPI.removeReport(att.getId()); + } else { + e.setCancelled(true); + e.getPlayer().sendMessage(ConfigUtil.getMessage("Malfunction.NoCommand")); + } + } + } + @EventHandler public void onStatusChange(@NotNull ChangeStatusEvent e) { if (e.getAfter() != Status.GLOBAL) { diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index a94c745..a87d1e1 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -8,8 +8,41 @@ MessageInConsole: true WaitingRow: MinutesPerPlayer: 1.5 Sign: - Row1: "&8[&6ThemeParkPlus&8]" - Row2: "&bWaitingRow" + Row1: '&8[&6ThemeParkPlus&8]' + Row2: '&bWaitingRow' +Malfunction: + AllowCommandReport: true + AllowCommandFix: false + Sign: + Row1: '&8[&6ThemeParkPlus&8]' + Row2: '&bMalfunction' + GUI: + Title: '&3Report Malfunction for %ridename%' + Buttons: + Report: + Slot: 2 + Material: GREEN_WOOL + Name: '&2&lReport Malfunction' + Lores: + - '&fClick here to report malfunction for %ridename%.' + - '&r' + - '&cAbuse of this function is not allowed!' + Fix: + Slot: 4 + Material: STONE_PICKAXE + Name: '&e&lFix Ride' + Lores: + - '&fClick here to fix %ridename%!' + - '&r' + - '&fReporter: &7%reporter%' + - '&fDate: &7%date%' + - '&fReason: &7%reason%' + Cancel: + Slot: 6 + Material: RED_WOOL + Name: '&4&lCancel Report' + Lores: + - '&fClick here to cancel.' Fastpass: Item: DisplayName: '&6Fastpass Ticket' diff --git a/src/main/resources/messages.yml b/src/main/resources/messages.yml index 8749371..e346250 100644 --- a/src/main/resources/messages.yml +++ b/src/main/resources/messages.yml @@ -38,4 +38,6 @@ WaitingRow: SignCreated: '&aYou''ve successfully created a waitingrow sign!' WrongLocation: '&cA waitingrow sign (from the attraction %ridename%) couldn''t be found! It will be deleted.' RedstoneTimer: - Started: '&aThe timer successfully started. It will go off in &f%sec1% &asecond(s), and will turn off in &f%sec2% &asecond(s) after that.' \ No newline at end of file + Started: '&aThe timer successfully started. It will go off in &f%sec1% &asecond(s), and will turn off in &f%sec2% &asecond(s) after that.' +Malfunction: + NoCommand: '&cYou can''t report or fix a ride using the command. Please use this Malfunction Sign at the ride.' \ No newline at end of file From acadac3c2a185f5624e710bed41afed7175c941e Mon Sep 17 00:00:00 2001 From: stijnb1234 Date: Sun, 7 Feb 2021 14:11:05 +0100 Subject: [PATCH 2/6] Small fixes --- .../themeparkplus/ThemeParkPlus.java | 70 +++++++++------- .../themeparkplus/sbutils/UpdateManager.java | 83 ++++++++++++------- .../themeparkplus/util/License.java | 9 +- 3 files changed, 96 insertions(+), 66 deletions(-) diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java index 9ddb15b..7239548 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java @@ -75,41 +75,15 @@ public final class ThemeParkPlus extends JavaPlugin { Bukkit.getLogger().info("[ThemeParkPlus] Checking license..."); + final String license = config.getFile().getString("License"); if (config.getFile().contains("License")) { - Bukkit.getLogger().info("[ThemeParkPlus] Licence code: " + config.getFile().getString("License")); + Bukkit.getLogger().info("[ThemeParkPlus] Licence code: " + license); } else { Bukkit.getLogger().severe("[ThemeParkPlus] Licence code unknown! Please change the config.yml!"); return; } - new License(this, "TP", config.getFile().getString("License")); - - if (getSConfig().getFile().getBoolean("UpdateChecker.Enabled")) { - UpdateManager updateManager = new UpdateManager(this, 6, UpdateManager.CheckType.SBDPLUGINS); - - updateManager.handleResponse((versionResponse, version) -> { - if (versionResponse == UpdateManager.VersionResponse.FOUND_NEW) { - Bukkit.getLogger().warning("[ThemeParkPlus] There is a new version available! Current: " + this.getDescription().getVersion() + " New: " + version.get()); - if (getSConfig().getFile().getBoolean("UpdateChecker.DownloadOnUpdate")) { - Bukkit.getLogger().info("[ThemeParkPlus] Trying to download the update..."); - - updateManager.handleDownloadResponse((downloadResponse, fileName) -> { - if (downloadResponse == UpdateManager.DownloadResponse.DONE) { - Bukkit.getLogger().info("[ThemeParkPlus] Update downloaded! If you restart your server, it will be loaded. Filename: " + fileName); - } else if (downloadResponse == UpdateManager.DownloadResponse.ERROR) { - Bukkit.getLogger().severe("[ThemeParkPlus] Something went wrong when trying downloading the latest version."); - } else if (downloadResponse == UpdateManager.DownloadResponse.UNAVAILABLE) { - Bukkit.getLogger().warning("[ThemeParkPlus] Unable to download the latest version."); - } - }).runUpdate(); - } - } else if (versionResponse == UpdateManager.VersionResponse.LATEST) { - Bukkit.getLogger().info("[ThemeParkPlus] You are running the latest version [" + this.getDescription().getVersion() + "]!"); - } else if (versionResponse == UpdateManager.VersionResponse.UNAVAILABLE) { - Bukkit.getLogger().severe("[ThemeParkPlus] Unable to perform an update check."); - } - }).check(); - } + new License(this, "TP", license); if (Bukkit.getPluginManager().getPlugin("ThemePark") == null) { Bukkit.getLogger().severe("[ThemeParkPlus] Missing ThemePark! Please install it first."); @@ -176,6 +150,44 @@ public final class ThemeParkPlus extends JavaPlugin { Bukkit.getLogger().info("[ThemeParkPlus] Loading metrics (can be disabled in the config of bStats)..."); new Metrics(this, 5023); + if (getSConfig().getFile().getBoolean("UpdateChecker.Enabled")) { + UpdateManager updateManager = new UpdateManager(this, 2, license); + + updateManager.handleResponse((versionResponse, version) -> { + switch (versionResponse) { + case FOUND_NEW: + Bukkit.getLogger().warning("[ThemeParkPlus] There is a new version available! Current: " + this.getDescription().getVersion() + " New: " + version.get()); + if (getSConfig().getFile().getBoolean("UpdateChecker.DownloadOnUpdate")) { + Bukkit.getLogger().info("[ThemeParkPlus] Trying to download the update. This could take some time..."); + + updateManager.handleDownloadResponse((downloadResponse, fileName) -> { + switch (downloadResponse) { + case DONE: + Bukkit.getLogger().info("[ThemeParkPlus] Update downloaded! If you restart your server, it will be loaded. Filename: " + fileName); + break; + case ERROR: + Bukkit.getLogger().severe("[ThemeParkPlus] Something went wrong when trying downloading the latest version."); + break; + case UNAVAILABLE: + Bukkit.getLogger().warning("[ThemeParkPlus] Unable to download the latest version."); + break; + } + }).runUpdate(); + } + break; + case LATEST: + Bukkit.getLogger().info("[ThemeParkPlus] You are running the latest version [" + this.getDescription().getVersion() + "]!"); + break; + case THIS_NEWER: + Bukkit.getLogger().info("[ThemeParkPlus] You are running a newer version [" + this.getDescription().getVersion() + "]! This is probably fine."); + break; + case UNAVAILABLE: + Bukkit.getLogger().severe("[ThemeParkPlus] Unable to perform an update check."); + break; + } + }).check(); + } + Bukkit.getLogger().info("[ThemeParkPlus] Plugin enabled!"); Bukkit.getLogger().info("[ThemeParkPlus] -------------------------------"); } diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/sbutils/UpdateManager.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/sbutils/UpdateManager.java index 6882ca8..f421429 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/sbutils/UpdateManager.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/sbutils/UpdateManager.java @@ -3,7 +3,6 @@ package nl.sbdeveloper.themeparkplus.sbutils; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonParser; -import nl.sbdeveloper.themeparkplus.ThemeParkPlus; import org.bukkit.Bukkit; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; @@ -25,39 +24,54 @@ import java.util.function.BiConsumer; * Update class for SBDevelopment * @author Stijn [SBDeveloper] * @since 05-03-2020 - * @version 1.6 + * @version 2.0 [26-12-2020] - This class supports the v2 Update API * - * © Stijn Bannink - All rights reserved. + *

© Stijn Bannink [stijnbannink23@gmail.com] - All rights reserved.

*/ public class UpdateManager { private static final String SPIGOT_API = "https://api.spigotmc.org/legacy/update.php?resource=%d"; private static final String SPIGOT_DOWNLOAD = "http://api.spiget.org/v2/resources/%s/download"; - /* Port 4000 is now legacy, 4443 has a SSL cert */ - /* As of 24-05-2020, using the legacy port because of SSL errors */ - private static final String SBDPLUGINS_API = "http://updates.sbdplugins.nl:4000/api/resources/%d"; - private static final String SBDPLUGINS_DOWNLOAD = "http://updates.sbdplugins.nl:4000/api/download/%d"; + private static final String SBDPLUGINS_API = "https://updates.sbdplugins.nl/api/v2/plugins/%d"; + private static final String SBDPLUGINS_DOWNLOAD = "https://updates.sbdplugins.nl/api/v2/download/%d"; private final Plugin plugin; private final Version currentVersion; private final int resourceID; private final CheckType type; + private final String license; + private BiConsumer versionResponse; private BiConsumer downloadResponse; /** - * Construct a new UpdateManager + * Construct a new UpdateManager for Spigot * * @param plugin The javaplugin (Main class) * @param resourceID The resourceID on spigot/sbdplugins - * @param type The check type */ - public UpdateManager(@NotNull Plugin plugin, int resourceID, CheckType type) { + public UpdateManager(@NotNull Plugin plugin, int resourceID) { this.plugin = plugin; this.currentVersion = new Version(plugin.getDescription().getVersion()); this.resourceID = resourceID; - this.type = type; + this.type = CheckType.SPIGOT; + this.license = null; + } + + /** + * Construct a new UpdateManager for SBDPlugins + * + * @param plugin The javaplugin (Main class) + * @param resourceID The resourceID on spigot/sbdplugins + * @param license The license for the download + */ + public UpdateManager(@NotNull Plugin plugin, int resourceID, String license) { + this.plugin = plugin; + this.currentVersion = new Version(plugin.getDescription().getVersion()); + this.resourceID = resourceID; + this.type = CheckType.SBDPLUGINS; + this.license = license; } /** @@ -89,7 +103,7 @@ public class UpdateManager { in = new BufferedReader(new InputStreamReader(con.getInputStream())); } else if (type == CheckType.SBDPLUGINS) { - HttpURLConnection con = (HttpURLConnection) new URL(String.format(SBDPLUGINS_API, this.resourceID)).openConnection(); + HttpsURLConnection con = (HttpsURLConnection) new URL(String.format(SBDPLUGINS_API, this.resourceID)).openConnection(); con.setRequestMethod("GET"); con.setRequestProperty("User-Agent", "Mozilla/5.0"); @@ -98,7 +112,7 @@ public class UpdateManager { if (in == null) return; - String version = null; + String version; String inputLine; StringBuilder response = new StringBuilder(); @@ -113,10 +127,10 @@ public class UpdateManager { JsonArray array = parser.parse(response.toString()).getAsJsonArray(); version = array.get(0).getAsJsonObject().get("name").getAsString(); - } else if (type == CheckType.SBDPLUGINS) { + } else { JsonObject object = parser.parse(response.toString()).getAsJsonObject(); - version = object.get("data").getAsJsonObject().get("version").getAsString(); + version = object.get("version").getAsString(); } if (version == null) return; @@ -134,7 +148,7 @@ public class UpdateManager { } public void runUpdate() { - File pluginFile = getPluginFile();// /plugins/XXX.jar + File pluginFile = getPluginFile(); // /plugins/XXX.jar if (pluginFile == null) { this.downloadResponse.accept(DownloadResponse.ERROR, null); Bukkit.getLogger().info("Pluginfile is null"); @@ -154,11 +168,12 @@ public class UpdateManager { ReadableByteChannel channel; try { //https://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java - HttpURLConnection connection; + int response; + InputStream stream; if (type == CheckType.SBDPLUGINS) { - connection = (HttpURLConnection) new URL(String.format(SBDPLUGINS_DOWNLOAD, this.resourceID)).openConnection(); + HttpURLConnection connection = (HttpURLConnection) new URL(String.format(SBDPLUGINS_DOWNLOAD, this.resourceID)).openConnection(); - String urlParameters = "license=" + ThemeParkPlus.getSConfig().getFile().getString("License") + "&port=" + Bukkit.getPort(); + String urlParameters = "license=" + license + "&port=" + Bukkit.getPort(); byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8); int postDataLength = postData.length; @@ -172,27 +187,31 @@ public class UpdateManager { DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); wr.write(postData); wr.close(); + + response = connection.getResponseCode(); + stream = connection.getInputStream(); } else { - connection = (HttpURLConnection) new URL(String.format(SPIGOT_DOWNLOAD, this.resourceID)).openConnection(); + HttpsURLConnection connection = (HttpsURLConnection) new URL(String.format(SPIGOT_DOWNLOAD, this.resourceID)).openConnection(); connection.setRequestProperty("User-Agent", "Mozilla/5.0"); + + response = connection.getResponseCode(); + stream = connection.getInputStream(); } - if (connection.getResponseCode() != 200) { - BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); + if (response != 200) { + BufferedReader in = new BufferedReader(new InputStreamReader(stream)); String inputLine; - StringBuilder response = new StringBuilder(); + StringBuilder responsestr = new StringBuilder(); while ((inputLine = in.readLine()) != null) { - response.append(inputLine); + responsestr.append(inputLine); } in.close(); - Bukkit.getLogger().info(response.toString()); - - throw new RuntimeException("Download returned status #" + connection.getResponseCode()); + throw new RuntimeException("Download returned status #" + response, new Throwable(responsestr.toString())); } - channel = Channels.newChannel(connection.getInputStream()); + channel = Channels.newChannel(stream); } catch (IOException e) { Bukkit.getScheduler().runTask(this.plugin, () -> this.downloadResponse.accept(DownloadResponse.ERROR, null)); e.printStackTrace(); @@ -243,7 +262,7 @@ public class UpdateManager { } } - public enum CheckType { + private enum CheckType { SPIGOT, SBDPLUGINS } @@ -260,13 +279,13 @@ public class UpdateManager { public static class Version { - private String version; + private final String version; public final String get() { return this.version; } - public Version(String version) { + private Version(String version) { if(version == null) throw new IllegalArgumentException("Version can not be null"); if(!version.matches("[0-9]+(\\.[0-9]+)*")) @@ -274,7 +293,7 @@ public class UpdateManager { this.version = version; } - public VersionResponse check(@NotNull Version that) { + private VersionResponse check(@NotNull Version that) { String[] thisParts = this.get().split("\\."); String[] thatParts = that.get().split("\\."); diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/util/License.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/util/License.java index 601b554..e1b2305 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/util/License.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/util/License.java @@ -88,12 +88,12 @@ public class License implements Listener { try { response = sendGETRequestJSON(url); } catch (IOException e) { - disable("Couldn't send the request."); + disable("Something went wrong while sending the request."); return; } if (response == null) { - disable("Couldn't send the request."); + disable("Something went wrong while sending the request. Did you even fill out a license?"); return; } @@ -175,12 +175,12 @@ public class License implements Listener { try { response = sendGETRequestJSON(url); } catch (IOException e) { - disable("Couldn't send the activate request."); + disable("Something went wrong while sending the request."); return; } if (response == null) { - disable("Couldn't send the activate request."); + disable("Something went wrong while sending the request. Did you even fill out a license?"); return; } @@ -241,7 +241,6 @@ public class License implements Listener { con.setRequestProperty("Authorization", "Basic " + authStringEnc); int code = con.getResponseCode(); if (code == 404) { - disable("404_error"); return null; } From 75a5c96d396e5b4ed2aeb37dcf714588adccf5fc Mon Sep 17 00:00:00 2001 From: stijnb1234 Date: Mon, 22 Feb 2021 19:15:17 +0100 Subject: [PATCH 3/6] Updated NBTAPI, v2.4.1 ready for release. --- pom.xml | 2 +- .../nl/sbdeveloper/themeparkplus/ThemeParkPlus.java | 5 +++-- .../nl/sbdeveloper/themeparkplus/api/PlusAPI.java | 3 +-- .../nl/sbdeveloper/themeparkplus/commands/TPPCMD.java | 10 +++++----- .../themeparkplus/listeners/MalfunctionListener.java | 1 - .../themeparkplus/listeners/SignListeners.java | 1 + 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/pom.xml b/pom.xml index c63ff9b..699ff3b 100644 --- a/pom.xml +++ b/pom.xml @@ -147,7 +147,7 @@ de.tr7zw item-nbt-api - 2.6.0 + 2.7.1 com.github.MilkBowl diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java index 7239548..fcf16e6 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java @@ -115,14 +115,15 @@ public final class ThemeParkPlus extends JavaPlugin { Bukkit.getLogger().info("[ThemeParkPlus] Loading listeners..."); Bukkit.getPluginManager().registerEvents(new DirectionalGateListener(), this); - Bukkit.getPluginManager().registerEvents(new FastpassListeners(), this); + Bukkit.getPluginManager().registerEvents(new FastpassListeners(), this); //TODO Detect if ThemePark is found. Bukkit.getPluginManager().registerEvents(new SignListeners(), this); - Bukkit.getPluginManager().registerEvents(new MalfunctionListener(), this); + Bukkit.getPluginManager().registerEvents(new MalfunctionListener(), this); //TODO Detect if ThemePark is found. Bukkit.getPluginManager().registerEvents(new WaitingTimeListener(), this); if (getSConfig().getFile().getBoolean("AntiFreerun.Enabled")) { Bukkit.getPluginManager().registerEvents(new AntiFreerunListener(), this); } + //TODO Detect if ThemePark is found. if (getSConfig().getFile().getBoolean("DiscordWebhook.Enabled")) { String URL = getSConfig().getFile().getString("DiscordWebhook.WebhookURL"); if (URL != null) { diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/api/PlusAPI.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/api/PlusAPI.java index 8a9d029..f574d6b 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/api/PlusAPI.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/api/PlusAPI.java @@ -1,7 +1,6 @@ package nl.sbdeveloper.themeparkplus.api; import de.tr7zw.changeme.nbtapi.NBTItem; -import nl.iobyte.themepark.api.attraction.Attraction; import nl.sbdeveloper.themeparkplus.ThemeParkPlus; import nl.sbdeveloper.themeparkplus.api.objects.Gate; import nl.sbdeveloper.themeparkplus.api.objects.MalfunctionReport; @@ -170,7 +169,7 @@ public class PlusAPI { * @return The ticket as ItemStack */ @Nullable - public static ItemStack getFastpassTicket(Attraction att) { + public static ItemStack getFastpassTicket(nl.iobyte.themepark.api.attraction.Attraction att) { String ticketName = ConfigUtil.makecolored(ThemeParkPlus.getSConfig().getFile().getString("Fastpass.Item.DisplayName")); ItemStack ticket = XMaterial.PAPER.parseItem(); diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/commands/TPPCMD.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/commands/TPPCMD.java index 9a0e2b2..067c48e 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/commands/TPPCMD.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/commands/TPPCMD.java @@ -1,7 +1,5 @@ package nl.sbdeveloper.themeparkplus.commands; -import nl.iobyte.themepark.api.API; -import nl.iobyte.themepark.api.attraction.Attraction; import nl.sbdeveloper.themeparkplus.ThemeParkPlus; import nl.sbdeveloper.themeparkplus.api.PlusAPI; import nl.sbdeveloper.themeparkplus.api.enums.WalkingDirection; @@ -196,17 +194,19 @@ public class TPPCMD implements CommandExecutor { } private boolean giveFPTicketCommand(CommandSender sender, String[] args) { + //TODO Detect if ThemePark is found. + if (args.length == 2 && !(sender instanceof Player)) { sender.sendMessage(ConfigUtil.getMessage("General.NoPlayer")); return true; } - if (!API.isAttraction(args[1])) { + if (!nl.iobyte.themepark.api.API.isAttraction(args[1])) { sender.sendMessage(ConfigUtil.getMessage("Fastpass.UnknownRide", Collections.singletonMap("%ridename%", args[1]))); return true; } - Attraction att = API.getAttraction(args[1]); + nl.iobyte.themepark.api.attraction.Attraction att = nl.iobyte.themepark.api.API.getAttraction(args[1]); Player target; if (args.length == 3) { @@ -404,7 +404,7 @@ public class TPPCMD implements CommandExecutor { Bukkit.getScheduler().runTaskLaterAsynchronously(ThemeParkPlus.getInstance(), () -> cub.getBlocks().forEach(block -> { if (block.getType().name().contains("REDSTONE_LAMP")) Bukkit.getScheduler().runTask(ThemeParkPlus.getInstance(), () -> LGUtil.zetLampUit(block)); - }), secOn * 20); + }), secOn * 20L); if (ConfigUtil.sendConsole(sender)) sender.sendMessage(ConfigUtil.getMessage("Lamps.TurnedOnSec", Collections.singletonMap("%sec%", String.valueOf(secOn)))); } return true; diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/MalfunctionListener.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/MalfunctionListener.java index c09e865..c6db944 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/MalfunctionListener.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/MalfunctionListener.java @@ -5,7 +5,6 @@ import nl.iobyte.themepark.api.attraction.Attraction; import nl.sbdeveloper.themeparkplus.ThemeParkPlus; import nl.sbdeveloper.themeparkplus.gui.MalfunctionGUI; import nl.sbdeveloper.themeparkplus.util.ConfigUtil; -import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.block.Sign; import org.bukkit.event.EventHandler; diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/SignListeners.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/SignListeners.java index 41ea2a1..ba125e0 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/SignListeners.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/SignListeners.java @@ -41,6 +41,7 @@ public class SignListeners implements Listener { return; } + //TODO Detect if ThemePark is found. if (!API.isAttraction(lines[2])) { p.sendMessage(ConfigUtil.getMessage("Fastpass.UnknownRide", Collections.singletonMap("ridename", e.getLine(2)))); return; From 84ab695ab8a34b09b4a42a52d66053b911d72a39 Mon Sep 17 00:00:00 2001 From: Stijn Bannink Date: Tue, 23 Feb 2021 10:54:27 +0100 Subject: [PATCH 4/6] Rename messages.yml to messages_en.yml --- src/main/resources/{messages.yml => messages_en.yml} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/main/resources/{messages.yml => messages_en.yml} (97%) diff --git a/src/main/resources/messages.yml b/src/main/resources/messages_en.yml similarity index 97% rename from src/main/resources/messages.yml rename to src/main/resources/messages_en.yml index e346250..aa27344 100644 --- a/src/main/resources/messages.yml +++ b/src/main/resources/messages_en.yml @@ -40,4 +40,4 @@ WaitingRow: RedstoneTimer: Started: '&aThe timer successfully started. It will go off in &f%sec1% &asecond(s), and will turn off in &f%sec2% &asecond(s) after that.' Malfunction: - NoCommand: '&cYou can''t report or fix a ride using the command. Please use this Malfunction Sign at the ride.' \ No newline at end of file + NoCommand: '&cYou can''t report or fix a ride using the command. Please use this Malfunction Sign at the ride.' From 54437672d0caf358195f4965e3796cb05fba386a Mon Sep 17 00:00:00 2001 From: stijnb1234 Date: Sun, 28 Feb 2021 12:25:45 +0100 Subject: [PATCH 5/6] Release v2.1.4: Fixed updatechecker (changed ID), and added saving for Malfunction GUI --- .../themeparkplus/ThemeParkPlus.java | 5 ++- .../themeparkplus/api/PlusAPI.java | 4 ++ .../themeparkplus/gui/MalfunctionGUI.java | 12 +++--- .../themeparkplus/managers/DBManager.java | 43 ++++++++++++++++++- src/main/resources/config.yml | 2 +- src/main/resources/messages.yml | 7 ++- 6 files changed, 62 insertions(+), 11 deletions(-) diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java index fcf16e6..bdc08c9 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java @@ -152,7 +152,7 @@ public final class ThemeParkPlus extends JavaPlugin { new Metrics(this, 5023); if (getSConfig().getFile().getBoolean("UpdateChecker.Enabled")) { - UpdateManager updateManager = new UpdateManager(this, 2, license); + UpdateManager updateManager = new UpdateManager(this, 7, license); updateManager.handleResponse((versionResponse, version) -> { switch (versionResponse) { @@ -195,8 +195,9 @@ public final class ThemeParkPlus extends JavaPlugin { @Override public void onDisable() { - Bukkit.getLogger().info("[ThemeParkPlus] Saving data to data file..."); + Bukkit.getLogger().info("[ThemeParkPlus] Saving data..."); data.save(); + data.closeConnection(); Bukkit.getLogger().info("[ThemeParkPlus] Plugin disabled!"); instance = null; diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/api/PlusAPI.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/api/PlusAPI.java index f574d6b..514b198 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/api/PlusAPI.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/api/PlusAPI.java @@ -38,6 +38,10 @@ public class PlusAPI { return reports.containsKey(rideID); } + public static HashMap getReports() { + return reports; + } + /** * Add a gate * diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/gui/MalfunctionGUI.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/gui/MalfunctionGUI.java index 2c3e7c9..23fd1f1 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/gui/MalfunctionGUI.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/gui/MalfunctionGUI.java @@ -18,6 +18,7 @@ import org.bukkit.inventory.ItemStack; import java.time.format.DateTimeFormatter; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Optional; @@ -31,9 +32,9 @@ public class MalfunctionGUI { PlusAPI.addReport(malfunctionReport); att.setStatus(Status.MALFUNCTION, null); //Player must be null, otherwise we will get a loop in the listener - //TODO Send message + p.sendMessage(ConfigUtil.getMessage("Malfunction.Reported", Collections.singletonMap("%ride%", att.getName()))); } else { - //TODO Already reported + p.sendMessage(ConfigUtil.getMessage("Malfunction.AlreadyReported", Collections.singletonMap("%ride%", att.getName()))); } p.closeInventory(); }); @@ -45,9 +46,9 @@ public class MalfunctionGUI { PlusAPI.removeReport(att.getId()); att.setStatus(Status.CLOSED, null); //Player must be null, otherwise we will get a loop in the listener - //TODO Send message + p.sendMessage(ConfigUtil.getMessage("Malfunction.Fixed", Collections.singletonMap("%ride%", att.getName()))); } else { - //TODO No malfunction + p.sendMessage(ConfigUtil.getMessage("Malfunction.AlreadyFixed", Collections.singletonMap("%ride%", att.getName()))); } p.closeInventory(); }); @@ -76,7 +77,7 @@ public class MalfunctionGUI { lores.forEach(lore -> loresFormatted.add(lore.replace("%ridename%", (String) data[0]))); break; case "Fix": - String ridename = "this ride"; + String ridename = ConfigUtil.getMessage("Malfunction.ThisRide"); String reporter = "-"; String date = "-"; String reason = "-"; @@ -96,7 +97,6 @@ public class MalfunctionGUI { } Optional stack = XMaterial.matchXMaterial(material); - return stack.map(xMaterial -> new ItemBuilder(xMaterial.parseItem()).name(name).lore(loresFormatted).build()).orElse(null); } } diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/managers/DBManager.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/managers/DBManager.java index b14a69f..7adb6d9 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/managers/DBManager.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/managers/DBManager.java @@ -4,6 +4,7 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import nl.sbdeveloper.themeparkplus.api.PlusAPI; import nl.sbdeveloper.themeparkplus.api.objects.Gate; +import nl.sbdeveloper.themeparkplus.api.objects.MalfunctionReport; import nl.sbdeveloper.themeparkplus.api.objects.WaitingRow; import nl.sbdeveloper.themeparkplus.sbutils.LocationSerializer; import nl.sbdeveloper.themeparkplus.sbutils.SQLiteDB; @@ -36,6 +37,10 @@ public class DBManager { query = "CREATE TABLE IF NOT EXISTS rows (rideID varchar(255) NOT NULL, rowData blob NOT NULL, UNIQUE (rideID))"; statement = con.prepareStatement(query); statement.execute(); + + query = "CREATE TABLE IF NOT EXISTS malfunction_reports (rideID varchar(255) NOT NULL, reportData blob NOT NULL, UNIQUE (rideID))"; + statement = con.prepareStatement(query); + statement.execute(); } catch (SQLException e) { e.printStackTrace(); } @@ -74,6 +79,20 @@ public class DBManager { WaitingRow row = gson.fromJson(json, WaitingRow.class); PlusAPI.addRow(row); } + + /* Load reports */ + query = "SELECT * FROM malfunction_reports"; + statement = con.prepareStatement(query); + ResultSet reportSet = statement.executeQuery(); + while (reportSet.next()) { + //Loading a gates... + byte[] blob = reportSet.getBytes("reportData"); + String json = new String(blob); + + Gson gson = getGson(); + MalfunctionReport report = gson.fromJson(json, MalfunctionReport.class); + PlusAPI.addReport(report); + } } public void save() { @@ -103,7 +122,6 @@ public class DBManager { } for (Map.Entry entry : PlusAPI.getRows().entrySet()) { - Gson gson = getGson(); byte[] blob = gson.toJson(entry.getValue()).getBytes(); @@ -125,6 +143,29 @@ public class DBManager { e.printStackTrace(); } } + + for (Map.Entry entry : PlusAPI.getReports().entrySet()) { + Gson gson = getGson(); + byte[] blob = gson.toJson(entry.getValue()).getBytes(); + + try { + String query = "INSERT INTO malfunction_reports (rideID, reportData) VALUES (?, ?)"; + PreparedStatement statement = con.prepareStatement(query); + statement.setString(1, entry.getKey()); + statement.setBytes(2, blob); + statement.executeUpdate(); + } catch (SQLException ignored) {} + + try { + String query2 = "UPDATE malfunction_reports SET reportData = ? WHERE rideID = ?"; + PreparedStatement statement2 = con.prepareStatement(query2); + statement2.setBytes(1, blob); + statement2.setString(2, entry.getKey()); + statement2.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + } } public void closeConnection() { diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index a87d1e1..ec7f48f 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,4 +1,4 @@ -License: 'TPABCD-1234-ABCD-1234SBD' +License: '' AntiFreerun: Enabled: false UpdateChecker: diff --git a/src/main/resources/messages.yml b/src/main/resources/messages.yml index e346250..aa4f560 100644 --- a/src/main/resources/messages.yml +++ b/src/main/resources/messages.yml @@ -40,4 +40,9 @@ WaitingRow: RedstoneTimer: Started: '&aThe timer successfully started. It will go off in &f%sec1% &asecond(s), and will turn off in &f%sec2% &asecond(s) after that.' Malfunction: - NoCommand: '&cYou can''t report or fix a ride using the command. Please use this Malfunction Sign at the ride.' \ No newline at end of file + NoCommand: '&cYou can''t report or fix a ride using the command. Please use this Malfunction Sign at the ride.' + Reported: '&aSuccessfully reported a malfunction for &f%ride%&a.' + AlreadyReported: '&cThere is already a malfunction reported for &f%ride%&c.' + Fixed: '&aThe ride &f%ride%&a is now successfully fixed.' + AlreadyFixed: '&cThe ride &f%ride%&a &chas no malfunction report, and therefore cannot be fixed.' + ThisRide: 'this ride' \ No newline at end of file From d0f3045c3497fa42b9977ad26f56f3d586a7e04c Mon Sep 17 00:00:00 2001 From: stijnb1234 Date: Sun, 28 Feb 2021 12:31:51 +0100 Subject: [PATCH 6/6] Removed ThemePark dependency --- .../themeparkplus/ThemeParkPlus.java | 44 ++++++++++++------- .../themeparkplus/commands/TPPCMD.java | 5 ++- .../listeners/SignListeners.java | 11 +++-- src/main/resources/messages_en.yml | 1 + 4 files changed, 39 insertions(+), 22 deletions(-) diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java index bdc08c9..ab56683 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java @@ -8,6 +8,7 @@ import nl.sbdeveloper.themeparkplus.listeners.*; import nl.sbdeveloper.themeparkplus.managers.DBManager; import nl.sbdeveloper.themeparkplus.sbutils.UpdateManager; import nl.sbdeveloper.themeparkplus.sbutils.YamlFile; +import nl.sbdeveloper.themeparkplus.util.ConfigUtil; import nl.sbdeveloper.themeparkplus.util.License; import org.bstats.bukkit.Metrics; import org.bukkit.Bukkit; @@ -37,6 +38,8 @@ public final class ThemeParkPlus extends JavaPlugin { private final int configVersion = 3; + private static boolean hasTPHook = false; + @Override public void onEnable() { instance = this; @@ -85,12 +88,6 @@ public final class ThemeParkPlus extends JavaPlugin { new License(this, "TP", license); - if (Bukkit.getPluginManager().getPlugin("ThemePark") == null) { - Bukkit.getLogger().severe("[ThemeParkPlus] Missing ThemePark! Please install it first."); - getServer().getPluginManager().disablePlugin(this); - return; - } - if (Bukkit.getPluginManager().getPlugin("WorldEdit") == null) { Bukkit.getLogger().severe("[ThemeParkPlus] Missing WorldEdit! Please install it first."); getServer().getPluginManager().disablePlugin(this); @@ -115,24 +112,33 @@ public final class ThemeParkPlus extends JavaPlugin { Bukkit.getLogger().info("[ThemeParkPlus] Loading listeners..."); Bukkit.getPluginManager().registerEvents(new DirectionalGateListener(), this); - Bukkit.getPluginManager().registerEvents(new FastpassListeners(), this); //TODO Detect if ThemePark is found. Bukkit.getPluginManager().registerEvents(new SignListeners(), this); - Bukkit.getPluginManager().registerEvents(new MalfunctionListener(), this); //TODO Detect if ThemePark is found. Bukkit.getPluginManager().registerEvents(new WaitingTimeListener(), this); if (getSConfig().getFile().getBoolean("AntiFreerun.Enabled")) { Bukkit.getPluginManager().registerEvents(new AntiFreerunListener(), this); } - //TODO Detect if ThemePark is found. - if (getSConfig().getFile().getBoolean("DiscordWebhook.Enabled")) { - String URL = getSConfig().getFile().getString("DiscordWebhook.WebhookURL"); - if (URL != null) { - Bukkit.getPluginManager().registerEvents(new StatusChangeListener(), this); + if (Bukkit.getPluginManager().getPlugin("ThemePark") == null) { + Bukkit.getLogger().severe("[ThemeParkPlus] Missing ThemePark! Some functions will not work without it!"); + } else { + Bukkit.getLogger().severe("[ThemeParkPlus] Enabling ThemePark hook..."); + hasTPHook = true; + } - Bukkit.getLogger().info("[ThemeParkPlus] Loading Discord webhook..."); - webhookURL = URL; - } else { - Bukkit.getLogger().severe("[ThemeParkPlus] Couldn't load the webhook builder! The URL is null."); + if (hasTPHook) { + Bukkit.getPluginManager().registerEvents(new FastpassListeners(), this); + Bukkit.getPluginManager().registerEvents(new MalfunctionListener(), this); + + if (getSConfig().getFile().getBoolean("DiscordWebhook.Enabled")) { + String URL = getSConfig().getFile().getString("DiscordWebhook.WebhookURL"); + if (URL != null) { + Bukkit.getPluginManager().registerEvents(new StatusChangeListener(), this); + + Bukkit.getLogger().info("[ThemeParkPlus] Loading Discord webhook..."); + webhookURL = URL; + } else { + Bukkit.getLogger().severe("[ThemeParkPlus] Couldn't load the webhook builder! The URL is null."); + } } } @@ -231,6 +237,10 @@ public final class ThemeParkPlus extends JavaPlugin { return spiGUI; } + public static boolean hasTPHook() { + return hasTPHook; + } + private boolean setupEconomy() { if (getServer().getPluginManager().getPlugin("Vault") == null) { return false; diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/commands/TPPCMD.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/commands/TPPCMD.java index 067c48e..5f831fb 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/commands/TPPCMD.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/commands/TPPCMD.java @@ -194,7 +194,10 @@ public class TPPCMD implements CommandExecutor { } private boolean giveFPTicketCommand(CommandSender sender, String[] args) { - //TODO Detect if ThemePark is found. + if (!ThemeParkPlus.hasTPHook()) { + sender.sendMessage(ConfigUtil.getMessage("General.RequiresThemePark")); + return true; + } if (args.length == 2 && !(sender instanceof Player)) { sender.sendMessage(ConfigUtil.getMessage("General.NoPlayer")); diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/SignListeners.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/SignListeners.java index ba125e0..fec1219 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/SignListeners.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/SignListeners.java @@ -1,6 +1,5 @@ package nl.sbdeveloper.themeparkplus.listeners; -import nl.iobyte.themepark.api.API; import nl.sbdeveloper.themeparkplus.ThemeParkPlus; import nl.sbdeveloper.themeparkplus.api.PlusAPI; import nl.sbdeveloper.themeparkplus.api.objects.WaitingRow; @@ -36,13 +35,17 @@ public class SignListeners implements Listener { //Only check themeparkplus signs! if (!lines[0].equalsIgnoreCase("[ThemeParkPlus]")) return; + if (!ThemeParkPlus.hasTPHook()) { + p.sendMessage(ConfigUtil.getMessage("General.RequiresThemePark")); + return; + } + if (!p.hasPermission("tpp.fastpass.create")) { p.sendMessage(ConfigUtil.getMessage("General.NoPermission")); return; } - //TODO Detect if ThemePark is found. - if (!API.isAttraction(lines[2])) { + if (!nl.iobyte.themepark.api.API.isAttraction(lines[2])) { p.sendMessage(ConfigUtil.getMessage("Fastpass.UnknownRide", Collections.singletonMap("ridename", e.getLine(2)))); return; } @@ -77,7 +80,7 @@ public class SignListeners implements Listener { e.setLine(0, wrLineOne); e.setLine(1, wrLineTwo); - e.setLine(2, ConfigUtil.makecolored(API.getAttraction(lines[2]).getName())); + e.setLine(2, ConfigUtil.makecolored(nl.iobyte.themepark.api.API.getAttraction(lines[2]).getName())); e.setLine(3, "0 min."); p.sendMessage(ConfigUtil.getMessage("WaitingRow.SignCreated")); diff --git a/src/main/resources/messages_en.yml b/src/main/resources/messages_en.yml index aa4f560..76e6c6a 100644 --- a/src/main/resources/messages_en.yml +++ b/src/main/resources/messages_en.yml @@ -3,6 +3,7 @@ General: NoPlayer: '&cYou have to be a player to do this.' IncorrectAmount: '&cThis amount is incorrect.' IncorrectSign: '&cThis sign is incorrect. Please read the wiki for more information.' + RequiresThemePark: '&cThis function requires ThemePark to work. Install it to use this function.' Gates: WrongDir: '&cYou can''t walk through this gate in that direction.' UnknownDir: '&cThis direction is unknown. Choose between: NORTH, SOUTH, EAST, WEST'