Class VersionFetcher
- All Implemented Interfaces:
AutoCloseable,Runnable
- Direct Known Subclasses:
ModrinthVersionFetcher,SpigotVersionFetcher
var fetcher = new VersionFetcher(currentVersion, pluginName, apiAddress);
fetcher.start(); // don't use run.
-
Constructor Summary
ConstructorsConstructorDescriptionVersionFetcher(String currentVersion, String pluginName, String api) Constructs an instance of theVersionFetcherclass.VersionFetcher(String currentVersion, String pluginName, String api, String resourceURL) Constructs an instance of the VersionFetcher class. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()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.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()voidupdateResourceURL(String resourceURL) Updates the resource URL for the current instance.
-
Constructor Details
-
VersionFetcher
Constructs an instance of theVersionFetcherclass.- Parameters:
currentVersion- the current version of the plugin or resourcepluginName- the name of the plugin or resourceapi- the API URL for checking updates
-
VersionFetcher
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
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.
-
updateResourceURL
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
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
-