|
| 1 | +package com.dev7ex.multiworld.util; |
| 2 | + |
| 3 | +import com.dev7ex.common.bukkit.plugin.BukkitPlugin; |
| 4 | +import lombok.AccessLevel; |
| 5 | +import lombok.Getter; |
| 6 | +import org.bukkit.util.Consumer; |
| 7 | +import org.jetbrains.annotations.NotNull; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.io.InputStream; |
| 11 | +import java.net.URL; |
| 12 | +import java.util.Scanner; |
| 13 | + |
| 14 | +/** |
| 15 | + * @author Dev7ex |
| 16 | + * @since 05.11.2024 |
| 17 | + */ |
| 18 | +@Getter(AccessLevel.PUBLIC) |
| 19 | +public class PluginUpdater { |
| 20 | + |
| 21 | + private final BukkitPlugin plugin; |
| 22 | + private boolean updateAvailable = false; |
| 23 | + private String newVersion; |
| 24 | + |
| 25 | + public PluginUpdater(@NotNull final BukkitPlugin plugin) { |
| 26 | + this.plugin = plugin; |
| 27 | + } |
| 28 | + |
| 29 | + public void checkAsync() { |
| 30 | + this.plugin.getServer().getScheduler().runTaskAsynchronously(this.plugin, () -> { |
| 31 | + try (final InputStream inputStream = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + this.plugin.getPluginIdentification().spigotResourceId()).openStream()) { |
| 32 | + try (final Scanner scanner = new Scanner(inputStream)) { |
| 33 | + final String currentVersion = this.plugin.getDescription().getVersion(); |
| 34 | + this.newVersion = scanner.next(); |
| 35 | + |
| 36 | + if (!currentVersion.equalsIgnoreCase(this.newVersion)) { |
| 37 | + if (this.compareVersions(currentVersion, this.newVersion) == -1) { |
| 38 | + this.updateAvailable = true; |
| 39 | + this.plugin.getServer().getScheduler().runTask(this.plugin, this::logUpdateMessage); |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + } catch (final IOException exception) { |
| 45 | + this.updateAvailable = false; |
| 46 | + this.plugin.getServer().getScheduler().runTask(this.plugin, this::logNoUpdateMessage); |
| 47 | + } |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Compares two versions in the format x.y.z-SNAPSHOT. |
| 53 | + * |
| 54 | + * @param currentVersion The current version of the plugin. |
| 55 | + * @param onlineVersion The latest available version online. |
| 56 | + * @return -1 if the current version is older than the online version, |
| 57 | + * 1 if the current version is newer, |
| 58 | + * 0 if both versions are equal. |
| 59 | + */ |
| 60 | + public int compareVersions(@NotNull final String currentVersion, @NotNull final String onlineVersion) { |
| 61 | + final String[] currentParts = currentVersion.replace("-SNAPSHOT", "").split("\\."); |
| 62 | + final String[] onlineParts = onlineVersion.replace("-SNAPSHOT", "").split("\\."); |
| 63 | + |
| 64 | + for (int i = 0; i < Math.max(currentParts.length, onlineParts.length); i++) { |
| 65 | + int currentPart = (i < currentParts.length) ? Integer.parseInt(currentParts[i]) : 0; |
| 66 | + int onlinePart = (i < onlineParts.length) ? Integer.parseInt(onlineParts[i]) : 0; |
| 67 | + |
| 68 | + if (currentPart < onlinePart) { |
| 69 | + return -1; // current version is older |
| 70 | + } else if (currentPart > onlinePart) { |
| 71 | + return 1; // current version is newer |
| 72 | + } |
| 73 | + } |
| 74 | + return 0; // versions are equal |
| 75 | + } |
| 76 | + |
| 77 | + public void logUpdateMessage() { |
| 78 | + this.plugin.getLogger().info(""); |
| 79 | + this.plugin.getLogger().info("There is a new update for MultiWorld"); |
| 80 | + this.plugin.getLogger().info("Please note that if you do not update, some functions may be deactivated."); |
| 81 | + this.plugin.getLogger().info("Current Version: " + this.plugin.getDescription().getVersion()); |
| 82 | + this.plugin.getLogger().info("New Version: " + this.newVersion); |
| 83 | + this.plugin.getLogger().info(""); |
| 84 | + } |
| 85 | + |
| 86 | + public void logNoUpdateMessage() { |
| 87 | + this.plugin.getLogger().info(""); |
| 88 | + this.plugin.getLogger().info("Your MultiWorld version is up to date!"); |
| 89 | + this.plugin.getLogger().info("Version: " + this.plugin.getDescription().getVersion()); |
| 90 | + this.plugin.getLogger().info(""); |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments