Skip to content

Commit

Permalink
perf(config): optimizes the configuration file save logic
Browse files Browse the repository at this point in the history
 - The manual invocation of save and load methods has been removed from the SetConfig command
 - Added logic in ConfigManager to save only when changes are configured
 - Removed unnecessary log output and simplified code structure
  • Loading branch information
Suisuroru committed Jan 21, 2025
1 parent 2124009 commit 31ed979
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command

try {
configManager.setConfigValue(configName, value);
configManager.save();
configManager.load();
sender.sendMessage("§a配置项 " + configName + " 已成功设置为 " + value);
} catch (Exception e) {
sender.sendMessage("§c修改配置项 " + configName + " 的值时出错,请检查配置文件或联系开发人员。");
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/fun/suya/suisuroru/config/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public void load() {

Properties defaultProperties = getDefaultProperties();
List<String> fieldNames = getConfigFieldNames();
boolean label = false;

for (String fieldName : fieldNames) {
try {
Expand All @@ -78,19 +79,17 @@ public void load() {
String value = properties.getProperty(fieldName);
if (value == null) {
value = defaultProperties.getProperty(fieldName);
LOGGER.info("Using default value for " + fieldName + ": " + value);
} else {
LOGGER.info("Setting config value for " + fieldName + ": " + value);
label = true;
}
Object parsedValue = parseValue(field.getType(), value);
field.set(null, convertToFieldType(field.getType(), parsedValue));
} catch (NoSuchFieldException | IllegalAccessException e) {
LOGGER.warning("Failed to set config value for " + fieldName + ": " + e.getMessage());
}
}

save();

if (label) {
save();
}
} catch (IOException e) {
LOGGER.warning("Failed to load config file " + Main.CONFIG_FILE.getAbsolutePath() + ": " + e.getMessage());
LOGGER.info("Using default config values.");
Expand All @@ -116,6 +115,7 @@ public void setConfigValue(String fieldName, String value) {
field.setAccessible(true);
Object parsedValue = parseValue(field.getType(), value);
field.set(null, parsedValue);
save();
} catch (Exception e) {
LOGGER.warning("Failed to parse config value for " + fieldName + ": " + e.getMessage());
}
Expand Down

0 comments on commit 31ed979

Please sign in to comment.