Skip to content

Commit

Permalink
General code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Meeples10 committed Dec 9, 2021
1 parent df3844c commit fb66a3e
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 524 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Alternatively, to explicitly specify a version from the command line and skip th

### Version compatibility

MCResourceAnalyzer 1.1.4 can analyze worlds generated with any version of Minecraft: Java Edition between Indev 0.31 20100122 and 1.18.
MCResourceAnalyzer 1.1.5 can analyze worlds generated with any version of Minecraft: Java Edition between Indev 0.31 20100122 and 1.18.

Note that Indev worlds with the `Long` and `Deep` world shapes are not supported.

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static void main(String[] args) {
Object returnedVersion = JOptionPane.showInputDialog(null,
"Select the format in which the region files were saved:", "Select Version",
JOptionPane.PLAIN_MESSAGE, null, RegionAnalyzer.Version.values(),
RegionAnalyzer.Version.ANVIL_2021);
RegionAnalyzer.Version.values()[0]);
if(!(returnedVersion instanceof RegionAnalyzer.Version)) System.exit(0);
selectedVersion = (RegionAnalyzer.Version) returnedVersion;
}
Expand All @@ -117,15 +117,16 @@ public static void main(String[] args) {
System.exit(1);
return;
}
if(analyzer == null) analyzer = new RegionAnalyzerAnvil2021();
if(analyzer == null) analyzer = new RegionAnalyzerAnvil118();
analyzer.setVersion(selectedVersion);
if(inputOverride) {
if(inputFile.isDirectory() != selectedVersion.usesDirectory()) {
System.err.println("Input must be a " + (selectedVersion.usesDirectory() ? "directory" : "file") + ": "
+ inputFile.getAbsolutePath());
System.exit(1);
}
}
analyzer.analyze(inputFile);
analyzer.run(inputFile);
System.out.println("Completed after " + millisToHMS(System.currentTimeMillis() - analyzer.getStartTime()));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,104 @@
package io.github.meeples10.mcresourceanalyzer;

import java.io.File;
import java.io.IOException;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;

public abstract class RegionAnalyzer {
private Version version;
public long chunkCount = 0;
public Map<String, Long> blockCounter = new HashMap<String, Long>();
public Map<String, HashMap<Integer, Long>> heightCounter = new HashMap<String, HashMap<Integer, Long>>();
private long firstStartTime;
public long endTime;

public RegionAnalyzer() {
firstStartTime = System.currentTimeMillis();
}

public abstract void analyze(File regionDir);
public void setVersion(Version version) {
this.version = version;
}

public void run(File input) {
analyze(input);

long totalBlocks = 0L;
for(String key : blockCounter.keySet()) {
totalBlocks += blockCounter.get(key);
}
System.out.println("--------------------------------\n" + blockCounter.size() + " unique blocks\n" + totalBlocks
+ " blocks total\n--------------------------------");

System.out.print("Sorting data... ");
heightCounter = heightCounter.entrySet().stream().sorted(Map.Entry.comparingByKey(new Comparator<String>() {
@Override
public int compare(String arg0, String arg1) {
return Long.compare(blockCounter.get(arg1), blockCounter.get(arg0));
}
})).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
System.out.println("Done");

double totalExcludingAir = (double) (totalBlocks - (blockCounter.containsKey("0") ? blockCounter.get("0") : 0)
- (blockCounter.containsKey("minecraft:air") ? blockCounter.get("minecraft:air") : 0)
- (blockCounter.containsKey("minecraft:cave_air") ? blockCounter.get("minecraft:cave_air") : 0));
System.out.print("Generating CSV... ");
String data = "id,";
int minY = getMinimumY();
int maxY = getMaximumY();
for(int i = minY; i <= maxY; i++) {
data += i + ",";
}
data += "total,percent_of_total,percent_excluding_air\n";
int digits = String.valueOf(blockCounter.size()).length();
String completionFormat = "[%0" + digits + "d/%0" + digits + "d]";
int keyIndex = 0;
for(String key : heightCounter.keySet()) {
keyIndex += 1;
System.out.print("\rGenerating CSV... " + String.format(completionFormat, keyIndex, blockCounter.size()));
data += key + ",";
for(int i = minY; i <= maxY; i++) {
if(!heightCounter.get(key).containsKey(i)) {
data += "0,";
} else {
data += heightCounter.get(key).get(i) + ",";
}
}
data += blockCounter.get(key) + ","
+ Main.DECIMAL_FORMAT.format(((double) blockCounter.get(key) / (double) totalBlocks) * 100.0d);
if(key.equals("0") || key.equals("minecraft:air") || key.equals("minecraft:cave_air")
|| key.equals("minecraft:void_air")) {
data += ",N/A";
} else {
data += "," + Main.DECIMAL_FORMAT.format(((double) blockCounter.get(key) / totalExcludingAir) * 100.0d);
}
data += "\n";
}
try {
File out = new File(Main.getOutputPrefix() + ".csv");
Main.writeStringToFile(out, data);
System.out.println("\nData written to " + out.getAbsolutePath());
} catch(IOException e) {
e.printStackTrace();
System.exit(1);
}
if(Main.generateTable) {
try {
File out = new File(Main.getOutputPrefix() + "_table.html");
Main.writeStringToFile(out, generateTable((double) totalBlocks, totalExcludingAir));
System.out.println("\nTable written to " + out.getAbsolutePath());
} catch(IOException e) {
e.printStackTrace();
System.exit(1);
}
}
}

public abstract void analyze(File input);

public String generateTable(double totalBlocks, double totalExcludingAir) {
int minY = getMinimumY();
Expand Down Expand Up @@ -92,7 +176,11 @@ int getMinimumY() {
}
}
}
return min;
if(version == Version.ANVIL_2021 || version == Version.ANVIL_118) {
return min < 0 ? min : 0;
} else {
return 0;
}
}

int getMaximumY() {
Expand All @@ -104,7 +192,11 @@ int getMaximumY() {
}
}
}
return max;
if(version == Version.INDEV || version == Version.ALPHA || version == Version.MCREGION) {
return 128;
} else {
return max > 255 ? max : 255;
}
}

public enum Version {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
Expand Down Expand Up @@ -45,82 +40,9 @@ public void analyze(File world) {
"Done (" + String.format("%.2f", (double) (System.currentTimeMillis() - startTime) / 1000) + "s)");
cnum++;
}
long duration = System.currentTimeMillis() - getStartTime();
System.out.println(("Completed analysis in " + Main.millisToHMS(duration) + " (" + chunkCount + " chunks)"));
long totalBlocks = 0L;
for(String key : blockCounter.keySet()) {
totalBlocks += blockCounter.get(key);
}
System.out.println("--------------------------------\n" + blockCounter.size() + " unique blocks\n" + totalBlocks
+ " blocks total\n--------------------------------");

System.out.print("Sorting data... ");
heightCounter = heightCounter.entrySet().stream().sorted(Map.Entry.comparingByKey(new Comparator<String>() {
@Override
public int compare(String arg0, String arg1) {
return Long.compare(blockCounter.get(arg1), blockCounter.get(arg0));
}
})).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
System.out.println("Done");

double totalExcludingAir = (double) (totalBlocks - (blockCounter.containsKey("0") ? blockCounter.get("0") : 0));
System.out.print("Generating CSV... ");
String data = "id,";
for(int i = 0; i < 256; i++) {
data += i + ",";
}
data += "total,percent_of_total,percent_excluding_air\n";
int digits = String.valueOf(blockCounter.size()).length();
String completionFormat = "[%0" + digits + "d/%0" + digits + "d]";
int keyIndex = 0;
for(String key : heightCounter.keySet()) {
keyIndex += 1;
System.out.print("\rGenerating CSV... " + String.format(completionFormat, keyIndex, blockCounter.size()));
data += (Main.modernizeIDs ? Main.getStringID(key) : key) + ",";
for(int i = 0; i < 256; i++) {
if(!heightCounter.get(key).containsKey(i)) {
data += "0,";
} else {
data += heightCounter.get(key).get(i) + ",";
}
}
data += blockCounter.get(key) + ","
+ Main.DECIMAL_FORMAT.format(((double) blockCounter.get(key) / (double) totalBlocks) * 100.0d);
if(key.equals("0")) {
data += ",N/A";
} else {
data += "," + Main.DECIMAL_FORMAT.format(((double) blockCounter.get(key) / totalExcludingAir) * 100.0d);
}
data += "\n";
}
try {
File out = new File(Main.getOutputPrefix() + ".csv");
Main.writeStringToFile(out, data);
System.out.println("\nData written to " + out.getAbsolutePath());
} catch(IOException e) {
e.printStackTrace();
System.exit(1);
}
if(Main.generateTable) {
try {
File out = new File(Main.getOutputPrefix() + "_table.html");
Main.writeStringToFile(out, generateTable((double) totalBlocks, totalExcludingAir));
System.out.println("\nTable written to " + out.getAbsolutePath());
} catch(IOException e) {
e.printStackTrace();
System.exit(1);
}
}
if(Main.saveStatistics) {
try {
Main.writeStringToFile(new File(Main.getOutputPrefix() + "_stats.txt"),
"chunk-count=" + chunkCount + "\nunique-blocks=" + blockCounter.size() + "\ntotal-blocks="
+ totalBlocks + "\nduration-millis=" + duration + "\nduration-readable="
+ Main.millisToHMS(duration));
} catch(IOException e) {
e.printStackTrace();
}
}
endTime = System.currentTimeMillis();
System.out.println(("Completed analysis in " + Main.millisToHMS(endTime - getStartTime()) + " (" + chunkCount
+ " chunks)"));
}

private void processChunk(File chunkFile) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
Expand Down Expand Up @@ -68,69 +67,6 @@ public int compare(String arg0, String arg1) {
}
})).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
System.out.println("Done");

double totalExcludingAir = (double) (totalBlocks
- (blockCounter.containsKey("minecraft:air") ? blockCounter.get("minecraft:air") : 0)
- (blockCounter.containsKey("minecraft:cave_air") ? blockCounter.get("minecraft:cave_air") : 0));
System.out.print("Generating CSV... ");
String data = "id,";
int minY = getMinimumY();
int maxY = getMaximumY();
for(int i = minY; i <= maxY; i++) {
data += i + ",";
}
data += "total,percent_of_total,percent_excluding_air\n";
int digits = String.valueOf(blockCounter.size()).length();
String completionFormat = "[%0" + digits + "d/%0" + digits + "d]";
int keyIndex = 0;
for(String key : heightCounter.keySet()) {
keyIndex += 1;
System.out.print("\rGenerating CSV... " + String.format(completionFormat, keyIndex, blockCounter.size()));
data += key + ",";
for(int i = minY; i <= maxY; i++) {
if(!heightCounter.get(key).containsKey(i)) {
data += "0,";
} else {
data += heightCounter.get(key).get(i) + ",";
}
}
data += blockCounter.get(key) + ","
+ Main.DECIMAL_FORMAT.format(((double) blockCounter.get(key) / (double) totalBlocks) * 100.0d);
if(key.equals("minecraft:air") || key.equals("minecraft:cave_air")) {
data += ",N/A";
} else {
data += "," + Main.DECIMAL_FORMAT.format(((double) blockCounter.get(key) / totalExcludingAir) * 100.0d);
}
data += "\n";
}
try {
File out = new File(Main.getOutputPrefix() + ".csv");
Main.writeStringToFile(out, data);
System.out.println("\nData written to " + out.getAbsolutePath());
} catch(IOException e) {
e.printStackTrace();
System.exit(1);
}
if(Main.generateTable) {
try {
File out = new File(Main.getOutputPrefix() + "_table.html");
Main.writeStringToFile(out, generateTable((double) totalBlocks, totalExcludingAir));
System.out.println("\nTable written to " + out.getAbsolutePath());
} catch(IOException e) {
e.printStackTrace();
System.exit(1);
}
}
if(Main.saveStatistics) {
try {
Main.writeStringToFile(new File(Main.getOutputPrefix() + "_stats.txt"),
"chunk-count=" + chunkCount + "\nunique-blocks=" + blockCounter.size() + "\ntotal-blocks="
+ totalBlocks + "\nduration-millis=" + duration + "\nduration-readable="
+ Main.millisToHMS(duration));
} catch(IOException e) {
e.printStackTrace();
}
}
}

private void processRegion(RegionFile r, String name, int x, int z) throws Exception {
Expand Down
Loading

0 comments on commit fb66a3e

Please sign in to comment.