forked from skywardmc/threadtweak
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigHandler.java
More file actions
52 lines (44 loc) · 1.85 KB
/
ConfigHandler.java
File metadata and controls
52 lines (44 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.github.getchoo.smoothboot.config;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import com.github.getchoo.smoothboot.SmoothBoot;
import net.minecraft.util.Util;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class ConfigHandler {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
public static SmoothBootConfig readConfig() throws IOException {
String configPath = System.getProperty("user.dir") + "/config/" + SmoothBoot.MOD_ID + ".json";
SmoothBoot.LOGGER.debug("Config path: {}", configPath);
// Read config
SmoothBootConfig config;
try (FileReader reader = new FileReader(configPath)) {
config = GSON.fromJson(reader, SmoothBootConfig.class);
if (config == null) {
throw new NullPointerException();
}
config.validate();
try (FileWriter writer = new FileWriter(configPath)) {
GSON.toJson(config, writer);
}
SmoothBoot.LOGGER.debug("Config: {}", config);
} catch (NullPointerException | JsonParseException | IOException e) {
// Create new config
config = new SmoothBootConfig();
File file = new File(configPath);
file.getParentFile().mkdirs();
try (FileWriter writer = new FileWriter(configPath)) {
GSON.toJson(config, writer);
SmoothBoot.LOGGER.debug("New config file created");
}
}
return config;
}
public static void openConfigFile() {
String configPath = System.getProperty("user.dir") + "/config/" + SmoothBoot.MOD_ID + ".json";
Util.getOperatingSystem().open(new File(configPath));
}
}