ConfigurationHandler.java 3.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
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.
18
 * This allows for global/default settings, or optional server-specific settings.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
 */

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 {
57 58 59 60 61 62
        private String apiKey = "";
        private String url = "https://api.openai.com/v1/chat/completions";
        private String model = "gpt-3.5-turbo";
        private int maxContextTokens = 16385;
        private int maxOutputTokens = 200;
        private double percentOfContext = 0.75;
63

64
        // Getters and setters for existing fields
65 66
        public String getApiKey() { return apiKey; }
        public void setApiKey(String apiKey) { this.apiKey = apiKey; }
67

68 69
        public String getUrl() { return url; }
        public void setUrl(String url) { this.url = url; }
70

71 72
        public String getModel() { return model; }
        public void setModel(String model) { this.model = model; }
73 74 75 76 77 78 79 80 81 82 83

        // Getters and setters for new fields
        public int getMaxContextTokens() { return maxContextTokens; }
        public void setMaxContextTokens(int maxContextTokens) { this.maxContextTokens = maxContextTokens; }

        public int getMaxOutputTokens() { return maxOutputTokens; }
        public void setMaxOutputTokens(int maxOutputTokens) { this.maxOutputTokens = maxOutputTokens; }

        public double getPercentOfContext() { return percentOfContext; }
        public void setPercentOfContext(double percentOfContext) { this.percentOfContext = percentOfContext; }

84 85
    }
}