Bind update notifications to spigot resource updates

The notification permission could be moved back into the other class, but well
This commit is contained in:
NotMyFault
2020-02-16 01:33:43 +01:00
parent 5ea03f12fb
commit 24b6b23d25
10 changed files with 81 additions and 289 deletions

View File

@ -49,7 +49,6 @@ import com.github.intellectualsites.plotsquared.plot.util.SetupUtils;
import com.github.intellectualsites.plotsquared.plot.util.StringMan;
import com.github.intellectualsites.plotsquared.plot.util.TaskManager;
import com.github.intellectualsites.plotsquared.plot.util.UUIDHandler;
import com.github.intellectualsites.plotsquared.plot.util.UpdateUtility;
import com.github.intellectualsites.plotsquared.plot.util.WorldUtil;
import com.github.intellectualsites.plotsquared.plot.util.block.GlobalBlockQueue;
import com.github.intellectualsites.plotsquared.plot.util.expiry.ExpireManager;
@ -89,7 +88,6 @@ import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.UUID;
import java.util.function.Consumer;
@ -127,7 +125,6 @@ import java.util.zip.ZipInputStream;
@Setter @Getter private ILogger logger;
// Platform / Version / Update URL
private PlotVersion version;
@Nullable @Getter private UpdateUtility updateUtility;
// Files and configuration
@Getter private File jarFile = null; // This file
private File storageFile;
@ -1622,13 +1619,13 @@ import java.util.zip.ZipInputStream;
String lastVersionString = this.getConfig().getString("version");
if (lastVersionString != null) {
String[] split = lastVersionString.split("\\.");
int[] lastVersion = new int[] {Integer.parseInt(split[0]), Integer.parseInt(split[1]),
Integer.parseInt(split[2])};
if (checkVersion(new int[] {3, 4, 0}, lastVersion)) {
int[] lastVersion = new int[]{Integer.parseInt(split[0]), Integer.parseInt(split[1]),
Integer.parseInt(split[2])};
if (checkVersion(new int[]{3, 4, 0}, lastVersion)) {
Settings.convertLegacy(configFile);
if (getConfig().contains("worlds")) {
ConfigurationSection worldSection =
getConfig().getConfigurationSection("worlds");
getConfig().getConfigurationSection("worlds");
worlds.set("worlds", worldSection);
try {
worlds.save(worldsFile);
@ -1641,7 +1638,6 @@ import java.util.zip.ZipInputStream;
}
}
Settings.load(configFile);
setupUpdateUtility();
//Sets the version information for the settings.yml file
try (InputStream stream = getClass().getResourceAsStream("/plugin.properties")) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(stream))) {
@ -1650,11 +1646,11 @@ import java.util.zip.ZipInputStream;
String dateString = br.readLine();
this.version = PlotVersion.tryParse(versionString, commitString, dateString);
Settings.DATE =
new Date(100 + version.year, version.month, version.day).toGMTString();
new Date(100 + version.year, version.month, version.day).toGMTString();
Settings.BUILD = "https://ci.athion.net/job/PlotSquared-Releases/" + version.build;
Settings.COMMIT =
"https://github.com/IntellectualSites/PlotSquared/commit/" + Integer
.toHexString(version.hash);
"https://github.com/IntellectualSites/PlotSquared/commit/" + Integer
.toHexString(version.hash);
System.out.println("Version is " + this.version);
}
} catch (IOException throwable) {
@ -1664,28 +1660,6 @@ import java.util.zip.ZipInputStream;
config = YamlConfiguration.loadConfiguration(configFile);
}
private void setupUpdateUtility() {
try {
copyFile("updater.properties", "config");
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
new FileInputStream(new File(new File(this.IMP.getDirectory(), "config"),
"updater.properties"))))) {
final Properties properties = new Properties();
properties.load(bufferedReader);
final boolean enabled =
Boolean.parseBoolean(properties.getOrDefault("enabled", true).toString());
if (enabled) {
this.updateUtility = new UpdateUtility(properties.getProperty("path"),
properties.getProperty("job"), properties.getProperty("artifact"));
}
} catch (final IOException throwable) {
throwable.printStackTrace();
}
} catch (final Throwable throwable) {
throwable.printStackTrace();
}
}
/**
* Setup all configuration files<br>
* - Config: settings.yml<br>

View File

@ -1,10 +1,8 @@
package com.github.intellectualsites.plotsquared.plot.commands;
import com.github.intellectualsites.plotsquared.commands.CommandDeclaration;
import com.github.intellectualsites.plotsquared.json.JSONObject;
import com.github.intellectualsites.plotsquared.plot.PlotSquared;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.util.HttpUtil;
import com.github.intellectualsites.plotsquared.plot.util.MainUtil;
import com.github.intellectualsites.plotsquared.plot.util.TaskManager;
@ -22,17 +20,8 @@ import com.github.intellectualsites.plotsquared.plot.util.TaskManager;
"$2>> $1&lAuthors$2: $1Citymonstret $2& $1Empire92 $2& $1MattBDev $2& $1dordsor21 $2& $1NotMyFault");
MainUtil.sendMessage(player,
"$2>> $1&lWiki$2: $1https://github.com/IntellectualSites/PlotSquared/wiki");
// MainUtil.sendMessage(player,
// "$2>> $1&lNewest Version$2: $1" + getNewestVersionString());
}
});
return true;
}
public String getNewestVersionString() {
String str = HttpUtil
.readUrl("https://api.github.com/repos/IntellectualSites/PlotSquared/releases/latest");
JSONObject release = new JSONObject(str);
return release.getString("name");
}
}

View File

@ -132,6 +132,9 @@ public class Settings extends Config {
// Titles
TITLES = config.getBoolean("titles", TITLES);
// Update Notifications
Enabled_Components.UPDATE_NOTIFICATIONS = config.getBoolean("update-notifications", Enabled_Components.UPDATE_NOTIFICATIONS);
// Teleportation
Teleport.DELAY = config.getInt("teleport.delay", Teleport.DELAY);
Teleport.ON_LOGIN = config.getBoolean("teleport.on_login", Teleport.ON_LOGIN);
@ -358,5 +361,6 @@ public class Settings extends Config {
@Comment("Delete plots when a player is banned") public static boolean BAN_DELETER = false;
@Comment({"Prevent possibly unsafe blocks from being used in plot components", "Can be bypassed with `/plot debugallowunsafe`"})
public static boolean PREVENT_UNSAFE = true;
@Comment("Whether we should notify you about updates or not.") public static boolean UPDATE_NOTIFICATIONS = true;
}
}

View File

@ -1,157 +0,0 @@
package com.github.intellectualsites.plotsquared.plot.util;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.incendo.jenkins.Jenkins;
import org.incendo.jenkins.objects.ArtifactDescription;
import org.incendo.jenkins.objects.BuildInfo;
import java.util.Collection;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class UpdateUtility {
private final String jobName;
private final Pattern artifactPattern;
private final Jenkins jenkins;
public UpdateUtility(@NonNull final String jenkinsPath, @NonNull final String jobName,
@NonNull final String artifactPattern) {
this.jobName = jobName;
this.artifactPattern = Pattern.compile(artifactPattern);
this.jenkins = Jenkins.newBuilder().withPath(jenkinsPath).build();
}
private void fetchLatestBuildInfo(final BiConsumer<BuildInfo, Throwable> whenDone) {
this.jenkins.getJobInfo(jobName).whenCompleteAsync((jobInfo, exception) -> {
if (jobInfo == null && exception != null) {
whenDone.accept(null, exception);
} else if (jobInfo != null) {
jobInfo.getLastSuccessfulBuild().getBuildInfo().whenComplete(whenDone);
} else {
whenDone.accept(null, new IllegalStateException(
String.format("Could not fetch job info for job %s", this.jobName)));
}
});
}
private void getMatchingArtifact(final BiConsumer<ArtifactDescription, Throwable> whenDone) {
this.fetchLatestBuildInfo((buildInfo, throwable) -> {
if (throwable != null) {
whenDone.accept(null, throwable);
} else {
final Collection<ArtifactDescription> artifacts = buildInfo.getArtifacts();
final Optional<ArtifactDescription> artifact =
artifacts.stream().filter(artifactDescription -> {
final String name = artifactDescription.getFileName();
final Matcher matcher = artifactPattern.matcher(name);
return matcher.matches();
}).findAny();
if (artifact.isPresent()) {
final ArtifactDescription artifactDescription = artifact.get();
whenDone.accept(artifactDescription, null);
} else {
whenDone.accept(null, new NullPointerException(String
.format("Could not find any matching artifacts in build %d",
buildInfo.getId())));
}
}
});
}
public void checkForUpdate(final String currentVersion,
final BiConsumer<UpdateDescription, Throwable> whenDone) {
this.getMatchingArtifact(((artifactDescription, throwable) -> {
if (throwable != null) {
whenDone.accept(null, new RuntimeException(String
.format("Failed to read artifact description: %s", throwable.getMessage()),
throwable));
} else {
try {
final String version = this.isNewer(currentVersion, artifactDescription);
if (version != null) {
whenDone
.accept(new UpdateDescription(version, artifactDescription.getUrl()),
null);
} else {
whenDone.accept(null, null);
}
} catch (final Throwable exception) {
whenDone.accept(null, new RuntimeException(
String.format("Failed to compare versions: %s", exception.getMessage()),
exception));
}
}
}));
}
private String isNewer(@NonNull final String currentVersion,
@NonNull final ArtifactDescription artifact) {
final Matcher matcher = artifactPattern.matcher(artifact.getFileName());
if (!matcher.matches()) {
throw new IllegalArgumentException(
"Artifact file name does not match artifact pattern");
}
final String version = matcher.group("version");
if (version == null) {
throw new IllegalArgumentException("Given artifact does not contain version");
}
return compareVersions(currentVersion, version) < 0 ? version : null;
}
/**
* Compare two given versions in the format $major.$minor
*
* @param oldVersion current version
* @param newVersion other version
* @return -1 if the current version is older, 1 is the versions are the same,
* and 1 if the current version is newer
*/
private int compareVersions(@NonNull final String oldVersion,
@NonNull final String newVersion) {
// Versions look this this: major.minor :P
final int[] oldNums = splitVersion(oldVersion);
final int[] newNums = splitVersion(newVersion);
if (oldNums == null || newNums == null) {
throw new IllegalArgumentException("Could not extract version data");
}
// Compare major version
if (oldNums[0] != -1 && newNums[0] != -1) {
if (oldNums[0] < newNums[0]) {
return -1;
} else if (oldNums[0] > newNums[0]) {
return 1;
}
}
// Compare minor versions
return Integer.compare(oldNums[1], newNums[1]);
}
private int[] splitVersion(@NonNull final String versionString) {
final String[] parts = versionString.split("\\.");
switch (parts.length) {
case 0:
return new int[] {-1, -1};
case 1:
return new int[] {-1, Integer.parseInt(parts[0])};
case 2:
return new int[] {Integer.parseInt(parts[0]), Integer.parseInt(parts[1])};
default:
return null;
}
}
@Getter @RequiredArgsConstructor public static class UpdateDescription {
private final String version;
private final String url;
}
}

View File

@ -1,8 +0,0 @@
# Whether update notifications are enabled
enabled=true
# CI path
path=https://ci.athion.net/
# Job name
job=PlotSquared-Releases
# Artifact pattern
artifact=^PlotSquared-Bukkit-(?<version>[0-9.]+).jar$