diff --git a/pom.xml b/pom.xml index a7964b0..fc87ab0 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.jeff_media SpigotUpdateChecker - 3.0.3 + 3.0.4 jar @@ -63,7 +63,7 @@ org.apache.maven.plugins maven-shade-plugin - 3.2.4 + 3.5.0 true @@ -82,6 +82,17 @@ + + + com.github.Anon8281:UniversalScheduler + + + + + com.github.Anon8281.universalScheduler + com.jeff_media.updatechecker.universalScheduler + + @@ -146,6 +157,10 @@ spigot-repo https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + jitpack.io + https://jitpack.io + @@ -167,6 +182,12 @@ 23.0.0 provided + + com.github.Anon8281 + UniversalScheduler + 0.1.6 + compile + diff --git a/src/main/java/com/jeff_media/updatechecker/UpdateChecker.java b/src/main/java/com/jeff_media/updatechecker/UpdateChecker.java index 501c6fb..fe80785 100644 --- a/src/main/java/com/jeff_media/updatechecker/UpdateChecker.java +++ b/src/main/java/com/jeff_media/updatechecker/UpdateChecker.java @@ -18,6 +18,9 @@ package com.jeff_media.updatechecker; +import com.github.Anon8281.universalScheduler.UniversalScheduler; +import com.github.Anon8281.universalScheduler.scheduling.schedulers.TaskScheduler; +import com.github.Anon8281.universalScheduler.scheduling.tasks.MyScheduledTask; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.plugin.Plugin; @@ -41,7 +44,7 @@ @SuppressWarnings("UnusedReturnValue") public class UpdateChecker { - static final String VERSION = "3.0.1"; + static final String VERSION = "3.0.4"; private static final String SPIGOT_CHANGELOG_SUFFIX = "/history"; private static final String SPIGOT_DOWNLOAD_LINK = "https://www.spigotmc.org/resources/"; private static final String SPIGOT_UPDATE_API = "https://api.spigotmc.org/simple/0.2/index.php?action=getResource&id=%s"; @@ -81,7 +84,9 @@ public class UpdateChecker { private BiConsumer onSuccess = (requesters, latestVersion) -> { }; private String paidDownloadLink = null; - private int taskId = -1; + private static TaskScheduler scheduler; + @Nullable + private MyScheduledTask updaterTask = null; private int timeout = 0; private String usedVersion; private String userAgentString = null; @@ -107,7 +112,6 @@ public UpdateChecker(@NotNull JavaPlugin plugin, @NotNull VersionSupplier suppli } private void init() { - Objects.requireNonNull(plugin, "Plugin cannot be null."); this.usedVersion = plugin.getDescription().getVersion().trim(); @@ -116,6 +120,8 @@ private void init() { usingPaidVersion = true; } + scheduler = UniversalScheduler.getScheduler(plugin); + if (!listenerAlreadyRegistered) { Bukkit.getPluginManager().registerEvents(new UpdateCheckListener(), plugin); listenerAlreadyRegistered = true; @@ -280,9 +286,9 @@ public UpdateChecker checkEveryXHours(double hours) { long ticks = ((int) seconds) * 20L; stop(); if (ticks > 0) { - taskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, () -> checkNow(Bukkit.getConsoleSender()), ticks, ticks); + updaterTask = getScheduler().runTaskTimer(() -> checkNow(Bukkit.getConsoleSender()), ticks, ticks); } else { - taskId = -1; + updaterTask = null; } return this; } @@ -293,10 +299,10 @@ public UpdateChecker checkEveryXHours(double hours) { * its previous task. */ public UpdateChecker stop() { - if (taskId != -1) { - Bukkit.getScheduler().cancelTask(taskId); + if (updaterTask != null) { + updaterTask.cancel(); } - taskId = -1; + updaterTask = null; return this; } @@ -320,7 +326,7 @@ public UpdateChecker checkNow(@Nullable CommandSender... requesters) { userAgentString = UserAgentBuilder.getDefaultUserAgent().build(); } - Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { + getScheduler().runTaskAsynchronously(() -> { UpdateCheckEvent updateCheckEvent; @@ -345,12 +351,12 @@ public UpdateChecker checkNow(@Nullable CommandSender... requesters) { updateCheckEvent = new UpdateCheckEvent(UpdateCheckSuccess.SUCCESS); } catch (final IOException exception) { updateCheckEvent = new UpdateCheckEvent(UpdateCheckSuccess.FAIL); - Bukkit.getScheduler().runTask(plugin, () -> getOnFail().accept(requesters, exception)); + getScheduler().runTask(() -> getOnFail().accept(requesters, exception)); } UpdateCheckEvent finalUpdateCheckEvent = updateCheckEvent.setRequesters(requesters); - Bukkit.getScheduler().runTask(plugin, () -> { + getScheduler().runTask(() -> { if (finalUpdateCheckEvent.getSuccess() == UpdateCheckSuccess.SUCCESS) { getOnSuccess().accept(requesters, latestVersion); @@ -616,6 +622,15 @@ protected Plugin getPlugin() { return plugin; } + /** + * Gets the TaskScheduler instance used by the UpdateChecker + * + * @return TaskScheduler instance used by the UpdateChecker + */ + public static TaskScheduler getScheduler() { + return scheduler; + } + /** * Gets the Spigot User ID of the user who downloaded the plugin if it's a premium plugin, otherwise "%%__USER__%%" *