From 9ebbeb3c150e8f0de1a2be849b2c69d944790941 Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Fri, 1 Apr 2022 02:54:22 -0500 Subject: [PATCH] Revamp update checker --- src/main/java/dev/plex/Plex.java | 2 +- .../java/dev/plex/command/impl/AdminCMD.java | 2 +- .../dev/plex/command/impl/DeopAllCMD.java | 2 +- .../java/dev/plex/command/impl/DeopCMD.java | 2 +- .../java/dev/plex/command/impl/ListCMD.java | 2 +- .../java/dev/plex/command/impl/OpAllCMD.java | 2 +- .../java/dev/plex/command/impl/OpCMD.java | 2 +- .../java/dev/plex/command/impl/RankCMD.java | 2 +- .../dev/plex/handlers/CommandHandler.java | 9 +- .../services/impl/UpdateCheckerService.java | 2 +- src/main/java/dev/plex/util/PlexLog.java | 7 ++ .../java/dev/plex/util/UpdateChecker.java | 84 +++++++++++++------ src/main/resources/config.yml | 7 +- 13 files changed, 85 insertions(+), 40 deletions(-) diff --git a/src/main/java/dev/plex/Plex.java b/src/main/java/dev/plex/Plex.java index ec627c0..f89f7e1 100644 --- a/src/main/java/dev/plex/Plex.java +++ b/src/main/java/dev/plex/Plex.java @@ -106,7 +106,7 @@ public class Plex extends JavaPlugin moduleManager.enableModules(); - system = config.getString("commands.permissions"); + system = config.getString("system"); try { diff --git a/src/main/java/dev/plex/command/impl/AdminCMD.java b/src/main/java/dev/plex/command/impl/AdminCMD.java index 69fff27..98ade2e 100644 --- a/src/main/java/dev/plex/command/impl/AdminCMD.java +++ b/src/main/java/dev/plex/command/impl/AdminCMD.java @@ -28,7 +28,7 @@ import org.jetbrains.annotations.Nullable; @CommandPermissions(level = Rank.OP, source = RequiredCommandSource.ANY) @CommandParameters(name = "admin", usage = "/ | remove | setrank | list>", aliases = "saconfig,slconfig,adminconfig,adminmanage", description = "Manage all admins") -@System("ranks") +@System(value = "ranks") public class AdminCMD extends PlexCommand { //TODO: Better return messages diff --git a/src/main/java/dev/plex/command/impl/DeopAllCMD.java b/src/main/java/dev/plex/command/impl/DeopAllCMD.java index c3eabcc..7b37e3e 100644 --- a/src/main/java/dev/plex/command/impl/DeopAllCMD.java +++ b/src/main/java/dev/plex/command/impl/DeopAllCMD.java @@ -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 diff --git a/src/main/java/dev/plex/command/impl/DeopCMD.java b/src/main/java/dev/plex/command/impl/DeopCMD.java index f9cbdab..17eeb60 100644 --- a/src/main/java/dev/plex/command/impl/DeopCMD.java +++ b/src/main/java/dev/plex/command/impl/DeopCMD.java @@ -16,7 +16,7 @@ import org.jetbrains.annotations.Nullable; @CommandParameters(name = "deop", description = "Deop a player on the server", usage = "/ ") @CommandPermissions(level = Rank.ADMIN, permission = "plex.deop") -@System("ranks") +@System(value = "ranks") public class DeopCMD extends PlexCommand { @Override diff --git a/src/main/java/dev/plex/command/impl/ListCMD.java b/src/main/java/dev/plex/command/impl/ListCMD.java index 43a2035..bebbbfd 100644 --- a/src/main/java/dev/plex/command/impl/ListCMD.java +++ b/src/main/java/dev/plex/command/impl/ListCMD.java @@ -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 diff --git a/src/main/java/dev/plex/command/impl/OpAllCMD.java b/src/main/java/dev/plex/command/impl/OpAllCMD.java index b5bb69e..703c81c 100644 --- a/src/main/java/dev/plex/command/impl/OpAllCMD.java +++ b/src/main/java/dev/plex/command/impl/OpAllCMD.java @@ -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 diff --git a/src/main/java/dev/plex/command/impl/OpCMD.java b/src/main/java/dev/plex/command/impl/OpCMD.java index 2940ca3..631c204 100644 --- a/src/main/java/dev/plex/command/impl/OpCMD.java +++ b/src/main/java/dev/plex/command/impl/OpCMD.java @@ -16,7 +16,7 @@ import org.jetbrains.annotations.Nullable; @CommandParameters(name = "op", description = "Op a player on the server", usage = "/ ") @CommandPermissions(level = Rank.OP) -@System("ranks") +@System(value = "ranks") public class OpCMD extends PlexCommand { @Override diff --git a/src/main/java/dev/plex/command/impl/RankCMD.java b/src/main/java/dev/plex/command/impl/RankCMD.java index 8e11bac..f0fdc63 100644 --- a/src/main/java/dev/plex/command/impl/RankCMD.java +++ b/src/main/java/dev/plex/command/impl/RankCMD.java @@ -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 diff --git a/src/main/java/dev/plex/handlers/CommandHandler.java b/src/main/java/dev/plex/handlers/CommandHandler.java index c67e716..346c69e 100644 --- a/src/main/java/dev/plex/handlers/CommandHandler.java +++ b/src/main/java/dev/plex/handlers/CommandHandler.java @@ -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())); } } diff --git a/src/main/java/dev/plex/services/impl/UpdateCheckerService.java b/src/main/java/dev/plex/services/impl/UpdateCheckerService.java index 15548bd..a9c59c7 100644 --- a/src/main/java/dev/plex/services/impl/UpdateCheckerService.java +++ b/src/main/java/dev/plex/services/impl/UpdateCheckerService.java @@ -16,7 +16,7 @@ public class UpdateCheckerService extends AbstractService { if (!newVersion) { - if (plugin.getUpdateChecker().check()) + if (plugin.getUpdateChecker().getUpdateStatusMessage("plexusorg/Plex", "master")) { newVersion = true; } diff --git a/src/main/java/dev/plex/util/PlexLog.java b/src/main/java/dev/plex/util/PlexLog.java index 000c90c..5c745dd 100644 --- a/src/main/java/dev/plex/util/PlexLog.java +++ b/src/main/java/dev/plex/util/PlexLog.java @@ -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++) diff --git a/src/main/java/dev/plex/util/UpdateChecker.java b/src/main/java/dev/plex/util/UpdateChecker.java index b6227d0..66cd2dd 100644 --- a/src/main/java/dev/plex/util/UpdateChecker.java +++ b/src/main/java/dev/plex/util/UpdateChecker.java @@ -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; + } } } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 821540c..f21b76a 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -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: