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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
package com.aizistral.nochatrestrictions.config;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.Optional;
import org.jetbrains.annotations.Nullable;
import com.aizistral.nochatrestrictions.core.NCRCore;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import net.minecraftforge.fml.loading.FMLPaths;
public record NCRConfig(boolean enableMod, boolean allowTelemetry, boolean allowProfanityFilter) {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private static @Nullable NCRConfig instance = null;
private static NCRConfig getDefault() {
return new NCRConfig(true, false, false);
}
public static NCRConfig getInstance() {
if (instance == null) {
instance = loadConfig("NoChatRestrictions.json");
}
return instance;
}
private static NCRConfig loadConfig(String fileName) {
NCRCore.LOGGER.debug("Reading config file {}...", fileName);
NCRConfig config = readFile(fileName).orElseGet(NCRConfig::getDefault);
NCRCore.LOGGER.info("Mod Enabled: {}", config.enableMod());
NCRCore.LOGGER.info("Telemetry Allowed: {}", config.allowTelemetry());
NCRCore.LOGGER.info("Profanity Filter Allowed: {}", config.allowProfanityFilter());
NCRCore.LOGGER.debug("Writing config file {} back to disk...", fileName);
writeFile(fileName, config);
return config;
}
private static Path getConfigFile(String fileName) {
return FMLPaths.CONFIGDIR.get().resolve(fileName);
}
private static Optional<NCRConfig> readFile(String fileName) {
Path file = getConfigFile(fileName);
if (!Files.isRegularFile(file))
return Optional.empty();
try (BufferedReader reader = Files.newBufferedReader(file)) {
JsonObject json = GSON.toJsonTree(getDefault()).getAsJsonObject();
for (Map.Entry<String, JsonElement> entry : JsonParser.parseReader(reader).getAsJsonObject().entrySet()) {
json.add(entry.getKey(), entry.getValue());
}
return Optional.of(GSON.fromJson(json, NCRConfig.class));
} catch (Exception ex) {
NCRCore.LOGGER.fatal("Could not read config file: {}", file);
NCRCore.LOGGER.fatal("This likely indicates the file is corrupted. "
+ "You can try deleting it to fix this problem. Full stacktrace below:");
NCRCore.LOGGER.catching(ex);
throw new RuntimeException("Could not read config file: " + file, ex);
}
}
private static void writeFile(String fileName, NCRConfig config) {
Path file = getConfigFile(fileName);
try {
Files.createDirectories(file.getParent());
try (BufferedWriter writer = Files.newBufferedWriter(file)) {
GSON.toJson(config, writer);
}
} catch (Exception ex) {
NCRCore.LOGGER.fatal("Could not write config file: {}", file);
NCRCore.LOGGER.fatal("Full stacktrace below:");
NCRCore.LOGGER.catching(ex);
throw new RuntimeException("Could not write config file: " + file,ex);
}
}
}
|