Add /plex update

This commit is contained in:
Taah 2022-04-06 20:55:54 -07:00
parent 5012d0478b
commit 1f653ab5df
2 changed files with 54 additions and 0 deletions

View File

@ -15,9 +15,11 @@ import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import dev.plex.util.PlexLog; import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -88,6 +90,15 @@ public class PlexCMD extends PlexCommand
return componentFromString("All modules reloaded!"); 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 else
{ {
return usage(); return usage();

View File

@ -7,14 +7,26 @@ import com.google.gson.JsonSyntaxException;
import dev.plex.Plex; import dev.plex.Plex;
import dev.plex.PlexBase; import dev.plex.PlexBase;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; 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.command.CommandSender;
import org.bukkit.util.FileUtil;
import org.json.JSONArray;
import org.json.JSONObject;
public class UpdateChecker extends PlexBase public class UpdateChecker extends PlexBase
{ {
@ -107,8 +119,39 @@ public class UpdateChecker extends PlexBase
default -> { default -> {
sender.sendMessage(Component.text("Your version of Plex is not up to date!", NamedTextColor.RED)); 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("Download a new version at: " + DOWNLOAD_PAGE).color(NamedTextColor.RED));
sender.sendMessage(Component.text("Or run: /plex update").color(NamedTextColor.RED));
return true; 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();
}
}
} }