|
| 1 | +package com.kamesuta.bungeepteropower; |
| 2 | + |
| 3 | +import javax.annotation.Nullable; |
| 4 | +import java.net.URI; |
| 5 | +import java.net.http.HttpClient; |
| 6 | +import java.net.http.HttpRequest; |
| 7 | +import java.net.http.HttpResponse; |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.Comparator; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Objects; |
| 12 | +import java.util.concurrent.CompletableFuture; |
| 13 | +import java.util.stream.Collectors; |
| 14 | + |
| 15 | +/** |
| 16 | + * Checks for updates via SpigotMC API |
| 17 | + */ |
| 18 | +public class UpdateChecker { |
| 19 | + /** |
| 20 | + * The resource ID of this plugin on SpigotMC |
| 21 | + */ |
| 22 | + private static final int ResourceId = 114883; |
| 23 | + /** |
| 24 | + * The current version |
| 25 | + */ |
| 26 | + private final String runningVersion; |
| 27 | + /** |
| 28 | + * The new version |
| 29 | + */ |
| 30 | + private String newVersion; |
| 31 | + |
| 32 | + /** |
| 33 | + * Create a new update checker |
| 34 | + * |
| 35 | + * @param runningVersion The current version |
| 36 | + */ |
| 37 | + public UpdateChecker(String runningVersion) { |
| 38 | + this.runningVersion = runningVersion; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Check for updates |
| 43 | + * |
| 44 | + * @return A future that completes when the update check is finished |
| 45 | + */ |
| 46 | + public CompletableFuture<Void> checkForUpdates() { |
| 47 | + // Check for updates via SpigotMC API |
| 48 | + HttpClient client = HttpClient.newHttpClient(); |
| 49 | + // Create a request |
| 50 | + HttpRequest request = HttpRequest.newBuilder() |
| 51 | + .uri(URI.create("https://api.spigotmc.org/legacy/update.php?resource=" + ResourceId)) |
| 52 | + .build(); |
| 53 | + |
| 54 | + // Execute request and register a callback |
| 55 | + return client.sendAsync(request, HttpResponse.BodyHandlers.ofString()) |
| 56 | + .thenApply(HttpResponse::body) |
| 57 | + .thenAccept(version -> { |
| 58 | + newVersion = version; |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Returns whether an update is available |
| 64 | + * |
| 65 | + * @return true if an update is available |
| 66 | + */ |
| 67 | + public boolean isUpdateAvailable() { |
| 68 | + if (newVersion == null) { |
| 69 | + // If the update check has not been completed, assume there is no update |
| 70 | + return false; |
| 71 | + } |
| 72 | + // Compare the versions |
| 73 | + return compareVersions(runningVersion, newVersion); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Get download link |
| 78 | + * |
| 79 | + * @return the download link of the new version |
| 80 | + */ |
| 81 | + public String getDownloadLink() { |
| 82 | + return "https://www.spigotmc.org/resources/" + ResourceId; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Get the current version |
| 87 | + * |
| 88 | + * @return the current version |
| 89 | + */ |
| 90 | + public String getRunningVersion() { |
| 91 | + return runningVersion; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Get the new version |
| 96 | + * |
| 97 | + * @return the new version |
| 98 | + */ |
| 99 | + public @Nullable String getNewVersion() { |
| 100 | + return newVersion; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Compares two versions and returns whether the new version is newer than the current version |
| 105 | + * |
| 106 | + * @param runningVersion The current version |
| 107 | + * @param newVersion The new version |
| 108 | + * @return true if the new version is newer than the current version |
| 109 | + */ |
| 110 | + public static boolean compareVersions(String runningVersion, String newVersion) { |
| 111 | + // Split the running versions into integers |
| 112 | + List<Integer> current = Arrays.stream(runningVersion.split("\\.")) |
| 113 | + .map(Integer::valueOf) |
| 114 | + .collect(Collectors.toList()); |
| 115 | + // Split the new versions into integers |
| 116 | + List<Integer> latest = Arrays.stream(newVersion.split("\\.")) |
| 117 | + .map(Integer::valueOf) |
| 118 | + .collect(Collectors.toList()); |
| 119 | + // Compare the versions |
| 120 | + int comparison = Objects.compare(current, latest, Comparator.<List<Integer>>comparingInt(List::size).thenComparing(List::hashCode)); |
| 121 | + return comparison < 0; |
| 122 | + } |
| 123 | +} |
0 commit comments