mirror of
https://github.com/amalthea-mc/ShopChest.git
synced 2024-11-08 19:51:05 +00:00
Revise and fix update checker
Use spiget API to get retrieve latest uploaded version.
This commit is contained in:
parent
59828df99d
commit
3a7d49d292
@ -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;
|
||||||
|
|
||||||
|
case FALSE:
|
||||||
|
latestVersion = "";
|
||||||
|
downloadLink = "";
|
||||||
|
isUpdateNeeded = false;
|
||||||
|
break;
|
||||||
|
|
||||||
} else if (result == UpdateCheckerResult.FALSE) {
|
case ERROR:
|
||||||
latestVersion = "";
|
latestVersion = "";
|
||||||
downloadLink = "";
|
downloadLink = "";
|
||||||
isUpdateNeeded = false;
|
isUpdateNeeded = false;
|
||||||
Bukkit.getConsoleSender().sendMessage("[ShopChest] " + LanguageUtils.getMessage(Message.UPDATE_NO_UPDATE));
|
getLogger().severe("An error occurred while checking for updates.");
|
||||||
} else {
|
break;
|
||||||
latestVersion = "";
|
|
||||||
downloadLink = "";
|
|
||||||
isUpdateNeeded = false;
|
|
||||||
Bukkit.getConsoleSender().sendMessage("[ShopChest] " + LanguageUtils.getMessage(Message.UPDATE_ERROR));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.runTaskAsynchronously(this);
|
}.runTaskAsynchronously(this);
|
||||||
|
@ -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");
|
||||||
|
Loading…
Reference in New Issue
Block a user