Commit 4dab7efe by Jonathan Thomas

Updating commands to add optional --config server, or --config default. Also…

Updating commands to add optional --config server, or --config default. Also adding configuration handler to actually create and load config files.
parent c6eb9ac9
Pipeline #11983 passed with stage
in 19 seconds
package com.owlmaddie.commands;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.WorldSavePath;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* The {@code ConfigurationHandler} class loads and saves configuration settings for this mod. It first
* checks for a config file in the world save folder, and if not found, falls back to the root folder.
* This allows for global/default settings, or server-specific settings.
*/
public class ConfigurationHandler {
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
private final Path serverConfigPath;
private final Path defaultConfigPath;
public ConfigurationHandler(MinecraftServer server) {
this.serverConfigPath = server.getSavePath(WorldSavePath.ROOT).resolve("creaturechat.json");
this.defaultConfigPath = Paths.get(".", "creaturechat.json"); // Assumes the default location is the server root or a similar logical default
}
public Config loadConfig() {
Config config = loadConfigFromFile(serverConfigPath);
if (config == null) {
config = loadConfigFromFile(defaultConfigPath);
}
return config != null ? config : new Config(); // Return new config if both are null
}
public void saveConfig(Config config, boolean useServerConfig) {
Path path = useServerConfig ? serverConfigPath : defaultConfigPath;
try (Writer writer = Files.newBufferedWriter(path)) {
gson.toJson(config, writer);
} catch (IOException e) {
e.printStackTrace();
}
}
private Config loadConfigFromFile(Path filePath) {
try (Reader reader = Files.newBufferedReader(filePath)) {
return gson.fromJson(reader, Config.class);
} catch (IOException e) {
return null; // File does not exist or other IO errors
}
}
public static class Config {
private String apiKey;
private String url;
private String model;
// getters and setters
public String getApiKey() { return apiKey; }
public void setApiKey(String apiKey) { this.apiKey = apiKey; }
public String getUrl() { return url; }
public void setUrl(String url) { this.url = url; }
public String getModel() { return model; }
public void setModel(String model) { this.model = model; }
}
}
...@@ -17,53 +17,62 @@ public class CreatureChatCommands { ...@@ -17,53 +17,62 @@ public class CreatureChatCommands {
public static void register() { public static void register() {
ServerLifecycleEvents.SERVER_STARTING.register(server -> { ServerLifecycleEvents.SERVER_STARTING.register(server -> {
CommandDispatcher<ServerCommandSource> dispatcher = server.getCommandManager().getDispatcher(); CommandDispatcher<ServerCommandSource> dispatcher = server.getCommandManager().getDispatcher();
registerBaseCommand(dispatcher); registerCommands(dispatcher);
}); });
} }
private static void registerBaseCommand(CommandDispatcher<ServerCommandSource> dispatcher) { public static void registerCommands(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(CommandManager.literal("creaturechat") dispatcher.register(CommandManager.literal("creaturechat")
.then(registerSetKeyCommand()) .then(registerSetCommand("key", "API key"))
.then(registerSetUrlCommand()) .then(registerSetCommand("url", "URL"))
.then(registerSetModelCommand()) .then(registerSetCommand("model", "model"))
.then(registerHelpCommand())); .then(registerHelpCommand()));
} }
private static LiteralArgumentBuilder<ServerCommandSource> registerSetKeyCommand() { private static LiteralArgumentBuilder<ServerCommandSource> registerSetCommand(String settingName, String settingDescription) {
return CommandManager.literal("key").then(CommandManager.literal("set").then(CommandManager.argument("key", StringArgumentType.word()) return CommandManager.literal(settingName)
.requires(source -> source.hasPermissionLevel(4)) .then(CommandManager.literal("set")
.executes(context -> { .then(CommandManager.argument("value", StringArgumentType.string())
String key = StringArgumentType.getString(context, "key"); .then(CommandManager.literal("--config")
context.getSource().sendFeedback(() -> Text.literal("API key set to: " + key), false); .then(CommandManager.literal("default")
return 1; .executes(context -> setConfig(context.getSource(), settingName, StringArgumentType.getString(context, "value"), false, settingDescription)))
}))); .then(CommandManager.literal("server")
} .executes(context -> setConfig(context.getSource(), settingName, StringArgumentType.getString(context, "value"), true, settingDescription)))
)
private static LiteralArgumentBuilder<ServerCommandSource> registerSetUrlCommand() { .executes(context -> setConfig(context.getSource(), settingName, StringArgumentType.getString(context, "value"), false, settingDescription)) // Default to server if not specified
return CommandManager.literal("url").then(CommandManager.literal("set").then(CommandManager.argument("url", StringArgumentType.string()) ));
.requires(source -> source.hasPermissionLevel(4))
.executes(context -> {
String url = StringArgumentType.getString(context, "url");
context.getSource().sendFeedback(() -> Text.literal("URL set to: " + url), false);
return 1;
})));
} }
private static LiteralArgumentBuilder<ServerCommandSource> registerSetModelCommand() { private static int setConfig(ServerCommandSource source, String settingName, String value, boolean useServerConfig, String settingDescription) {
return CommandManager.literal("model").then(CommandManager.literal("set").then(CommandManager.argument("model", StringArgumentType.word()) ConfigurationHandler configHandler = new ConfigurationHandler(source.getServer());
.requires(source -> source.hasPermissionLevel(4)) ConfigurationHandler.Config config = configHandler.loadConfig();
.executes(context -> { switch (settingName) {
String model = StringArgumentType.getString(context, "model"); case "key":
context.getSource().sendFeedback(() -> Text.literal("Model set to: " + model), false); config.setApiKey(value);
return 1; break;
}))); case "url":
config.setUrl(value);
break;
case "model":
config.setModel(value);
break;
}
configHandler.saveConfig(config, useServerConfig);
source.sendFeedback(() -> Text.literal(settingDescription + " set to: " + value + " in " + (useServerConfig ? "server" : "default") + " configuration."), false);
return 1;
} }
private static LiteralArgumentBuilder<ServerCommandSource> registerHelpCommand() { private static LiteralArgumentBuilder<ServerCommandSource> registerHelpCommand() {
return CommandManager.literal("help") return CommandManager.literal("help")
.requires(source -> source.hasPermissionLevel(4)) .requires(source -> source.hasPermissionLevel(4)) // Restricts this command to high-level permissions
.executes(context -> { .executes(context -> {
context.getSource().sendFeedback(() -> Text.literal("Usage:\n/creaturechat key set <key>\n/creaturechat url set <url>\n/creaturechat model set <model>"), false); String helpMessage = "Usage of CreatureChat Commands:\n"
+ "/creaturechat key set <key> - Sets the API key.\n"
+ "/creaturechat url set <url> - Sets the URL.\n"
+ "/creaturechat model set <model> - Sets the model.\n"
+ "\n"
+ "Optional: Append [--config default | server] to any command to specify configuration scope. If --config is not specified, 'default' is assumed'.";
context.getSource().sendFeedback(() -> Text.literal(helpMessage), false);
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