Revise and fix update checker

Use spiget API to get retrieve latest uploaded version.
This commit is contained in:
Eric 2018-11-05 16:52:46 +01:00
parent 59828df99d
commit 3a7d49d292
2 changed files with 46 additions and 37 deletions

View File

@ -5,13 +5,10 @@ import com.wasteofplastic.askyblock.ASkyBlock;
import de.epiceric.shopchest.command.ShopCommand; import de.epiceric.shopchest.command.ShopCommand;
import de.epiceric.shopchest.config.Config; import de.epiceric.shopchest.config.Config;
import de.epiceric.shopchest.config.HologramFormat; import de.epiceric.shopchest.config.HologramFormat;
import de.epiceric.shopchest.config.Placeholder;
import de.epiceric.shopchest.event.ShopInitializedEvent; import de.epiceric.shopchest.event.ShopInitializedEvent;
import de.epiceric.shopchest.external.PlotSquaredShopFlag; import de.epiceric.shopchest.external.PlotSquaredShopFlag;
import de.epiceric.shopchest.external.WorldGuardShopFlag; import de.epiceric.shopchest.external.WorldGuardShopFlag;
import de.epiceric.shopchest.language.LanguageUtils; import de.epiceric.shopchest.language.LanguageUtils;
import de.epiceric.shopchest.language.Message;
import de.epiceric.shopchest.language.Replacement;
import de.epiceric.shopchest.listeners.AreaShopListener; import de.epiceric.shopchest.listeners.AreaShopListener;
import de.epiceric.shopchest.listeners.BlockExplodeListener; import de.epiceric.shopchest.listeners.BlockExplodeListener;
import de.epiceric.shopchest.listeners.ChestProtectListener; import de.epiceric.shopchest.listeners.ChestProtectListener;
@ -344,29 +341,34 @@ public class ShopChest extends JavaPlugin {
UpdateChecker uc = new UpdateChecker(ShopChest.this); UpdateChecker uc = new UpdateChecker(ShopChest.this);
UpdateCheckerResult result = uc.check(); UpdateCheckerResult result = uc.check();
Bukkit.getConsoleSender().sendMessage("[ShopChest] " + LanguageUtils.getMessage(Message.UPDATE_CHECKING)); switch (result) {
if (result == UpdateCheckerResult.TRUE) { case TRUE:
latestVersion = uc.getVersion(); latestVersion = uc.getVersion();
downloadLink = uc.getLink(); downloadLink = uc.getLink();
isUpdateNeeded = true; isUpdateNeeded = true;
Bukkit.getConsoleSender().sendMessage("[ShopChest] " + LanguageUtils.getMessage(Message.UPDATE_AVAILABLE, new Replacement(Placeholder.VERSION, latestVersion)));
for (Player p : getServer().getOnlinePlayers()) { getLogger().warning(String.format("Version %s is available! You are running version %s.",
if (p.hasPermission(Permissions.UPDATE_NOTIFICATION)) { latestVersion, getDescription().getVersion()));
Utils.sendUpdateMessage(ShopChest.this, p);
for (Player p : getServer().getOnlinePlayers()) {
if (p.hasPermission(Permissions.UPDATE_NOTIFICATION)) {
Utils.sendUpdateMessage(ShopChest.this, p);
}
} }
} break;
} else if (result == UpdateCheckerResult.FALSE) { case FALSE:
latestVersion = ""; latestVersion = "";
downloadLink = ""; downloadLink = "";
isUpdateNeeded = false; isUpdateNeeded = false;
Bukkit.getConsoleSender().sendMessage("[ShopChest] " + LanguageUtils.getMessage(Message.UPDATE_NO_UPDATE)); break;
} else {
latestVersion = ""; case ERROR:
downloadLink = ""; latestVersion = "";
isUpdateNeeded = false; downloadLink = "";
Bukkit.getConsoleSender().sendMessage("[ShopChest] " + LanguageUtils.getMessage(Message.UPDATE_ERROR)); isUpdateNeeded = false;
getLogger().severe("An error occurred while checking for updates.");
break;
} }
} }
}.runTaskAsynchronously(this); }.runTaskAsynchronously(this);

View File

@ -2,11 +2,14 @@ package de.epiceric.shopchest.utils;
import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.ShopChest;
import java.io.BufferedReader;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class UpdateChecker { public class UpdateChecker {
private ShopChest plugin; private ShopChest plugin;
@ -20,27 +23,31 @@ public class UpdateChecker {
/** /**
* Check if an update is needed * Check if an update is needed
* *
* @return {@link UpdateCheckerResult#TRUE} if an update is available, {@link UpdateCheckerResult#FALSE} if no update is needed or {@link UpdateCheckerResult#ERROR} if an error occurred * @return {@link UpdateCheckerResult#TRUE} if an update is available,
* {@link UpdateCheckerResult#FALSE} if no update is needed or
* {@link UpdateCheckerResult#ERROR} if an error occurred
*/ */
public UpdateCheckerResult check() { public UpdateCheckerResult check() {
try { try {
plugin.debug("Checking for updates..."); plugin.debug("Checking for updates...");
URL url = new URL("https://textuploader.com/all1l/raw"); URL url = new URL("https://api.spiget.org/v2/resources/11431/versions?size=1&page=1&sort=-releaseDate");
URLConnection conn = url.openConnection(); URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0"); conn.setRequestProperty("User-Agent", "ShopChest/UpdateChecker");
conn.connect();
InputStreamReader isr = new InputStreamReader(conn.getInputStream()); InputStreamReader reader = new InputStreamReader(conn.getInputStream());
BufferedReader br = new BufferedReader(isr); JsonElement element = new JsonParser().parse(reader);
String line = br.readLine(); if (element.isJsonArray()) {
JsonObject result = element.getAsJsonArray().get(0).getAsJsonObject();
isr.close(); String id = result.get("id").getAsString();
br.close(); version = result.get("name").getAsString();
link = "https://www.spigotmc.org/resources/shopchest.11431/download?version=" + id;
version = line.split("\\|")[0]; } else {
link = "https://www.spigotmc.org/resources/shopchest.11431/download?version=" + line.split("\\|")[1]; plugin.debug("Failed to check for updates");
plugin.debug("Result: " + element.toString());
return UpdateCheckerResult.ERROR;
}
if (plugin.getDescription().getVersion().equals(version)) { if (plugin.getDescription().getVersion().equals(version)) {
plugin.debug("No update found"); plugin.debug("No update found");