Fix update checker

- No more NPEs
 - Use the better spigot API
 - No obnoxious tooltip displaying the link without being clickable
This commit is contained in:
dordsor21
2020-04-21 08:56:23 +01:00
parent 67736bfbca
commit 8d61e6f111
3 changed files with 49 additions and 41 deletions

View File

@ -25,16 +25,16 @@
*/
package com.plotsquared.bukkit.util;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.configuration.Captions;
import com.plotsquared.core.configuration.Settings;
import org.bukkit.Bukkit;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
@ -51,32 +51,32 @@ public class UpdateUtility implements Listener {
}
public void updateChecker() {
new BukkitRunnable() {
public void run() {
Bukkit.getScheduler().runTaskAsynchronously(UpdateUtility.this.javaPlugin, () -> {
if (Settings.Enabled_Components.UPDATE_NOTIFICATIONS) {
try {
HttpsURLConnection connection = (HttpsURLConnection) new URL("https://api.spigotmc.org/legacy/update.php?resource=77506").openConnection();
connection.setRequestMethod("GET");
spigotVersion = (new BufferedReader(new InputStreamReader(connection.getInputStream()))).readLine();
} catch (IOException e) {
PlotSquared.log(
Captions.PREFIX + "&cUnable to check for updates because: " + e);
this.cancel();
return;
}
if (!internalVersion.equals(spigotVersion)) {
PlotSquared.log(Captions.PREFIX + "&6There appears to be a PlotSquared update available!");
PlotSquared.log(Captions.PREFIX + "&6You are running version " + internalVersion + ", &6latest version is " + spigotVersion);
PlotSquared.log(Captions.PREFIX + "&6https://www.spigotmc.org/resources/77506/updates");
} else {
PlotSquared.log(Captions.PREFIX + "Congratulations! You are running the latest PlotSquared version.");
}
}
this.cancel();
});
Bukkit.getScheduler().runTaskTimerAsynchronously(this.javaPlugin, () -> {
try {
HttpsURLConnection connection = (HttpsURLConnection) new URL(
"https://api.spigotmc.org/simple/0.1/index.php?action=getResource&id=77506")
.openConnection();
connection.setRequestMethod("GET");
JsonObject result = (new JsonParser())
.parse(new JsonReader(new InputStreamReader(connection.getInputStream())))
.getAsJsonObject();
spigotVersion = result.get("current_version").toString();
} catch (IOException e) {
PlotSquared.log(Captions.PREFIX + "&cUnable to check for updates because: " + e);
return;
}
}.runTaskTimer(this.javaPlugin, 0L, 12000L);
if (!internalVersion.equals(spigotVersion)) {
PlotSquared
.log(Captions.PREFIX + "&6There appears to be a PlotSquared update available!");
PlotSquared.log(Captions.PREFIX + "&6You are running version " + internalVersion
+ ", &6latest version is " + spigotVersion);
PlotSquared
.log(Captions.PREFIX + "&6https://www.spigotmc.org/resources/77506/updates");
} else {
PlotSquared.log(Captions.PREFIX
+ "Congratulations! You are running the latest PlotSquared version.");
}
}, 0L, 12000L);
}
}