Add caching to the updater

This commit is contained in:
2022-04-01 15:00:55 -05:00
parent bbb41aa052
commit bb92805fa5
10 changed files with 51 additions and 26 deletions

View File

@ -18,7 +18,16 @@ import org.bukkit.command.CommandSender;
public class UpdateChecker extends PlexBase
{
private static final String DOWNLOAD_PAGE = "https://ci.plex.us.org/job/Plex/";
/*
* -4 = Never checked for updates
* -3 = Likely rate limited
* -2 = Unknown commit
* -1 = Error occurred
* 0 = Up to date
* > 0 = Number of commits behind
*/
private final String DOWNLOAD_PAGE = "https://ci.plex.us.org/job/Plex/";
private int distance = -4;
// Adapted from Paper
private int fetchDistanceFromGitHub(@Nonnull String repo, @Nonnull String branch, @Nonnull String hash)
@ -31,6 +40,10 @@ public class UpdateChecker extends PlexBase
{
return -2; // Unknown commit
}
if (connection.getResponseCode() == HttpURLConnection.HTTP_FORBIDDEN)
{
return -3; // Rate limited likely
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charsets.UTF_8)))
{
JsonObject obj = new Gson().fromJson(reader, JsonObject.class);
@ -55,10 +68,27 @@ public class UpdateChecker extends PlexBase
}
}
public boolean getUpdateStatusMessage(CommandSender sender)
public boolean getUpdateStatusMessage(CommandSender sender, boolean cached)
{
int distance;
distance = fetchDistanceFromGitHub("plexusorg/Plex", "master", Plex.build.head);
// If it's -4, it hasn't checked for updates yet
if (distance == -4)
{
distance = fetchDistanceFromGitHub("plexusorg/Plex", "master", Plex.build.head);
PlexLog.debug("Never checked for updates, checking now...");
}
else
{
// If the request isn't asked to be cached, fetch it
if (!cached)
{
distance = fetchDistanceFromGitHub("plexusorg/Plex", "master", Plex.build.head);
PlexLog.debug("We have checked for updates before, but this request was not asked to be cached.");
}
else
{
PlexLog.debug("We have checked for updates before, using cache.");
}
}
switch (distance)
{