diff --git a/src/main/java/dev/plex/command/impl/PlexCMD.java b/src/main/java/dev/plex/command/impl/PlexCMD.java index 4031956..c2932c5 100644 --- a/src/main/java/dev/plex/command/impl/PlexCMD.java +++ b/src/main/java/dev/plex/command/impl/PlexCMD.java @@ -15,9 +15,11 @@ import java.util.List; import java.util.stream.Collectors; import dev.plex.util.PlexLog; +import dev.plex.util.PlexUtils; import net.kyori.adventure.text.Component; import org.apache.commons.lang.StringUtils; import org.bukkit.command.CommandSender; +import org.bukkit.command.ConsoleCommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -88,6 +90,15 @@ public class PlexCMD extends PlexCommand return componentFromString("All modules reloaded!"); } } + else if (args[0].equalsIgnoreCase("update")) + { + if (sender instanceof Player player && !PlexUtils.DEVELOPERS.contains(player.getUniqueId().toString())) + { + return messageComponent("noPermissionConsole"); + } + plugin.getUpdateChecker().updateJar(); + return null; + } else { return usage(); diff --git a/src/main/java/dev/plex/util/UpdateChecker.java b/src/main/java/dev/plex/util/UpdateChecker.java index 8d37e02..bb8c855 100644 --- a/src/main/java/dev/plex/util/UpdateChecker.java +++ b/src/main/java/dev/plex/util/UpdateChecker.java @@ -7,14 +7,26 @@ 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; +import org.apache.http.HttpResponse; +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.command.CommandSender; +import org.bukkit.util.FileUtil; +import org.json.JSONArray; +import org.json.JSONObject; public class UpdateChecker extends PlexBase { @@ -107,8 +119,39 @@ public class UpdateChecker extends PlexBase default -> { sender.sendMessage(Component.text("Your version of Plex is not up to date!", NamedTextColor.RED)); sender.sendMessage(Component.text("Download a new version at: " + DOWNLOAD_PAGE).color(NamedTextColor.RED)); + sender.sendMessage(Component.text("Or run: /plex update").color(NamedTextColor.RED)); return true; } } } + + public void updateJar() + { + CloseableHttpClient client = HttpClients.createDefault(); + HttpGet get = new HttpGet(DOWNLOAD_PAGE + "lastSuccessfulBuild/api/json"); + try + { + HttpResponse response = client.execute(get); + JSONObject object = new JSONObject(EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8)); + JSONObject artifact = object.getJSONArray("artifacts").getJSONObject(0); + String name = artifact.getString("displayPath"); + PlexLog.log("Downloading latest Plex jar file: " + name); + CompletableFuture.runAsync(() -> { + try + { + FileUtils.copyURLToFile( + new URL(DOWNLOAD_PAGE + "lastSuccessfulBuild/artifact/build/libs/" + name), + new File(Plex.get().getDataFolder() + File.separator + "..", name) + ); + PlexLog.log("Saved new jar. Please restart your server."); + } catch (IOException e) + { + e.printStackTrace(); + } + }); + } catch (IOException e) + { + e.printStackTrace(); + } + } }