Class FetchVersion

java.lang.Object
one.tranic.t.base.updater.FetchVersion
All Implemented Interfaces:
AutoCloseable, Runnable

public class FetchVersion extends Object implements AutoCloseable, Runnable
The FetchVersion class is responsible for managing the fetching and updating of a plugin's version.

It performs periodic version checks, determines whether an update is available, and provides formatted messages displaying update statuses.

This class implements both the AutoCloseable and Runnable interfaces for resource management and asynchronous operations, respectively.

  • Constructor Summary

    Constructors
    Constructor
    Description
    FetchVersion(String currentVersion, String pluginName, String resourceId)
    Constructs a FetchVersion instance for version checking and updating purposes.
    FetchVersion(String currentVersion, String pluginName, String resourceId, String resourceURL)
    Constructs a new FetchVersion instance responsible for managing version fetching and updates.
  • 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.
    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
     

    Methods inherited from class java.lang.Object

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

    • FetchVersion

      public FetchVersion(String currentVersion, String pluginName, String resourceId)
      Constructs a FetchVersion instance for version checking and updating purposes.
      Parameters:
      currentVersion - the current version of the associated plugin or application.
      pluginName - the name of the plugin or application being checked for updates.
      resourceId - the unique identifier for the resource being checked, typically corresponding to its ID on the update server.
    • FetchVersion

      public FetchVersion(String currentVersion, String pluginName, String resourceId, String resourceURL)
      Constructs a new FetchVersion instance responsible for managing version fetching and updates.
      Parameters:
      currentVersion - the current version of the plugin as a String
      pluginName - the name of the plugin the version fetch is associated with
      resourceId - the resource ID of the plugin on the platform
      resourceURL - the URL of the resource; if null, a default URL is generated based on the resourceId
  • 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.
    • 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