Skip to content

Commit

Permalink
Add --json option
Browse files Browse the repository at this point in the history
  • Loading branch information
Meeples10 committed Dec 15, 2023
1 parent c0c0653 commit 23444d1
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ hs_err_pid*
/*.mclevel
/*_stats.txt
/bedrock/
/*.json
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,20 @@ 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

- `-h`, `--help`: Show usage help.
- `-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 `<table></table>` 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)
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>io.github.meeples10.mcresourceanalyzer</groupId>
<artifactId>mc-resource-analyzer</artifactId>
<version>1.2.4</version>
<version>1.2.5</version>
<packaging>jar</packaging>

<name>MCResourceAnalyzer</name>
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/io/github/meeples10/mcresourceanalyzer/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public boolean accept(File dir, String name) {
public static final List<Integer> 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;
Expand All @@ -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();
Expand Down Expand Up @@ -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")
Expand All @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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="
Expand Down Expand Up @@ -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<Integer, Long> 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;
}
Expand Down

0 comments on commit 23444d1

Please sign in to comment.