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

@ -190,7 +190,9 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain
return;
}
if (Settings.Enabled_Components.UPDATE_NOTIFICATIONS) {
new UpdateUtility(this).updateChecker();
}
if (PremiumVerification.isPremium()) {
PlotSquared.log(Captions.PREFIX + "&6PlotSquared version licensed to Spigot user " + getUserID());

View File

@ -26,6 +26,9 @@
package com.plotsquared.bukkit.listener;
import com.destroystokyo.paper.MaterialTags;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader;
import com.plotsquared.bukkit.player.BukkitPlayer;
import com.plotsquared.bukkit.util.BukkitUtil;
import com.plotsquared.bukkit.util.UpdateUtility;
@ -660,11 +663,13 @@ public class PlayerEvents extends PlotListener implements Listener {
&& Settings.Enabled_Components.UPDATE_NOTIFICATIONS) {
try {
HttpsURLConnection connection = (HttpsURLConnection) new URL(
"https://api.spigotmc.org/legacy/update.php?resource=77506").openConnection();
"https://api.spigotmc.org/simple/0.1/index.php?action=getResource&id=77506")
.openConnection();
connection.setRequestMethod("GET");
spigotVersion =
(new BufferedReader(new InputStreamReader(connection.getInputStream())))
.readLine();
JsonObject result = (new JsonParser())
.parse(new JsonReader(new InputStreamReader(connection.getInputStream())))
.getAsJsonObject();
spigotVersion = result.get("current_version").toString();
} catch (IOException e) {
new PlotMessage(Captions.PREFIX
+ "Unable to check for updates, check console for further information.")
@ -674,14 +679,15 @@ public class PlayerEvents extends PlotListener implements Listener {
}
try {
if (!UpdateUtility.internalVersion.equals(UpdateUtility.spigotVersion)) {
if (!UpdateUtility.internalVersion.equals(spigotVersion)) {
new PlotMessage("-----------------------------------").send(pp);
new PlotMessage(Captions.PREFIX + "There appears to be a PlotSquared update available!")
.color("$1").tooltip("https://www.spigotmc.org/resources/77506/updates").send(pp);
new PlotMessage(Captions.PREFIX + "The latest version is " + spigotVersion).color("$1").tooltip("https://www.spigotmc.org/resources/77506/updates")
.send(pp);
new PlotMessage(Captions.PREFIX + "https://www.spigotmc.org/resources/77506/updates")
.color("$1").tooltip("https://www.spigotmc.org/resources/77506/updates").send(pp);
new PlotMessage(
Captions.PREFIX + "There appears to be a PlotSquared update available!")
.color("$1").send(pp);
new PlotMessage(Captions.PREFIX + "The latest version is " + spigotVersion)
.color("$1").send(pp);
new PlotMessage(Captions.PREFIX + "Download at:").color("$1").send(pp);
player.sendMessage(" https://www.spigotmc.org/resources/77506/updates");
new PlotMessage("-----------------------------------").send(pp);
}
} catch (final Exception e) {

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) {
Bukkit.getScheduler().runTaskTimerAsynchronously(this.javaPlugin, () -> {
try {
HttpsURLConnection connection = (HttpsURLConnection) new URL("https://api.spigotmc.org/legacy/update.php?resource=77506").openConnection();
HttpsURLConnection connection = (HttpsURLConnection) new URL(
"https://api.spigotmc.org/simple/0.1/index.php?action=getResource&id=77506")
.openConnection();
connection.setRequestMethod("GET");
spigotVersion = (new BufferedReader(new InputStreamReader(connection.getInputStream()))).readLine();
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);
this.cancel();
PlotSquared.log(Captions.PREFIX + "&cUnable to check for updates because: " + e);
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");
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();
});
}
}.runTaskTimer(this.javaPlugin, 0L, 12000L);
PlotSquared.log(Captions.PREFIX
+ "Congratulations! You are running the latest PlotSquared version.");
}
}, 0L, 12000L);
}
}