Revamp update checker

This commit is contained in:
2022-04-01 02:54:22 -05:00
parent 8ebb1c91a0
commit 9ebbeb3c15
13 changed files with 85 additions and 40 deletions

View File

@ -1,6 +1,8 @@
package dev.plex.util;
import dev.plex.Plex;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
@ -18,6 +20,11 @@ public class PlexLog
Bukkit.getConsoleSender().sendMessage(String.format(ChatColor.YELLOW + "[Plex] " + ChatColor.GRAY + "%s", message));
}
public static void log(Component component)
{
Bukkit.getConsoleSender().sendMessage(Component.text("[Plex] ").color(NamedTextColor.YELLOW).append(component).colorIfAbsent(NamedTextColor.GRAY));
}
public static void error(String message, Object... strings)
{
for (int i = 0; i < strings.length; i++)

View File

@ -1,52 +1,86 @@
package dev.plex.util;
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;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import javax.annotation.Nonnull;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.ChatColor;
public class UpdateChecker extends PlexBase
{
private final String currentVersion = plugin.getDescription().getVersion();
private static final String DOWNLOAD_PAGE = "https://ci.plex.us.org/job/Plex/";
public boolean check()
// Adapted from Paper
private int fetchDistanceFromGitHub(@Nonnull String repo, @Nonnull String branch, @Nonnull String hash)
{
if (currentVersion.contains("-SNAPSHOT"))
{
PlexLog.log("Snapshot version detected, not checking for updates.");
return true;
}
try
{
String versionLink = "https://plex.us.org/updater/check/";
URL url = new URL(versionLink);
URLConnection con = url.openConnection();
InputStreamReader isr = new InputStreamReader(con.getInputStream());
BufferedReader reader = new BufferedReader(isr);
if (!reader.ready())
HttpURLConnection connection = (HttpURLConnection)new URL("https://api.github.com/repos/" + repo + "/compare/" + branch + "..." + hash).openConnection();
connection.connect();
if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND)
{
return false;
return -2; // Unknown commit
}
String newVersion = reader.readLine();
reader.close();
if (!newVersion.equals(currentVersion))
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charsets.UTF_8)))
{
PlexLog.log(ChatColor.RED + "There is a new version of Plex available: " + newVersion);
return true;
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;
};
}
else
catch (JsonSyntaxException | NumberFormatException e)
{
return false;
e.printStackTrace();
return -1;
}
}
catch (IOException e)
{
PlexLog.error("There was an error checking for updates!");
return false;
e.printStackTrace();
return -1;
}
}
public boolean getUpdateStatusMessage(@Nonnull String repo, @Nonnull String branch)
{
int distance;
distance = fetchDistanceFromGitHub(repo, branch, Plex.build.head);
switch (distance)
{
case -1 -> {
PlexLog.log(ChatColor.RED + "There was an error checking for updates.");
return false;
}
case 0 -> {
PlexLog.log(ChatColor.GREEN + "Your version of Plex is up to date!");
return true;
}
case -2 -> {
PlexLog.log(ChatColor.RED + "Unknown version, unable to check for updates.");
return false;
}
default -> {
PlexLog.log(Component.text("Your version of Plex is not up to date!", NamedTextColor.RED)
.append(Component.newline())
.append(Component.text("Download the new version at: ")
.append(Component.text(DOWNLOAD_PAGE, NamedTextColor.RED))));
return true;
}
}
}
}