Plex/src/main/java/dev/plex/util/UpdateChecker.java

115 lines
4.1 KiB
Java
Raw Normal View History

package dev.plex.util;
2022-04-01 07:54:22 +00:00
import com.google.common.base.Charsets;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import dev.plex.Plex;
import dev.plex.PlexBase;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
2022-04-01 07:54:22 +00:00
import java.net.HttpURLConnection;
import java.net.URL;
2022-04-01 07:54:22 +00:00
import javax.annotation.Nonnull;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
2022-04-01 08:08:17 +00:00
import org.bukkit.command.CommandSender;
public class UpdateChecker extends PlexBase
{
2022-04-01 20:00:55 +00:00
/*
* -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;
2022-04-01 07:54:22 +00:00
// Adapted from Paper
private int fetchDistanceFromGitHub(@Nonnull String repo, @Nonnull String branch, @Nonnull String hash)
{
try
{
2022-04-01 07:54:22 +00:00
HttpURLConnection connection = (HttpURLConnection)new URL("https://api.github.com/repos/" + repo + "/compare/" + branch + "..." + hash).openConnection();
connection.connect();
if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND)
{
2022-04-01 07:54:22 +00:00
return -2; // Unknown commit
}
2022-04-01 20:00:55 +00:00
if (connection.getResponseCode() == HttpURLConnection.HTTP_FORBIDDEN)
{
return -3; // Rate limited likely
}
2022-04-01 07:54:22 +00:00
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charsets.UTF_8)))
{
2022-04-01 07:54:22 +00:00
JsonObject obj = new Gson().fromJson(reader, JsonObject.class);
String status = obj.get("status").getAsString();
return switch (status)
{
case "identical" -> 0;
case "behind" -> obj.get("behind_by").getAsInt();
default -> -1;
};
}
2022-04-01 07:54:22 +00:00
catch (JsonSyntaxException | NumberFormatException e)
{
2022-04-01 07:54:22 +00:00
e.printStackTrace();
return -1;
}
}
catch (IOException e)
{
2022-04-01 07:54:22 +00:00
e.printStackTrace();
return -1;
}
}
2022-04-01 20:00:55 +00:00
public boolean getUpdateStatusMessage(CommandSender sender, boolean cached)
2022-04-01 07:54:22 +00:00
{
2022-04-01 20:00:55 +00:00
// 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.");
}
}
2022-04-01 07:54:22 +00:00
switch (distance)
{
case -1 -> {
2022-04-01 08:08:17 +00:00
sender.sendMessage(Component.text("There was an error checking for updates.").color(NamedTextColor.RED));
2022-04-01 07:54:22 +00:00
return false;
}
case 0 -> {
2022-04-01 08:08:17 +00:00
sender.sendMessage(Component.text("Your version of Plex is up to date!").color(NamedTextColor.GREEN));
2022-04-05 20:46:21 +00:00
return false;
2022-04-01 07:54:22 +00:00
}
case -2 -> {
2022-04-01 08:08:17 +00:00
sender.sendMessage(Component.text("Unknown version, unable to check for updates.").color(NamedTextColor.RED));
2022-04-01 07:54:22 +00:00
return false;
}
default -> {
2022-04-01 08:08:17 +00:00
sender.sendMessage(Component.text("Your version of Plex is not up to date!", NamedTextColor.RED));
2022-04-01 08:17:42 +00:00
sender.sendMessage(Component.text("Download a new version at: " + DOWNLOAD_PAGE).color(NamedTextColor.RED));
2022-04-01 07:54:22 +00:00
return true;
}
}
}
}