From ea862e152fb890970375884c70be2440cb195315 Mon Sep 17 00:00:00 2001 From: Taah Date: Wed, 6 Apr 2022 21:08:03 -0700 Subject: [PATCH 1/2] check for update status instead of sending msg --- .../java/dev/plex/command/impl/PlexCMD.java | 5 ++ .../java/dev/plex/util/UpdateChecker.java | 77 +++++++++++++------ 2 files changed, 60 insertions(+), 22 deletions(-) diff --git a/src/main/java/dev/plex/command/impl/PlexCMD.java b/src/main/java/dev/plex/command/impl/PlexCMD.java index c2932c5..ee3dcb9 100644 --- a/src/main/java/dev/plex/command/impl/PlexCMD.java +++ b/src/main/java/dev/plex/command/impl/PlexCMD.java @@ -17,6 +17,7 @@ import java.util.stream.Collectors; import dev.plex.util.PlexLog; import dev.plex.util.PlexUtils; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.MiniMessage; import org.apache.commons.lang.StringUtils; import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; @@ -96,6 +97,10 @@ public class PlexCMD extends PlexCommand { return messageComponent("noPermissionConsole"); } + if (!plugin.getUpdateChecker().getUpdateStatus(false)) + { + return MiniMessage.miniMessage().deserialize("Plex is already up to date!"); + } plugin.getUpdateChecker().updateJar(); return null; } diff --git a/src/main/java/dev/plex/util/UpdateChecker.java b/src/main/java/dev/plex/util/UpdateChecker.java index bb8c855..4b0a6ba 100644 --- a/src/main/java/dev/plex/util/UpdateChecker.java +++ b/src/main/java/dev/plex/util/UpdateChecker.java @@ -6,15 +6,6 @@ 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.File; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.URL; -import java.nio.charset.StandardCharsets; -import java.util.concurrent.CompletableFuture; -import javax.annotation.Nonnull; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import org.apache.commons.io.FileUtils; @@ -23,11 +14,20 @@ import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; +import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; -import org.bukkit.util.FileUtil; -import org.json.JSONArray; import org.json.JSONObject; +import javax.annotation.Nonnull; +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.concurrent.CompletableFuture; + public class UpdateChecker extends PlexBase { /* @@ -46,7 +46,7 @@ public class UpdateChecker extends PlexBase { try { - HttpURLConnection connection = (HttpURLConnection)new URL("https://api.github.com/repos/" + repo + "/compare/" + branch + "..." + hash).openConnection(); + HttpURLConnection connection = (HttpURLConnection) new URL("https://api.github.com/repos/" + repo + "/compare/" + branch + "..." + hash).openConnection(); connection.connect(); if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { @@ -66,20 +66,54 @@ public class UpdateChecker extends PlexBase case "behind" -> obj.get("behind_by").getAsInt(); default -> -1; }; - } - catch (JsonSyntaxException | NumberFormatException e) + } catch (JsonSyntaxException | NumberFormatException e) { e.printStackTrace(); return -1; } - } - catch (IOException e) + } catch (IOException e) { e.printStackTrace(); return -1; } } + public boolean getUpdateStatus(boolean cached) + { + if (distance == -4) + { + distance = fetchDistanceFromGitHub("plexusorg/Plex", "master", Plex.build.head); + PlexLog.debug("Never checked for updates, checking now..."); + } else + { + // If the request isn't asked to be cached, fetch it + if (!cached) + { + distance = fetchDistanceFromGitHub("plexusorg/Plex", "master", Plex.build.head); + PlexLog.debug("We have checked for updates before, but this request was not asked to be cached."); + } else + { + PlexLog.debug("We have checked for updates before, using cache."); + } + } + + switch (distance) + { + case -1 -> { + return false; + } + case 0 -> { + return false; + } + case -2 -> { + return false; + } + default -> { + return true; + } + } + } + public boolean getUpdateStatusMessage(CommandSender sender, boolean cached) { // If it's -4, it hasn't checked for updates yet @@ -87,16 +121,14 @@ public class UpdateChecker extends PlexBase { distance = fetchDistanceFromGitHub("plexusorg/Plex", "master", Plex.build.head); PlexLog.debug("Never checked for updates, checking now..."); - } - else + } else { // If the request isn't asked to be cached, fetch it if (!cached) { distance = fetchDistanceFromGitHub("plexusorg/Plex", "master", Plex.build.head); PlexLog.debug("We have checked for updates before, but this request was not asked to be cached."); - } - else + } else { PlexLog.debug("We have checked for updates before, using cache."); } @@ -136,12 +168,13 @@ public class UpdateChecker extends PlexBase JSONObject artifact = object.getJSONArray("artifacts").getJSONObject(0); String name = artifact.getString("displayPath"); PlexLog.log("Downloading latest Plex jar file: " + name); - CompletableFuture.runAsync(() -> { + CompletableFuture.runAsync(() -> + { try { FileUtils.copyURLToFile( new URL(DOWNLOAD_PAGE + "lastSuccessfulBuild/artifact/build/libs/" + name), - new File(Plex.get().getDataFolder() + File.separator + "..", name) + new File(Bukkit.getUpdateFolder(), name) ); PlexLog.log("Saved new jar. Please restart your server."); } catch (IOException e) From ca18b121643e09fe6d5e5e160d584a444c4fcb07 Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Wed, 6 Apr 2022 23:14:44 -0500 Subject: [PATCH 2/2] Try this --- Jenkinsfile | 16 +++++++++++ .../java/dev/plex/util/UpdateChecker.java | 28 ++++++++++++------- 2 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 Jenkinsfile diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..dfd8be3 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,16 @@ +pipeline { + agent any + stages { + stage('build') { + steps { + sh './gradlew build javadoc publish --no-daemon' + } + } + } + post { + always { + archiveArtifacts artifacts: 'build/libs/*.jar', fingerprint: true + deleteWs() + } + } +} \ No newline at end of file diff --git a/src/main/java/dev/plex/util/UpdateChecker.java b/src/main/java/dev/plex/util/UpdateChecker.java index 4b0a6ba..a846f13 100644 --- a/src/main/java/dev/plex/util/UpdateChecker.java +++ b/src/main/java/dev/plex/util/UpdateChecker.java @@ -46,7 +46,7 @@ public class UpdateChecker extends PlexBase { try { - HttpURLConnection connection = (HttpURLConnection) new URL("https://api.github.com/repos/" + repo + "/compare/" + branch + "..." + hash).openConnection(); + HttpURLConnection connection = (HttpURLConnection)new URL("https://api.github.com/repos/" + repo + "/compare/" + branch + "..." + hash).openConnection(); connection.connect(); if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { @@ -66,12 +66,14 @@ public class UpdateChecker extends PlexBase case "behind" -> obj.get("behind_by").getAsInt(); default -> -1; }; - } catch (JsonSyntaxException | NumberFormatException e) + } + catch (JsonSyntaxException | NumberFormatException e) { e.printStackTrace(); return -1; } - } catch (IOException e) + } + catch (IOException e) { e.printStackTrace(); return -1; @@ -84,14 +86,16 @@ public class UpdateChecker extends PlexBase { distance = fetchDistanceFromGitHub("plexusorg/Plex", "master", Plex.build.head); PlexLog.debug("Never checked for updates, checking now..."); - } else + } + else { // If the request isn't asked to be cached, fetch it if (!cached) { distance = fetchDistanceFromGitHub("plexusorg/Plex", "master", Plex.build.head); PlexLog.debug("We have checked for updates before, but this request was not asked to be cached."); - } else + } + else { PlexLog.debug("We have checked for updates before, using cache."); } @@ -121,14 +125,16 @@ public class UpdateChecker extends PlexBase { distance = fetchDistanceFromGitHub("plexusorg/Plex", "master", Plex.build.head); PlexLog.debug("Never checked for updates, checking now..."); - } else + } + else { // If the request isn't asked to be cached, fetch it if (!cached) { distance = fetchDistanceFromGitHub("plexusorg/Plex", "master", Plex.build.head); PlexLog.debug("We have checked for updates before, but this request was not asked to be cached."); - } else + } + else { PlexLog.debug("We have checked for updates before, using cache."); } @@ -174,15 +180,17 @@ public class UpdateChecker extends PlexBase { FileUtils.copyURLToFile( new URL(DOWNLOAD_PAGE + "lastSuccessfulBuild/artifact/build/libs/" + name), - new File(Bukkit.getUpdateFolder(), name) + new File(Bukkit.getUpdateFolderFile(), name) ); PlexLog.log("Saved new jar. Please restart your server."); - } catch (IOException e) + } + catch (IOException e) { e.printStackTrace(); } }); - } catch (IOException e) + } + catch (IOException e) { e.printStackTrace(); }