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

85 lines
3.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 07:54:22 +00:00
private static final String DOWNLOAD_PAGE = "https://ci.plex.us.org/job/Plex/";
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 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 08:08:17 +00:00
public boolean getUpdateStatusMessage(CommandSender sender)
2022-04-01 07:54:22 +00:00
{
int distance;
2022-04-01 08:08:17 +00:00
distance = fetchDistanceFromGitHub("plexusorg/Plex", "master", Plex.build.head);
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-01 07:54:22 +00:00
return true;
}
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;
}
}
}
}