diff options
| author | fatpigsarefat <fatpigsarefat@outlook.com> | 2018-04-15 20:03:06 +0100 |
|---|---|---|
| committer | fatpigsarefat <fatpigsarefat@outlook.com> | 2018-04-15 20:04:38 +0100 |
| commit | fa91b320cb5ddbd19ac54b8504834270feb0f75a (patch) | |
| tree | 6743345f191edfd14b371970de589c2cb3552656 /src/me/fatpigsarefat/quests/updater/Updater.java | |
| parent | 2361783c0982de3a8fd02fd96d10f5362af8b983 (diff) | |
Initial commit
Diffstat (limited to 'src/me/fatpigsarefat/quests/updater/Updater.java')
| -rw-r--r-- | src/me/fatpigsarefat/quests/updater/Updater.java | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/me/fatpigsarefat/quests/updater/Updater.java b/src/me/fatpigsarefat/quests/updater/Updater.java new file mode 100644 index 00000000..0dca333d --- /dev/null +++ b/src/me/fatpigsarefat/quests/updater/Updater.java @@ -0,0 +1,59 @@ +package me.fatpigsarefat.quests.updater; + +import me.fatpigsarefat.quests.obj.Messages; +import org.bukkit.plugin.Plugin; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.logging.Level; + +public class Updater { + + private static final int PROJECT_ID = 23696; + private String installedVersion; + private String returnedVersion; + private URL api; + private Plugin plugin; + private boolean updateReady; + + public Updater(Plugin plugin) { + this.plugin = plugin; + this.installedVersion = plugin.getDescription().getVersion(); + try { + this.api = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + PROJECT_ID); + } catch (MalformedURLException ignored) { + // shit + fan + } + } + + public String getLink() { + return "https://www.spigotmc.org/resources/" + PROJECT_ID; + } + + public boolean check() { + try { + URLConnection con = api.openConnection(); + returnedVersion = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine(); + if (!returnedVersion.equals(installedVersion)) { + plugin.getLogger().log(Level.INFO, "A new version " + returnedVersion + " was found on Spigot (your version: " + installedVersion + "). Please update me! <3 - Link: " + getLink()); + updateReady = true; + } + } catch (IOException e) { + plugin.getLogger().log(Level.WARNING, "Failed to check for updates. You can check manually at " + getLink()); + // probably offline + } + return false; + } + + public boolean isUpdateReady() { + return updateReady; + } + + public String getMessage() { + return Messages.QUEST_UPDATER.getMessage().replace("{newver}", returnedVersion).replace("{oldver}", installedVersion).replace("{link}", getLink()); + } +}
\ No newline at end of file |
