Skip to content

Commit 09dcfb8

Browse files
authored
Merge pull request #292 from Furqit/improved-configs
Split config into multiple files for better clarity and easier management, Closes Issue #276
2 parents 160245b + cf7b1eb commit 09dcfb8

File tree

4 files changed

+249
-78
lines changed

4 files changed

+249
-78
lines changed

AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/AnarchyExploitFixes.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import de.tr7zw.nbtapi.NBT;
55
import me.xginko.aef.commands.AEFCommand;
66
import me.xginko.aef.config.Config;
7+
import me.xginko.aef.config.ConfigMigration;
78
import me.xginko.aef.config.Translation;
89
import me.xginko.aef.modules.AEFModule;
910
import me.xginko.aef.utils.KyoriUtil;
@@ -165,6 +166,13 @@ public void onEnable() {
165166
prefixedLogger.info("Registering Permissions");
166167
AEFPermission.registerAll();
167168

169+
prefixedLogger.info("Checking for config migration");
170+
ConfigMigration configMigration = new ConfigMigration(this);
171+
boolean migrated = configMigration.migrateIfNeeded();
172+
if (migrated) {
173+
prefixedLogger.info("Config migration completed successfully!");
174+
}
175+
168176
prefixedLogger.info("Loading Config");
169177
reloadConfiguration();
170178

AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/config/Config.java

Lines changed: 131 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.cryptomorin.xseries.XSound;
44
import io.github.thatsmusic99.configurationmaster.api.ConfigFile;
55
import io.github.thatsmusic99.configurationmaster.api.ConfigSection;
6+
import io.github.thatsmusic99.configurationmaster.impl.CMMemorySection;
67
import me.xginko.aef.AnarchyExploitFixes;
78
import me.xginko.aef.utils.LocaleUtil;
89
import me.xginko.aef.utils.MathUtil;
@@ -12,13 +13,17 @@
1213

1314
import java.io.File;
1415
import java.time.Duration;
15-
import java.util.List;
16-
import java.util.Locale;
17-
import java.util.Map;
16+
import java.util.*;
1817

1918
public class Config {
2019

21-
private final ConfigFile config;
20+
private final AnarchyExploitFixes plugin;
21+
private final HashMap<String, ConfigFile> configFiles = new HashMap<>();
22+
private static final Set<String> configSections = new HashSet<>(Arrays.asList(
23+
"language", "general", "misc", "chat", "elytra",
24+
"chunk-limits", "lag-preventions", "patches", "illegals",
25+
"dupe-preventions", "preventions", "combat"
26+
));
2227
public final Locale default_lang;
2328
public final Sound elytra_too_fast_sound;
2429
public final Component cmd_say_format;
@@ -33,16 +38,28 @@ public class Config {
3338
elytra_actionbar_enabled, elytra_show_chunkage, elytra_play_too_fast_sound,
3439
elytra_teleport_back, elytra_calculate_3D, permissions_hook, update_checker_enabled;
3540

41+
public static Set<String> getConfigSections() {
42+
return Collections.unmodifiableSet(configSections);
43+
}
44+
3645
public Config() throws Exception {
37-
AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance();
38-
// Load config.yml with ConfigMaster
39-
this.config = ConfigFile.loadConfig(new File(plugin.getDataFolder(), "config.yml"));
46+
this.plugin = AnarchyExploitFixes.getInstance();
47+
48+
File settingsDir = new File(plugin.getDataFolder(), "settings");
49+
if (!settingsDir.exists()) {
50+
settingsDir.mkdirs();
51+
}
52+
53+
// Initialize all config files
54+
initConfigFile("config");
55+
for (String section : configSections) {
56+
initConfigFile("settings/" + section);
57+
}
4058

41-
config.set("plugin-version", plugin.getPluginMeta().getVersion());
42-
config.set("server-version", plugin.getServer().getVersion());
59+
loadAllConfigs();
4360

44-
// Pre-structure to force order
45-
structureConfig();
61+
getConfig("config").set("plugin-version", plugin.getPluginMeta().getVersion());
62+
getConfig("config").set("server-version", plugin.getServer().getVersion());
4663

4764
// Language Settings
4865
this.default_lang = LocaleUtil.localeForLanguageTag(getString("language.default-language", "en_us", """
@@ -76,7 +93,7 @@ The time in ticks (1 sec = 20 ticks) a checked tps will be cached\s
7693
Help command that shows a small command overview for players.""");
7794
this.cmd_toggleConMsgs_enabled = getBoolean("general.commands.toggleconnectionmsgs.enable", true, """
7895
If you don't use join leave/messages, you can set this to false.""");
79-
config.addComment("general.commands", """
96+
addComment("general.commands", """
8097
A server restart is required when changing a command's enable status!""");
8198

8299
// Elytra Speed
@@ -115,121 +132,167 @@ The time in ticks (1 sec = 20 ticks) a checked tps will be cached\s
115132
this.elytra_enable_netherceiling = getBoolean("elytra.elytra-speed.Nether-Ceiling.enable", true);
116133

117134
// Misc
118-
config.addDefault("misc.join-leave-messages.enable", true); // add default here so enable option shows up first.
135+
addDefault("misc.join-leave-messages.enable", true); // add default here so enable option shows up first.
119136
this.connectionMsgsAreOnByDefault = getBoolean("misc.join-leave-messages.connection-messages-on-by-default", true, """
120137
If set to true, players will see join/leave messages by default\s
121138
and enter /toggleconnectionmsgs to disable them.\s
122139
If set to false will work the other way around.""");
123-
config.addDefault("misc.join-leave-messages.show-in-console", false); // add default here so show-in-console option is not misplaced.
140+
addDefault("misc.join-leave-messages.show-in-console", false); // add default here so show-in-console option is not misplaced.
141+
142+
saveAllConfigs();
143+
}
144+
145+
private void initConfigFile(String name) throws Exception {
146+
ConfigFile configFile = ConfigFile.loadConfig(new File(plugin.getDataFolder(), name + ".yml"));
147+
configFiles.put(name, configFile);
148+
}
149+
150+
private ConfigFile getConfig(String name) {
151+
return configFiles.getOrDefault(name, null);
124152
}
125153

126154
public void saveConfig() {
127155
try {
128-
config.save();
156+
saveAllConfigs();
129157
} catch (Exception e) {
130-
AnarchyExploitFixes.prefixedLogger().error("Failed to save config file!", e);
158+
AnarchyExploitFixes.prefixedLogger().error("Failed to save config files!", e);
131159
}
132160
}
133161

134-
private void structureConfig() {
135-
createTitledSection("Language", "language");
136-
createTitledSection("General", "general");
137-
createTitledSection("Miscellaneous", "misc");
138-
createTitledSection("Chat", "chat");
139-
createTitledSection("Elytra", "elytra");
140-
createTitledSection("Chunk Limits", "chunk-limits");
141-
createTitledSection("Lag Preventions", "lag-preventions");
142-
createTitledSection("Patches", "patches");
143-
createTitledSection("Illegals", "illegals");
144-
createTitledSection("Dupe Preventions", "dupe-preventions");
145-
createTitledSection("Preventions", "preventions");
146-
createTitledSection("Combat", "combat");
162+
private void loadAllConfigs() {
163+
for (ConfigFile config : configFiles.values()) {
164+
try {
165+
config.load();
166+
} catch (Exception e) {
167+
AnarchyExploitFixes.prefixedLogger().error("Failed to load config file!", e);
168+
}
169+
}
147170
}
148171

149-
public ConfigFile master() {
150-
return config;
172+
private void saveAllConfigs() {
173+
for (ConfigFile config : configFiles.values()) {
174+
try {
175+
config.save();
176+
} catch (Exception e) {
177+
AnarchyExploitFixes.prefixedLogger().error("Failed to save config file!", e);
178+
}
179+
}
180+
}
181+
182+
private String[] getConfigNameAndPath(String path) {
183+
if (!path.contains(".")) {
184+
return new String[]{"config", path};
185+
}
186+
187+
String section = path.substring(0, path.indexOf("."));
188+
String newPath = path.substring(path.indexOf(".") + 1);
189+
190+
if (configSections.contains(section)) {
191+
return new String[]{"settings/" + section, newPath};
192+
} else {
193+
return new String[]{"config", path};
194+
}
151195
}
152196

153-
public void createTitledSection(String title, String path) {
154-
config.addSection(title);
155-
config.addDefault(path, null);
197+
private <T> T getConfigValue(String path, T defaultValue, String comment, ConfigValueGetter<T> getter) {
198+
String[] configAndPath = getConfigNameAndPath(path);
199+
ConfigFile config = getConfig(configAndPath[0]);
200+
if (config == null) return defaultValue;
201+
202+
if (comment != null) {
203+
config.addDefault(configAndPath[1], defaultValue, comment);
204+
} else {
205+
config.addDefault(configAndPath[1], defaultValue);
206+
}
207+
208+
return getter.get(config, configAndPath[1], defaultValue);
156209
}
157210

158211
public boolean getBoolean(String path, boolean def, String comment) {
159-
config.addDefault(path, def, comment);
160-
return config.getBoolean(path, def);
212+
return getConfigValue(path, def, comment, CMMemorySection::getBoolean);
161213
}
162214

163215
public boolean getBoolean(String path, boolean def) {
164-
config.addDefault(path, def);
165-
return config.getBoolean(path, def);
216+
return getConfigValue(path, def, null, CMMemorySection::getBoolean);
166217
}
167218

168219
public String getString(String path, String def, String comment) {
169-
config.addDefault(path, def, comment);
170-
return config.getString(path, def);
220+
return getConfigValue(path, def, comment, CMMemorySection::getString);
171221
}
172222

173223
public String getString(String path, String def) {
174-
config.addDefault(path, def);
175-
return config.getString(path, def);
224+
return getConfigValue(path, def, null, CMMemorySection::getString);
176225
}
177226

178227
public double getDouble(String path, double def, String comment) {
179-
config.addDefault(path, def, comment);
180-
return config.getDouble(path, def);
228+
return getConfigValue(path, def, comment, CMMemorySection::getDouble);
181229
}
182230

183231
public double getDouble(String path, double def) {
184-
config.addDefault(path, def);
185-
return config.getDouble(path, def);
232+
return getConfigValue(path, def, null, CMMemorySection::getDouble);
186233
}
187234

188235
public int getInt(String path, int def, String comment) {
189-
config.addDefault(path, def, comment);
190-
return config.getInteger(path, def);
236+
return getConfigValue(path, def, comment, CMMemorySection::getInteger);
191237
}
192238

193239
public int getInt(String path, int def) {
194-
config.addDefault(path, def);
195-
return config.getInteger(path, def);
240+
return getConfigValue(path, def, null, CMMemorySection::getInteger);
196241
}
197242

198243
public long getLong(String path, long def, String comment) {
199-
config.addDefault(path, def, comment);
200-
return config.getLong(path, def);
244+
return getConfigValue(path, def, comment, CMMemorySection::getLong);
201245
}
202246

203247
public long getLong(String path, long def) {
204-
config.addDefault(path, def);
205-
return config.getLong(path, def);
248+
return getConfigValue(path, def, null, CMMemorySection::getLong);
206249
}
207250

208251
public List<String> getList(String path, List<String> def, String comment) {
209-
config.addDefault(path, def, comment);
210-
return config.getStringList(path);
252+
return getConfigValue(path, def, comment, (config, p, d) -> config.getStringList(p));
211253
}
212254

213255
public List<String> getList(String path, List<String> def) {
214-
config.addDefault(path, def);
215-
return config.getStringList(path);
256+
return getConfigValue(path, def, null, (config, p, d) -> config.getStringList(p));
216257
}
217258

218259
public ConfigSection getConfigSection(String path, Map<String, Object> defaultKeyValue) {
219-
config.addDefault(path, null);
220-
config.makeSectionLenient(path);
221-
defaultKeyValue.forEach((string, object) -> config.addExample(path+"."+string, object));
222-
return config.getConfigSection(path);
260+
String[] configAndPath = getConfigNameAndPath(path);
261+
ConfigFile config = getConfig(configAndPath[0]);
262+
if (config == null) return null;
263+
config.addDefault(configAndPath[1], null);
264+
config.makeSectionLenient(configAndPath[1]);
265+
defaultKeyValue.forEach((string, object) -> config.addExample(configAndPath[1] + "." + string, object));
266+
return config.getConfigSection(configAndPath[1]);
223267
}
224268

225269
public ConfigSection getConfigSection(String path, Map<String, Object> defaultKeyValue, String comment) {
226-
config.addDefault(path, null, comment);
227-
config.makeSectionLenient(path);
228-
defaultKeyValue.forEach((string, object) -> config.addExample(path+"."+string, object));
229-
return config.getConfigSection(path);
270+
String[] configAndPath = getConfigNameAndPath(path);
271+
ConfigFile config = getConfig(configAndPath[0]);
272+
if (config == null) return null;
273+
config.addDefault(configAndPath[1], null, comment);
274+
config.makeSectionLenient(configAndPath[1]);
275+
defaultKeyValue.forEach((string, object) -> config.addExample(configAndPath[1] + "." + string, object));
276+
return config.getConfigSection(configAndPath[1]);
230277
}
231278

232279
public void addComment(String path, String comment) {
233-
config.addComment(path, comment);
280+
String[] configAndPath = getConfigNameAndPath(path);
281+
ConfigFile config = getConfig(configAndPath[0]);
282+
if (config != null) {
283+
config.addComment(configAndPath[1], comment);
284+
}
285+
}
286+
287+
public void addDefault(String path, Object value) {
288+
String[] configAndPath = getConfigNameAndPath(path);
289+
ConfigFile config = getConfig(configAndPath[0]);
290+
if (config != null) {
291+
config.addDefault(configAndPath[1], value);
292+
}
293+
}
294+
295+
private interface ConfigValueGetter<T> {
296+
T get(ConfigFile config, String path, T defaultValue);
234297
}
235-
}
298+
}

0 commit comments

Comments
 (0)