Skip to content

Commit

Permalink
Add more options to .ip & .im
Browse files Browse the repository at this point in the history
  • Loading branch information
Raik176 committed Nov 8, 2023
1 parent fb2c8b4 commit c8e6a02
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 41 deletions.
191 changes: 162 additions & 29 deletions src/main/java/com/osiris/autoplug/client/console/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
Expand Down Expand Up @@ -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 <spigot<id>/bukkit<id>> | Installs a new plugin (.ip)");
AL.info(".install mod <modrinth<id>/curseforge<id>> | Installs a new mod (.im)");
AL.info(".install plugin spigot|bukkit|github|modrinth|search <id> | 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)");
Expand Down Expand Up @@ -333,21 +335,97 @@ private static void findJavaInstallationsInDrive(File directory, List<String> 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);
Expand All @@ -371,30 +449,85 @@ 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();
File modsDir = FileManager.convertRelativeToAbsolutePath(updaterConfig.mods_updater_path.asString());
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);
Expand All @@ -407,11 +540,11 @@ public static boolean installMod(String command) throws Exception {
task.start();
new UtilsTasks().printResultsWhenDone(myManager.manager);
List<MinecraftMod> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,18 +36,26 @@ 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;
String type = ".jar";
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);
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand Down
Loading

0 comments on commit c8e6a02

Please sign in to comment.