Class UpdateChecker

java.lang.Object
com.jeff_media.updatechecker.UpdateChecker

public class UpdateChecker extends Object
Main class. Automatically checks for updates.
  • Constructor Details

    • UpdateChecker

      public UpdateChecker(@NotNull org.bukkit.plugin.java.JavaPlugin plugin, @NotNull VersionSupplier supplier)
      Initializes an UpdateChecker instance with a custom VersionSupplier.
      Parameters:
      plugin - Instance of your plugin
      supplier - VersionSupplier that supplies the latest version of your plugin
    • UpdateChecker

      public UpdateChecker(@NotNull org.bukkit.plugin.java.JavaPlugin plugin, @NotNull UpdateCheckSource updateCheckSource, @NotNull String parameter)
      Initializes an UpdateChecker instance.
      Parameters:
      plugin - Instance of your plugin
      updateCheckSource - Source where to check for updates. To use a custom source, see UpdateChecker(JavaPlugin, VersionSupplier)
      parameter - Parameter for the update checker source. See UpdateCheckSource for more informatino
  • Method Details

    • getInstance

      @Deprecated public static UpdateChecker getInstance()
      Deprecated.
      As of SpigotUpdateChecker 1.4.0, more than one instance can exist at the same time. Keep track of the instances you created yourself.
      Gets the current UpdateChecker singleton if it has been created, otherwise null.
      Returns:
      UpdateChecker instance being ran, or null if UpdateChecker(JavaPlugin, UpdateCheckSource, String) wasn't called yet.
    • init

      @Deprecated public static UpdateChecker init(@NotNull org.bukkit.plugin.java.JavaPlugin plugin, int spigotResourceId)
      Initializes the UpdateChecker instance. HAS to be called before the UpdateChecker can run.
      Parameters:
      plugin - Main class of your plugin
      spigotResourceId - SpigotMC Resource ID to get the latest version String from the SpigotMC Web API
      Returns:
      UpdateChecker instance being ran
    • init

      @Deprecated public static UpdateChecker init(@NotNull org.bukkit.plugin.java.JavaPlugin plugin, @NotNull String apiLink)
      Initializes the UpdateChecker instance. HAS to be called before the UpdateChecker can run.
      Parameters:
      plugin - Main class of your plugin
      apiLink - HTTP(S) link to a file containing a string with the latest version of your plugin.
      Returns:
      UpdateChecker instance being ran
    • isSuppressUpToDateMessage

      public boolean isSuppressUpToDateMessage()
      Returns whether the message "You are using the latest version of " will be suppressed.
      Returns:
      True when the message will be suppressed, otherwise false
    • checkEveryXHours

      public UpdateChecker checkEveryXHours(double hours)
      Starts to check every X hours for updates. If a task is already running, it gets cancelled and replaced with the new one, so don't be afraid to use this in your reload function. The first check will also happen after X hours so you might want to call checkNow() too. When you set notifyRequesters to true (default), the Console will get a notification about the check result.
      Parameters:
      hours - Amount of hours in between checks
      Returns:
      UpdateChecker instance being ran
    • stop

      public UpdateChecker stop()
      Stops the scheduled update checks. THIS IS NOT NEEDED when calling checkEveryXHours(double) again, as the UpdateChecker will automatically stop its previous task.
    • checkNow

      public UpdateChecker checkNow(@Nullable org.bukkit.command.CommandSender... requesters)
      Checks for updates now and sends the result to the given list of CommandSenders. Can be null to silently check for updates.
      Parameters:
      requesters - CommandSenders to send the result to, or null
    • isUsingLatestVersion

      public boolean isUsingLatestVersion()
      Checks whether the latest found version of the plugin is being used.
      Returns:
      true if the latest found version is the one currently in use, otherwise false
    • isOtherVersionNewer

      public static boolean isOtherVersionNewer(String myVersion, String otherVersion)
      Checks whether one version is really newer than another according to the semantic versioning scheme, including letters.
      Parameters:
      myVersion - One version string
      otherVersion - Another version string
      Returns:
      true if the other version is indeed newer, otherwise false
    • getOnFail

      public BiConsumer<org.bukkit.command.CommandSender[],Exception> getOnFail()
      Gets the task that will run when/after the update check fails.
      Returns:
      Task that will run when/after the update check fails.
    • getOnSuccess

      public BiConsumer<org.bukkit.command.CommandSender[],String> getOnSuccess()
      Gets the task that will run when/after the update check succeeds.
      Returns:
      Task that will run when/after the update check succeeds.
    • checkNow

      public UpdateChecker checkNow()
      Checks for updates now and sends the result to the console when notifyRequesters is set to true (default)
    • getAppropriateDownloadLinks

      public List<String> getAppropriateDownloadLinks()
      Returns a list of applicable Download links.

      If using the free version and there are links for the free and paid version, element 0 will be the link to the paid version and element will be the link to the free version

      If using the paid version, there will be only one element containing the link to the paid version, or, if that is not set, the link to the free version.

      If there is no paid version, there will be only one element containing the link to the free version, or, if that is not set, the link to the plus version.

      If no download links are set, returns an empty list.

      Returns:
      List of zero to two download links. If the list contains two links, the first element is the paid download link.
    • getChangelogLink

      public String getChangelogLink()
      Returns the changelog link
      Returns:
      Changelog Link
    • setChangelogLink

      public UpdateChecker setChangelogLink(int resourceId)
      Sets a link to your plugin's changelog generated from your plugin's SpigotMC/Polymart Resource ID
      Parameters:
      resourceId - Spigot/Polymart Resource ID
      Returns:
      UpdateChecker instance being ran
    • setChangelogLink

      public UpdateChecker setChangelogLink(@Nullable String link)
      Sets a link to your plugin's changelog.
      Parameters:
      link - Changelog link
      Returns:
      UpdateChecker instance being ran
    • getSupportLink

      @Nullable public String getSupportLink()
      Returns the support link
      Returns:
      Support Link
    • setSupportLink

      @NotNull public UpdateChecker setSupportLink(@Nullable String link)
      Sets a link to your plugin's support channel.
      Parameters:
      link - Support link
      Returns:
      UpdateChecker instance being ran
    • getDonationLink

      public String getDonationLink()
      Returns the donation link
      Returns:
      Donation link
    • setDonationLink

      public UpdateChecker setDonationLink(@Nullable String donationLink)
      Sets a link to your plugin's donation website
      Parameters:
      donationLink - Donation link
      Returns:
      UpdateChecker instance being ran
    • getLastCheckResult

      public UpdateCheckResult getLastCheckResult()
      Returns the last successful UpdateCheckResult
      Returns:
      Last successful UpdateCheckResult
    • getLatestVersion

      public String getLatestVersion()
      Returns the latest version string found by the UpdateChecker, or null if all checks until yet have failed.
      Returns:
      Latest version string found by the UpdateChecker
    • getNameFreeVersion

      public String getNameFreeVersion()
      Returns the name/suffix of the free plugin version
      Returns:
      Name/suffix of the free plugin version
    • setNameFreeVersion

      public UpdateChecker setNameFreeVersion(String nameFreeVersion)
      Sets the name/suffix for the free version's name. E.g. when you set this to "Free", the Download link for the free version will be shown as "Download (Free): [Link]"
      Parameters:
      nameFreeVersion - Name/suffix of the free plugin version
      Returns:
      UpdateChecker instance being ran
    • getNamePaidVersion

      public String getNamePaidVersion()
      Returns the name/suffix of the paid plugin version
      Returns:
      Name/suffix of the paid plugin version
    • setNamePaidVersion

      public UpdateChecker setNamePaidVersion(String namePaidVersion)
      Sets the name/suffix for the paid version's name. E.g. when you set this to "Platinum version", the Download link for the paid version will be shown as "Download (Platinum version): [Link]"
      Parameters:
      namePaidVersion - Name/suffix of the paid plugin version
      Returns:
      UpdateChecker instance being ran
    • getNotifyPermission

      @Nullable public String getNotifyPermission()
      Returns the permission required to receive UpdateChecker messages on join
      Returns:
      Permission required to receive UpdateChecker messages on join, or null if not set
    • getPlugin

      protected org.bukkit.plugin.Plugin getPlugin()
      Gets the plugin that instantiated this UpdateChecker instance
      Returns:
      Plugin that instantiated this UpdateChecker instance
    • getSpigotUserId

      public String getSpigotUserId()
      Gets the Spigot User ID of the user who downloaded the plugin if it's a premium plugin, otherwise "%%__USER__%%"
      Returns:
      Spigot User ID of the user who downloaded the plugin if it's a premium plugin, otherwise "%%__USER__%%"
    • getUsedVersion

      public String getUsedVersion()
      Gets the version string of the currently used plugin version
      Returns:
      Version string of the currently used plugin version
    • setUsedVersion

      public UpdateChecker setUsedVersion(String usedVersion)
      Sets the version string of the currently used plugin version. By default, this is the version defined in the plugin.yml file.
      Parameters:
      usedVersion - new version string
    • isCheckedAtLeastOnce

      public boolean isCheckedAtLeastOnce()
      Checks whether the update checker already ran.
      Returns:
      True when the update checker already ran, otherwise false
    • isColoredConsoleOutput

      public boolean isColoredConsoleOutput()
      Returns whether colored console output is enabled
      Returns:
      true when colored console output is enabled, otherwise false
    • setColoredConsoleOutput

      public UpdateChecker setColoredConsoleOutput(boolean coloredConsoleOutput)
      Sets whether or not the used and latest version will be displayed in color in the console
      Parameters:
      coloredConsoleOutput - Whether to use color in the console output
      Returns:
      UpdateChecker instance being ran
    • isNotifyOpsOnJoin

      public boolean isNotifyOpsOnJoin()
      Returns whether OPs will be notified on join when a new version is available
      Returns:
      true when OPs will be notified on join when a new version is available, otherwise false
    • setNotifyOpsOnJoin

      public UpdateChecker setNotifyOpsOnJoin(boolean notifyOpsOnJoin)
      Whether or not to inform OPs on join when there is a new version available.
      Parameters:
      notifyOpsOnJoin - Whether to inform on OPs on join when there is a new version available
      Returns:
      UpdateChecker instance being ran
    • isNotifyRequesters

      public boolean isNotifyRequesters()
      Gets whether the given CommandSenders will be informed about UpdateChecker results
      Returns:
      Whether or not to inform given CommandSenders about UpdateChecker results
    • setNotifyRequesters

      public UpdateChecker setNotifyRequesters(boolean notify)
      Whether or not CommandSenders who request an update check will be notified of the result. When you use your own tasks using onSuccess and onFail, consider setting this to false.
      Parameters:
      notify - Whether or not to notify given CommandSenders about UpdateChecker results
      Returns:
      true when CommandSenders will be notified, otherwise false
    • isUsingPaidVersion

      public boolean isUsingPaidVersion()
      Returns whether the paid version of the plugin is installed.
      Returns:
      True if the paid version is used, otherwise false
    • setUsingPaidVersion

      public UpdateChecker setUsingPaidVersion(boolean paidVersion)
      Tells the UpdateChecker whether the server already uses the paid version of your plugin. If yes, the downloads to the free version are not shown. You can ignore this if you only offer one version of your plugin. When this value is not set, the Update Checker automatically sets this to true by checking the %%__USER__%% placeholder, see https://www.spigotmc.org/wiki/premium-resource-placeholders-identifiers/
      Parameters:
      paidVersion - Whether or not the user is using the paid version of your plugin
      Returns:
      UpdateChecker instance being ran
    • onFail

      public UpdateChecker onFail(BiConsumer<org.bukkit.command.CommandSender[],Exception> onFail)
      Sets a task that will run when/after the update check has failed.
      Parameters:
      onFail - Task that will run when/after the update check has failed.
      Returns:
      UpdateChecker instance being ran
    • onSuccess

      public UpdateChecker onSuccess(BiConsumer<org.bukkit.command.CommandSender[],String> onSuccess)
      Sets a task that will run when/after the update check has succeeded.
      Parameters:
      onSuccess - Task that will run when/after the update check has succeeded.
      Returns:
      UpdateChecker instance being ran
    • setDownloadLink

      public UpdateChecker setDownloadLink(int resourceId)
      Sets the download link for your plugin generated from your plugin's SpigotMC/Polymart Resource ID. Use this if there is only one version of your plugin, either only a free or only a paid version.
      Parameters:
      resourceId - Spigot/Polymart Resource ID
      Returns:
      UpdateChecker instance being ran
    • setDownloadLink

      public UpdateChecker setDownloadLink(@Nullable String downloadLink)
      Sets the download link for your plugin. Use this if there is only one version of your plugin, either only a free or only a paid version.
      Parameters:
      downloadLink - Download link
      Returns:
      UpdateChecker instance being ran
    • suppressUpToDateMessage

      public UpdateChecker suppressUpToDateMessage(boolean suppress)
      Sets whether the message "You are using the latest version of " should be suppressed. Defaults to false
      Parameters:
      suppress - Whether to suppress the message "You are using the latest version of "
      Returns:
      UpdateChecker instance being ran
    • setFreeDownloadLink

      public UpdateChecker setFreeDownloadLink(int resourceId)
      Sets the download link for the free version of your plugin generated from your plugin's SpigotMC/Polymart Resource ID. Use this if there is both, a free and a paid version of your plugin available.
      Parameters:
      resourceId - Spigot/Polymart Resource ID of the free version
      Returns:
      UpdateChecker instance being ran
    • setFreeDownloadLink

      public UpdateChecker setFreeDownloadLink(@Nullable String freeDownloadLink)
      Sets the download link for the free version of your plugin. Use this if there is both, a free and a paid version of your plugin available.
      Parameters:
      freeDownloadLink - Download link of the free version
      Returns:
      UpdateChecker instance being ran
    • setNotifyByPermissionOnJoin

      public UpdateChecker setNotifyByPermissionOnJoin(@Nullable String permission)
      Sets the permission needed to be informed about UpdateChecker results on join.
      Parameters:
      permission - Permission needed to be informed about UpdateChecker results on join
      Returns:
      UpdateChecker instance being ran
    • setPaidDownloadLink

      public UpdateChecker setPaidDownloadLink(int resourceId)
      Sets the download link for the paid version of your plugin generated from your plugin's SpigotMC/Polymart Resource ID. Use this if there is both, a free and a paid version of your plugin available.
      Parameters:
      resourceId - Spigot/Polymart Resource ID of the paid version
      Returns:
      UpdateChecker instance being ran
    • setPaidDownloadLink

      public UpdateChecker setPaidDownloadLink(@NotNull String link)
      Sets the download link for the paid version of your plugin. Use this if there is both, a free and a paid version of your plugin available.
      Parameters:
      link - Download link of the paid version
      Returns:
      UpdateChecker instance being ran
    • setTimeout

      public UpdateChecker setTimeout(int timeout)
      Sets the timeout for the HTTP(S) connection in milliseconds. 0 = use Java's default value
      Parameters:
      timeout - Timeout in milliseconds, or 0 to use Java's default value
    • setUserAgent

      public UpdateChecker setUserAgent(@NotNull UserAgentBuilder userAgentBuilder)
      Sets the UserAgent string using a UserAgentBuilder
      Parameters:
      userAgentBuilder - UserAgentBuilder instance
      Returns:
      UpdateChecker instance being ran
    • setUserAgent

      public UpdateChecker setUserAgent(@Nullable String userAgent)
      Sets the UserAgent string using plain text
      Parameters:
      userAgent - UserAgent string
      Returns:
      UpdateChecker instance being ran