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 {
public static void register() {
ServerLifecycleEvents.SERVER_STARTING.register(server -> {
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")
.then(registerSetKeyCommand())
.then(registerSetUrlCommand())
.then(registerSetModelCommand())
.then(registerSetCommand("key", "API key"))
.then(registerSetCommand("url", "URL"))
.then(registerSetCommand("model", "model"))
.then(registerHelpCommand()));
}
private static LiteralArgumentBuilder<ServerCommandSource> registerSetKeyCommand() {
return CommandManager.literal("key").then(CommandManager.literal("set").then(CommandManager.argument("key", StringArgumentType.word())
.requires(source -> source.hasPermissionLevel(4))
.executes(context -> {
String key = StringArgumentType.getString(context, "key");
context.getSource().sendFeedback(() -> Text.literal("API key set to: " + key), false);
return 1;
})));
}
private static LiteralArgumentBuilder<ServerCommandSource> registerSetUrlCommand() {
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> registerSetCommand(String settingName, String settingDescription) {
return CommandManager.literal(settingName)
.then(CommandManager.literal("set")
.then(CommandManager.argument("value", StringArgumentType.string())
.then(CommandManager.literal("--config")
.then(CommandManager.literal("default")
.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)))
)
.executes(context -> setConfig(context.getSource(), settingName, StringArgumentType.getString(context, "value"), false, settingDescription)) // Default to server if not specified
));
}
private static LiteralArgumentBuilder<ServerCommandSource> registerSetModelCommand() {
return CommandManager.literal("model").then(CommandManager.literal("set").then(CommandManager.argument("model", StringArgumentType.word())
.requires(source -> source.hasPermissionLevel(4))
.executes(context -> {
String model = StringArgumentType.getString(context, "model");
context.getSource().sendFeedback(() -> Text.literal("Model set to: " + model), false);
return 1;
})));
private static int setConfig(ServerCommandSource source, String settingName, String value, boolean useServerConfig, String settingDescription) {
ConfigurationHandler configHandler = new ConfigurationHandler(source.getServer());
ConfigurationHandler.Config config = configHandler.loadConfig();
switch (settingName) {
case "key":
config.setApiKey(value);
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() {
return CommandManager.literal("help")
.requires(source -> source.hasPermissionLevel(4))
.requires(source -> source.hasPermissionLevel(4)) // Restricts this command to high-level permissions
.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;
});
}
......
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