Skip to content

Commit 7debc23

Browse files
authored
fix: /bmreload now reverts to previous config on failure (#1034)
1 parent 0950c61 commit 7debc23

4 files changed

Lines changed: 352 additions & 33 deletions

File tree

common/src/main/java/me/confuser/banmanager/common/BanManagerPlugin.java

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -253,28 +253,42 @@ public final void disable() {
253253
}
254254

255255
public void setupConfigs() {
256-
new MessagesConfig(dataFolder, logger).load();
257-
258-
config = new DefaultConfig(dataFolder, logger);
259-
config.load();
260-
261-
consoleConfig = new ConsoleConfig(dataFolder, logger);
262-
consoleConfig.load();
263-
264-
schedulesConfig = new SchedulesConfig(dataFolder, logger);
265-
schedulesConfig.load();
256+
MessagesConfig newMessagesConfig = new MessagesConfig(dataFolder, logger);
257+
if (!newMessagesConfig.load()) {
258+
logger.warning("Failed to reload messages.yml, keeping previous messages");
259+
}
266260

267-
exemptionsConfig = new ExemptionsConfig(dataFolder, logger);
268-
exemptionsConfig.load();
261+
config = reloadConfig(new DefaultConfig(dataFolder, logger), config, "config.yml");
262+
consoleConfig = reloadConfig(new ConsoleConfig(dataFolder, logger), consoleConfig, "console.yml");
263+
schedulesConfig = reloadConfig(new SchedulesConfig(dataFolder, logger), schedulesConfig, "schedules.yml");
264+
exemptionsConfig = reloadConfig(new ExemptionsConfig(dataFolder, logger), exemptionsConfig, "exemptions.yml");
265+
reasonsConfig = reloadConfig(new ReasonsConfig(dataFolder, logger), reasonsConfig, "reasons.yml");
266+
geoIpConfig = reloadConfig(new GeoIpConfig(dataFolder, logger), geoIpConfig, "geoip.yml");
267+
webhookConfig = reloadConfig(new WebhookConfig(dataFolder, logger), webhookConfig, "webhooks.yml");
268+
}
269269

270-
reasonsConfig = new ReasonsConfig(dataFolder, logger);
271-
reasonsConfig.load();
270+
/**
271+
* Attempts to load a new config. If loading succeeds, returns the new config.
272+
* If loading fails and a previous config exists, logs a warning and returns the previous config.
273+
* If loading fails and no previous config exists (first load), returns the new (failed) config.
274+
*
275+
* @param newConfig the newly created config to load
276+
* @param currentConfig the current config (may be null on first load)
277+
* @param fileName the config file name for logging
278+
* @param <T> the config type
279+
* @return the config to use
280+
*/
281+
private <T extends Config> T reloadConfig(T newConfig, T currentConfig, String fileName) {
282+
if (newConfig.load()) {
283+
return newConfig;
284+
}
272285

273-
geoIpConfig = new GeoIpConfig(dataFolder, logger);
274-
geoIpConfig.load();
286+
if (currentConfig != null) {
287+
logger.warning("Failed to reload " + fileName + ", keeping previous settings");
288+
return currentConfig;
289+
}
275290

276-
webhookConfig = new WebhookConfig(dataFolder, logger);
277-
webhookConfig.load();
291+
return newConfig;
278292
}
279293

280294
private void disableDatabaseLogging() {

common/src/main/java/me/confuser/banmanager/common/configs/Config.java

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public abstract class Config {
1515
protected File dataFolder;
1616
@Getter
1717
protected CommonLogger logger;
18+
@Getter
19+
private boolean safeToSave = false;
1820

1921
public Config(File dataFolder, File file, CommonLogger logger) {
2022
this.dataFolder = dataFolder;
@@ -46,40 +48,62 @@ public Config setFile(File input) {
4648

4749
/**
4850
* Lazy load
51+
*
52+
* @return true if the config was loaded successfully, false otherwise
4953
*/
50-
public void load() {
51-
if (file != null) {
52-
try {
53-
onLoad(file);
54-
55-
afterLoad();
56-
} catch (Exception e) {
57-
e.printStackTrace();
58-
}
59-
} else {
54+
public boolean load() {
55+
if (file == null) {
6056
new InvalidConfigurationException("File cannot be null!").printStackTrace();
57+
return false;
58+
}
59+
60+
try {
61+
if (!onLoad(file)) {
62+
safeToSave = false;
63+
return false;
64+
}
65+
66+
afterLoad();
67+
safeToSave = true;
68+
return true;
69+
} catch (Exception e) {
70+
e.printStackTrace();
71+
safeToSave = false;
72+
return false;
6173
}
6274
}
6375

6476
public abstract void afterLoad();
6577

6678
public abstract void onSave();
6779

68-
public void onLoad(File file) throws Exception {
80+
/**
81+
* Load configuration from file
82+
*
83+
* @param file the file to load
84+
* @return true if the config was loaded successfully, false otherwise
85+
* @throws Exception if the file does not exist
86+
*/
87+
public boolean onLoad(File file) throws Exception {
6988
if (!file.exists()) {
7089
throw new Exception("File " + file.getName() + " does not exist");
7190
}
7291

7392
try {
7493
conf.load(file);
94+
return true;
7595
} catch (InvalidConfigurationException e) {
7696
logger.severe("Invalid yaml file " + file.getName());
77-
78-
return;
97+
return false;
7998
}
8099
}
81100

82101
public void save() {
102+
if (!safeToSave) {
103+
logger.warning("Skipping save for " + file.getName() + " - config was not loaded successfully");
104+
return;
105+
}
106+
83107
onSave();
84108

85109
try {

common/src/main/java/me/confuser/banmanager/common/util/Message.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,25 @@ public static String getString(String key) {
5454

5555
public static void load(YamlConfiguration config, CommonLogger commonLogger) {
5656
logger = commonLogger;
57-
messages.clear();
5857

58+
if (config.getConfigurationSection("messages") == null) {
59+
commonLogger.warning("Messages section not found in messages.yml, keeping previous messages");
60+
return;
61+
}
62+
63+
HashMap<String, String> newMessages = new HashMap<>(10);
5964
for (String key : config.getConfigurationSection("messages").getKeys(true)) {
60-
messages.put(key, config.getString("messages." + key).replace("\\n", "\n"));
65+
String value = config.getString("messages." + key);
66+
if (value != null) {
67+
newMessages.put(key, value.replace("\\n", "\n"));
68+
}
69+
}
70+
71+
if (!newMessages.isEmpty()) {
72+
messages.clear();
73+
messages.putAll(newMessages);
74+
} else {
75+
commonLogger.warning("No messages loaded from messages.yml, keeping previous messages");
6176
}
6277
}
6378

0 commit comments

Comments
 (0)