From 7090ea51ccf632ba322b05881c2ef8198c32bb5c Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Mon, 27 Apr 2020 16:56:48 +1200 Subject: [PATCH] Finish update system --- .../commands/LibsDisguisesCommand.java | 18 ++++- .../commands/libsdisguises/LDUpdate.java | 68 ++++++++++++---- .../utilities/translations/LibsMsg.java | 17 ++-- .../disguise/utilities/updates/LDGithub.java | 1 + .../disguise/utilities/updates/LDJenkins.java | 44 +++++++--- .../utilities/updates/UpdateChecker.java | 80 ++++++++++++------- 6 files changed, 165 insertions(+), 63 deletions(-) diff --git a/src/main/java/me/libraryaddict/disguise/commands/LibsDisguisesCommand.java b/src/main/java/me/libraryaddict/disguise/commands/LibsDisguisesCommand.java index 786bc064..709b9cec 100644 --- a/src/main/java/me/libraryaddict/disguise/commands/LibsDisguisesCommand.java +++ b/src/main/java/me/libraryaddict/disguise/commands/LibsDisguisesCommand.java @@ -135,7 +135,23 @@ public class LibsDisguisesCommand implements CommandExecutor, TabCompleter { continue; } - tabs.addAll(command.getTabComplete()); + if (origArgs.length <= 1) { + tabs.addAll(command.getTabComplete()); + } else { + for (String s : command.getTabComplete()) { + if (!s.contains(" ")) { + continue; + } + + String[] split = s.split(" "); + + if (!args[0].equalsIgnoreCase(split[0])) { + continue; + } + + tabs.add(split[1]); + } + } } return filterTabs(tabs, origArgs); diff --git a/src/main/java/me/libraryaddict/disguise/commands/libsdisguises/LDUpdate.java b/src/main/java/me/libraryaddict/disguise/commands/libsdisguises/LDUpdate.java index 19a4da8b..8ab99626 100644 --- a/src/main/java/me/libraryaddict/disguise/commands/libsdisguises/LDUpdate.java +++ b/src/main/java/me/libraryaddict/disguise/commands/libsdisguises/LDUpdate.java @@ -1,11 +1,12 @@ package me.libraryaddict.disguise.commands.libsdisguises; +import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.updates.UpdateChecker; import me.libraryaddict.disguise.utilities.plugin.PluginInformation; import me.libraryaddict.disguise.utilities.translations.LibsMsg; -import org.bukkit.Bukkit; +import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; @@ -22,7 +23,7 @@ public class LDUpdate implements LDCommand { // Update by download // Update check // Update to latest dev build - return Arrays.asList("update", "update?", "update!"); + return Arrays.asList("update", "update dev", "update release", "changelog", "update!"); } @Override @@ -30,6 +31,25 @@ public class LDUpdate implements LDCommand { return "libsdisguises.update"; } + private void doChangelog(CommandSender sender) { + UpdateChecker checker = LibsDisguises.getInstance().getUpdateChecker(); + + if (checker.getUpdate() == null) { + sender.sendMessage(LibsMsg.UPDATE_REQUIRED.get()); + return; + } + + if (!checker.getUpdate().isReleaseBuild()) { + sender.sendMessage( + ChatColor.GOLD + "You are on build " + (LibsDisguises.getInstance().isNumberedBuild() ? "#" : "") + + LibsDisguises.getInstance().getBuildNo()); + } + + for (String msg : checker.getUpdate().getChangelog()) { + sender.sendMessage(ChatColor.GOLD + msg); + } + } + @Override public void onCommand(CommandSender sender, String[] args) { UpdateChecker checker = LibsDisguises.getInstance().getUpdateChecker(); @@ -39,20 +59,33 @@ public class LDUpdate implements LDCommand { return; } - boolean check = args[0].endsWith("?"); - boolean force = args[0].endsWith("!"); + if (args[0].equalsIgnoreCase("changelog")) { + doChangelog(sender); + return; + } - if (!check && !force && checker.getUpdate() != null) { - if (checker.getUpdate().getVersion().equals(checker.getUpdate().isReleaseBuild() ? - LibsDisguises.getInstance().getDescription().getDescription() : - LibsDisguises.getInstance().getBuildNumber())) { + boolean releaseBuilds = checker.isUsingReleaseBuilds(); + + if (args.length > 1) { + if (args[1].equalsIgnoreCase("dev")) { + releaseBuilds = false; + } else if (args[1].equalsIgnoreCase("release")) { + releaseBuilds = true; + } else { + sender.sendMessage(LibsMsg.LIBS_UPDATE_UNKNOWN_BRANCH.get()); + return; + } + + DisguiseConfig.setUsingReleaseBuilds(releaseBuilds); + } + + if (checker.getUpdate() != null && checker.getUpdate().isReleaseBuild() == releaseBuilds) { + if (checker.isServerLatestVersion()) { sender.sendMessage(LibsMsg.UPDATE_ON_LATEST.get()); return; } - if (checker.getLastDownload() != null && checker.getUpdate().getVersion() - .equals(checker.isUsingReleaseBuilds() ? checker.getLastDownload().getVersion() : - checker.getLastDownload().getBuildNumber())) { + if (checker.isOnLatestUpdate(true)) { sender.sendMessage(LibsMsg.UPDATE_ALREADY_DOWNLOADED.get()); return; } @@ -63,7 +96,7 @@ public class LDUpdate implements LDCommand { public void run() { LibsMsg updateResult = null; - if (check || checker.getUpdate() == null || force) { + if (checker.getUpdate() == null || args.length > 1 || checker.isOldUpdate()) { updateResult = checker.doUpdateCheck(); } @@ -72,12 +105,17 @@ public class LDUpdate implements LDCommand { return; } - if (!checker.isUpdateReady()) { - sender.sendMessage(LibsMsg.UPDATE_ON_LATEST.get()); + if (checker.isOnLatestUpdate(true)) { + if (checker.getLastDownload() != null) { + sender.sendMessage(LibsMsg.UPDATE_ALREADY_DOWNLOADED.get()); + } else { + sender.sendMessage(LibsMsg.UPDATE_ON_LATEST.get()); + } + return; } - if (check) { + if (!args[0].endsWith("!")) { if (updateResult != null) { sender.sendMessage(updateResult.get()); } else { diff --git a/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java b/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java index 62eda66b..c075a2cb 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java @@ -121,8 +121,9 @@ public enum LibsMsg { PLEASE_WAIT(ChatColor.GRAY + "Please wait..."), INVALID_CLONE(ChatColor.DARK_RED + "Unknown method '%s' - Valid methods are 'IgnoreEquipment' 'DoSneakSprint' " + "'DoSneak' 'DoSprint'"), - LIBS_COMMAND_WRONG_ARG( - ChatColor.RED + "[LibsDisguises] Did you mean 'reload', 'scoreboard', 'permtest', 'json' or 'metainfo'?"), + LIBS_COMMAND_WRONG_ARG(ChatColor.RED + "[LibsDisguises] Invalid argument, use /libsdisguises help"), + LIBS_UPDATE_UNKNOWN_BRANCH( + ChatColor.RED + "[LibsDisguises] Invalid argument, use 'dev' or 'release' to switch branches"), LIMITED_RADIUS(ChatColor.RED + "Limited radius to %s! Don't want to make too much lag right?"), LISTEN_ENTITY_ENTITY_DISG_ENTITY(ChatColor.RED + "Disguised %s as a %s!"), LISTEN_ENTITY_ENTITY_DISG_ENTITY_FAIL(ChatColor.RED + "Failed to disguise %s as a %s!"), @@ -147,6 +148,7 @@ public enum LibsMsg { UPDATE_ALREADY_DOWNLOADED(ChatColor.RED + "That update has already been downloaded!"), UPDATE_FAILED(ChatColor.RED + "LibsDisguises update failed! Check console for errors."), UPDATE_SUCCESS(ChatColor.DARK_GREEN + "LibsDisguises update success! Restart server to update!"), + UPDATE_REQUIRED(ChatColor.RED + "LibsDisguises requies an update check before it can give you that!"), UPDATE_INFO(ChatColor.DARK_GREEN + "Lib's Disguises v%s, build %s, built %s and size %skb"), UPDATE_IN_PROGRESS(ChatColor.DARK_GREEN + "LibsDisguises is now downloading an update..."), NO_PERM_DISGUISE(ChatColor.RED + "You do not have permission for that disguise!"), @@ -196,12 +198,13 @@ public enum LibsMsg { UNDISRADIUS(ChatColor.RED + "Successfully undisguised %s entities!"), UPDATE_READY(ChatColor.RED + "[LibsDisguises] " + ChatColor.DARK_RED + "There is a update ready to be downloaded! You are using " + ChatColor.RED + "v%s" + ChatColor.DARK_RED + - ", the new version is " + ChatColor.RED + "v%s" + ChatColor.DARK_RED + - "! Use /libsdisguises update to automatically update!"), + ", the new version is " + ChatColor.RED + "v%s" + ChatColor.DARK_RED + "!"), UPDATE_READY_SNAPSHOT(ChatColor.RED + "[LibsDisguises] " + ChatColor.DARK_RED + - "There is a new build of Lib's Disguises! You are using " + ChatColor.RED + "#%s" + ChatColor.DARK_RED + - ", the latest build is " + ChatColor.RED + "#%s" + ChatColor.DARK_RED + "!" + ChatColor.RED + - "\nhttps://ci.md-5.net/job/LibsDisguises/lastSuccessfulBuild/ or use /libsdisguises update"), + "There is a new build of Lib's Disguises! You are using " + ChatColor.RED + "%s" + ChatColor.DARK_RED + + ", the latest build is " + ChatColor.RED + "#%s" + ChatColor.DARK_RED + "!"), + UPDATE_HOW(ChatColor.DARK_AQUA + "Use " + ChatColor.AQUA + "/libsdisgusies changelog" + ChatColor.DARK_AQUA + + " to see what changed, use " + ChatColor.AQUA + "/libsdisguises update!" + ChatColor.DARK_AQUA + + " to download the update!"), VIEW_SELF_ON(ChatColor.GREEN + "Toggled viewing own disguise on!"), VIEW_SELF_OFF(ChatColor.GREEN + "Toggled viewing own disguise off!"), CLICK_TO_COPY(ChatColor.GREEN + "Click to Copy:"), diff --git a/src/main/java/me/libraryaddict/disguise/utilities/updates/LDGithub.java b/src/main/java/me/libraryaddict/disguise/utilities/updates/LDGithub.java index 635baea1..00092891 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/updates/LDGithub.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/updates/LDGithub.java @@ -50,6 +50,7 @@ public class LDGithub { public DisguiseUpdate getLatestRelease() { try { + DisguiseUtilities.getLogger().info("Now looking for update on Github.."); // We're connecting to md_5's jenkins REST api URL url = new URL("https://api.github.com/repos/libraryaddict/LibsDisguises/releases/latest"); // Creating a connection diff --git a/src/main/java/me/libraryaddict/disguise/utilities/updates/LDJenkins.java b/src/main/java/me/libraryaddict/disguise/utilities/updates/LDJenkins.java index fa45ced7..0e77f287 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/updates/LDJenkins.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/updates/LDJenkins.java @@ -3,9 +3,8 @@ package me.libraryaddict.disguise.utilities.updates; import com.google.gson.Gson; import lombok.AllArgsConstructor; import lombok.Getter; -import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.utilities.DisguiseUtilities; -import me.libraryaddict.disguise.utilities.plugin.PluginInformation; +import org.bukkit.ChatColor; import java.io.BufferedReader; import java.io.InputStream; @@ -14,8 +13,8 @@ import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.ArrayList; -import java.util.Arrays; import java.util.Date; +import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -48,8 +47,9 @@ public class LDJenkins { */ private Map fetchLastSnapshotBuild() { try { + DisguiseUtilities.getLogger().info("Now looking for update on Jenkins.."); // We're connecting to md_5's jenkins REST api - URL url = new URL("https://ci.md-5.net/job/LibsDisguises/lastSuccessfulBuild/api/json"); + URL url = new URL("https://ci.md-5.net/job/LibsDisguises/api/json?tree=builds[changeSet[items[msg]],id,result]"); // Creating a connection HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setDefaultUseCaches(false); @@ -77,28 +77,50 @@ public class LDJenkins { public DisguiseUpdate getLatestSnapshot() { Map lastBuild = fetchLastSnapshotBuild(); - if (lastBuild == null || !lastBuild.containsKey("id") || !lastBuild.containsKey("timestamp")) { + if (lastBuild == null || !lastBuild.containsKey("builds")) { return null; } ArrayList changelog = new ArrayList<>(); + String version = null; - if (lastBuild.get("changeSet") instanceof Map) { - Object items = ((Map) lastBuild.get("changeSet")).get("items"); + for (Map map : (List) lastBuild.get("builds")) { + String result = (String) map.get("result"); - if (items instanceof Map[]) { - for (Map item : (Map[]) items) { + if (!"SUCCESS".equalsIgnoreCase(result)) { + continue; + } + + if (changelog.isEmpty()) { + version = (String) map.get("id"); + } + + Object items = ((Map) map.get("changeSet")).get("items"); + boolean release = false; + + if (items instanceof List) { + for (Map item : (List) items) { String msg = (String) item.get("msg"); if (msg == null) { continue; } - changelog.add(msg); + changelog.add("#" + map.get("id") + ": " + ChatColor.YELLOW + msg); + + release = release || msg.toLowerCase().matches("release.? .*"); } } + + if (release) { + break; + } } - return new JenkinsUpdate((String) lastBuild.get("id"), changelog.toArray(new String[0])); + if (changelog.isEmpty()) { + return null; + } + + return new JenkinsUpdate(version, changelog.toArray(new String[0])); } } diff --git a/src/main/java/me/libraryaddict/disguise/utilities/updates/UpdateChecker.java b/src/main/java/me/libraryaddict/disguise/utilities/updates/UpdateChecker.java index 27690144..521b6aef 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/updates/UpdateChecker.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/updates/UpdateChecker.java @@ -11,7 +11,6 @@ import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.craftbukkit.libs.org.apache.commons.io.FileUtils; import org.bukkit.entity.Player; -import org.bukkit.scheduler.BukkitTask; import java.io.BufferedReader; import java.io.File; @@ -42,10 +41,51 @@ public class UpdateChecker { this.resourceID = resourceID; } + public boolean isServerLatestVersion() { + return isOnLatestUpdate(false); + } + + public boolean isOnLatestUpdate(boolean includeDownloaded) { + if (getUpdate() == null) { + return false; + } + + boolean isRelease = + includeDownloaded && getLastDownload() != null ? !getLastDownload().getVersion().contains("-SNAPSHOT") : + LibsDisguises.getInstance().isReleaseBuild(); + + if (getUpdate().isReleaseBuild() != isRelease) { + return false; + } + + String version; + + if (getUpdate().isReleaseBuild()) { + if (lastDownload != null && includeDownloaded) { + version = lastDownload.getVersion(); + } else { + version = LibsDisguises.getInstance().getDescription().getVersion(); + } + } else { + if (lastDownload != null && includeDownloaded) { + version = lastDownload.getBuildNumber(); + } else { + version = LibsDisguises.getInstance().getBuildNo(); + } + } + + return getUpdate() != null && getUpdate().getVersion().equals(version); + } + public boolean isDownloading() { return downloading.get(); } + public boolean isOldUpdate() { + return getUpdate() == null || + getUpdate().getFetched().before(new Date(System.currentTimeMillis() - TimeUnit.HOURS.toMillis(1))); + } + public boolean isUsingReleaseBuilds() { DisguiseConfig.UpdatesBranch builds = DisguiseConfig.getUpdatesBranch(); @@ -71,30 +111,6 @@ public class UpdateChecker { } } - public boolean isUpdateReady() { - if (getUpdate() == null) { - return false; - } - - String version; - - if (getUpdate().isReleaseBuild()) { - if (lastDownload != null) { - version = lastDownload.getVersion(); - } else { - version = LibsDisguises.getInstance().getDescription().getVersion(); - } - } else { - if (lastDownload != null) { - version = lastDownload.getBuildNumber(); - } else { - version = LibsDisguises.getInstance().getBuildNo(); - } - } - - return getUpdate() != null && !getUpdate().getVersion().equals(version); - } - public void doAutoUpdateCheck() { try { DisguiseUpdate oldUpdate = getUpdate(); @@ -103,7 +119,8 @@ public class UpdateChecker { doUpdateCheck(); - if (!isUpdateReady() || (oldUpdate != null && oldUpdate.getVersion().equals(getUpdate().getVersion()))) { + if (isOnLatestUpdate(true) || + (oldUpdate != null && oldUpdate.getVersion().equals(getUpdate().getVersion()))) { return; } @@ -164,11 +181,13 @@ public class UpdateChecker { if (getUpdate().isReleaseBuild()) { String currentVersion = LibsDisguises.getInstance().getDescription().getVersion(); - if (!isNewerVersion(currentVersion, getUpdate().getVersion())) { + if (LibsDisguises.getInstance().isReleaseBuild() && + !isNewerVersion(currentVersion, getUpdate().getVersion())) { return LibsMsg.UPDATE_ON_LATEST; } - updateMessage = new String[]{LibsMsg.UPDATE_READY.get(currentVersion, getUpdate().getVersion())}; + updateMessage = new String[]{LibsMsg.UPDATE_READY.get(currentVersion, getUpdate().getVersion()), + LibsMsg.UPDATE_HOW.get()}; } else { if (!getUpdate().getVersion().matches("[0-9]+")) { return LibsMsg.UPDATE_FAILED; @@ -180,8 +199,11 @@ public class UpdateChecker { return LibsMsg.UPDATE_ON_LATEST; } + String build = LibsDisguises.getInstance().getBuildNo(); + updateMessage = new String[]{ - LibsMsg.UPDATE_READY_SNAPSHOT.get(LibsDisguises.getInstance().getBuildNo(), newBuild)}; + LibsMsg.UPDATE_READY_SNAPSHOT.get((build.matches("[0-9]+") ? "#" : "") + build, newBuild), + LibsMsg.UPDATE_HOW.get()}; } return null;