4
0
Fork 0
mirror of https://github.com/Anvilcraft/modpacktools synced 2024-09-30 04:59:05 +02:00

fix alphabetical sorting and reformat/cleanup

This commit is contained in:
LordMZTE 2020-05-02 21:29:56 +02:00
parent 3b0df41008
commit b1ab51a932
5 changed files with 306 additions and 297 deletions

View file

@ -7,42 +7,42 @@ public class Main {
public static final Config CONFIG = new Config();
public static void main(String[] args) {
if(args.length == 0) {
Commands.help();
return;
}
if(args.length == 0) {
Commands.help();
return;
}
switch(args[0].toLowerCase() /* ignores case of commands */) {
case "help":
default:
Commands.help();
break;
case "init":
Commands.init();
break;
case "addmod":
Commands.addMod(args);
break;
case "buildtwitch":
Commands.buildTwitch();
break;
case "buildmodpackjson":
Commands.buildModpackJSON();
break;
case "buildraw":
Commands.buildRaw();
break;
case "buildserver":
Commands.buildServer(args);
break;
case "downloadmods":
Commands.downloadMods(args);
break;
case "createmodlist":
Commands.createModlist(args);
break;
case "makeserver":
Commands.makeServer(args);
break;
case "init":
Commands.init();
break;
case "addmod":
Commands.addMod(args);
break;
case "buildtwitch":
Commands.buildTwitch();
break;
case "buildmodpackjson":
Commands.buildModpackJSON();
break;
case "buildraw":
Commands.buildRaw();
break;
case "buildserver":
Commands.buildServer(args);
break;
case "downloadmods":
Commands.downloadMods(args);
break;
case "createmodlist":
Commands.createModlist(args);
break;
case "makeserver":
Commands.makeServer(args);
break;
}
}

View file

@ -25,222 +25,224 @@ import static j2html.TagCreator.*;
public class Commands {
/**
* Prints out all available commands
*/
public static void help() {
System.out.println("Help Goes here!");
}
/**
* Prints out all available commands
*/
public static void help() {
System.out.println("Help Goes here!");
}
/**
* Creates a modpack dev environment in the current folder
*/
public static void init() {
/**
* Creates a modpack dev environment in the current folder
*/
public static void init() {
}
}
//Commands for modpack devs (only available in a modpack dev environment)
//Commands for modpack devs (only available in a modpack dev environment)
/**
* Adds a mod to the modpack
* @param modlink Can be a link to a curseforge file or to a file download
*/
public static void addMod(String[] modlink) {
//Check if the command has the correct number of args
if(modlink.length >= 2) {
//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(modlink[1].matches(regex)) {
try {
//remove fileID
System.out.println("Getting ID");
CurseProject project = CurseAPI.project(HttpUrl.get(modlink[1].replaceAll(endPartRegex, ""))).get();
int projectID = project.id();
//extract fileID
Pattern pattern = Pattern.compile("[0-9]+$");
Matcher matcher = pattern.matcher(modlink[1]);
int fileID = 0;
if(matcher.find()) {
fileID = Integer.parseInt(matcher.group(0));
}
File manifestFile = new File(Main.CONFIG.JAR_LOCATION, Main.CONFIG.CONFIG.get("manifestFile").getAsString());
System.out.println("Reading Manifest");
JsonObject manifest = Util.readJsonFile(manifestFile);
//Get Mods in manifest file
JsonArray files = manifest.getAsJsonArray("files");
//Check if Mod already exsits
for(JsonElement file : files) {
if(file.getAsJsonObject().get("projectID").getAsInt() == projectID) {
System.out.println("The mod is already installed!");
return;
}
}
System.out.println("Adding Mod " + project.name());
//Construct Mod
JsonObject mod = new JsonObject();
mod.addProperty("projectID", projectID);
mod.addProperty("fileID", fileID);
//Add Mod to array
files.add(mod);
//Remove old file array from manifest
manifest.remove("files");
//Add new file array to manifest
manifest.add("files", files);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
//Overwrite Old Manifest File
FileWriter manifestWriter = new FileWriter(manifestFile, false);
System.out.println("Printing Manifest");
gson.toJson(manifest, manifestWriter);
manifestWriter.close();
} catch(CurseException | IOException e) {
e.printStackTrace();
}
}else {
System.out.println("Link Must match " + regex);
}
}else {
System.out.println("Syntax: addmod <curseforge url>");
}
}
/**
* Adds a mod to the modpack
*
* @param modlink Can be a link to a curseforge file or to a file download
*/
public static void addMod(String[] modlink) {
//Check if the command has the correct number of args
if(modlink.length >= 2) {
//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(modlink[1].matches(regex)) {
try {
//remove fileID
System.out.println("Getting ID");
CurseProject project = CurseAPI.project(HttpUrl.get(modlink[1].replaceAll(endPartRegex, ""))).get();
int projectID = project.id();
//extract fileID
Pattern pattern = Pattern.compile("[0-9]+$");
Matcher matcher = pattern.matcher(modlink[1]);
int fileID = 0;
if(matcher.find()) {
fileID = Integer.parseInt(matcher.group(0));
}
File manifestFile = new File(Main.CONFIG.JAR_LOCATION, Main.CONFIG.CONFIG.get("manifestFile").getAsString());
System.out.println("Reading Manifest");
JsonObject manifest = Util.readJsonFile(manifestFile);
//Get Mods in manifest file
JsonArray files = manifest.getAsJsonArray("files");
//Check if Mod already exsits
for(JsonElement file : files) {
if(file.getAsJsonObject().get("projectID").getAsInt() == projectID) {
System.out.println("The mod is already installed!");
return;
}
}
System.out.println("Adding Mod " + project.name());
//Construct Mod
JsonObject mod = new JsonObject();
mod.addProperty("projectID", projectID);
mod.addProperty("fileID", fileID);
//Add Mod to array
files.add(mod);
//Remove old file array from manifest
manifest.remove("files");
//Add new file array to manifest
manifest.add("files", files);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
//Overwrite Old Manifest File
FileWriter manifestWriter = new FileWriter(manifestFile, false);
System.out.println("Printing Manifest");
gson.toJson(manifest, manifestWriter);
manifestWriter.close();
} catch(CurseException | IOException e) {
e.printStackTrace();
}
} else {
System.out.println("Link Must match " + regex);
}
} else {
System.out.println("Syntax: addmod <curseforge url>");
}
}
/**
* Builds the modpack as a Twitch modpack zip
*/
public static void buildTwitch() {
/**
* Builds the modpack as a Twitch modpack zip
*/
public static void buildTwitch() {
}
}
/**
* Builds the modpack as a modpack.json
*/
public static void buildModpackJSON() {
/**
* Builds the modpack as a modpack.json
*/
public static void buildModpackJSON() {
}
}
/**
* Builds the modpack as a raw zip file (for example for the Technic Launcher)
*/
public static void buildRaw() {
/**
* Builds the modpack as a raw zip file (for example for the Technic Launcher)
*/
public static void buildRaw() {
}
}
/**
* Builds the modpack as a server
* @param dir The directory where to create the server
*/
public static void buildServer(String[] dir) {
/**
* Builds the modpack as a server
*
* @param dir The directory where to create the server
*/
public static void buildServer(String[] dir) {
}
}
/**
* Downloads all mods in this pack
* @param dir The mods directory
*/
public static void downloadMods(String[] dir) {
/**
* Downloads all mods in this pack
*
* @param dir The mods directory
*/
public static void downloadMods(String[] dir) {
}
}
/**
* Creates a modlist of this pack
* @param format 1 Can be html or csv, 2 can be any valid file to write to
*/
public static void createModlist(String[] format) {
if(format.length >= 3) {
if(format[1].equalsIgnoreCase("csv")) {
File csvFile = new File(format[2]);
if(csvFile.exists()) {
System.out.println("Delete " + csvFile);
return;
}
System.out.println("Printing CSV into " + csvFile);
Appendable out;
CSVFormat format1;
try(CSVPrinter printer = new CSVPrinter(new FileWriter(csvFile), CSVFormat.EXCEL.withDelimiter(';'))) {
printer.printRecord("Name", "Authors", "Link", "Downloads", "ID");
printer.println();
ArrayList<ModInfo> modlist = ModInfo.getModInfo();
Collections.sort(modlist, Comparator.comparing(ModInfo :: getName));
for(ModInfo mod : modlist) {
String name = mod.getName();
String[] authorArr = mod.getAuthors();
String link = mod.getLink();
int downloads = mod.getDownloads();
int id = mod.getId();
StringBuilder sb = new StringBuilder();
for(String author : authorArr) {
sb.append(author);
sb.append(", ");
}
String authors = sb.toString();
authors = authors.substring(0, authors.length() - 2);
/**
* Creates a modlist of this pack
*
* @param format 1 Can be html or csv, 2 can be any valid file to write to
*/
public static void createModlist(String[] format) {
if(format.length >= 3) {
if(format[1].equalsIgnoreCase("csv")) {
File csvFile = new File(format[2]);
if(csvFile.exists()) {
System.out.println("Delete " + csvFile);
return;
}
System.out.println("Printing CSV into " + csvFile);
try(CSVPrinter printer = new CSVPrinter(new FileWriter(csvFile), CSVFormat.EXCEL.withDelimiter(';'))) {
printer.printRecord("Name", "Authors", "Link", "Downloads", "ID");
printer.println();
ArrayList<ModInfo> modlist = ModInfo.getModInfo();
Collections.sort(modlist, Comparator.comparing(a -> a.getName().toLowerCase()));
for(ModInfo mod : modlist) {
String name = mod.getName();
String[] authorArr = mod.getAuthors();
String link = mod.getLink();
int downloads = mod.getDownloads();
int id = mod.getId();
StringBuilder sb = new StringBuilder();
for(String author : authorArr) {
sb.append(author);
sb.append(", ");
}
String authors = sb.toString();
authors = authors.substring(0, authors.length() - 2);
printer.printRecord(name, authors, link, downloads, id);
}
}catch(IOException e) {
e.printStackTrace();
}
} else if(format[1].equalsIgnoreCase("html")) {
File htmlFile = new File(format[2]);
if(htmlFile.exists()) {
System.out.println("Delete " + htmlFile);
return;
}
ArrayList<ModInfo> mods = ModInfo.getModInfo();
Collections.sort(mods, Comparator.comparing(ModInfo :: getName));
ContainerTag table = body(
TagCreator.table(TagCreator.attrs("#mods"), TagCreator.tbody(
tr(td(b("Name")),
td(b("Authors")),
td(b("ID")),
td(b("Downloads"))
),
TagCreator.each(mods, i -> {
StringBuilder sb = new StringBuilder();
for(String author : i.getAuthors()) {
sb.append(author);
sb.append(", ");
}
String authors = sb.toString();
authors = authors.substring(0, authors.length() - 2);
printer.printRecord(name, authors, link, downloads, id);
}
} catch(IOException e) {
e.printStackTrace();
}
} else if(format[1].equalsIgnoreCase("html")) {
File htmlFile = new File(format[2]);
if(htmlFile.exists()) {
System.out.println("Delete " + htmlFile);
return;
}
ArrayList<ModInfo> mods = ModInfo.getModInfo();
Collections.sort(mods, Comparator.comparing(a -> a.getName().toLowerCase()));
ContainerTag table = body(
TagCreator.table(TagCreator.attrs("#mods"), TagCreator.tbody(
tr(td(b("Name")),
td(b("Authors")),
td(b("ID")),
td(b("Downloads"))
),
TagCreator.each(mods, i -> {
StringBuilder sb = new StringBuilder();
for(String author : i.getAuthors()) {
sb.append(author);
sb.append(", ");
}
String authors = sb.toString();
authors = authors.substring(0, authors.length() - 2);
return tr(td(a(i.getName())
.withHref(i.getLink())
.withRel("noopener noreferrer")
.withTarget("_blank")),
td(authors),
td(String.valueOf(i.getId())),
td(String.valueOf(i.getDownloads())));
})
))
);
try {
System.out.println("Writing HTML");
FileWriter htmlWrite = new FileWriter(htmlFile);
htmlWrite.write(table.render());
htmlWrite.close();
} catch(IOException e) {
e.printStackTrace();
}
}else {
System.out.println("Expected Either HTML or CSV as format");
}
}else {
System.out.println("Syntax: createmodlist <csv/html> <file>");
}
}
return tr(td(a(i.getName())
.withHref(i.getLink())
.withRel("noopener noreferrer")
.withTarget("_blank")),
td(authors),
td(String.valueOf(i.getId())),
td(String.valueOf(i.getDownloads())));
})
))
);
try {
System.out.println("Writing HTML");
FileWriter htmlWrite = new FileWriter(htmlFile);
htmlWrite.write(table.render());
htmlWrite.close();
} catch(IOException e) {
e.printStackTrace();
}
} else {
System.out.println("Expected Either HTML or CSV as format");
}
} else {
System.out.println("Syntax: createmodlist <csv/html> <file>");
}
}
//Commands for users (available outside a modpack dev environment)
//Commands for users (available outside a modpack dev environment)
/**
* Creates a server from a modpack zip file
* @param args The path to the zip file, The directory where to create the server
*/
public static void makeServer(String[] args) {
}
/**
* Creates a server from a modpack zip file
*
* @param args The path to the zip file, The directory where to create the server
*/
public static void makeServer(String[] args) {
}
}

View file

@ -12,75 +12,75 @@ import java.util.ArrayList;
public class ModInfo {
private String name;
private JsonArray authors;
@SerializedName("websiteUrl")
private String link;
@SerializedName("downloadCount")
private int downloads;
private int id;
private String name;
private JsonArray authors;
@SerializedName("websiteUrl")
private String link;
@SerializedName("downloadCount")
private int downloads;
private int id;
private ModInfo(String name, JsonArray authors, String link, int downloads, int id) {
this.name = name;
this.authors = authors;
this.link = link;
this.downloads = downloads;
this.id = id;
}
private ModInfo(String name, JsonArray authors, String link, int downloads, int id) {
this.name = name;
this.authors = authors;
this.link = link;
this.downloads = downloads;
this.id = id;
}
public String getName() {
return name;
}
public String getName() {
return name;
}
public String[] getAuthors() {
ArrayList<String> authorArr = new ArrayList<>();
for(JsonElement author : authors) {
JsonObject authorObj = (JsonObject) author;
authorArr.add(authorObj.get("name").getAsString());
}
return authorArr.toArray(new String[authorArr.size()]);
}
public String[] getAuthors() {
ArrayList<String> authorArr = new ArrayList<>();
for(JsonElement author : authors) {
JsonObject authorObj = (JsonObject) author;
authorArr.add(authorObj.get("name").getAsString());
}
return authorArr.toArray(new String[authorArr.size()]);
}
public String getLink() {
return link;
}
public String getLink() {
return link;
}
public int getDownloads() {
return downloads;
}
public int getDownloads() {
return downloads;
}
public int getId() {
return id;
}
public int getId() {
return id;
}
public static ArrayList<ModInfo> getModInfo() {
try {
System.out.println("Getting Info From Curse API");
File manifestFile = new File(Main.CONFIG.JAR_LOCATION, Main.CONFIG.CONFIG.get("manifestFile").getAsString());
//Read manifest
JsonObject manifest = Util.readJsonFile(manifestFile);
JsonArray files = manifest.getAsJsonArray("files");
public static ArrayList<ModInfo> getModInfo() {
try {
System.out.println("Getting Info From Curse API");
File manifestFile = new File(Main.CONFIG.JAR_LOCATION, Main.CONFIG.CONFIG.get("manifestFile").getAsString());
//Read manifest
JsonObject manifest = Util.readJsonFile(manifestFile);
JsonArray files = manifest.getAsJsonArray("files");
ArrayList<Integer> fileIds = new ArrayList<>();
for(JsonElement file : files) {
fileIds.add(((JsonObject) file).get("projectID").getAsInt());
}
String responseStr = Util.httpPostString(new URL("https://addons-ecs.forgesvc.net/api/v2/addon"),
fileIds.toString(),
"application/json; utf-8",
"application/json");
JsonArray response = (JsonArray) JsonParser.parseString(responseStr);
ArrayList<Integer> fileIds = new ArrayList<>();
for(JsonElement file : files) {
fileIds.add(((JsonObject) file).get("projectID").getAsInt());
}
String responseStr = Util.httpPostString(new URL("https://addons-ecs.forgesvc.net/api/v2/addon"),
fileIds.toString(),
"application/json; utf-8",
"application/json");
JsonArray response = (JsonArray) JsonParser.parseString(responseStr);
ArrayList<ModInfo> modInfos = new ArrayList<>();
Gson gson = new Gson();
for(JsonElement mod : response) {
modInfos.add(gson.fromJson(mod, ModInfo.class));
}
return modInfos;
} catch(MalformedURLException e) {
e.printStackTrace();
}
return null;
}
ArrayList<ModInfo> modInfos = new ArrayList<>();
Gson gson = new Gson();
for(JsonElement mod : response) {
modInfos.add(gson.fromJson(mod, ModInfo.class));
}
return modInfos;
} catch(MalformedURLException e) {
e.printStackTrace();
}
return null;
}
}

View file

@ -10,6 +10,7 @@ public class Config {
public final File JAR_LOCATION;
public final File CONFIG_LOCATION;
public final JsonObject CONFIG;
public Config() {
//Get the Location of the jarfile
File JAR_LOCATION1;
@ -29,19 +30,20 @@ public class Config {
e.printStackTrace();
}
JAR_LOCATION = JAR_LOCATION1;
CONFIG_LOCATION = new File(JAR_LOCATION, CONFIG_NAME);
CONFIG_LOCATION = new File(JAR_LOCATION, CONFIG_NAME);
CONFIG = readConfig();
}
/**
* reads the config it it exists and otherwise copies it
*
* @return the json object of the config file
*/
private JsonObject readConfig() {
if(CONFIG_LOCATION.exists()) {
//parse file to json
return Util.readJsonFile(CONFIG_LOCATION);
}else {
} else {
//copy from resources
try {
InputStream in = ClassLoader.getSystemResourceAsStream(CONFIG_NAME);
@ -52,9 +54,10 @@ public class Config {
in.close();
out.close();
return readConfig();
}catch(IOException e) {
} catch(IOException e) {
e.printStackTrace();
}
}return null;
}
return null;
}
}

View file

@ -6,10 +6,12 @@ import com.google.gson.JsonParser;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
public class Util {
/**
* Reads a Json File
*
* @param file the file to read
* @return the file content as JsonObject
*/
@ -31,10 +33,11 @@ public class Util {
/**
* sends a http post request
* @param url the url to send the request to
*
* @param url the url to send the request to
* @param contentType what content type should be used. Example: "application/json; utf-8"
* @param accept what content is accepted. Example: "application/json"
* @param payload the payload to send
* @param accept what content is accepted. Example: "application/json"
* @param payload the payload to send
* @return the response as string
*/
public static String httpPostString(URL url, String payload, String contentType, String accept) {
@ -46,10 +49,10 @@ public class Util {
con.setDoOutput(true);
OutputStream outs = con.getOutputStream();
byte[] input = payload.getBytes("UTF-8");
byte[] input = payload.getBytes(StandardCharsets.UTF_8);
outs.write(input);
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8));
StringBuffer sb = new StringBuffer();
String responseLine;
while((responseLine = br.readLine()) != null) {
@ -59,6 +62,7 @@ public class Util {
return sb.toString();
} catch(IOException e) {
e.printStackTrace();
}return null;
}
return null;
}
}