Class FetchVersion
- All Implemented Interfaces:
AutoCloseable,Runnable
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
ConstructorsConstructorDescriptionFetchVersion(String currentVersion, String pluginName, String resourceId) Constructs aFetchVersioninstance 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 TypeMethodDescriptionvoidclose()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.ComponentBuilds 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.ComponentGenerates and returns an update notification message in a formatted style.booleanDetermines whether there is an update available for the current version of the associated plugin or resource.booleanDetermines whether the cached data has expired based on the time elapsed since the last update compared to the predefined cache expiry threshold.voidrun()Initiates an asynchronous version check process by scheduling a task through an executor.voidstart()
-
Constructor Details
-
FetchVersion
Constructs aFetchVersioninstance 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 StringpluginName- the name of the plugin the version fetch is associated withresourceId- the resource ID of the plugin on the platformresourceURL- 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
stopmethod 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 thehasUpdatemethod to determine if an update is available.Tasks are managed using the static executor from the
TBaseclass. Any exceptions during the sleep period of the task are ignored. -
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:
closein interfaceAutoCloseable
-
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
Componentrepresenting 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
Componentrepresenting 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
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
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
-