Commit 1c6ddbae by Jonathan Thomas

- Output red error if config commands fail to save config file

- Updated save-related error message text
parent 50d27268
Pipeline #12158 passed with stage
in 1 minute 51 seconds
...@@ -8,7 +8,8 @@ All notable changes to **CreatureChat** are documented in this file. The format ...@@ -8,7 +8,8 @@ All notable changes to **CreatureChat** are documented in this file. The format
### Changed ### Changed
- Simplified saving of chat data (no more renaming files) - Simplified saving of chat data (no more renaming files)
- Send message to all ops when saving chat data errors (first auto-save happens at 1 minute after launching) - If chat data fails to save, send message to all ops (first auto-save happens at 1 minute after launching)
- If /creaturechat commands fail to save, send message to all ops (fail loudly) and display RED error message
## [1.0.2] - 2024-05-07 ## [1.0.2] - 2024-05-07
......
...@@ -485,7 +485,7 @@ public class ChatDataManager { ...@@ -485,7 +485,7 @@ public class ChatDataManager {
try (Writer writer = new OutputStreamWriter(new FileOutputStream(saveFile), StandardCharsets.UTF_8)) { try (Writer writer = new OutputStreamWriter(new FileOutputStream(saveFile), StandardCharsets.UTF_8)) {
GSON.toJson(this.entityChatDataMap, writer); GSON.toJson(this.entityChatDataMap, writer);
} catch (Exception e) { } catch (Exception e) {
String errorMessage = "Error saving chat data to file system. No chat history will be saved. Check file permissions. " + e.getMessage(); String errorMessage = "Error saving `chatdata.json`. No CreatureChat chat history was saved! " + e.getMessage();
LOGGER.error(errorMessage, e); LOGGER.error(errorMessage, e);
ServerPackets.sendMessageToAllOps(server, errorMessage); ServerPackets.sendMessageToAllOps(server, errorMessage);
} }
......
...@@ -2,8 +2,11 @@ package com.owlmaddie.commands; ...@@ -2,8 +2,11 @@ package com.owlmaddie.commands;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.owlmaddie.network.ServerPackets;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import net.minecraft.util.WorldSavePath; import net.minecraft.util.WorldSavePath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
...@@ -19,6 +22,7 @@ import java.nio.file.Paths; ...@@ -19,6 +22,7 @@ import java.nio.file.Paths;
*/ */
public class ConfigurationHandler { public class ConfigurationHandler {
public static final Logger LOGGER = LoggerFactory.getLogger("creaturechat");
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create(); private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
private final Path serverConfigPath; private final Path serverConfigPath;
private final Path defaultConfigPath; private final Path defaultConfigPath;
...@@ -36,12 +40,16 @@ public class ConfigurationHandler { ...@@ -36,12 +40,16 @@ public class ConfigurationHandler {
return config != null ? config : new Config(); // Return new config if both are null return config != null ? config : new Config(); // Return new config if both are null
} }
public void saveConfig(Config config, boolean useServerConfig) { public boolean saveConfig(Config config, boolean useServerConfig) {
Path path = useServerConfig ? serverConfigPath : defaultConfigPath; Path path = useServerConfig ? serverConfigPath : defaultConfigPath;
try (Writer writer = Files.newBufferedWriter(path)) { try (Writer writer = Files.newBufferedWriter(path)) {
gson.toJson(config, writer); gson.toJson(config, writer);
return true;
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); String errorMessage = "Error saving `creaturechat.json`. CreatureChat config was not saved. " + e.getMessage();
LOGGER.error(errorMessage, e);
ServerPackets.sendMessageToAllOps(ServerPackets.serverInstance, errorMessage);
return false;
} }
} }
......
...@@ -62,9 +62,15 @@ public class CreatureChatCommands { ...@@ -62,9 +62,15 @@ public class CreatureChatCommands {
config.setModel(value); config.setModel(value);
break; break;
} }
configHandler.saveConfig(config, useServerConfig);
Text feedbackMessage = Text.literal(settingDescription + " Set Successfully!").formatted(Formatting.GREEN);; Text feedbackMessage;
if (configHandler.saveConfig(config, useServerConfig)) {
// succeeded
feedbackMessage = Text.literal(settingDescription + " Set Successfully!").formatted(Formatting.GREEN);
} else {
// failed
feedbackMessage = Text.literal(settingDescription + " Set Failed!").formatted(Formatting.RED);
}
source.sendFeedback(() -> feedbackMessage, false); source.sendFeedback(() -> feedbackMessage, false);
LOGGER.info("Command executed: " + feedbackMessage.getString()); LOGGER.info("Command executed: " + feedbackMessage.getString());
return 1; return 1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment