4
0
Fork 0
mirror of https://github.com/Anvilcraft/modpacktools synced 2024-06-02 10:39:59 +02:00

remove unused ModInfo class and broken AddMod command (to be rewritten)

This commit is contained in:
LordMZTE 2020-07-27 14:33:24 +02:00
parent 2c4b29c959
commit b9d14f7f1f
2 changed files with 0 additions and 233 deletions

View file

@ -1,96 +0,0 @@
package ley.anvil.modpacktools.commandhelper;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.annotations.SerializedName;
import ley.anvil.modpacktools.Main;
import ley.anvil.modpacktools.util.Util;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
public class ModInfo {
private final String name;
private final JsonArray authors;
@SerializedName("websiteUrl")
private final String link;
@SerializedName("downloadCount")
private final int downloads;
private final int id;
private final JsonArray latestFiles;
private ModInfo(String name, JsonArray authors, String link, int downloads, int id, JsonArray latestFiles) {
this.name = name;
this.authors = authors;
this.link = link;
this.downloads = downloads;
this.id = id;
this.latestFiles = latestFiles;
}
public static ArrayList<ModInfo> getModInfo() {
try {
System.out.println("Getting Info From Curse API");
File manifestFile = new File(Main.getCONFIG().getJarLocation(), Main.getCONFIG().getConfig()
.getPath(
"Locations",
"manifestFile"
));
//Read manifest
JsonObject manifest = Util.readAsJson(manifestFile);
JsonArray files = manifest.getAsJsonArray("files");
ArrayList<Integer> fileIds = new ArrayList<>();
files.forEach(file -> fileIds.add(((JsonObject)file).get("projectID").getAsInt()));
String responseStr = Util.httpPostStr(new URL("https://addons-ecs.forgesvc.net/api/v2/addon"),
fileIds.toString(),
"application/json; charset=utf-8",
Collections.singletonMap("Accept", "application/json")
);
JsonArray response = (JsonArray)JsonParser.parseString(responseStr);
ArrayList<ModInfo> modInfos = new ArrayList<>();
Gson gson = new Gson();
response.forEach(mod -> modInfos.add(gson.fromJson(mod, ModInfo.class)));
return modInfos;
}catch(IOException e) {
e.printStackTrace();
}
return null;
}
public String getName() {
return name;
}
public String[] getAuthors() {
ArrayList<String> authorArr = new ArrayList<>();
authors.forEach(author -> {
authorArr.add(((JsonObject)author).get("name").getAsString());
});
return authorArr.toArray(new String[authorArr.size()]);
}
public String getLink() {
return link;
}
public int getDownloads() {
return downloads;
}
public int getId() {
return id;
}
public String getDownload() {
return latestFiles.get(0).getAsJsonObject().get("downloadUrl").getAsString();
}
}

View file

@ -1,137 +0,0 @@
package ley.anvil.modpacktools.commands;
import com.therandomlabs.curseapi.CurseAPI;
import com.therandomlabs.curseapi.CurseException;
import com.therandomlabs.curseapi.project.CurseProject;
import ley.anvil.addonscript.v1.AddonscriptJSON;
import ley.anvil.modpacktools.Main;
import ley.anvil.modpacktools.command.CommandReturn;
import ley.anvil.modpacktools.command.ICommand;
import ley.anvil.modpacktools.command.LoadCommand;
import okhttp3.HttpUrl;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@LoadCommand
public class AddMod implements ICommand {
@Override
public CommandReturn execute(String[] args) {
//Check if the command has the correct number of args
if(args.length >= 2) {
AddonscriptJSON json = Main.getMPJH().getJson();
AddonscriptJSON.Version version = null;
if(json != null && json.versions != null) {
if(json.versions.size() == 1) {
version = json.versions.get(0);
}else {
for(AddonscriptJSON.Version v : json.versions) {
if(v.versionid == -1) {
version = v;
break;
}
}
}
}
if(version == null) {
throw new RuntimeException("Error: The modpack.json does not include a version with id -1");
}
//The url must match this
String regex = "(?m)^(http)(s)?://(www\\.)?(curseforge.com/minecraft/mc-mods/)[0-z,\\-]+/(files)/[0-9]+$";
String endPartRegex = "(/files/)[0-9]+$";
if(args[1].matches(regex)) {
try {
//remove fileID
System.out.println("Getting ID");
CurseProject project = CurseAPI.project(HttpUrl.get(args[1].replaceAll(endPartRegex, ""))).get();
int projectID = project.id();
//extract fileID
Pattern pattern = Pattern.compile("[0-9]+$");
Matcher matcher = pattern.matcher(args[1]);
int fileID = 0;
if(matcher.find()) {
fileID = Integer.parseInt(matcher.group(0));
}
System.out.println("Reading Addonscript");
//Get Mods in manifest file
//Check if Mod already exsits
for(AddonscriptJSON.Relation file : version.relations) {
if(file.file != null && file.file.artifact != null) {
String[] parts = file.file.artifact.split(":");
if(parts.length == 3 && parts[0].equals("curse")) {
int projID = Integer.parseInt(parts[1]);
if(projID == projectID) {
return CommandReturn.fail("The Mod Is already Installed");
}
}
}
}
System.out.println("Adding Mod " + project.name());
//Construct Mod
AddonscriptJSON.Relation rel = new AddonscriptJSON.Relation();
rel.file = new AddonscriptJSON.File();
rel.file.repository = "curse";
rel.file.artifact = "curse.maven:" + projectID + ":" + fileID;
rel.type = "mod";
rel.options = new ArrayList<>();
rel.options.add("client");
rel.options.add("server");
rel.options.add("required");
rel.options.add("included");
rel.id = ""+ projectID;
rel.file.installer = "internal.dir:mods";
//Add Mod to array
if(version.relations == null) {
version.relations = new ArrayList<>();
}
version.relations.add(rel);
}catch(CurseException e) {
e.printStackTrace();
}
}else {
AddonscriptJSON.Relation rel = new AddonscriptJSON.Relation();
rel.file = new AddonscriptJSON.File();
rel.file.link = args[1];
rel.id = args[1];
rel.type = "mod";
rel.options = new ArrayList<>();
rel.options.add("client");
rel.options.add("server");
rel.options.add("required");
rel.options.add("included");
rel.file.installer = "internal.dir:mods";
if(version.relations == null) {
version.relations = new ArrayList<>();
}
version.relations.add(rel);
}
//Overwrite Old Manifest File
FileWriter manifestWriter = null;
try {
manifestWriter = new FileWriter(Main.getMPJH().getModpackJsonFile(), false);
System.out.println("Printing Manifest");
json.write(manifestWriter);
manifestWriter.close();
}catch(IOException e) {
e.printStackTrace();
}
}else {
return CommandReturn.fail("Syntax: addmod <curseforge url>");
}
return CommandReturn.success();
}
@Override
public String getName() {
return "addmod";
}
@Override
public String getHelpMessage() {
return "This command adds a mod to the pack";
}
}