From 23444d1ef10027d386f87fdd9efeaa488bf1bafd Mon Sep 17 00:00:00 2001 From: Meeples10 Date: Thu, 14 Dec 2023 23:23:45 -0500 Subject: [PATCH] Add --json option --- .gitignore | 1 + README.md | 5 ++-- pom.xml | 2 +- .../meeples10/mcresourceanalyzer/Main.java | 19 ++++++++----- .../mcresourceanalyzer/RegionAnalyzer.java | 28 ++++++++++++++++++- 5 files changed, 44 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index ff027ae..648aa47 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,4 @@ hs_err_pid* /*.mclevel /*_stats.txt /bedrock/ +/*.json diff --git a/README.md b/README.md index c474ece..3deadc2 100644 --- a/README.md +++ b/README.md @@ -15,12 +15,12 @@ Note that the numbers for `minecraft:air` may be inaccurate at high Y values due ### Command line arguments ``` -java -jar mc-resource-analyzer-x.x.x.jar [-hmsStV] [-B=PATH] [-M=PATH] [-o=STRING] [-T=PATH] [-v=VERSION] [INPUT] +java -jar mc-resource-analyzer-x.x.x.jar [-hjmsStV] [-B=PATH] [-M=PATH] [-o=STRING] [-T=PATH] [-v=VERSION] [INPUT] ``` #### Positional arguments -- `INPUT` (default: `region`): The to the region directory or .mclevel file to analyze. Note that the path is relative to the program's working directory. +- `INPUT` (default: `region`): The to the region directory or .mclevel file to analyze. Both relative and absolute paths are supported. #### Options and flags @@ -28,6 +28,7 @@ java -jar mc-resource-analyzer-x.x.x.jar [-hmsStV] [-B=PATH] [-M=PATH] [-o=STRIN - `-t`, `--table`: Generates a simple HTML table with the collected data. - `-T`, `--table-template`: When used in conjunction with `table`, the generated table will replace any instances of the string `{{{TABLE}}}` in a copy of the template file. Note that the table will not include `
` tags when using this argument. - `-s`, `--statistics`: Outputs a file with statistics about the analysis. +- `-j`, `--json`: Outputs a JSON file with containing the counts of each block. - `-o`, `--output-prefix`: Use this argument to add a prefix to the program's output files. For example, using `-o abc` would result in the files `abc.csv` and `abc_table.html`. - `-v`, `--version-select`: Use this argument if you want to analyze a world that was not generated with the latest version of Minecraft. Selecting a version that does not match the version with which the regions were generated may result in unexpected behavior. The following versions are supported: - `ANVIL_118` for 1.18 to 1.20.4 (default) diff --git a/pom.xml b/pom.xml index 4765102..a70fdd4 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ io.github.meeples10.mcresourceanalyzer mc-resource-analyzer - 1.2.4 + 1.2.5 jar MCResourceAnalyzer diff --git a/src/main/java/io/github/meeples10/mcresourceanalyzer/Main.java b/src/main/java/io/github/meeples10/mcresourceanalyzer/Main.java index cb32853..e073415 100644 --- a/src/main/java/io/github/meeples10/mcresourceanalyzer/Main.java +++ b/src/main/java/io/github/meeples10/mcresourceanalyzer/Main.java @@ -35,7 +35,8 @@ public boolean accept(File dir, String name) { public static final List BLOCKS_TO_MERGE = new ArrayList<>(); static RegionAnalyzer.Version selectedVersion = RegionAnalyzer.Version.values()[0]; static File inputFile = new File("region"); - static boolean saveStatistics = false; + static boolean writeStatistics = false; + static boolean writeJSON = false; static boolean generateTable = false; static boolean modernizeIDs = false; static boolean silent = false; @@ -61,13 +62,14 @@ public static void main(String[] args) { } catch(IOException e) { e.printStackTrace(); } - Main.printf("Save statistics: %b\nGenerate HTML table: %b%s\nRegion version: %s\nModernize block IDs: %b" - + "\nBlock IDs: %d\nBlock IDs to merge: %d\nInput: %s\nOutput prefix: %s\n--------------------------------\n", - saveStatistics, generateTable, + Main.printf( + "Write statistics: %b\nGenerate HTML table: %b%s\nWrite JSON: %b\nRegion version: %s\nModernize block IDs: %b" + + "\nBlock IDs: %d\nBlock IDs to merge: %d\nInput: %s\nOutput prefix: %s\n--------------------------------\n", + writeStatistics, generateTable, (generateTable ? ("\nTable template: " + (tableTemplatePath.equals("") ? "(none)" : tableTemplatePath)) : ""), - selectedVersion, modernizeIDs, BLOCK_NAMES.size(), BLOCKS_TO_MERGE.size(), inputFile.getPath(), - (outputPrefix.equals("") ? "(default)" : outputPrefix)); + writeJSON, selectedVersion, modernizeIDs, BLOCK_NAMES.size(), BLOCKS_TO_MERGE.size(), + inputFile.getPath(), (outputPrefix.equals("") ? "(default)" : outputPrefix)); RegionAnalyzer analyzer; try { analyzer = selectedVersion.getAnalyzerInstance(); @@ -110,6 +112,8 @@ public String[] getVersion() throws Exception { .build()); spec.addOption(OptionSpec.builder("-s", "--statistics") .description("Outputs a file with statistics about the analysis.").build()); + spec.addOption(OptionSpec.builder("-j", "--json") + .description("Outputs a JSON file containing the counts of each block.").build()); spec.addOption( OptionSpec.builder("-S", "--silent").description("Silences all output aside from errors.").build()); spec.addOption(OptionSpec.builder("-m", "--modernize-ids") @@ -133,7 +137,8 @@ public String[] getVersion() throws Exception { private static int parseArgs(ParseResult pr) { if(pr.hasMatchedPositional(0)) inputFile = new File((String) pr.matchedPositional(0).getValue()); - saveStatistics = pr.hasMatchedOption('s'); + writeStatistics = pr.hasMatchedOption('s'); + writeJSON = pr.hasMatchedOption('j'); generateTable = pr.hasMatchedOption('t'); modernizeIDs = pr.hasMatchedOption('m'); silent = pr.hasMatchedOption('S'); diff --git a/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzer.java b/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzer.java index 724342c..0bba6ab 100644 --- a/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzer.java +++ b/src/main/java/io/github/meeples10/mcresourceanalyzer/RegionAnalyzer.java @@ -139,7 +139,17 @@ public int compare(String arg0, String arg1) { System.exit(1); } } - if(Main.saveStatistics) { + if(Main.writeJSON) { + try { + File out = new File(Main.getOutputPrefix() + ".json"); + Main.writeStringToFile(out, generateJSON(minY, maxY)); + Main.printf("\nJSON written to %s\n", out.getAbsolutePath()); + } catch(IOException e) { + e.printStackTrace(); + System.exit(1); + } + } + if(Main.writeStatistics) { try { Main.writeStringToFile(new File(Main.getOutputPrefix() + "_stats.txt"), "chunk-count=" + chunkCount + "\nunique-blocks=" + blockCounter.size() + "\ntotal-blocks=" @@ -224,6 +234,22 @@ public String generateTable(double totalBlocks, double totalExcludingAir) { } } + public String generateJSON(int minY, int maxY) { + StringBuilder sb = new StringBuilder(); + for(String key : heightCounter.keySet()) { + sb.append(",\n\""); + sb.append(key); + sb.append("\":["); + Map map = heightCounter.get(key); + for(int i = minY; i <= maxY; i++) { + sb.append(map.getOrDefault(i, 0L)); + if(i != maxY) sb.append(","); + } + sb.append("]"); + } + return "{" + sb.substring(1) + "\n}"; + } + public long getStartTime() { return firstStartTime; }