From 88c9546a88d400e98a508dd3ab2c6b197b411eb4 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Fri, 24 Apr 2020 17:04:55 +1200 Subject: [PATCH] Give update info on command update --- .../commands/libsdisguises/LDUpdate.java | 11 ++++++-- .../disguise/utilities/LibsPremium.java | 13 +++++----- .../disguise/utilities/UpdateChecker.java | 26 ++++++++++--------- .../utilities/plugin/PluginInformation.java | 12 ++++++--- .../utilities/translations/LibsMsg.java | 1 + 5 files changed, 40 insertions(+), 23 deletions(-) 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 eb115ca8..89bb6a53 100644 --- a/src/main/java/me/libraryaddict/disguise/commands/libsdisguises/LDUpdate.java +++ b/src/main/java/me/libraryaddict/disguise/commands/libsdisguises/LDUpdate.java @@ -2,6 +2,7 @@ package me.libraryaddict.disguise.commands.libsdisguises; import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.utilities.UpdateChecker; +import me.libraryaddict.disguise.utilities.plugin.PluginInformation; import me.libraryaddict.disguise.utilities.translations.LibsMsg; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; @@ -51,7 +52,7 @@ public class LDUpdate implements LDCommand { new BukkitRunnable() { @Override public void run() { - boolean result; + PluginInformation result; if (force) { result = checker.grabLatestSnapshot(); @@ -59,15 +60,21 @@ public class LDUpdate implements LDCommand { result = checker.grabSnapshotBuild(); } - if (!result) { + if (result == null) { sender.sendMessage(LibsMsg.UPDATE_FAILED.get()); return; } sender.sendMessage(LibsMsg.UPDATE_SUCCESS.get()); // Update success, please restart to update + sender.sendMessage(LibsMsg.UPDATE_INFO + .get(result.getVersion(), result.getBuildNumber(), result.getParsedBuildDate().toString(), + result.getSize() / 1024)); if (sender instanceof Player) { Bukkit.getConsoleSender().sendMessage(LibsMsg.UPDATE_SUCCESS.get()); + sender.sendMessage(LibsMsg.UPDATE_INFO + .get(result.getVersion(), result.getBuildNumber(), result.getParsedBuildDate().toString(), + result.getSize() / 1024)); } } }.runTaskAsynchronously(LibsDisguises.getInstance()); diff --git a/src/main/java/me/libraryaddict/disguise/utilities/LibsPremium.java b/src/main/java/me/libraryaddict/disguise/utilities/LibsPremium.java index 956ee496..51c3425f 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/LibsPremium.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/LibsPremium.java @@ -112,7 +112,7 @@ public class LibsPremium { //return premiumVersion.equals(currentVersion); } - private static PluginInformation getInformation(File file) throws Exception { + public static PluginInformation getInformation(File file) throws Exception { try (URLClassLoader cl = new URLClassLoader(new URL[]{file.toURI().toURL()})) { Class c = cl.loadClass(LibsPremium.class.getName()); @@ -166,8 +166,8 @@ public class LibsPremium { String pluginVersion = config.getString("version"); - return new PluginInformation(userId, resourceId, downloadId, premium, pluginVersion, pluginBuildNumber, - pluginBuildDate); + return new PluginInformation(file.length(), userId, resourceId, downloadId, premium, pluginVersion, + pluginBuildNumber, pluginBuildDate); } } @@ -243,7 +243,7 @@ public class LibsPremium { if (bisectHosted = new BisectHosting().isBisectHosted("LibsDisguises")) { DisguiseUtilities.getLogger().info("Hosted by BisectHosting! Premium enabled!"); - paidInformation = new PluginInformation("0", "32453", "0", true, "0", "#0", "0"); + paidInformation = new PluginInformation(0, "0", "32453", "0", true, "0", "#0", "0"); thisPluginIsPaidFor = true; } else { @@ -305,8 +305,9 @@ public class LibsPremium { buildNo = "#" + buildNo; } - pluginInformation = new PluginInformation(getUserID(), getResourceID(), getDownloadID(), - isPremium(getResourceID(), getUserID()), version, buildNo, pluginBuildDate); + pluginInformation = new PluginInformation(LibsDisguises.getInstance().getFile().length(), getUserID(), + getResourceID(), getDownloadID(), isPremium(getResourceID(), getUserID()), version, buildNo, + pluginBuildDate); } if (!isPremium() || !LibsDisguises.getInstance().isReleaseBuild()) { diff --git a/src/main/java/me/libraryaddict/disguise/utilities/UpdateChecker.java b/src/main/java/me/libraryaddict/disguise/utilities/UpdateChecker.java index 0b43289c..875f8547 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/UpdateChecker.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/UpdateChecker.java @@ -3,6 +3,7 @@ package me.libraryaddict.disguise.utilities; import com.google.gson.Gson; import lombok.Getter; import me.libraryaddict.disguise.LibsDisguises; +import me.libraryaddict.disguise.utilities.plugin.PluginInformation; import org.bukkit.Bukkit; import org.bukkit.craftbukkit.libs.org.apache.commons.io.FileUtils; @@ -31,38 +32,38 @@ public class UpdateChecker { this.resourceID = resourceID; } - public boolean grabSnapshotBuild() { + public PluginInformation grabSnapshotBuild() { if (getLatestSnapshot() == 0) { throw new IllegalArgumentException(); } if (lastDownload == -1) { - return false; + return null; } if (getLatestSnapshot() == lastDownload) { - return false; + return null; } return grabSnapshotBuild(getLatestSnapshot()); } - public boolean grabSnapshotBuild(int buildNo) { - boolean result = grabSnapshotBuild( + public PluginInformation grabSnapshotBuild(int buildNo) { + PluginInformation result = grabSnapshotBuild( "https://ci.md-5.net/job/LibsDisguises/" + buildNo + "/artifact/target/LibsDisguises.jar"); - if (result) { + if (result != null) { lastDownload = buildNo; } return result; } - public boolean grabLatestSnapshot() { - boolean result = grabSnapshotBuild( + public PluginInformation grabLatestSnapshot() { + PluginInformation result = grabSnapshotBuild( "https://ci.md-5.net/job/LibsDisguises/lastSuccessfulBuild/artifact/target/LibsDisguises.jar"); - if (result) { + if (result != null) { lastDownload = LibsDisguises.getInstance().getBuildNumber(); } @@ -77,7 +78,7 @@ public class UpdateChecker { return lastDownload; } - public boolean grabSnapshotBuild(String urlString) { + public PluginInformation grabSnapshotBuild(String urlString) { DisguiseUtilities.getLogger().info("Now downloading latest build of Lib's Disguises from " + urlString); lastDownload = -1; @@ -101,7 +102,8 @@ public class UpdateChecker { } DisguiseUtilities.getLogger().info("Download success!"); - return true; + + return LibsPremium.getInformation(dest); } catch (Exception ex) { // Failed, set the last download back to previous build @@ -111,7 +113,7 @@ public class UpdateChecker { ex.printStackTrace(); } - return false; + return null; } public void checkSnapshotUpdate(int buildNumber) { diff --git a/src/main/java/me/libraryaddict/disguise/utilities/plugin/PluginInformation.java b/src/main/java/me/libraryaddict/disguise/utilities/plugin/PluginInformation.java index 48b681a2..3c6a20be 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/plugin/PluginInformation.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/plugin/PluginInformation.java @@ -1,5 +1,7 @@ package me.libraryaddict.disguise.utilities.plugin; +import lombok.Getter; + import java.text.SimpleDateFormat; import java.util.Date; @@ -7,6 +9,8 @@ import java.util.Date; * Created by libraryaddict on 20/06/2019. */ public class PluginInformation { + @Getter + private long size; private String userID; private String resourceID; private String downloadID; @@ -15,8 +19,9 @@ public class PluginInformation { private String buildNumber; private String buildDate; - public PluginInformation(String userID, String resourceID, String downloadID, boolean premium, String version, - String buildNumber, String buildDate) { + public PluginInformation(long size, String userID, String resourceID, String downloadID, boolean premium, + String version, String buildNumber, String buildDate) { + this.size = size; this.userID = userID; this.resourceID = resourceID; this.downloadID = downloadID; @@ -65,6 +70,7 @@ public class PluginInformation { } public boolean isLegit() { - return getUserID().matches("[0-9]+") && !getUserID().equals("12345") && getResourceID().equals("32453") && getDownloadID().matches("-?[0-9]+"); + return getUserID().matches("[0-9]+") && !getUserID().equals("12345") && getResourceID().equals("32453") && + getDownloadID().matches("-?[0-9]+"); } } 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 98456d91..ca7d38f5 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java @@ -148,6 +148,7 @@ public enum LibsMsg { UPDATE_ON_LATEST(ChatColor.RED + "You are already on the latest version of LibsDisguises!"), UPDATE_FAILED(ChatColor.RED + "LibsDisguises update failed! Check console for errors."), UPDATE_SUCCESS(ChatColor.DARK_GREEN + "LibsDisguises update success! Restart server to update!"), + UPDATE_INFO(ChatColor.DARK_GREEN + "Lib's Disguises v%s, build %b, 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!"), NO_MODS_LISTENING(ChatColor.RED + "This server is not listening for mods!"),