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

View File

@ -2,11 +2,14 @@ package de.epiceric.shopchest.utils;
import de.epiceric.shopchest.ShopChest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class UpdateChecker {
private ShopChest plugin;
@ -20,27 +23,31 @@ public class UpdateChecker {
/**
* 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() {
try {
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();
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0");
conn.connect();
conn.setRequestProperty("User-Agent", "ShopChest/UpdateChecker");
InputStreamReader isr = new InputStreamReader(conn.getInputStream());
BufferedReader br = new BufferedReader(isr);
InputStreamReader reader = new InputStreamReader(conn.getInputStream());
JsonElement element = new JsonParser().parse(reader);
String line = br.readLine();
isr.close();
br.close();
version = line.split("\\|")[0];
link = "https://www.spigotmc.org/resources/shopchest.11431/download?version=" + line.split("\\|")[1];
if (element.isJsonArray()) {
JsonObject result = element.getAsJsonArray().get(0).getAsJsonObject();
String id = result.get("id").getAsString();
version = result.get("name").getAsString();
link = "https://www.spigotmc.org/resources/shopchest.11431/download?version=" + id;
} else {
plugin.debug("Failed to check for updates");
plugin.debug("Result: " + element.toString());
return UpdateCheckerResult.ERROR;
}
if (plugin.getDescription().getVersion().equals(version)) {
plugin.debug("No update found");