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; return;
} }
new UpdateUtility(this).updateChecker(); if (Settings.Enabled_Components.UPDATE_NOTIFICATIONS) {
new UpdateUtility(this).updateChecker();
}
if (PremiumVerification.isPremium()) { if (PremiumVerification.isPremium()) {
PlotSquared.log(Captions.PREFIX + "&6PlotSquared version licensed to Spigot user " + getUserID()); PlotSquared.log(Captions.PREFIX + "&6PlotSquared version licensed to Spigot user " + getUserID());

View File

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

View File

@ -25,16 +25,16 @@
*/ */
package com.plotsquared.bukkit.util; 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.PlotSquared;
import com.plotsquared.core.configuration.Captions; import com.plotsquared.core.configuration.Captions;
import com.plotsquared.core.configuration.Settings;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;
@ -51,32 +51,32 @@ public class UpdateUtility implements Listener {
} }
public void updateChecker() { public void updateChecker() {
new BukkitRunnable() { Bukkit.getScheduler().runTaskTimerAsynchronously(this.javaPlugin, () -> {
public void run() { try {
Bukkit.getScheduler().runTaskAsynchronously(UpdateUtility.this.javaPlugin, () -> { HttpsURLConnection connection = (HttpsURLConnection) new URL(
if (Settings.Enabled_Components.UPDATE_NOTIFICATIONS) { "https://api.spigotmc.org/simple/0.1/index.php?action=getResource&id=77506")
try { .openConnection();
HttpsURLConnection connection = (HttpsURLConnection) new URL("https://api.spigotmc.org/legacy/update.php?resource=77506").openConnection(); connection.setRequestMethod("GET");
connection.setRequestMethod("GET"); JsonObject result = (new JsonParser())
spigotVersion = (new BufferedReader(new InputStreamReader(connection.getInputStream()))).readLine(); .parse(new JsonReader(new InputStreamReader(connection.getInputStream())))
} catch (IOException e) { .getAsJsonObject();
PlotSquared.log( spigotVersion = result.get("current_version").toString();
Captions.PREFIX + "&cUnable to check for updates because: " + e); } catch (IOException e) {
this.cancel(); PlotSquared.log(Captions.PREFIX + "&cUnable to check for updates because: " + e);
return; 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();
});
} }
}.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);
} }
} }