package net.t2code.t2codelib.SPIGOT.api.update; import net.t2code.t2codelib.SPIGOT.api.messages.T2Csend; import net.t2code.t2codelib.SPIGOT.system.T2CodeLibMain; import net.t2code.t2codelib.SPIGOT.system.config.config.SelectLibConfig; import net.t2code.t2codelib.T2CupdateObject; import net.t2code.t2codelib.T2CupdateWebData; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitRunnable; import org.json.JSONArray; import org.json.JSONObject; import java.io.*; import java.net.URL; import java.net.URLConnection; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; import java.util.function.Consumer; public class T2CupdateCheckerGit { private final JavaPlugin plugin; private T2CupdateObject t2CupdateObject; public T2CupdateCheckerGit(JavaPlugin plugin, String prefix, String gitKey, Integer spigotID, String discord, Boolean updateCheckOnJoin, Boolean seePreReleaseUpdates, Integer timeInterval) { this.plugin = plugin; String RepoURL = "https://git.t2code.net/api/v1/repos/" + gitKey + "/releases?limit=1"; if (!seePreReleaseUpdates) { RepoURL = RepoURL + "&pre-release=false"; } String finalRepoURL = RepoURL; Integer finalInterval; if (timeInterval < 1) { finalInterval = 1; } else finalInterval = timeInterval; Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, new Runnable() { @Override public void run() { if (SelectLibConfig.getUpdateCheckFullDisable()) return; getVersion((webData) -> { T2CupdateObject update = new T2CupdateObject( plugin.getName(), plugin.getDescription().getVersion(), webData, t2CupdateObject != null && t2CupdateObject.load, !plugin.getDescription().getVersion().equals(webData.getVersion()), updateCheckOnJoin ); T2CupdateAPI.pluginVersions.put(plugin.getName(), update); if (T2CupdateAPI.pluginVersions.get(plugin.getName()) == null){ T2Csend.debugmsg(T2CodeLibMain.getPlugin(),plugin.getName() + " UpdateAPI T2CupdateAPI.pluginVersions.get(plugin.getName()) == null"); return; } if (T2CupdateAPI.pluginVersions.get(plugin.getName()).updateAvailable) { if (!update.load) { new BukkitRunnable() { @Override public void run() { update.load = true; T2CupdateAPI.sendUpdateMsg(prefix, discord, webData, plugin); } }.runTaskLaterAsynchronously(plugin, 600L); } else T2CupdateAPI.sendUpdateMsg(prefix, discord, webData, plugin); } else { if (!update.load) { T2Csend.console(prefix + " §2No update found."); update.load = true; } } t2CupdateObject = update; }, plugin.getDescription().getVersion(), spigotID, finalRepoURL, updateCheckOnJoin, seePreReleaseUpdates, timeInterval); } }, 10L, finalInterval * 60 * 20L); } public void getVersion(Consumer consumer, String pluginVersion, Integer spigotID, String URL, Boolean updateCheckOnJoin, Boolean seePreReleaseUpdates, Integer timeInterval) { if (!plugin.isEnabled()) { return; } Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> { try { 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 updateTitle = obj.getString("name"); String version = obj.getString("tag_name"); String updateDescription = obj.getString("body").replace("\n", "
").replace("\r", "").replace("'", "''").replace("**", ""); String updateUrl = obj.getString("html_url"); boolean preRelease = obj.getBoolean("prerelease"); 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 publishedAt = outputFormat.format(parsedDate); String gitURL = updateUrl; 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 (!preRelease) { downloadURL = "https://www.spigotmc.org/resources/" + spigotID; updateUrl = "https://www.spigotmc.org/resources/" + spigotID; } T2CupdateWebData webData = new T2CupdateWebData(updateTitle, version, updateDescription, updateUrl, publishedAt, downloadURL, gitURL, preRelease); consumer.accept(webData); } catch (Exception exception) { Boolean load = false; if (T2CupdateAPI.pluginVersions.containsKey(plugin.getName())) { load = T2CupdateAPI.pluginVersions.get(plugin.getName()).load; } T2CupdateObject update = new T2CupdateObject( plugin.getName(), pluginVersion, null, load, false, updateCheckOnJoin ); T2CupdateAPI.pluginVersions.put(plugin.getName(), update); T2Csend.error(this.plugin,"§4 Cannot look for updates: " + exception.getMessage()); exception.printStackTrace(); } }); } }