Class UpdateChecker

java.lang.Object
org.kingdoms.utils.network.UpdateChecker
All Implemented Interfaces:
org.bukkit.event.Listener

public final class UpdateChecker extends Object implements org.bukkit.event.Listener
A simple async update checker that uses SpigotMC official General API. This API can also accept API keys, but this program does not use any.

Before using this class, you'll have to invoke the checkForUpdates() method at least once. The method can be invoked multiple times if you want to recheck for updates.

The automatic downloader uses SpiGet API. You can only download the plugin if it's a free plugin. Premium resources are not supported and will not be supported as it needs the user's information to access the account, which is something that you shouldn't really bother doing.
Also it's not possible to update the plugin automatically using the downloaded JAR file. The owner must manually change it unless you make another plugin that handles your updates, which is not recommended.

No schedulers or async tasks need to be performed for any of the methods. They're already handled by CompletableFuture.

Example:
The simplest and most common way of doing this in your main class is:

      UpdateChecker updater = new UpdateChecker(this, $RESOURCE_ID);
      updater.checkForUpdates()
         .thenRunAsync(updater::sendUpdates) // notify console
         .thenRunAsync(updater::downloadUpdate); // Only for free plugins - not really recommended.
 

To get your $RESOURCE_ID you need to go to your plugin's Spigot page and the link should look like this https://www.spigotmc.org/resources/PLUGIN-NAME.$RESOURCE_ID/

  • Constructor Details

    • UpdateChecker

      public UpdateChecker()
  • Method Details

    • getCurrentVersion

      public static String getCurrentVersion()
    • getLastHttpResponseCode

      public static int getLastHttpResponseCode()
      Gets the last HTTP response code received from fetchLatestVersion() ()} A list of response codes with names can be found in HttpURLConnection Any other codes other than HttpURLConnection.HTTP_OK is not normal.
      Returns:
      the last HTTP response code received while getting the version.
      Since:
      1.0.0
    • checkForUpdates

      public static CompletableFuture<Void> checkForUpdates()
      Initialization - Can be called multiple times.
      A simple of waiting and preparing for the next action is to use CompletableFuture.thenRunAsync(Runnable)
      Since:
      1.0.0
      See Also:
    • canUpdate

      public static boolean canUpdate()
      You need to run checkForUpdates() first to use this.
      Returns:
      true if there's a new version available and you should update, otherwise false.
      Since:
      1.0.0
      See Also:
    • getLatestVersion

      public static String getLatestVersion()
      You need to run checkForUpdates() first to use this.
      Returns:
      the latest version that was obtained from fetchLatestVersion() ()}
      Since:
      1.0.0
      See Also:
    • sendUpdates

      public static void sendUpdates()
      Sends plugin updates to console.
      Since:
      1.0.0
    • onJoinNotify

      public static void onJoinNotify(org.bukkit.event.player.PlayerJoinEvent event)
      Notify admins about updates when they join the server.
      Since:
      1.0.0
    • updateText

      public static String updateText()
      Returns:
      the update text used when an update is available.
      Since:
      1.0.0
    • fetchLatestVersionLegacy

      @NotNull public static @NotNull CompletableFuture<String> fetchLatestVersionLegacy()
      Gets the version of a Spigot plugin resource asynchronous.
      Returns:
      the plugin's version or null if no response or empty version string.
      Since:
      1.0.0
      See Also:
    • fetchLatestVersion

      public static CompletableFuture<String> fetchLatestVersion()
    • downloadUpdate

      public static CompletableFuture<Void> downloadUpdate()
      Downloads any available update to plugin's data folder using SpiGet API asynchronous.
      Only works with free plugins. Don't waste your time making one for premium ones. Uses HTTP request.
      Returns:
      a future task which you don't really need to do anything with it.
      Since:
      1.0.0
    • isHigher

      public static boolean isHigher(String currentVer, @Nullable @Nullable String newVer)
      Compares two program versions.
      This also supports BETA and SNAPSHOT suffixes. Note that these versions are ignored as a new update. If you want to change this, you have to refer to the prereleases region inside the method.
      It also supports multiple version schemes such as 1.0.0 or 1.0.2.0.5.7b


      Examples

           2.0.0 > 1.0.0, 2.2.0 > 2.0.0, 2.2.2 > 2.2.0
           1.0.0.0.0 > 1.0.0 (Not really, but still...)
           1.0.0 = 1.0.0-beta = 1.0.0-SNAPSHOT = 1.0.0b = 1.0.0a
       
      Parameters:
      newVer - the new version.
      Returns:
      true if the newVer is higher than oldVer, otherwise false.
      Since:
      1.0.0
    • getJarName

      public static String getJarName()
    • getDownloadPath

      public static Path getDownloadPath()
    • downloadRepositoryToConfiguredPath

      public static CompletableFuture<Void> downloadRepositoryToConfiguredPath()
    • downloadRepository

      @Nonnull public static CompletableFuture<Void> downloadRepository(Path to)