Class VersionFetcher

java.lang.Object
one.tranic.t.base.updater.VersionFetcher
All Implemented Interfaces:
AutoCloseable, Runnable
Direct Known Subclasses:
ModrinthVersionFetcher, SpigotVersionFetcher

public abstract class VersionFetcher extends Object implements AutoCloseable, Runnable
usage:
     var fetcher = new VersionFetcher(currentVersion, pluginName, apiAddress);
     fetcher.start(); // don't use run.
 
  • Constructor Summary

    Constructors
    Constructor
    Description
    VersionFetcher(String currentVersion, String pluginName, String api)
    Constructs an instance of the VersionFetcher class.
    VersionFetcher(String currentVersion, String pluginName, String api, String resourceURL)
    Constructs an instance of the VersionFetcher class.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes the resources associated with the FetchVersion instance by stopping the update thread if it is alive.
    Fetches the latest version of the associated plugin or resource from the update server.
    Retrieves the latest version of the plugin or resource.
    net.kyori.adventure.text.Component
    Builds and returns a message component indicating that no update is available.
    Retrieves the URL of the resource associated with this object.
    @NotNull net.kyori.adventure.text.Component
    Generates and returns an update notification message in a formatted style.
    boolean
    Determines whether there is an update available for the current version of the associated plugin or resource.
    boolean
    Determines whether the cached data has expired based on the time elapsed since the last update compared to the predefined cache expiry threshold.
    void
    run()
    Initiates an asynchronous version check process by scheduling a task through an executor.
    void
     
    void
    Updates the resource URL for the current instance.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • VersionFetcher

      public VersionFetcher(String currentVersion, String pluginName, String api)
      Constructs an instance of the VersionFetcher class.
      Parameters:
      currentVersion - the current version of the plugin or resource
      pluginName - the name of the plugin or resource
      api - the API URL for checking updates
    • VersionFetcher

      public VersionFetcher(String currentVersion, String pluginName, String api, String resourceURL)
      Constructs an instance of the VersionFetcher class.
      Parameters:
      currentVersion - the current version of the plugin.
      pluginName - the name of the plugin for which version updates are being fetched.
      api - the URL of the API endpoint to check for updates.
      resourceURL - the URL of the plugin's resource page. If null, defaults to "SpigotMC".
  • Method Details

    • run

      public void run()
      Initiates an asynchronous version check process by scheduling a task through an executor.

      This method first invokes the stop method to terminate any previously scheduled task. It then submits a new task to be executed asynchronously. The task sleeps for a defined period (2 hours) and subsequently calls the hasUpdate method to determine if an update is available.

      Tasks are managed using the static executor from the TBase class. Any exceptions during the sleep period of the task are ignored.

      Specified by:
      run in interface Runnable
    • start

      public void start()
    • close

      public void close()
      Closes the resources associated with the FetchVersion instance by stopping the update thread if it is alive.

      This method interrupts the update thread, preventing any ongoing processes from running, and subsequently sets the update thread to null. It ensures that redundant threads are not left running, which helps in releasing resources and avoiding potential memory leaks.

      Specified by:
      close in interface AutoCloseable
    • getUpdateMessage

      @NotNull public @NotNull net.kyori.adventure.text.Component getUpdateMessage()
      Generates and returns an update notification message in a formatted style.

      The message is created based on the placeholders in the update message template, replacing them with the current plugin's name, version, the latest version available, and the corresponding resource URL.

      Returns:
      a formatted Component representing the update message.
    • getNoUpdateMessage

      public net.kyori.adventure.text.Component getNoUpdateMessage()
      Builds and returns a message component indicating that no update is available.

      The message content is derived from the pre-defined noUpdateTemplate.

      Returns:
      a Component representing the "no update available" message.
    • isExpired

      public boolean isExpired()
      Determines whether the cached data has expired based on the time elapsed since the last update compared to the predefined cache expiry threshold.
      Returns:
      true if the cached data has expired; false otherwise
    • getLatestVersion

      public String getLatestVersion()
      Retrieves the latest version of the plugin or resource.

      This method returns the most recent version that has been checked or retrieved, which can be compared against the current version to determine if an update is available.

      Returns:
      a string representing the latest version, or null if no version has been retrieved yet.
    • getResourceURL

      public String getResourceURL()
      Retrieves the URL of the resource associated with this object.
      Returns:
      the resource URL as a String.
    • updateResourceURL

      public void updateResourceURL(String resourceURL)
      Updates the resource URL for the current instance.
      Parameters:
      resourceURL - the new URL of the resource to be associated with this instance
    • hasUpdate

      public boolean hasUpdate()
      Determines whether there is an update available for the current version of the associated plugin or resource. This method checks the latest known version against the current version and may fetch a new version if necessary.
      Returns:
      true if an update is available (i.e., the latest version differs from the current version); false otherwise
    • fetchLatestVersion

      public String fetchLatestVersion()
      Fetches the latest version of the associated plugin or resource from the update server.

      This method establishes a connection to the specified update server URL, reads the response, and retrieves the latest version information as a string. If an error occurs during the process, the stack trace is printed, and the method returns null.

      Returns:
      the latest version as a string, or null if an error occurs or the version cannot be retrieved