From f70767b361fa2c34265f0a44bac57c0ab2573a8a Mon Sep 17 00:00:00 2001 From: Jkobs <66284145+Minzilein2003@users.noreply.github.com> Date: Mon, 14 Nov 2022 14:41:45 +0100 Subject: [PATCH] New Bungee Updater --- .../BUNGEE/api/messages/T2CBtemplate.java | 2 +- .../BUNGEE/api/update/T2CBupdateAPI.java | 133 +++++++++++------- .../BUNGEE/api/update/T2CBupdateObject.java | 12 -- .../t2codelib/BUNGEE/system/T2CBload.java | 4 +- .../t2codelib/BUNGEE/system/T2CodeBMain.java | 2 +- .../SPIGOT/api/update/T2CupdateAPI.java | 2 + .../SPIGOT/api/update/T2CupdateChecker.java | 1 + .../api/update/T2CupdateCheckerGit.java | 2 + .../SPIGOT/system/cmd/CmdExecuter.java | 2 +- .../t2codelib/SPIGOT/system/cmd/Commands.java | 3 - .../api/update => }/T2CupdateObject.java | 2 +- .../api/update => }/T2CupdateWebData.java | 2 +- 12 files changed, 94 insertions(+), 73 deletions(-) delete mode 100644 src/main/java/net/t2code/t2codelib/BUNGEE/api/update/T2CBupdateObject.java rename src/main/java/net/t2code/t2codelib/{SPIGOT/api/update => }/T2CupdateObject.java (92%) rename src/main/java/net/t2code/t2codelib/{SPIGOT/api/update => }/T2CupdateWebData.java (94%) diff --git a/src/main/java/net/t2code/t2codelib/BUNGEE/api/messages/T2CBtemplate.java b/src/main/java/net/t2code/t2codelib/BUNGEE/api/messages/T2CBtemplate.java index a599df1..cc47a10 100644 --- a/src/main/java/net/t2code/t2codelib/BUNGEE/api/messages/T2CBtemplate.java +++ b/src/main/java/net/t2code/t2codelib/BUNGEE/api/messages/T2CBtemplate.java @@ -39,7 +39,7 @@ public class T2CBtemplate { } public static void sendInfo(CommandSender sender, Plugin plugin, String prefix, String spigot, String discord, String autor) { - String publicVersion = T2CBupdateAPI.bungeePluginVersins.get(plugin.getDescription().getName()).publicVersion; + String publicVersion = T2CBupdateAPI.bungeePluginVersins.get(plugin.getDescription().getName()).webData.getVersion(); String pluginVersion = plugin.getDescription().getVersion(); T2CBsend.sender(sender, prefix + "§4======= " + prefix + " §4======="); T2CBsend.sender(sender, prefix + " §2Autor: §6" + autor); diff --git a/src/main/java/net/t2code/t2codelib/BUNGEE/api/update/T2CBupdateAPI.java b/src/main/java/net/t2code/t2codelib/BUNGEE/api/update/T2CBupdateAPI.java index fccb1d8..9088a95 100644 --- a/src/main/java/net/t2code/t2codelib/BUNGEE/api/update/T2CBupdateAPI.java +++ b/src/main/java/net/t2code/t2codelib/BUNGEE/api/update/T2CBupdateAPI.java @@ -1,20 +1,26 @@ package net.t2code.t2codelib.BUNGEE.api.update; +import net.md_5.bungee.BungeeCord; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.plugin.Plugin; import net.t2code.t2codelib.BUNGEE.api.messages.T2CBsend; - -import java.io.IOException; -import java.io.InputStream; +import net.t2code.t2codelib.T2CupdateObject; +import net.t2code.t2codelib.T2CupdateWebData; +import org.json.JSONArray; +import org.json.JSONObject; +import java.io.BufferedReader; +import java.io.InputStreamReader; import java.net.URL; +import java.net.URLConnection; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.HashMap; -import java.util.Scanner; import java.util.concurrent.TimeUnit; import java.util.function.Consumer; public class T2CBupdateAPI { - public static HashMap bungeePluginVersins = new HashMap(); + public static HashMap bungeePluginVersins = new HashMap<>(); public static void sendUpdateMsg(String Prefix, String Spigot, String Discord, String foundVersion, String update_version) { T2CBsend.console("§4=========== " + Prefix + " §4==========="); @@ -24,29 +30,34 @@ public class T2CBupdateAPI { T2CBsend.console("§6You can find more information on Discord: §e" + Discord); T2CBsend.console("§4=========== " + Prefix + " §4==========="); } + public static void sendUpdateMsg(String Prefix, String Spigot, String Discord, String foundVersion, String update_version, CommandSender sender) { - T2CBsend.sender(sender,"§4=========== " + Prefix + " §4==========="); - T2CBsend.sender(sender,"§6A new version was found!"); - T2CBsend.sender(sender,"§6Your version: §c" + foundVersion + " §7- §6Current version: §a" + update_version); - T2CBsend.sender(sender,"§6You can download it here: §e" + Spigot); - T2CBsend.sender(sender,"§6You can find more information on Discord: §e" + Discord); - T2CBsend.sender(sender,"§4=========== " + Prefix + " §4==========="); + T2CBsend.sender(sender, "§4=========== " + Prefix + " §4==========="); + T2CBsend.sender(sender, "§6A new version was found!"); + T2CBsend.sender(sender, "§6Your version: §c" + foundVersion + " §7- §6Current version: §a" + update_version); + T2CBsend.sender(sender, "§6You can download it here: §e" + Spigot); + T2CBsend.sender(sender, "§6You can find more information on Discord: §e" + Discord); + T2CBsend.sender(sender, "§4=========== " + Prefix + " §4==========="); } + private static Boolean noUpdate = true; private static String pluginVersion; - public static void onUpdateCheckTimer(Plugin plugin, String Prefix, String Spigot, String Discord, Integer SpigotID) { + + public static void onUpdateCheckTimer(Plugin plugin, String Prefix, String Spigot, String Discord, Integer SpigotID, String url) { ProxyServer.getInstance().getScheduler().schedule(plugin, new Runnable() { public void run() { (new T2CBupdateAPI(plugin, SpigotID)).getVersion((update_version) -> { pluginVersion = plugin.getDescription().getVersion(); - T2CBupdateObject update = new T2CBupdateObject( + T2CupdateObject update = new T2CupdateObject( plugin.getDescription().getName(), - pluginVersion, - update_version + plugin.getDescription().getVersion(), + update_version, + false, + !plugin.getDescription().getVersion().equals(update_version.getVersion()) ); bungeePluginVersins.put(plugin.getDescription().getName(), update); - if (!pluginVersion.replace("_Bungee", "").equalsIgnoreCase(update_version)) { - sendUpdateMsg(Prefix, Spigot, Discord, pluginVersion, update_version); + if (!pluginVersion.replace("_Bungee", "").equalsIgnoreCase(update_version.getVersion())) { + sendUpdateMsg(Prefix, Spigot, Discord, pluginVersion, update_version.getVersion()); noUpdate = true; } else { if (noUpdate) { @@ -54,10 +65,11 @@ public class T2CBupdateAPI { noUpdate = false; } } - },Prefix, pluginVersion); + }, pluginVersion, SpigotID, url); } }, 0, 20 * 60 * 60L, TimeUnit.SECONDS); } + private Plugin plugin; private int resourceId; @@ -66,47 +78,66 @@ public class T2CBupdateAPI { this.resourceId = resourceId; } - public void getVersion(Consumer consumer, String Prefix, String pluginVersion) { - ProxyServer.getInstance().getScheduler().runAsync(this.plugin, () -> { + public void getVersion(Consumer consumer, String pluginVersion, Integer spigotID, String URL) { + BungeeCord.getInstance().getScheduler().runAsync(this.plugin, () -> { try { - InputStream inputStream = (new URL("https://api.spigotmc.org/legacy/update.php?resource=" + this.resourceId)).openStream(); - try { - Scanner scanner = new Scanner(inputStream); + URL url = new URL(URL); + URLConnection yc = url.openConnection(); + BufferedReader in = new BufferedReader( + new InputStreamReader( + yc.getInputStream())); + String inputLine; + String data = ""; + while ((inputLine = in.readLine()) != null) + data = inputLine; + in.close(); + data = data.substring(1, data.length() - 1); + if (data.isEmpty()) { + consumer.accept(null); + return; + } + JSONObject obj = new JSONObject(data); + String UpdateName = obj.getString("name"); + String tag_name = obj.getString("tag_name"); + String body = obj.getString("body").replace("\n", "
").replace("\r", "").replace("'", "''"); + String updateurl = obj.getString("html_url"); + boolean prerelease = obj.getBoolean("prerelease"); - try { - if (scanner.hasNext()) { - consumer.accept(scanner.next()); - } - } catch (Throwable var8) { - try { - scanner.close(); - } catch (Throwable var7) { - var8.addSuppressed(var7); - } - throw var8; - } - scanner.close(); - } catch (Throwable var9) { - if (inputStream != null) { - try { - inputStream.close(); - } catch (Throwable var6) { - var9.addSuppressed(var6); - } - } - throw var9; + String date = obj.getString("published_at"); + SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); + SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a"); + Date parsedDate = inputFormat.parse(date); + String formattedDate = outputFormat.format(parsedDate); + + JSONArray downloadArray = obj.getJSONArray("assets"); + String downloadURL; + if (downloadArray.isEmpty()) { + downloadURL = "https://www.spigotmc.org/resources/" + spigotID; + } else { + downloadURL = downloadArray.getJSONObject(0).getString("browser_download_url"); } - if (inputStream != null) { - inputStream.close(); + + if (!prerelease) { + downloadURL = "https://www.spigotmc.org/resources/" + spigotID; + updateurl = "https://www.spigotmc.org/resources/" + spigotID; } - } catch (IOException var10) { - T2CBupdateObject update = new T2CBupdateObject( + + T2CupdateWebData webData = new T2CupdateWebData(UpdateName, tag_name, body, updateurl, formattedDate, downloadURL, prerelease); + consumer.accept(webData); + } catch (Exception var10) { + Boolean load = false; + if (bungeePluginVersins.containsKey(plugin.getDescription().getName())) { + load = bungeePluginVersins.get(plugin.getDescription().getName()).load; + } + T2CupdateObject update = new T2CupdateObject( plugin.getDescription().getName(), pluginVersion, - "§4No public version found!" + null, + load, + false ); bungeePluginVersins.put(plugin.getDescription().getName(), update); - this.plugin.getLogger().severe(Prefix + "§4 Cannot look for updates: " + var10.getMessage()); + this.plugin.getLogger().severe("§4 Cannot look for updates: " + var10.getMessage()); } }); } diff --git a/src/main/java/net/t2code/t2codelib/BUNGEE/api/update/T2CBupdateObject.java b/src/main/java/net/t2code/t2codelib/BUNGEE/api/update/T2CBupdateObject.java deleted file mode 100644 index 114ab3c..0000000 --- a/src/main/java/net/t2code/t2codelib/BUNGEE/api/update/T2CBupdateObject.java +++ /dev/null @@ -1,12 +0,0 @@ -package net.t2code.t2codelib.BUNGEE.api.update; - -public class T2CBupdateObject { - public String pluginName; - public String pluginVersion; - public String publicVersion; - public T2CBupdateObject(String pluginName, String pluginVersion, String publicVersion){ - this.pluginName = pluginName; - this.pluginVersion = pluginVersion; - this.publicVersion = publicVersion; - } -} diff --git a/src/main/java/net/t2code/t2codelib/BUNGEE/system/T2CBload.java b/src/main/java/net/t2code/t2codelib/BUNGEE/system/T2CBload.java index d3c8a7e..d5243b7 100644 --- a/src/main/java/net/t2code/t2codelib/BUNGEE/system/T2CBload.java +++ b/src/main/java/net/t2code/t2codelib/BUNGEE/system/T2CBload.java @@ -13,7 +13,7 @@ import net.t2code.t2codelib.BUNGEE.system.pluginMessaging.opSecurity.T2CapiOpSec import java.io.IOException; public class T2CBload { - public static void onLoad(Plugin plugin, String prefix, String autor, String version, String spigot, String discord, Integer spigotID, Integer bstatsID) { + public static void onLoad(Plugin plugin, String prefix, String autor, String version, String spigot, String discord, Integer spigotID, Integer bstatsID, String url) { long long_ = System.currentTimeMillis(); T2CBsend.console(prefix + "§4============================= " + prefix + " §4============================="); T2CBsend.console(prefix + " §2Autor: §6" + String.valueOf(autor).replace("[", "").replace("]", "")); @@ -33,7 +33,7 @@ public class T2CBload { throw new RuntimeException(e); } - T2CBupdateAPI.onUpdateCheckTimer(plugin, prefix, spigot, discord, spigotID); + T2CBupdateAPI.onUpdateCheckTimer(plugin, prefix, spigot, discord, spigotID,url); //API plugin.getProxy().registerChannel("t2c:bcmd"); diff --git a/src/main/java/net/t2code/t2codelib/BUNGEE/system/T2CodeBMain.java b/src/main/java/net/t2code/t2codelib/BUNGEE/system/T2CodeBMain.java index 2379741..f044239 100644 --- a/src/main/java/net/t2code/t2codelib/BUNGEE/system/T2CodeBMain.java +++ b/src/main/java/net/t2code/t2codelib/BUNGEE/system/T2CodeBMain.java @@ -23,7 +23,7 @@ public class T2CodeBMain extends Plugin { plugin = this; orgVersion = plugin.getDescription().getVersion(); autor = plugin.getDescription().getAuthor(); - T2CBload.onLoad(plugin, Util.getPrefix(), autor, orgVersion, Util.getSpigot(), Util.getDiscord(), Util.getSpigotID(), Util.getBstatsID()); + T2CBload.onLoad(plugin, Util.getPrefix(), autor, orgVersion, Util.getSpigot(), Util.getDiscord(), Util.getSpigotID(), Util.getBstatsID(),Util.getGit()); String[] fv = orgVersion.split("_"); plugin.getDescription().setVersion(fv[0]); version = plugin.getDescription().getVersion(); diff --git a/src/main/java/net/t2code/t2codelib/SPIGOT/api/update/T2CupdateAPI.java b/src/main/java/net/t2code/t2codelib/SPIGOT/api/update/T2CupdateAPI.java index b0a1787..06af3e1 100644 --- a/src/main/java/net/t2code/t2codelib/SPIGOT/api/update/T2CupdateAPI.java +++ b/src/main/java/net/t2code/t2codelib/SPIGOT/api/update/T2CupdateAPI.java @@ -4,6 +4,8 @@ import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend; import net.t2code.t2codelib.SPIGOT.system.T2CodeLibMain; import net.t2code.t2codelib.SPIGOT.system.UpdateType; import net.t2code.t2codelib.SPIGOT.system.config.config.SelectLibConfig; +import net.t2code.t2codelib.T2CupdateObject; +import net.t2code.t2codelib.T2CupdateWebData; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; diff --git a/src/main/java/net/t2code/t2codelib/SPIGOT/api/update/T2CupdateChecker.java b/src/main/java/net/t2code/t2codelib/SPIGOT/api/update/T2CupdateChecker.java index da33cc7..02bf593 100644 --- a/src/main/java/net/t2code/t2codelib/SPIGOT/api/update/T2CupdateChecker.java +++ b/src/main/java/net/t2code/t2codelib/SPIGOT/api/update/T2CupdateChecker.java @@ -1,5 +1,6 @@ package net.t2code.t2codelib.SPIGOT.api.update; +import net.t2code.t2codelib.T2CupdateObject; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; diff --git a/src/main/java/net/t2code/t2codelib/SPIGOT/api/update/T2CupdateCheckerGit.java b/src/main/java/net/t2code/t2codelib/SPIGOT/api/update/T2CupdateCheckerGit.java index 60f0dfd..a2fd6cb 100644 --- a/src/main/java/net/t2code/t2codelib/SPIGOT/api/update/T2CupdateCheckerGit.java +++ b/src/main/java/net/t2code/t2codelib/SPIGOT/api/update/T2CupdateCheckerGit.java @@ -1,5 +1,7 @@ package net.t2code.t2codelib.SPIGOT.api.update; +import net.t2code.t2codelib.T2CupdateObject; +import net.t2code.t2codelib.T2CupdateWebData; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; import org.json.JSONArray; diff --git a/src/main/java/net/t2code/t2codelib/SPIGOT/system/cmd/CmdExecuter.java b/src/main/java/net/t2code/t2codelib/SPIGOT/system/cmd/CmdExecuter.java index da6cbc1..32bcf3a 100644 --- a/src/main/java/net/t2code/t2codelib/SPIGOT/system/cmd/CmdExecuter.java +++ b/src/main/java/net/t2code/t2codelib/SPIGOT/system/cmd/CmdExecuter.java @@ -2,7 +2,7 @@ package net.t2code.t2codelib.SPIGOT.system.cmd; import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend; import net.t2code.t2codelib.SPIGOT.api.update.T2CupdateAPI; -import net.t2code.t2codelib.SPIGOT.api.update.T2CupdateObject; +import net.t2code.t2codelib.T2CupdateObject; import net.t2code.t2codelib.SPIGOT.system.config.config.SelectLibConfig; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; diff --git a/src/main/java/net/t2code/t2codelib/SPIGOT/system/cmd/Commands.java b/src/main/java/net/t2code/t2codelib/SPIGOT/system/cmd/Commands.java index 9c1bd36..6b7f4b9 100644 --- a/src/main/java/net/t2code/t2codelib/SPIGOT/system/cmd/Commands.java +++ b/src/main/java/net/t2code/t2codelib/SPIGOT/system/cmd/Commands.java @@ -3,10 +3,7 @@ package net.t2code.t2codelib.SPIGOT.system.cmd; import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend; import net.t2code.t2codelib.SPIGOT.api.messages.T2Ctemplate; import net.t2code.t2codelib.SPIGOT.api.update.T2CupdateAPI; -import net.t2code.t2codelib.SPIGOT.api.update.T2CupdateObject; -import net.t2code.t2codelib.SPIGOT.api.update.T2CupdateWebData; import net.t2code.t2codelib.SPIGOT.system.T2CodeLibMain; -import net.t2code.t2codelib.SPIGOT.system.UpdateType; import net.t2code.t2codelib.Util; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; diff --git a/src/main/java/net/t2code/t2codelib/SPIGOT/api/update/T2CupdateObject.java b/src/main/java/net/t2code/t2codelib/T2CupdateObject.java similarity index 92% rename from src/main/java/net/t2code/t2codelib/SPIGOT/api/update/T2CupdateObject.java rename to src/main/java/net/t2code/t2codelib/T2CupdateObject.java index e0e5238..feedd0a 100644 --- a/src/main/java/net/t2code/t2codelib/SPIGOT/api/update/T2CupdateObject.java +++ b/src/main/java/net/t2code/t2codelib/T2CupdateObject.java @@ -1,4 +1,4 @@ -package net.t2code.t2codelib.SPIGOT.api.update; +package net.t2code.t2codelib; public class T2CupdateObject { diff --git a/src/main/java/net/t2code/t2codelib/SPIGOT/api/update/T2CupdateWebData.java b/src/main/java/net/t2code/t2codelib/T2CupdateWebData.java similarity index 94% rename from src/main/java/net/t2code/t2codelib/SPIGOT/api/update/T2CupdateWebData.java rename to src/main/java/net/t2code/t2codelib/T2CupdateWebData.java index cd72606..f523d81 100644 --- a/src/main/java/net/t2code/t2codelib/SPIGOT/api/update/T2CupdateWebData.java +++ b/src/main/java/net/t2code/t2codelib/T2CupdateWebData.java @@ -1,4 +1,4 @@ -package net.t2code.t2codelib.SPIGOT.api.update; +package net.t2code.t2codelib; import lombok.Getter;