Skip to content

Commit c086e6c

Browse files
committed
Config System
1 parent c5a16bf commit c086e6c

File tree

7 files changed

+182
-0
lines changed

7 files changed

+182
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package fun.mntale.atdeprecated.config;
2+
3+
import java.io.File;
4+
5+
public class Config {
6+
private static final Manager configManager = new Manager(new File("atdeprecated_config.toml"));
7+
8+
public static void init() {
9+
registerModules();
10+
configManager.preload();
11+
}
12+
13+
public static void load() {
14+
configManager.load();
15+
}
16+
17+
private static void registerModules() {
18+
configManager.registerModule(Global.REMOVED_CONFIG);
19+
}
20+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package fun.mntale.atdeprecated.config;
2+
3+
import fun.mntale.atdeprecated.config.modules.removed.RemovedConfig;
4+
5+
public class Global {
6+
public static final RemovedConfig REMOVED_CONFIG = new RemovedConfig();
7+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package fun.mntale.atdeprecated.config;
2+
3+
import com.electronwill.nightconfig.core.file.CommentedFileConfig;
4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class Manager {
10+
private final File configFile;
11+
private final List<Module> modules = new ArrayList<>();
12+
private CommentedFileConfig config;
13+
14+
public Manager(File configFile) {
15+
this.configFile = configFile;
16+
}
17+
18+
public void registerModule(Module module) {
19+
modules.add(module);
20+
}
21+
22+
public void preload() {
23+
if (!configFile.exists()) {
24+
try {
25+
configFile.getParentFile().mkdirs();
26+
configFile.createNewFile();
27+
} catch (IOException e) {
28+
throw new RuntimeException("Failed to create config file", e);
29+
}
30+
}
31+
config = CommentedFileConfig.of(configFile);
32+
config.load();
33+
}
34+
35+
public void load() {
36+
for (Module module : modules) {
37+
for (Setting<?> setting : module.getSettings()) {
38+
loadSetting(module, setting);
39+
}
40+
}
41+
save();
42+
}
43+
44+
private <T> void loadSetting(Module module, Setting<T> setting) {
45+
if (setting instanceof RemovedSetting) {
46+
loadRemovedSetting(module, (RemovedSetting) setting);
47+
return;
48+
}
49+
String path = module.getName() + "." + setting.getKey();
50+
Object value = config.get(path);
51+
if (value == null) {
52+
config.set(path, setting.getDefaultValue());
53+
config.setComment(path, setting.getComment());
54+
setting.setValue(setting.getDefaultValue());
55+
} else {
56+
// This is a simplification. A real implementation would need to handle type conversion and validation.
57+
setting.setValue((T) value);
58+
}
59+
}
60+
61+
private void loadRemovedSetting(Module module, RemovedSetting setting) {
62+
String path = module.getName() + "." + setting.getKey();
63+
if (config.contains(path)) {
64+
config.remove(path);
65+
}
66+
}
67+
68+
public void save() {
69+
for (Module module : modules) {
70+
for (Setting<?> setting : module.getSettings()) {
71+
if (setting instanceof RemovedSetting) {
72+
continue;
73+
}
74+
String path = module.getName() + "." + setting.getKey();
75+
config.set(path, setting.getValue());
76+
config.setComment(path, setting.getComment());
77+
}
78+
}
79+
config.save();
80+
}
81+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package fun.mntale.atdeprecated.config;
2+
3+
import java.util.List;
4+
5+
public interface Module {
6+
String getName();
7+
8+
List<Setting<?>> getSettings();
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package fun.mntale.atdeprecated.config;
2+
3+
public class RemovedSetting extends Setting<Object> {
4+
public RemovedSetting(String module, String key) {
5+
super(key, null, null);
6+
}
7+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package fun.mntale.atdeprecated.config;
2+
3+
public class Setting<T> {
4+
private final String key;
5+
private final T defaultValue;
6+
private final String comment;
7+
private T value;
8+
9+
public Setting(String key, T defaultValue, String comment) {
10+
this.key = key;
11+
this.defaultValue = defaultValue;
12+
this.comment = comment;
13+
this.value = defaultValue;
14+
}
15+
16+
public String getKey() {
17+
return key;
18+
}
19+
20+
public T getDefaultValue() {
21+
return defaultValue;
22+
}
23+
24+
public String getComment() {
25+
return comment;
26+
}
27+
28+
public T getValue() {
29+
return value;
30+
}
31+
32+
public void setValue(T value) {
33+
this.value = value;
34+
}
35+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package fun.mntale.atdeprecated.config.modules.removed;
2+
3+
import fun.mntale.atdeprecated.config.Module;
4+
import fun.mntale.atdeprecated.config.RemovedSetting;
5+
import fun.mntale.atdeprecated.config.Setting;
6+
7+
import java.util.Collections;
8+
import java.util.List;
9+
10+
public class RemovedConfig implements Module {
11+
12+
public final Setting<?> removed = new RemovedSetting("cat", "test");
13+
14+
@Override
15+
public String getName() {
16+
return "removed_config";
17+
}
18+
19+
@Override
20+
public List<Setting<?>> getSettings() {
21+
return Collections.singletonList(removed);
22+
}
23+
}

0 commit comments

Comments
 (0)