|
| 1 | +package de.timecoding.cc.api; |
| 2 | + |
| 3 | +import de.timecoding.cc.CubicCountdown; |
| 4 | +import org.apache.http.HttpResponse; |
| 5 | +import org.apache.http.client.methods.HttpGet; |
| 6 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 7 | +import org.apache.http.impl.client.HttpClientBuilder; |
| 8 | +import org.apache.http.util.EntityUtils; |
| 9 | +import org.bukkit.Bukkit; |
| 10 | +import org.bukkit.ChatColor; |
| 11 | +import org.json.JSONObject; |
| 12 | + |
| 13 | +import java.io.*; |
| 14 | +import java.net.URL; |
| 15 | + |
| 16 | +public class AutoUpdater { |
| 17 | + |
| 18 | + |
| 19 | + private final CubicCountdown plugin; |
| 20 | + private final String downloadBase = "https://github.com/TimeCodings/CubicCountdown/releases/download/"; |
| 21 | + private final String pluginVersion; |
| 22 | + private boolean autoUpdaterEnabled = true; |
| 23 | + private final String newestPluginVersion; |
| 24 | + private final boolean sent = false; |
| 25 | + |
| 26 | + public AutoUpdater(CubicCountdown plugin, String newestPluginVersion) { |
| 27 | + this.plugin = plugin; |
| 28 | + this.pluginVersion = plugin.getDescription().getVersion(); |
| 29 | + if(this.plugin.getConfigHandler().keyExists("AutoUpdater")) { |
| 30 | + this.autoUpdaterEnabled = this.plugin.getConfigHandler().getBoolean("AutoUpdater"); |
| 31 | + } |
| 32 | + this.newestPluginVersion = newestPluginVersion; |
| 33 | + checkForPluginUpdate(); |
| 34 | + } |
| 35 | + |
| 36 | + public String getNewestPluginVersion() { |
| 37 | + String url = "https://api.github.com/repos/TimeCodings/CubicCountdown/releases/latest"; |
| 38 | + try { |
| 39 | + CloseableHttpClient httpClient = HttpClientBuilder.create().build(); |
| 40 | + HttpGet request = new HttpGet(url); |
| 41 | + request.addHeader("content-type", "application/vnd.github.v3+json"); |
| 42 | + HttpResponse result = httpClient.execute(request); |
| 43 | + JSONObject json = new JSONObject(EntityUtils.toString(result.getEntity(), "UTF-8")); |
| 44 | + if(json != null){ |
| 45 | + return json.get("tag_name").toString(); |
| 46 | + } |
| 47 | + } catch (IOException ex) { |
| 48 | + Bukkit.getConsoleSender().sendMessage("§cCould not fetch the newest §eCubicCountdown §crelease! You may be offline!"); |
| 49 | + } |
| 50 | + return pluginVersion; |
| 51 | + } |
| 52 | + |
| 53 | + public boolean pluginUpdateAvailable() { |
| 54 | + return !getNewestPluginVersion().replace("v", "").equalsIgnoreCase(pluginVersion); |
| 55 | + } |
| 56 | + |
| 57 | + public void checkForPluginUpdate() { |
| 58 | + Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "Checking for updates..."); |
| 59 | + if (pluginUpdateAvailable()) { |
| 60 | + if (autoUpdaterEnabled) { |
| 61 | + Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Update found! (" + getNewestPluginVersion() + ") Trying to download the newest update..."); |
| 62 | + //Trying to download update |
| 63 | + Bukkit.getScheduler().runTaskAsynchronously(this.plugin, new Runnable() { |
| 64 | + @Override |
| 65 | + public void run() { |
| 66 | + downloadUpdate(); |
| 67 | + } |
| 68 | + }); |
| 69 | + } else { |
| 70 | + Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Update found (" + getNewestPluginVersion() + ")! You can download it here: "+ChatColor.YELLOW+this.downloadBase + ""+getNewestPluginVersion()+"/CubicCountdown.jar"); |
| 71 | + } |
| 72 | + } else { |
| 73 | + Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "No update found! You're running the latest version (v" + pluginVersion + ")"); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private boolean downloadUpdate() { |
| 78 | + try { |
| 79 | + URL download = new URL(this.downloadBase+getNewestPluginVersion()+"/CubicCountdown.jar?timestamp=" + System.currentTimeMillis()); |
| 80 | + BufferedInputStream in = null; |
| 81 | + FileOutputStream fout = null; |
| 82 | + BufferedOutputStream bout = null; |
| 83 | + try { |
| 84 | + Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "Trying to download the newest CubicCountdown update..."); |
| 85 | + in = new BufferedInputStream(download.openStream()); |
| 86 | + fout = new FileOutputStream("plugins//" + this.getPluginNameByJar()); |
| 87 | + bout = new BufferedOutputStream(fout); |
| 88 | + final byte[] data = new byte[1024]; |
| 89 | + int count; |
| 90 | + while ((count = in.read(data, 0, 1024)) >= 0) { |
| 91 | + bout.write(data, 0, count); |
| 92 | + } |
| 93 | + } catch (Exception e) { |
| 94 | + Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Failed to download the newest CubicCountdown update!"); |
| 95 | + return false; |
| 96 | + } finally { |
| 97 | + if (in != null) { |
| 98 | + in.close(); |
| 99 | + } |
| 100 | + if (bout != null) { |
| 101 | + bout.close(); |
| 102 | + } |
| 103 | + } |
| 104 | + Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Successfully downloaded the CubicCountdown update!"); |
| 105 | + Bukkit.getConsoleSender().sendMessage(ChatColor.AQUA + "The plugin will now try to reload the server..."); |
| 106 | + Bukkit.reload(); |
| 107 | + return true; |
| 108 | + } catch (IOException e) { |
| 109 | + Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "Failed to download the newest CubicCountdown update!"); |
| 110 | + return false; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private String getPluginNameByJar() { |
| 115 | + return new File(CubicCountdown.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getName(); |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments