Update checker improvements and 5.10.4

- Add config option to disable poll once it's found an update (true by default)
 - Better PlotVersion stuff can now detect if you're using a later version than the spigot API, and not tell you about it
This commit is contained in:
dordsor21 2020-04-30 10:17:19 +01:00
parent 53ca62e8fc
commit 577fe3037f
4 changed files with 90 additions and 21 deletions

View File

@ -29,10 +29,13 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import com.plotsquared.core.PlotSquared; import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.PlotVersion;
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.BukkitTask;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
import java.io.IOException; import java.io.IOException;
@ -41,19 +44,20 @@ import java.net.URL;
public class UpdateUtility implements Listener { public class UpdateUtility implements Listener {
public static String internalVersion; public static PlotVersion internalVersion;
public static String spigotVersion; public static String spigotVersion;
public static boolean hasUpdate; public static boolean hasUpdate;
private static BukkitTask task;
public final JavaPlugin javaPlugin; public final JavaPlugin javaPlugin;
private boolean notify = true; private boolean notify = true;
public UpdateUtility(final JavaPlugin javaPlugin) { public UpdateUtility(final JavaPlugin javaPlugin) {
this.javaPlugin = javaPlugin; this.javaPlugin = javaPlugin;
internalVersion = javaPlugin.getDescription().getVersion(); internalVersion = PlotSquared.get().getVersion();
} }
public void updateChecker() { public void updateChecker() {
Bukkit.getScheduler().runTaskTimerAsynchronously(this.javaPlugin, () -> { task = Bukkit.getScheduler().runTaskTimerAsynchronously(this.javaPlugin, () -> {
try { try {
HttpsURLConnection connection = (HttpsURLConnection) new URL( HttpsURLConnection connection = (HttpsURLConnection) new URL(
"https://api.spigotmc.org/simple/0.1/index.php?action=getResource&id=77506") "https://api.spigotmc.org/simple/0.1/index.php?action=getResource&id=77506")
@ -68,19 +72,26 @@ public class UpdateUtility implements Listener {
return; return;
} }
if (!internalVersion.equals(spigotVersion)) { if (internalVersion.isLaterVersion(spigotVersion)) {
PlotSquared PlotSquared
.log(Captions.PREFIX + "&6There appears to be a PlotSquared update available!"); .log(Captions.PREFIX + "&6There appears to be a PlotSquared update available!");
PlotSquared.log(Captions.PREFIX + "&6You are running version " + internalVersion PlotSquared.log(Captions.PREFIX + "&6You are running version " + internalVersion.versionString()
+ ", &6latest version is " + spigotVersion); + ", &6latest version is " + spigotVersion);
PlotSquared PlotSquared
.log(Captions.PREFIX + "&6https://www.spigotmc.org/resources/77506/updates"); .log(Captions.PREFIX + "&6https://www.spigotmc.org/resources/77506/updates");
hasUpdate = true; hasUpdate = true;
if (Settings.UpdateChecker.NOTIFY_ONCE) {
cancelTask();
}
} else if (notify) { } else if (notify) {
notify = false; notify = false;
PlotSquared.log(Captions.PREFIX PlotSquared.log(Captions.PREFIX
+ "Congratulations! You are running the latest PlotSquared version."); + "Congratulations! You are running the latest PlotSquared version.");
} }
}, 0L, 36000L); }, 0L, Settings.UpdateChecker.POLL_RATE * 60 * 20);
}
private void cancelTask() {
Bukkit.getScheduler().runTaskLater(javaPlugin, () -> task.cancel(), 20L);
} }
} }

View File

@ -26,19 +26,31 @@
package com.plotsquared.core; package com.plotsquared.core;
public class PlotVersion { public class PlotVersion {
public final int year, month, day, hash, build; public final int year, month, day, hash;
public final String versionString;
public final int[] version;
public PlotVersion(int year, int month, int day, int hash, int build) { public PlotVersion(int year, int month, int day, int hash, String versionString) {
this.year = year; this.year = year;
this.month = month; this.month = month;
this.day = day; this.day = day;
this.hash = hash; this.hash = hash;
this.build = build; this.versionString = versionString.substring(versionString.indexOf('=') + 1);
version = new int[3];
String[] verArray = versionString.substring(versionString.indexOf('=') + 1).split("\\.");
version[0] = verArray.length > 0 ? Integer.parseInt(verArray[0]) : 0;
version[1] = verArray.length > 1 ? Integer.parseInt(verArray[1]) : 0;
version[2] = verArray.length > 2 ? Integer.parseInt(verArray[2]) : 0;
} }
public PlotVersion(String version, String commit, String date) { public PlotVersion(String versionString, String commit, String date) {
String[] split = version.substring(version.indexOf('=') + 1).split("\\."); this.versionString = versionString.substring(versionString.indexOf('=') + 1);
this.build = Integer.parseInt(split[1]); version = new int[3];
String[] verArray = this.versionString.split("\\.");
version[0] = verArray.length > 0 ? Integer.parseInt(verArray[0]) : 0;
version[1] = verArray.length > 1 ? Integer.parseInt(verArray[1]) : 0;
version[2] = verArray.length > 2 ? Integer.parseInt(verArray[2]) : 0;
this.hash = Integer.parseInt(commit.substring(commit.indexOf('=') + 1), 16); this.hash = Integer.parseInt(commit.substring(commit.indexOf('=') + 1), 16);
String[] split1 = date.substring(date.indexOf('=') + 1).split("\\."); String[] split1 = date.substring(date.indexOf('=') + 1).split("\\.");
this.year = Integer.parseInt(split1[0]); this.year = Integer.parseInt(split1[0]);
@ -46,27 +58,68 @@ public class PlotVersion {
this.day = Integer.parseInt(split1[2]); this.day = Integer.parseInt(split1[2]);
} }
public static PlotVersion tryParse(String version, String commit, String date) { public static PlotVersion tryParse(String versionString, String commit, String date) {
try { try {
return new PlotVersion(version, commit, date); return new PlotVersion(versionString, commit, date);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return new PlotVersion(0, 0, 0, 0, 0); return new PlotVersion(0, 0, 0, 0, "0");
} }
} }
public String versionString() { public String versionString() {
if (hash == 0 && build == 0) { if (hash == 0 && versionString == null) {
return "NoVer-SNAPSHOT"; return "NoVer-SNAPSHOT";
} else { } else {
return "5." + build; return versionString;
} }
} }
@Override public String toString() {
if (hash == 0 && build == 0) { @Override
public String toString() {
if (hash == 0 && versionString == null) {
return "PlotSquared-NoVer-SNAPSHOT"; return "PlotSquared-NoVer-SNAPSHOT";
} else { } else {
return "PlotSquared-5." + build; return "PlotSquared-" + versionString;
} }
} }
/**
* Compare a given version string with the one cached here.
*
* @param versionString the version to compare
* @return true if the given version is a "later" version
*/
public boolean isLaterVersion(String versionString) {
String[] verArray = versionString.split("\\.");
int one = Integer.parseInt(verArray[0]);
int two = Integer.parseInt(verArray[1]);
int three = Integer.parseInt(verArray[2]);
if (one > version[0]) {
return true;
} else if (one == version[0] && two > version[1]) {
return true;
} else {
return one == version[0] && two == version[1] && three > version[2];
}
}
/**
* Compare a given version with the one cached here.
*
* @param verArray the version to compare
* @return true if the given version is a "later" version
*/
public boolean isLaterVersion(int[] verArray) {
if (verArray[0] > version[0]) {
return true;
} else if (verArray[0] == version[0] && verArray[1] > version[1]) {
return true;
} else {
return verArray[0] == version[0] && verArray[1] == version[1]
&& verArray[2] > version[2];
}
}
} }

View File

@ -244,6 +244,11 @@ public class Settings extends Config {
@Comment("Replace wall when merging") public static boolean MERGE_REPLACE_WALL = true; @Comment("Replace wall when merging") public static boolean MERGE_REPLACE_WALL = true;
} }
@Comment("Update checker settings") public static final class UpdateChecker {
@Comment("How often to poll for updates (in minutes)") public static int POLL_RATE = 360;
@Comment("Only notify console once after an update is found") public static boolean NOTIFY_ONCE = true;
}
@Comment("Schematic Settings") public static final class Schematics { @Comment("Schematic Settings") public static final class Schematics {
@Comment("Whether schematic based generation should paste schematic on top of plots, or from Y=1") @Comment("Whether schematic based generation should paste schematic on top of plots, or from Y=1")

View File

@ -29,7 +29,7 @@ ext {
git = Grgit.open(dir: new File(rootDir.toString() + "/.git")) git = Grgit.open(dir: new File(rootDir.toString() + "/.git"))
} }
version = "5.10.3" version = "5.10.4"
description = rootProject.name description = rootProject.name