blob: 38f00a804cec41514fa0c2708ad6b2f45756aa91 (
plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
package me.shedaniel.rei.client;
import me.shedaniel.rei.RoughlyEnoughItemsCore;
import net.fabricmc.loader.api.FabricLoader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
public class ConfigHelper {
private final File configFile;
private REIConfig config;
private boolean craftableOnly;
public ConfigHelper() {
this.configFile = new File(FabricLoader.getInstance().getConfigDirectory(), "rei.json");
this.craftableOnly = false;
try {
loadConfig();
RoughlyEnoughItemsCore.LOGGER.info("REI: Config is loaded.");
} catch (IOException e) {
e.printStackTrace();
}
}
public void saveConfig() throws IOException {
configFile.getParentFile().mkdirs();
if (!configFile.exists() && !configFile.createNewFile()) {
RoughlyEnoughItemsCore.LOGGER.error("REI: Failed to save config! Overwriting with default config.");
config = new REIConfig();
return;
}
FileWriter writer = new FileWriter(configFile, false);
try {
REIConfig.GSON.toJson(config, writer);
} finally {
writer.close();
}
}
public void loadConfig() throws IOException {
if (!configFile.exists() || !configFile.canRead()) {
config = new REIConfig();
saveConfig();
return;
}
boolean failed = false;
try {
config = REIConfig.GSON.fromJson(new InputStreamReader(Files.newInputStream(configFile.toPath())), REIConfig.class);
} catch (Exception e) {
failed = true;
}
if (failed || config == null) {
RoughlyEnoughItemsCore.LOGGER.error("REI: Failed to load config! Overwriting with default config.");
config = new REIConfig();
}
saveConfig();
}
public REIConfig getConfig() {
return config;
}
public boolean craftableOnly() {
return craftableOnly;
}
public void toggleCraftableOnly() {
craftableOnly = !craftableOnly;
}
}
|