From c8e6a021cd8b6ff1bc8939624feb67155c93a393 Mon Sep 17 00:00:00 2001 From: Raik176 Date: Wed, 8 Nov 2023 23:28:20 +0100 Subject: [PATCH] Add more options to .ip & .im --- .../autoplug/client/console/Commands.java | 191 +++++++++++++++--- .../tasks/updater/mods/ModrinthAPI.java | 26 ++- .../updater/plugins/MinecraftPlugin.java | 7 + .../tasks/updater/plugins/ResourceFinder.java | 10 +- .../updater/plugins/TaskPluginsUpdater.java | 14 ++ .../tasks/updater/search/GithubSearch.java | 2 +- 6 files changed, 209 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/osiris/autoplug/client/console/Commands.java b/src/main/java/com/osiris/autoplug/client/console/Commands.java index 26ddd1e9..532f26e5 100644 --- a/src/main/java/com/osiris/autoplug/client/console/Commands.java +++ b/src/main/java/com/osiris/autoplug/client/console/Commands.java @@ -45,6 +45,8 @@ import java.nio.file.*; import java.nio.file.attribute.BasicFileAttributes; import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * Listens for input started with . @@ -91,8 +93,8 @@ public static boolean execute(@NotNull String command) { AL.info(".server info | Shows details about this server (.si)"); AL.info(""); AL.info("Direct install commands:"); - AL.info(".install plugin /bukkit> | Installs a new plugin (.ip)"); - AL.info(".install mod /curseforge> | Installs a new mod (.im)"); + AL.info(".install plugin spigot|bukkit|github|modrinth|search | Installs a new plugin (.ip)"); + AL.info(".install mod modrinth|curseforge | Installs a new mod (.im)"); AL.info(""); AL.info("Update checking commands: (note that all the checks below"); AL.info("ignore the cool-down and behave according to the selected profile)"); @@ -333,21 +335,97 @@ private static void findJavaInstallationsInDrive(File directory, List in } } + static final Pattern pluginPattern = Pattern.compile("(spigot|bukkit|github|modrinth|search)([\\w.!?_,\\-]+)"); public static boolean installPlugin(String command) throws Exception { String input = command.replaceFirst("\\.install plugin", "").replaceFirst("\\.ip", "").trim(); SearchResult result = null; String tempName = "NEW_PLUGIN"; File pluginsDir = FileManager.convertRelativeToAbsolutePath(new UpdaterConfig().plugins_updater_path.asString()); - if (input.contains("spigot")) { - int spigotId = Integer.parseInt(input.replace("spigot", "")); - result = new ResourceFinder().findPluginBySpigotId(new MinecraftPlugin(new File(pluginsDir + "/" + tempName).getAbsolutePath(), tempName, "0", "", spigotId, 0, "")); - } else if (input.contains("bukkit")) { - int bukkitId = Integer.parseInt(input.replace("bukkit", "")); - result = new ResourceFinder().findPluginByBukkitId(new MinecraftPlugin(new File(pluginsDir + "/" + tempName).getAbsolutePath(), - tempName, "0", "", 0, bukkitId, "")); - } else { - AL.warn("Your id \"" + input + "\" must start with either spigot or bukkit."); - return false; + String[] vars = input.split(" "); + MinecraftPlugin plugin = new MinecraftPlugin(new File(pluginsDir + "/" + tempName).getAbsolutePath(), + tempName, "0", "", 0, 0, ""); + String mcVersion = new UpdaterConfig().mods_updater_version.asString(); + + if (mcVersion == null) mcVersion = new UtilsMinecraft().getInstalledVersion(); + if (mcVersion == null) throw new NullPointerException("Failed to determine Minecraft version."); + if (vars.length < 2) { //Try the regex support thing (Probably would be better to just remove whitespaces from the input) + Matcher m = pluginPattern.matcher(input); + if (!m.find()) return false; + vars = new String[] {m.group(1), m.group(2)}; + } + switch (vars[0]) { + case "spigot": + int spigotId = Integer.parseInt(vars[1]); + result = new ResourceFinder().findPluginBySpigotId(new MinecraftPlugin(new File(pluginsDir + "/" + tempName).getAbsolutePath(), tempName, "0", "", spigotId, 0, "")); + break; + case "bukkit": + int bukkitId = Integer.parseInt(vars[1]); + result = new ResourceFinder().findPluginByBukkitId(new MinecraftPlugin(new File(pluginsDir + "/" + tempName).getAbsolutePath(), + tempName, "0", "", 0, bukkitId, "")); + break; + case "github": + String[] split = vars[1].split("/"); + if (split.length < 2) { + AL.warn("The github format must be githubrepo/asset"); + return false; + } + plugin.setGithubRepoName(split[0]); + plugin.setGithubAssetName(split[1]); + result = new ResourceFinder().findByGithubUrl(plugin); + break; + case "modrinth": + MinecraftPlugin plugin2 = new MinecraftPlugin(new File(pluginsDir + "/" + tempName).getAbsolutePath(), + tempName, "0", "", 0, 0, ""); + plugin2.setModrinthId(vars[1]); + result = new ResourceFinder().findPluginByModrinthId(plugin2, mcVersion); + break; + case "search": + //Try every single way to download a plugin and stop if it finds something + try { // SPIGOT + int id = Integer.parseInt(vars[1]); + plugin.setSpigotId(id); + result = new ResourceFinder().findPluginBySpigotId(plugin); + } catch (Exception e) { + plugin.setSpigotId(0); + } + if (result == null) { + try { // BUKKIT + int id = Integer.parseInt(vars[1]); + plugin.setBukkitId(id); + result = new ResourceFinder().findPluginByBukkitId(plugin); + } catch (Exception e) { + plugin.setBukkitId(0); + } + } + if (result == null) { + try { // GITHUB + split = vars[1].split("/"); + if (split.length > 1) { + plugin.setGithubRepoName(split[0]); + plugin.setGithubAssetName(split[1]); + result = new ResourceFinder().findByGithubUrl(plugin); + } + } catch (Exception e) { + plugin.setGithubAssetName(null); + plugin.setGithubRepoName(null); + } + } + if (result == null) { + try { //MODRINTH + plugin.setModrinthId(vars[1]); + result = new ResourceFinder().findPluginByModrinthId(plugin, mcVersion); + } catch (Exception e) { + plugin.setModrinthId(null); + } + } + if (result == null || result.isError() || result.plugin == null) { + AL.warn("No plugin with that id found."); + return false; + } + break; + default: + AL.warn("Invalid syntax, use '.help' for more info."); //too lazy to add every argument here + return false; } if (result.isError()) { AL.warn(result.exception); @@ -371,8 +449,15 @@ public static boolean installPlugin(String command) throws Exception { return true; } + static final Pattern modPattern = Pattern.compile("(curse|forge|curseforge|modrinth|github|search)([\\w.!?_,\\-]+)"); public static boolean installMod(String command) throws Exception { String input = command.replaceFirst("\\.install mod", "").replaceFirst("\\.im", "").trim(); + String[] vars = input.split(" "); + if (vars.length < 2) { //Try the regex support thing (Probably would be better to just remove whitespaces from the input) + Matcher m = modPattern.matcher(input); + if (!m.find()) return false; + vars = new String[] {m.group(1), m.group(2)}; + } SearchResult result = null; String tempName = "NEW_MOD"; UpdaterConfig updaterConfig = new UpdaterConfig(); @@ -380,21 +465,69 @@ public static boolean installMod(String command) throws Exception { String mcVersion = updaterConfig.mods_updater_version.asString(); if (mcVersion == null) mcVersion = new UtilsMinecraft().getInstalledVersion(); if (mcVersion == null) throw new NullPointerException("Failed to determine Minecraft version."); + MinecraftMod mod = new MinecraftMod(new File(modsDir + "/" + tempName).getAbsolutePath(), tempName, "0", + "", "0", "0", ""); - if (input.contains("modrinth")) { - String modrinthId = input.replace("modrinth", ""); - result = new ResourceFinder().findModByModrinthId(new InstalledModLoader(), - new MinecraftMod(new File(modsDir + "/" + tempName).getAbsolutePath(), tempName, "0", - "", modrinthId, "0", ""), - mcVersion); - } else if (input.contains("curseforge")) { - String curseforgeId = input.replace("curseforge", ""); - result = new ResourceFinder().findModByCurseforgeId(new InstalledModLoader(), new MinecraftMod(new File(modsDir + "/" + tempName).getAbsolutePath(), - tempName, "0", "", "0", curseforgeId, ""), - mcVersion, updaterConfig.mods_update_check_name_for_mod_loader.asBoolean()); - } else { - AL.warn("Your id \"" + input + "\" must start with either modrinth or curseforge."); - return false; + switch (vars[0]) { + case "modrinth": + mod.modrinthId = vars[1]; + result = new ResourceFinder().findModByModrinthId(new InstalledModLoader(), + mod, mcVersion); + break; + case "curse": + case "forge": + case "curseforge": + mod.curseforgeId = vars[1]; + result = new ResourceFinder().findModByCurseforgeId(new InstalledModLoader(), mod, + mcVersion, updaterConfig.mods_update_check_name_for_mod_loader.asBoolean()); + break; + case "github": + String[] split = vars[1].split("/"); + if (split.length < 2) { + AL.warn("The github format must be githubrepo/asset"); + return false; + } + mod.githubRepoName = split[0]; + mod.githubAssetName = split[1]; + result = new ResourceFinder().findByGithubUrl(mod); + break; + case "search": + try { //MODRINTH + mod.modrinthId = vars[1]; + result = new ResourceFinder().findModByModrinthId(new InstalledModLoader(), mod, mcVersion); + } catch (Exception e) { + mod.modrinthId = null; + } + if (result == null) { + try { // GITHUB + split = vars[1].split("/"); + if (split.length > 1) { + mod.githubRepoName = split[0]; + mod.githubAssetName = split[1]; + result = new ResourceFinder().findByGithubUrl(mod); + } + } catch (Exception e) { + mod.githubRepoName = null; + mod.githubAssetName = null; + } + } + if (result == null) { + try { //CURSEFORGE + mod.curseforgeId = vars[1]; + result = new ResourceFinder().findModByCurseforgeId(new InstalledModLoader(), mod, + mcVersion, updaterConfig.mods_update_check_name_for_mod_loader.asBoolean()); + } catch (Exception e) { + mod.curseforgeId = null; + } + } + if (result == null || result.isError() || result.plugin == null) { + AL.warn("No mod with that id found."); + return false; + } + break; + default: + AL.warn("Invalid syntax, use '.help' for more info."); //too lazy to add every argument here + return false; } if (result.isError()) { AL.warn(result.exception); @@ -407,11 +540,11 @@ public static boolean installMod(String command) throws Exception { task.start(); new UtilsTasks().printResultsWhenDone(myManager.manager); List plugins = new UtilsMinecraft().getMods(modsDir); - for (MinecraftMod mod : plugins) { - if (mod.installationPath.equals(finalDest.getAbsolutePath())) { + for (MinecraftMod mod2 : plugins) { + if (mod2.installationPath.equals(finalDest.getAbsolutePath())) { // Replace tempName with actual plugin name finalDest = new UtilsFile().renameFile(task.getFinalDest(), - new File(mod.installationPath).getName().replace(tempName, mod.getName())); + new File(mod2.installationPath).getName().replace(tempName, mod2.getName())); } } AL.info("Installed to: " + finalDest); diff --git a/src/main/java/com/osiris/autoplug/client/tasks/updater/mods/ModrinthAPI.java b/src/main/java/com/osiris/autoplug/client/tasks/updater/mods/ModrinthAPI.java index 4b6726d1..76acd539 100644 --- a/src/main/java/com/osiris/autoplug/client/tasks/updater/mods/ModrinthAPI.java +++ b/src/main/java/com/osiris/autoplug/client/tasks/updater/mods/ModrinthAPI.java @@ -9,6 +9,7 @@ package com.osiris.autoplug.client.tasks.updater.mods; import com.google.gson.JsonObject; +import com.osiris.autoplug.client.tasks.updater.plugins.MinecraftPlugin; import com.osiris.autoplug.client.tasks.updater.search.SearchResult; import com.osiris.autoplug.client.utils.UtilsURL; import com.osiris.jlib.json.Json; @@ -35,10 +36,18 @@ private boolean isInt(String s) { * Requires a modrithId (chars or number), or curseforgeId (no number, but chars). * If the id contains chars its usually the mods slugs. */ - public SearchResult searchUpdate(InstalledModLoader modLoader, MinecraftMod mod, String mcVersion) { + public SearchResult searchUpdateMod(InstalledModLoader modLoader, MinecraftMod mod, String mcVersion) { if (mod.modrinthId == null && !isInt(mod.curseforgeId)) mod.modrinthId = mod.curseforgeId; // Slug - String url = baseUrl + "/project/" + mod.modrinthId + "/version?loaders=[\"" + - (modLoader.isFabric || modLoader.isQuilt ? "fabric" : "forge") + "\"]&game_versions=[\"" + mcVersion + "\"]"; + SearchResult res = searchUpdate((modLoader.isFabric || modLoader.isQuilt ? "fabric" : "forge"),mod.modrinthId,mcVersion, mod.installationPath, mod.forceLatest); + res.mod = mod; + return res; + } + public SearchResult searchUpdatePlugin(MinecraftPlugin plugin, String mcVersion) { //TODO: probably don't hardcode spigot and papermc + return searchUpdate("spigot\",\"paper",plugin.getModrinthId(), mcVersion, plugin.getInstallationPath(), false); + } + private SearchResult searchUpdate(String loader, String id, String mcVersion, String installPath, boolean forceLatest) { + + String url = baseUrl + "/project/" + id + "/version?loaders=[\"" + loader + "\"]&game_versions=[\"" + mcVersion + "\"]"; url = new UtilsURL().clean(url); Exception exception = null; String latest = null; @@ -46,7 +55,7 @@ public SearchResult searchUpdate(InstalledModLoader modLoader, MinecraftMod mod, String downloadUrl = null; byte code = 0; try { - if (mod.modrinthId == null) + if (id == null) throw new Exception("Modrinth-id is null!"); // Modrinth id can be slug or actual id AL.debug(this.getClass(), url); @@ -55,10 +64,10 @@ public SearchResult searchUpdate(InstalledModLoader modLoader, MinecraftMod mod, release = Json.getAsJsonArray(url) .get(0).getAsJsonObject(); } catch (Exception e) { - if (!isInt(mod.modrinthId)) { // Try another url, with slug replaced _ with - - url = baseUrl + "/project/" + mod.modrinthId.replace("_", "-") + if (!isInt(id)) { // Try another url, with slug replaced _ with - + url = baseUrl + "/project/" + id.replace("_", "-") + "/version?loaders=[\"" + - (modLoader.isFabric || modLoader.isQuilt ? "fabric" : "forge") + "\"]" + (mod.forceLatest ? "" : "&game_versions=[\"" + mcVersion + "\"]"); + loader + "\"]" + (forceLatest ? "" : "&game_versions=[\"" + mcVersion + "\"]"); AL.debug(this.getClass(), url); release = Json.getAsJsonArray(url) .get(0).getAsJsonObject(); @@ -67,7 +76,7 @@ public SearchResult searchUpdate(InstalledModLoader modLoader, MinecraftMod mod, } latest = release.get("version_number").getAsString().replaceAll("[^0-9.]", ""); // Before passing over remove everything except numbers and dots - if (new File(mod.installationPath).lastModified() < Instant.parse(release.get("date_published").getAsString()).toEpochMilli()) + if (new File(installPath).lastModified() < Instant.parse(release.get("date_published").getAsString()).toEpochMilli()) code = 1; JsonObject releaseDownload = release.getAsJsonArray("files").get(0).getAsJsonObject(); downloadUrl = releaseDownload.get("url").getAsString(); @@ -81,7 +90,6 @@ public SearchResult searchUpdate(InstalledModLoader modLoader, MinecraftMod mod, code = 2; } SearchResult result = new SearchResult(null, code, latest, downloadUrl, type, null, null, false); - result.mod = mod; result.setException(exception); return result; } diff --git a/src/main/java/com/osiris/autoplug/client/tasks/updater/plugins/MinecraftPlugin.java b/src/main/java/com/osiris/autoplug/client/tasks/updater/plugins/MinecraftPlugin.java index 4603e381..8b545871 100644 --- a/src/main/java/com/osiris/autoplug/client/tasks/updater/plugins/MinecraftPlugin.java +++ b/src/main/java/com/osiris/autoplug/client/tasks/updater/plugins/MinecraftPlugin.java @@ -24,6 +24,7 @@ public class MinecraftPlugin { private String jenkinsProjectUrl; private String jenkinsArtifactName; private int jenkinsBuildId; + private String modrinthId; public MinecraftPlugin(String installationPath, String name, String version, String author, int spigotId, int bukkitId, String customDownloadURL) { this.installationPath = installationPath; @@ -59,6 +60,12 @@ public void setJenkinsBuildId(int jenkinsBuildId) { this.jenkinsBuildId = jenkinsBuildId; } + public String getModrinthId() { + return modrinthId; + } + + public void setModrinthId(String modrinthId) { this.modrinthId = modrinthId; } + public String getConfigPath() { return configPath; } diff --git a/src/main/java/com/osiris/autoplug/client/tasks/updater/plugins/ResourceFinder.java b/src/main/java/com/osiris/autoplug/client/tasks/updater/plugins/ResourceFinder.java index 66740dc5..a5af354c 100644 --- a/src/main/java/com/osiris/autoplug/client/tasks/updater/plugins/ResourceFinder.java +++ b/src/main/java/com/osiris/autoplug/client/tasks/updater/plugins/ResourceFinder.java @@ -20,6 +20,7 @@ import com.osiris.autoplug.client.tasks.updater.search.spigot.SpigotSearchByAuthor; import com.osiris.autoplug.client.tasks.updater.search.spigot.SpigotSearchById; import com.osiris.autoplug.client.tasks.updater.search.spigot.SpigotSearchByName; +import com.osiris.autoplug.client.utils.UtilsMinecraft; public class ResourceFinder { @@ -48,7 +49,7 @@ public SearchResult findUnknownPlugin(MinecraftPlugin plugin) { */ public SearchResult findByModrinthOrCurseforge(InstalledModLoader modLoader, MinecraftMod mod, String mcVersion, boolean checkNameForModLoader) { // Do spigot search by name - SearchResult sr = new ModrinthAPI().searchUpdate(modLoader, mod, mcVersion); + SearchResult sr = new ModrinthAPI().searchUpdateMod(modLoader, mod, mcVersion); if (sr == null || sr.getResultCode() == 2 || sr.getResultCode() == 3) { //Couldn't find author or resource via first search @@ -73,9 +74,14 @@ public SearchResult findPluginByBukkitId(MinecraftPlugin plugin) { sr.plugin = plugin; return sr; } + public SearchResult findPluginByModrinthId(MinecraftPlugin plugin, String mcVersion) { + SearchResult sr = new ModrinthAPI().searchUpdatePlugin(plugin, mcVersion); + sr.plugin = plugin; + return sr; + } public SearchResult findModByModrinthId(InstalledModLoader modLoader, MinecraftMod mod, String mcVersion) { - SearchResult sr = new ModrinthAPI().searchUpdate(modLoader, mod, mcVersion); + SearchResult sr = new ModrinthAPI().searchUpdateMod(modLoader, mod, mcVersion); sr.mod = mod; return sr; } diff --git a/src/main/java/com/osiris/autoplug/client/tasks/updater/plugins/TaskPluginsUpdater.java b/src/main/java/com/osiris/autoplug/client/tasks/updater/plugins/TaskPluginsUpdater.java index ad3f0a88..ade16078 100644 --- a/src/main/java/com/osiris/autoplug/client/tasks/updater/plugins/TaskPluginsUpdater.java +++ b/src/main/java/com/osiris/autoplug/client/tasks/updater/plugins/TaskPluginsUpdater.java @@ -184,6 +184,7 @@ else if (e.getHttpErrorCode() == 404) { YamlSection jenkinsProjectUrl = pluginsConfig.put(name, plName, "alternatives", "jenkins", "project-url"); YamlSection jenkinsArtifactName = pluginsConfig.put(name, plName, "alternatives", "jenkins", "artifact-name"); YamlSection jenkinsBuildId = pluginsConfig.put(name, plName, "alternatives", "jenkins", "build-id").setDefValues("0"); + YamlSection modrinthId = pluginsConfig.put(name, plName, "alternatives", "modrinth", "plugin-id"); // The plugin devs can add their spigot/bukkit ids to their plugin.yml files if (installedPlugin.getSpigotId() != 0 && spigotId.asString() != null && spigotId.asInt() == 0) // Don't update the value, if the user has already set it @@ -254,6 +255,7 @@ else if (e.getHttpErrorCode() == 404) { installedPlugin.setJenkinsProjectUrl(jenkinsProjectUrl.asString()); installedPlugin.setJenkinsArtifactName(jenkinsArtifactName.asString()); installedPlugin.setJenkinsBuildId(jenkinsBuildId.asInt()); + installedPlugin.setModrinthId(modrinthId.asString()); // Check for missing plugin details in plugin.yml if (jenkinsArtifactName.asString() != null && jenkinsProjectUrl.asString() != null) @@ -319,6 +321,7 @@ else if (installedPlugin.getVersion() == null || installedPlugin.getVersion().tr int sizeGithubPlugins = 0; int sizeSpigotPlugins = 0; int sizeBukkitPlugins = 0; + int sizeModrinthPlugins = 0; int sizeUnknownPlugins = 0; ExecutorService executorService; @@ -327,6 +330,13 @@ else if (installedPlugin.getVersion() == null || installedPlugin.getVersion().tr else executorService = Executors.newSingleThreadExecutor(); List> activeFutures = new ArrayList<>(); + UpdaterConfig updaterConfig = new UpdaterConfig(); + + String mcVersion = updaterConfig.mods_updater_version.asString(); + + if (mcVersion == null) mcVersion = new UtilsMinecraft().getInstalledVersion(); + if (mcVersion == null) throw new NullPointerException("Failed to determine Minecraft version."); + for (MinecraftPlugin pl : includedPlugins) { try { @@ -344,6 +354,10 @@ else if (installedPlugin.getVersion() == null || installedPlugin.getVersion().tr sizeBukkitPlugins++; // BUKKIT PLUGIN pl.setIgnoreContentType(true); // TODO temporary workaround for xamazon-json content type curseforge/bukkit issue: https://github.com/Osiris-Team/AutoPlug-Client/issues/109 activeFutures.add(executorService.submit(() -> new ResourceFinder().findPluginByBukkitId(pl))); + } else if (pl.getModrinthId() != null) { // MODRINTH PLUGIN + sizeModrinthPlugins++; + String finalMcVersion = mcVersion; + activeFutures.add(executorService.submit(() -> new ResourceFinder().findPluginByModrinthId(pl, finalMcVersion))); } else { sizeUnknownPlugins++; // UNKNOWN PLUGIN pl.setIgnoreContentType(true); // TODO temporary workaround for xamazon-json content type curseforge/bukkit issue: https://github.com/Osiris-Team/AutoPlug-Client/issues/109 diff --git a/src/main/java/com/osiris/autoplug/client/tasks/updater/search/GithubSearch.java b/src/main/java/com/osiris/autoplug/client/tasks/updater/search/GithubSearch.java index c9cbfd59..f07db710 100644 --- a/src/main/java/com/osiris/autoplug/client/tasks/updater/search/GithubSearch.java +++ b/src/main/java/com/osiris/autoplug/client/tasks/updater/search/GithubSearch.java @@ -29,7 +29,7 @@ public SearchResult search(String githubRepoName, String githubAssetName, String String latestVersion = null; String fileName = null; try { - JsonObject latestRelease = Json.getAsObject("https://api.github.com/repos/" + githubRepoName + "/releases/latest"); + JsonObject latestRelease = Json.getAsObject("https://api.github.com/repos/" + githubRepoName + "/" + githubAssetName + "/releases/latest"); latestVersion = latestRelease.get("tag_name").getAsString(); if (latestVersion != null) latestVersion = latestVersion.replaceAll("[^0-9.]", ""); // Before passing over remove everything except numbers and dots