From a4b0b8b6d74db832693a95313e78c1795df037a0 Mon Sep 17 00:00:00 2001 From: Emre Date: Mon, 6 Nov 2023 19:32:42 +0300 Subject: [PATCH] Added update checker added update checker fixed custom placeholder not working in gui's --- .../main/java/net/leaderos/plugin/Bukkit.java | 2 + .../net/leaderos/plugin/helpers/ChatUtil.java | 13 +++++ .../modules/bazaar/gui/BazaarAddItemGui.java | 2 +- .../plugin/modules/bazaar/gui/BazaarGui.java | 2 +- .../modules/donations/gui/DonationGui.java | 2 +- .../modules/webstore/gui/MainWebStoreGui.java | 2 +- .../modules/webstore/gui/WebStoreGui.java | 2 +- .../main/java/net/leaderos/bungee/Bungee.java | 2 + shared/pom.xml | 6 ++ .../leaderos/shared/helpers/UpdateUtil.java | 58 +++++++++++++++++++ .../java/net/leaderos/velocity/Velocity.java | 4 +- 11 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 shared/src/main/java/net/leaderos/shared/helpers/UpdateUtil.java diff --git a/bukkit/src/main/java/net/leaderos/plugin/Bukkit.java b/bukkit/src/main/java/net/leaderos/plugin/Bukkit.java index f609f4490..db5177725 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/Bukkit.java +++ b/bukkit/src/main/java/net/leaderos/plugin/Bukkit.java @@ -22,6 +22,7 @@ import net.leaderos.plugin.modules.bazaar.BazaarModule; import net.leaderos.plugin.modules.webstore.WebStoreModule; import net.leaderos.plugin.modules.auth.AuthModule; +import net.leaderos.shared.helpers.UpdateUtil; import org.bukkit.command.CommandSender; import org.bukkit.plugin.java.JavaPlugin; @@ -88,6 +89,7 @@ public void onLoad() { public void onEnable() { commandManager = BukkitCommandManager.create(this); setupCommands(); + new UpdateUtil(getDescription().getVersion()); // Loads modules LeaderOSAPI.getModuleManager().registerModule(new AuthModule()); LeaderOSAPI.getModuleManager().registerModule(new DiscordModule()); diff --git a/bukkit/src/main/java/net/leaderos/plugin/helpers/ChatUtil.java b/bukkit/src/main/java/net/leaderos/plugin/helpers/ChatUtil.java index 210913dc8..a314d59d6 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/helpers/ChatUtil.java +++ b/bukkit/src/main/java/net/leaderos/plugin/helpers/ChatUtil.java @@ -1,5 +1,6 @@ package net.leaderos.plugin.helpers; +import me.clip.placeholderapi.PlaceholderAPI; import net.leaderos.plugin.Bukkit; import net.leaderos.shared.helpers.Placeholder; import net.md_5.bungee.api.ChatColor; @@ -88,6 +89,18 @@ public static String replacePlaceholders(String string, Placeholder... placehold return color(string); } + /** + * Replaces placeholders on string + *

also format chat messages too @see ChatUtil#color(String)

+ * + * @param string to be converted + * @return converted string value + */ + public static String replacePlaceholders(String string) { + string = PlaceholderAPI.setPlaceholders(null, string); + return color(string); + } + /** * Replaces placeholder data on list * diff --git a/bukkit/src/main/java/net/leaderos/plugin/modules/bazaar/gui/BazaarAddItemGui.java b/bukkit/src/main/java/net/leaderos/plugin/modules/bazaar/gui/BazaarAddItemGui.java index 95b6b7416..8daa1991c 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/modules/bazaar/gui/BazaarAddItemGui.java +++ b/bukkit/src/main/java/net/leaderos/plugin/modules/bazaar/gui/BazaarAddItemGui.java @@ -47,7 +47,7 @@ public static void showGui(Player player, int itemAmount) { // Gui template as array String[] layout = Bukkit.getInstance().getModulesFile().getBazaar().getGui().getAddItemLayout().toArray(new String[0]); // Inventory object - String guiName = ChatUtil.color(Bukkit.getInstance().getLangFile().getGui().getBazaarGui().getAddItemGuiName()); + String guiName = ChatUtil.replacePlaceholders(Bukkit.getInstance().getLangFile().getGui().getBazaarGui().getAddItemGuiName()); InventoryGui gui = new InventoryGui(Bukkit.getInstance(), null, guiName, layout); // Filler item for empty slots ItemStack fillerItem = GuiHelper.getFiller(Bukkit.getInstance().getModulesFile().getBazaar().getGui().getFillerItem().isUseFiller(), Bukkit.getInstance().getModulesFile().getBazaar().getGui().getFillerItem().getMaterial()); diff --git a/bukkit/src/main/java/net/leaderos/plugin/modules/bazaar/gui/BazaarGui.java b/bukkit/src/main/java/net/leaderos/plugin/modules/bazaar/gui/BazaarGui.java index 9024f7408..4c4321b7c 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/modules/bazaar/gui/BazaarGui.java +++ b/bukkit/src/main/java/net/leaderos/plugin/modules/bazaar/gui/BazaarGui.java @@ -37,7 +37,7 @@ public static void showGui(Player player) { // Gui template as array String[] layout = Bukkit.getInstance().getModulesFile().getBazaar().getGui().getLayout().toArray(new String[0]); // Inventory object - String guiName = ChatUtil.color(Bukkit.getInstance().getLangFile().getGui().getBazaarGui().getGuiName()); + String guiName = ChatUtil.replacePlaceholders(Bukkit.getInstance().getLangFile().getGui().getBazaarGui().getGuiName()); InventoryGui gui = new InventoryGui(Bukkit.getInstance(), null, guiName, layout); // Filler item for empty slots gui.setFiller(GuiHelper.getFiller(Bukkit.getInstance().getModulesFile().getBazaar().getGui().getFillerItem().isUseFiller(), Bukkit.getInstance().getModulesFile().getBazaar().getGui().getFillerItem().getMaterial())); diff --git a/bukkit/src/main/java/net/leaderos/plugin/modules/donations/gui/DonationGui.java b/bukkit/src/main/java/net/leaderos/plugin/modules/donations/gui/DonationGui.java index 4a568ce54..cce5e5830 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/modules/donations/gui/DonationGui.java +++ b/bukkit/src/main/java/net/leaderos/plugin/modules/donations/gui/DonationGui.java @@ -39,7 +39,7 @@ public static void showGui(Player player) { // Gui template as array String[] layout = Bukkit.getInstance().getModulesFile().getDonations().getGui().getLayout().toArray(new String[0]); // Inventory object - String guiName = ChatUtil.color(Bukkit.getInstance().getLangFile().getGui().getDonationsGui().getGuiName()); + String guiName = ChatUtil.replacePlaceholders(Bukkit.getInstance().getLangFile().getGui().getDonationsGui().getGuiName()); InventoryGui gui = new InventoryGui(Bukkit.getInstance(), null, guiName, layout); // Filler item for empty slots gui.setFiller(GuiHelper.getFiller(Bukkit.getInstance().getModulesFile().getDonations().getGui().getFillerItem().isUseFiller(), Bukkit.getInstance().getModulesFile().getDonations().getGui().getFillerItem().getMaterial())); diff --git a/bukkit/src/main/java/net/leaderos/plugin/modules/webstore/gui/MainWebStoreGui.java b/bukkit/src/main/java/net/leaderos/plugin/modules/webstore/gui/MainWebStoreGui.java index 0d9491eee..9bce0f25c 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/modules/webstore/gui/MainWebStoreGui.java +++ b/bukkit/src/main/java/net/leaderos/plugin/modules/webstore/gui/MainWebStoreGui.java @@ -34,7 +34,7 @@ public static void showGui(Player player) { // Gui template as array String[] layout = Bukkit.getInstance().getModulesFile().getWebStore().getGui().getLandingGuiLayout().toArray(new String[0]); // Inventory object - String guiName = ChatUtil.color(Bukkit.getInstance().getLangFile().getGui().getWebStoreGui().getGuiName()); + String guiName = ChatUtil.replacePlaceholders(Bukkit.getInstance().getLangFile().getGui().getWebStoreGui().getGuiName()); InventoryGui gui = new InventoryGui(Bukkit.getInstance(), null, guiName, layout); // Filler item for empty slots gui.setFiller(GuiHelper.getFiller(Bukkit.getInstance().getModulesFile().getWebStore().getGui().getFillerItem().isUseFiller(), Bukkit.getInstance().getModulesFile().getWebStore().getGui().getFillerItem().getMaterial())); diff --git a/bukkit/src/main/java/net/leaderos/plugin/modules/webstore/gui/WebStoreGui.java b/bukkit/src/main/java/net/leaderos/plugin/modules/webstore/gui/WebStoreGui.java index 620f53f93..80b422b70 100644 --- a/bukkit/src/main/java/net/leaderos/plugin/modules/webstore/gui/WebStoreGui.java +++ b/bukkit/src/main/java/net/leaderos/plugin/modules/webstore/gui/WebStoreGui.java @@ -44,7 +44,7 @@ public static void showGui(Player player, Category categoryObj) { // Gui template as array String[] layout = Bukkit.getInstance().getModulesFile().getWebStore().getGui().getLayout().toArray(new String[0]); // Inventory object - String guiName = ChatUtil.color(Bukkit.getInstance().getLangFile().getGui().getWebStoreGui().getGuiName()); + String guiName = ChatUtil.replacePlaceholders(Bukkit.getInstance().getLangFile().getGui().getWebStoreGui().getGuiName()); InventoryGui gui = new InventoryGui(Bukkit.getInstance(), null, guiName, layout); // Filler item for empty slots gui.setFiller(GuiHelper.getFiller(Bukkit.getInstance().getModulesFile().getWebStore().getGui().getFillerItem().isUseFiller(), Bukkit.getInstance().getModulesFile().getWebStore().getGui().getFillerItem().getMaterial())); diff --git a/bungee/src/main/java/net/leaderos/bungee/Bungee.java b/bungee/src/main/java/net/leaderos/bungee/Bungee.java index cea2e8574..02fb7cc9d 100644 --- a/bungee/src/main/java/net/leaderos/bungee/Bungee.java +++ b/bungee/src/main/java/net/leaderos/bungee/Bungee.java @@ -13,6 +13,7 @@ import net.leaderos.bungee.modules.credit.CreditModule; import net.leaderos.bungee.modules.discord.DiscordModule; import net.leaderos.shared.Shared; +import net.leaderos.shared.helpers.UpdateUtil; import net.md_5.bungee.api.plugin.Plugin; import java.io.File; @@ -67,6 +68,7 @@ public void onEnable() { instance = this; Bungee.moduleManager = new ModuleManager(); setupFiles(); + new UpdateUtil(getDescription().getVersion()); shared = new Shared(getConfigFile().getSettings().getUrl(), getConfigFile().getSettings().getApiKey()); Bungee.getInstance().getProxy().getPluginManager().registerCommand(Bungee.getInstance(), new LeaderOSCommand("leaderos")); diff --git a/shared/pom.xml b/shared/pom.xml index 3023388b3..1d507c543 100644 --- a/shared/pom.xml +++ b/shared/pom.xml @@ -29,6 +29,12 @@ socket.io-client 2.1.0 + + + com.google.code.gson + gson + 2.10.1 + \ No newline at end of file diff --git a/shared/src/main/java/net/leaderos/shared/helpers/UpdateUtil.java b/shared/src/main/java/net/leaderos/shared/helpers/UpdateUtil.java new file mode 100644 index 000000000..fcc3d45cd --- /dev/null +++ b/shared/src/main/java/net/leaderos/shared/helpers/UpdateUtil.java @@ -0,0 +1,58 @@ +package net.leaderos.shared.helpers; + +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + +/** + * @author amownyy + * @since 1.0 + */ +public class UpdateUtil { + + /** + * Constructor of UpdateUtil + */ + public UpdateUtil(String version) { + checkForUpdates(version); + } + + /** + * Checks for updates + */ + public void checkForUpdates(String version) { + try { + URL url = new URL("https://api2.leaderos.net/plugin-version-check.php?version=" + version); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + + int responseCode = connection.getResponseCode(); + if (responseCode == HttpURLConnection.HTTP_OK) { + BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String inputLine; + StringBuffer response = new StringBuffer(); + + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + in.close(); + + JsonParser parser = new JsonParser(); + JsonObject jsonObject = parser.parse(response.toString()).getAsJsonObject(); + + if (jsonObject.get("update").getAsBoolean()) { + System.out.println("There is a new update available for LeaderOS Plugin! Please update to " + jsonObject.get("version").getAsString()); + } + } else { + throw new RuntimeException("Failed to check for updates! Response code: " + responseCode); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/velocity/src/main/java/net/leaderos/velocity/Velocity.java b/velocity/src/main/java/net/leaderos/velocity/Velocity.java index 4e714f42e..08a8948e7 100644 --- a/velocity/src/main/java/net/leaderos/velocity/Velocity.java +++ b/velocity/src/main/java/net/leaderos/velocity/Velocity.java @@ -14,6 +14,7 @@ import lombok.Getter; import lombok.Setter; import net.leaderos.shared.Shared; +import net.leaderos.shared.helpers.UpdateUtil; import net.leaderos.velocity.api.ModuleManager; import net.leaderos.velocity.commands.LeaderOSCommand; import net.leaderos.velocity.configuration.Config; @@ -35,7 +36,7 @@ */ @Getter @Setter -@Plugin(id = "leaderos", name = "LeaderOS", version = "1.0.0-SNAPSHOT", +@Plugin(id = "leaderos", name = "LeaderOS", version = "1.0", url = "https://leaderos.net", description = "LeaderOS Plugin for Velocity", authors = {"leaderos"}) public class Velocity { @@ -111,6 +112,7 @@ public Velocity(ProxyServer server, Logger logger, @DataDirectory Path dataDirec public void onProxyInitialize(ProxyInitializeEvent event) { commandManager = getServer().getCommandManager(); setupFiles(); + new UpdateUtil("1.0"); this.shared = new Shared(getConfigFile().getSettings().getUrl(), getConfigFile().getSettings().getApiKey()); this.moduleManager = new ModuleManager();