Revamp update checker

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

View File

@ -106,7 +106,7 @@ public class Plex extends JavaPlugin
moduleManager.enableModules();
system = config.getString("commands.permissions");
system = config.getString("system");
try
{

View File

@ -28,7 +28,7 @@ import org.jetbrains.annotations.Nullable;
@CommandPermissions(level = Rank.OP, source = RequiredCommandSource.ANY)
@CommandParameters(name = "admin", usage = "/<command> <add <player> | remove <player> | setrank <player> <rank> | list>", aliases = "saconfig,slconfig,adminconfig,adminmanage", description = "Manage all admins")
@System("ranks")
@System(value = "ranks")
public class AdminCMD extends PlexCommand
{
//TODO: Better return messages

View File

@ -15,7 +15,7 @@ import org.jetbrains.annotations.Nullable;
@CommandParameters(name = "deopall", description = "Deop everyone on the server", aliases = "deopa")
@CommandPermissions(level = Rank.ADMIN)
@System("ranks")
@System(value = "ranks")
public class DeopAllCMD extends PlexCommand
{
@Override

View File

@ -16,7 +16,7 @@ import org.jetbrains.annotations.Nullable;
@CommandParameters(name = "deop", description = "Deop a player on the server", usage = "/<command> <player>")
@CommandPermissions(level = Rank.ADMIN, permission = "plex.deop")
@System("ranks")
@System(value = "ranks")
public class DeopCMD extends PlexCommand
{
@Override

View File

@ -17,7 +17,7 @@ import org.jetbrains.annotations.Nullable;
@CommandParameters(name = "list", description = "Show a list of all online players")
@CommandPermissions(level = Rank.OP, permission = "plex.list")
@System("ranks")
@System(value = "ranks")
public class ListCMD extends PlexCommand
{
@Override

View File

@ -15,7 +15,7 @@ import org.jetbrains.annotations.Nullable;
@CommandParameters(name = "opall", description = "Op everyone on the server", aliases = "opa")
@CommandPermissions(level = Rank.ADMIN)
@System("ranks")
@System(value = "ranks")
public class OpAllCMD extends PlexCommand
{
@Override

View File

@ -16,7 +16,7 @@ import org.jetbrains.annotations.Nullable;
@CommandParameters(name = "op", description = "Op a player on the server", usage = "/<command> <player>")
@CommandPermissions(level = Rank.OP)
@System("ranks")
@System(value = "ranks")
public class OpCMD extends PlexCommand
{
@Override

View File

@ -15,7 +15,7 @@ import org.jetbrains.annotations.Nullable;
@CommandPermissions(level = Rank.OP, permission = "plex.rank", source = RequiredCommandSource.ANY)
@CommandParameters(name = "rank", description = "Displays your rank")
@System("ranks")
@System(value = "ranks")
public class RankCMD extends PlexCommand
{
@Override

View File

@ -22,22 +22,26 @@ public class CommandHandler extends PlexBase
try
{
System annotation = clazz.getDeclaredAnnotation(System.class);
// TODO: Annotations are always null?
if (annotation != null)
{
if (annotation.value().equalsIgnoreCase(plugin.getSystem()))
PlexLog.debug(clazz.getName() + " has annotations");
if (annotation.value().equalsIgnoreCase(plugin.getSystem().toLowerCase()))
{
commands.add(clazz.getConstructor().newInstance());
return;
PlexLog.debug("Adding " + clazz.getName() + " as a rank command");
}
if (plugin.config.getBoolean("debug") && annotation.debug())
{
commands.add(clazz.getConstructor().newInstance());
PlexLog.debug("Adding " + clazz.getName() + " as a debug command");
}
}
else
{
commands.add(clazz.getConstructor().newInstance());
// PlexLog.debug("Adding command normally " + clazz.getName());
}
}
catch (InvocationTargetException | InstantiationException | IllegalAccessException | NoSuchMethodException ex)
@ -46,6 +50,7 @@ public class CommandHandler extends PlexBase
}
});
PlexLog.debug("Test");
PlexLog.log(String.format("Registered %s commands from %s classes!", commands.size(), commandSet.size()));
}
}

View File

@ -16,7 +16,7 @@ public class UpdateCheckerService extends AbstractService
{
if (!newVersion)
{
if (plugin.getUpdateChecker().check())
if (plugin.getUpdateChecker().getUpdateStatusMessage("plexusorg/Plex", "master"))
{
newVersion = true;
}

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;
}
}
}
}

View File

@ -26,10 +26,9 @@ chat:
# Color code for name color
name-color: 'f'
# Settings for commands relating to Plex
commands:
# Should Plex use a "true op" system with ranks or only permission nodes
permissions: ranks
# Should Plex use a "true op" system with ranks or only permission nodes
# Options are "permissions" or "ranks"
system: ranks
data:
central: