Skip to content

Commit 204b97a

Browse files
committed
prettyprint json files by default
1 parent cbf1b83 commit 204b97a

14 files changed

+109
-51
lines changed

src/main/java/net/hypixel/resourcepack/Converter.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66

77
public abstract class Converter {
88

9-
public abstract void convert(PackConverter main, Pack pack) throws IOException;
9+
protected PackConverter packConverter;
10+
11+
public Converter(PackConverter packConverter) {
12+
this.packConverter = packConverter;
13+
}
14+
15+
public abstract void convert(Pack pack) throws IOException;
1016

1117
}

src/main/java/net/hypixel/resourcepack/Main.java

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ public class Main {
88

99
public static void main(String[] args) throws IOException {
1010
OptionSet optionSet = Options.PARSER.parse(args);
11+
if (optionSet.has(Options.HELP)) {
12+
Options.PARSER.printHelpOn(System.out);
13+
return;
14+
}
15+
1116
new PackConverter(optionSet).run();
1217

1318
System.out.println("Done, press any key to exit!");

src/main/java/net/hypixel/resourcepack/Options.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ public class Options {
1212

1313
public static final OptionParser PARSER = new OptionParser();
1414

15-
public static final OptionSpec<Void> HELP = PARSER.acceptsAll(Arrays.asList("h", "help"), "Print this message.").forHelp();
15+
public static final OptionSpec<Void> HELP = PARSER.acceptsAll(Arrays.asList("?", "h", "help"), "Print this message.").forHelp();
1616

1717
public static final OptionSpec<Path> INPUT_DIR = PARSER.acceptsAll(Arrays.asList("i", "input", "input-dir"), "Input directory for the packs")
1818
.withRequiredArg()
1919
.withValuesConvertedBy(new PathConverter())
2020
.defaultsTo(Paths.get("./"));
2121

22+
public static final OptionSpec<Void> MINIFY = PARSER.accepts("minify", "Minify the json files.");
23+
2224
public static class PathConverter implements ValueConverter<Path> {
2325

2426
@Override

src/main/java/net/hypixel/resourcepack/PackConverter.java

+21-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.hypixel.resourcepack;
22

33
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
45
import joptsimple.OptionSet;
56
import net.hypixel.resourcepack.impl.*;
67
import net.hypixel.resourcepack.pack.Pack;
@@ -13,27 +14,32 @@
1314

1415
public class PackConverter {
1516

16-
public static final Gson GSON = new Gson();
1717
public static final boolean DEBUG = true;
1818

1919
protected final OptionSet optionSet;
20+
protected final Gson gson;
21+
2022
protected final Map<Class<? extends Converter>, Converter> converters = new LinkedHashMap<>();
2123

2224
public PackConverter(OptionSet optionSet) {
2325
this.optionSet = optionSet;
2426

27+
GsonBuilder gsonBuilder = new GsonBuilder();
28+
if (!this.optionSet.has(Options.MINIFY)) gsonBuilder.setPrettyPrinting();
29+
this.gson = gsonBuilder.create();
30+
2531
// this needs to be run first, other converters might reference new directory names
26-
this.registerConverter(new NameConverter());
32+
this.registerConverter(new NameConverter(this));
2733

28-
this.registerConverter(new PackMetaConverter());
34+
this.registerConverter(new PackMetaConverter(this));
2935

30-
this.registerConverter(new ModelConverter());
31-
this.registerConverter(new SpacesConverter());
32-
this.registerConverter(new SoundsConverter());
33-
this.registerConverter(new ParticleConverter());
34-
this.registerConverter(new BlockStateConverter());
35-
this.registerConverter(new AnimationConverter());
36-
this.registerConverter(new MapIconConverter());
36+
this.registerConverter(new ModelConverter(this));
37+
this.registerConverter(new SpacesConverter(this));
38+
this.registerConverter(new SoundsConverter(this));
39+
this.registerConverter(new ParticleConverter(this));
40+
this.registerConverter(new BlockStateConverter(this));
41+
this.registerConverter(new AnimationConverter(this));
42+
this.registerConverter(new MapIconConverter(this));
3743
}
3844

3945
public void registerConverter(Converter converter) {
@@ -58,7 +64,7 @@ public void run() throws IOException {
5864
System.out.println(" Running Converters");
5965
for (Converter converter : converters.values()) {
6066
if (PackConverter.DEBUG) System.out.println(" Running " + converter.getClass().getSimpleName());
61-
converter.convert(this, pack);
67+
converter.convert(pack);
6268
}
6369

6470
pack.getHandler().finish();
@@ -69,6 +75,10 @@ public void run() throws IOException {
6975
});
7076
}
7177

78+
public Gson getGson() {
79+
return gson;
80+
}
81+
7282
@Override
7383
public String toString() {
7484
return "PackConverter{" +

src/main/java/net/hypixel/resourcepack/Util.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.hypixel.resourcepack;
22

3+
import com.google.gson.Gson;
34
import com.google.gson.JsonObject;
45
import com.google.gson.stream.JsonReader;
56

@@ -11,7 +12,7 @@
1112
import java.util.Comparator;
1213

1314
public final class Util {
14-
15+
1516
private Util() {
1617
throw new UnsupportedOperationException("This class cannot be instantiated");
1718
}
@@ -41,11 +42,11 @@ public static boolean fileExistsCorrectCasing(Path path) throws IOException {
4142
return path.toString().equals(path.toFile().getCanonicalPath());
4243
}
4344

44-
public static JsonObject readJsonResource(String path) {
45+
public static JsonObject readJsonResource(Gson gson, String path) {
4546
try (InputStream stream = PackConverter.class.getResourceAsStream(path)) {
4647
if (stream == null) return null;
4748
try (InputStreamReader streamReader = new InputStreamReader(stream)) {
48-
return PackConverter.GSON.fromJson(streamReader, JsonObject.class);
49+
return gson.fromJson(streamReader, JsonObject.class);
4950
}
5051
} catch (IOException e) {
5152
throw new RuntimeException(e);
@@ -61,13 +62,13 @@ public static BufferedImage readImageResource(String path) {
6162
}
6263
}
6364

64-
public static JsonObject readJson(Path path) throws IOException {
65-
return Util.readJson(path, JsonObject.class);
65+
public static JsonObject readJson(Gson gson, Path path) throws IOException {
66+
return Util.readJson(gson, path, JsonObject.class);
6667
}
6768

68-
public static <T> T readJson(Path path, Class<T> clazz) throws IOException {
69+
public static <T> T readJson(Gson gson, Path path, Class<T> clazz) throws IOException {
6970
// TODO Improvement: this will fail if there is a BOM in the file
70-
return PackConverter.GSON.fromJson(new JsonReader(new FileReader(path.toFile())), clazz);
71+
return gson.fromJson(new JsonReader(new FileReader(path.toFile())), clazz);
7172
}
7273

7374
/**

src/main/java/net/hypixel/resourcepack/impl/AnimationConverter.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@
1616

1717
public class AnimationConverter extends Converter {
1818

19+
public AnimationConverter(PackConverter packConverter) {
20+
super(packConverter);
21+
}
22+
1923
@Override
20-
public void convert(PackConverter main, Pack pack) throws IOException {
24+
public void convert(Pack pack) throws IOException {
2125
fixAnimations(pack.getWorkingPath().resolve("assets" + File.separator + "minecraft" + File.separator + "textures" + File.separator + "block"));
2226
fixAnimations(pack.getWorkingPath().resolve("assets" + File.separator + "minecraft" + File.separator + "textures" + File.separator + "item"));
2327
}
@@ -29,7 +33,7 @@ protected void fixAnimations(Path animations) throws IOException {
2933
.filter(file -> file.toString().endsWith(".png.mcmeta"))
3034
.forEach(file -> {
3135
try {
32-
JsonObject json = Util.readJson(file);
36+
JsonObject json = Util.readJson(packConverter.getGson(), file);
3337

3438
boolean anyChanges = false;
3539
JsonElement animationElement = json.get("animation");
@@ -44,7 +48,7 @@ protected void fixAnimations(Path animations) throws IOException {
4448
}
4549

4650
if (anyChanges) {
47-
Files.write(file, Collections.singleton(json.toString()), Charset.forName("UTF-8"));
51+
Files.write(file, Collections.singleton(packConverter.getGson().toJson(json)), Charset.forName("UTF-8"));
4852

4953
if (PackConverter.DEBUG) System.out.println(" Converted " + file.getFileName());
5054
}

src/main/java/net/hypixel/resourcepack/impl/BlockStateConverter.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,20 @@
1818

1919
public class BlockStateConverter extends Converter {
2020

21+
public BlockStateConverter(PackConverter packConverter) {
22+
super(packConverter);
23+
}
24+
2125
@Override
22-
public void convert(PackConverter main, Pack pack) throws IOException {
26+
public void convert(Pack pack) throws IOException {
2327
Path states = pack.getWorkingPath().resolve("assets" + File.separator + "minecraft" + File.separator + "blockstates");
2428
if (!states.toFile().exists()) return;
2529

2630
Files.list(states)
2731
.filter(file -> file.toString().endsWith(".json"))
2832
.forEach(file -> {
2933
try {
30-
JsonObject json = Util.readJson(file);
34+
JsonObject json = Util.readJson(packConverter.getGson(), file);
3135

3236
boolean anyChanges = false;
3337
JsonObject variantsObject = json.getAsJsonObject("variants");
@@ -64,7 +68,7 @@ public void convert(PackConverter main, Pack pack) throws IOException {
6468
}
6569

6670
if (anyChanges) {
67-
Files.write(file, Collections.singleton(json.toString()), Charset.forName("UTF-8"));
71+
Files.write(file, Collections.singleton(packConverter.getGson().toJson(json)), Charset.forName("UTF-8"));
6872

6973
if (PackConverter.DEBUG) System.out.println(" Converted " + file.getFileName());
7074
}

src/main/java/net/hypixel/resourcepack/impl/MapIconConverter.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ public class MapIconConverter extends Converter {
1818

1919
protected Map<Long, Long> mapping = new HashMap<>();
2020

21-
public MapIconConverter() {
21+
public MapIconConverter(PackConverter packConverter) {
22+
super(packConverter);
23+
2224
mapping.put(pack(0, 0), pack(0, 0));
2325
mapping.put(pack(8, 0), pack(8, 0));
2426
mapping.put(pack(16, 0), pack(16, 0));
@@ -32,7 +34,7 @@ public MapIconConverter() {
3234
}
3335

3436
@Override
35-
public void convert(PackConverter main, Pack pack) throws IOException {
37+
public void convert(Pack pack) throws IOException {
3638
Path imagePath = pack.getWorkingPath().resolve("assets" + File.separator + "minecraft" + File.separator + "textures" + File.separator + "map" + File.separator + "map_icons.png");
3739
if (!imagePath.toFile().exists()) return;
3840

src/main/java/net/hypixel/resourcepack/impl/ModelConverter.java

+12-8
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,26 @@
1717

1818
public class ModelConverter extends Converter {
1919

20+
public ModelConverter(PackConverter packConverter) {
21+
super(packConverter);
22+
}
23+
2024
@Override
21-
public void convert(PackConverter main, Pack pack) throws IOException {
25+
public void convert(Pack pack) throws IOException {
2226
Path models = pack.getWorkingPath().resolve("assets" + File.separator + "minecraft" + File.separator + "models");
2327

24-
remapModelJson(main, models.resolve("block"));
25-
remapModelJson(main, models.resolve("item"));
28+
remapModelJson(models.resolve("block"));
29+
remapModelJson(models.resolve("item"));
2630
}
2731

28-
protected void remapModelJson(PackConverter main, Path path) throws IOException {
32+
protected void remapModelJson(Path path) throws IOException {
2933
if (!path.toFile().exists()) return;
3034

3135
Files.list(path)
3236
.filter(path1 -> path1.toString().endsWith(".json"))
3337
.forEach(model -> {
3438
try {
35-
JsonObject jsonObject = Util.readJson(model);
39+
JsonObject jsonObject = Util.readJson(packConverter.getGson(), model);
3640

3741
// minify the json so we can replace spaces in paths easily
3842
// TODO Improvement: handle this in a cleaner way?
@@ -44,9 +48,9 @@ protected void remapModelJson(PackConverter main, Path path) throws IOException
4448
Files.write(model, Collections.singleton(content), Charset.forName("UTF-8"));
4549

4650
// handle the remapping of textures, for models that use default texture names
47-
jsonObject = Util.readJson(model);
51+
jsonObject = Util.readJson(packConverter.getGson(), model);
4852
if (jsonObject.has("textures")) {
49-
NameConverter nameConverter = main.getConverter(NameConverter.class);
53+
NameConverter nameConverter = packConverter.getConverter(NameConverter.class);
5054

5155
JsonObject textureObject = jsonObject.getAsJsonObject("textures");
5256
for (Map.Entry<String, JsonElement> entry : textureObject.entrySet()) {
@@ -59,7 +63,7 @@ protected void remapModelJson(PackConverter main, Path path) throws IOException
5963
}
6064
}
6165

62-
Files.write(model, Collections.singleton(jsonObject.toString()), Charset.forName("UTF-8"));
66+
Files.write(model, Collections.singleton(packConverter.getGson().toJson(jsonObject)), Charset.forName("UTF-8"));
6367
} catch (IOException e) {
6468
throw Util.propagate(e);
6569
}

src/main/java/net/hypixel/resourcepack/impl/NameConverter.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ public class NameConverter extends Converter {
1919
protected final Mapping blockMapping = new BlockMapping();
2020
protected final Mapping itemMapping = new ItemMapping();
2121

22+
public NameConverter(PackConverter packConverter) {
23+
super(packConverter);
24+
}
25+
2226
@Override
23-
public void convert(PackConverter main, Pack pack) throws IOException {
27+
public void convert(Pack pack) throws IOException {
2428
Path models = pack.getWorkingPath().resolve("assets" + File.separator + "minecraft" + File.separator + "models");
2529
if (models.resolve("blocks").toFile().exists()) Files.move(models.resolve("blocks"), models.resolve("block"));
2630
renameAll(blockMapping, ".json", models.resolve("block"));
@@ -86,11 +90,11 @@ public String remap(String in) {
8690

8791
}
8892

89-
protected static class BlockMapping extends Mapping {
93+
protected class BlockMapping extends Mapping {
9094

9195
@Override
9296
protected void load() {
93-
JsonObject blocks = Util.readJsonResource("/blocks.json");
97+
JsonObject blocks = Util.readJsonResource(packConverter.getGson(), "/blocks.json");
9498
if (blocks != null) {
9599
for (Map.Entry<String, JsonElement> entry : blocks.entrySet()) {
96100
this.mapping.put(entry.getKey(), entry.getValue().getAsString());
@@ -100,11 +104,11 @@ protected void load() {
100104

101105
}
102106

103-
protected static class ItemMapping extends Mapping {
107+
protected class ItemMapping extends Mapping {
104108

105109
@Override
106110
protected void load() {
107-
JsonObject items = Util.readJsonResource("/items.json");
111+
JsonObject items = Util.readJsonResource(packConverter.getGson(), "/items.json");
108112
if (items != null) {
109113
for (Map.Entry<String, JsonElement> entry : items.entrySet()) {
110114
this.mapping.put(entry.getKey(), entry.getValue().getAsString());

src/main/java/net/hypixel/resourcepack/impl/PackMetaConverter.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import com.google.gson.JsonObject;
44
import net.hypixel.resourcepack.Converter;
5-
import net.hypixel.resourcepack.pack.Pack;
65
import net.hypixel.resourcepack.PackConverter;
76
import net.hypixel.resourcepack.Util;
7+
import net.hypixel.resourcepack.pack.Pack;
88

99
import java.io.IOException;
1010
import java.nio.charset.Charset;
@@ -14,12 +14,16 @@
1414

1515
public class PackMetaConverter extends Converter {
1616

17+
public PackMetaConverter(PackConverter packConverter) {
18+
super(packConverter);
19+
}
20+
1721
@Override
18-
public void convert(PackConverter main, Pack pack) throws IOException {
22+
public void convert(Pack pack) throws IOException {
1923
Path file = pack.getWorkingPath().resolve("pack.mcmeta");
2024
if (!file.toFile().exists()) return;
2125

22-
JsonObject json = Util.readJson(file);
26+
JsonObject json = Util.readJson(packConverter.getGson(), file);
2327
{
2428
JsonObject meta = json.getAsJsonObject("meta");
2529
if (meta == null) meta = new JsonObject();
@@ -33,6 +37,6 @@ public void convert(PackConverter main, Pack pack) throws IOException {
3337
json.add("pack", packObject);
3438
}
3539

36-
Files.write(file, Collections.singleton(json.toString()), Charset.forName("UTF-8"));
40+
Files.write(file, Collections.singleton(packConverter.getGson().toJson(json)), Charset.forName("UTF-8"));
3741
}
3842
}

src/main/java/net/hypixel/resourcepack/impl/ParticleConverter.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313

1414
public class ParticleConverter extends Converter {
1515

16+
public ParticleConverter(PackConverter packConverter) {
17+
super(packConverter);
18+
}
19+
1620
@Override
17-
public void convert(PackConverter main, Pack pack) throws IOException {
21+
public void convert(Pack pack) throws IOException {
1822
Path imagePath = pack.getWorkingPath().resolve("assets" + File.separator + "minecraft" + File.separator + "textures" + File.separator + "particle" + File.separator + "particles.png");
1923
if (!imagePath.toFile().exists()) return;
2024

0 commit comments

Comments
 (0)