This commit is contained in:
ayunami2000 2022-04-07 00:41:31 -04:00
commit c6aa4cf0a4
3 changed files with 77 additions and 15 deletions

16
Jenkinsfile vendored Normal file
View File

@ -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()
}
}
}

View File

@ -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("<red>Plex is already up to date!");
}
plugin.getUpdateChecker().updateJar();
return null;
}

View File

@ -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
{
/*
@ -80,6 +80,44 @@ public class UpdateChecker extends PlexBase
}
}
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
@ -136,20 +174,23 @@ 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.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();
}