config: Add Config and ConfigField classes

This commit is contained in:
Oliver
2025-07-19 18:40:56 +02:00
parent 1b436a1154
commit 298cc16fac
4 changed files with 133 additions and 6 deletions

View File

@@ -14,7 +14,9 @@ repositories {
} }
dependencies { dependencies {
implementation("org.jetbrains:annotations:26.0.2") compileOnly("io.papermc.paper:paper-api:1.21.8-R0.1-SNAPSHOT")
compileOnly("de.oliver.FancyAnalytics:logger:0.0.6")
compileOnly("org.jetbrains:annotations:26.0.2")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.12.2") testImplementation("org.junit.jupiter:junit-jupiter-api:5.12.2")
testImplementation("org.junit.jupiter:junit-jupiter-engine:5.12.2") testImplementation("org.junit.jupiter:junit-jupiter-engine:5.12.2")
@@ -24,7 +26,7 @@ dependencies {
tasks { tasks {
compileJava { compileJava {
options.encoding = Charsets.UTF_8.name() options.encoding = Charsets.UTF_8.name()
options.release.set(17) options.release.set(21)
} }
java { java {
@@ -43,3 +45,7 @@ tasks {
useJUnitPlatform() useJUnitPlatform()
} }
} }
java {
toolchain.languageVersion.set(JavaLanguageVersion.of(21))
}

View File

@@ -0,0 +1,109 @@
package com.fancyinnovations.config;
import de.oliver.fancyanalytics.logger.ExtendedFancyLogger;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class Config {
private final ExtendedFancyLogger logger;
private final File configFile;
private final Map<String, ConfigField<?>> fields;
private final Map<String, Object> values;
public Config(ExtendedFancyLogger logger, String configFilePath) {
this.logger = logger;
this.configFile = new File(configFilePath);
this.fields = new ConcurrentHashMap<>();
this.values = new ConcurrentHashMap<>();
}
public void addField(ConfigField<?> field) {
fields.put(field.path(), field);
}
public <T> T get(String path) {
ConfigField<?> field = fields.get(path);
if (field != null) {
Object value = values.computeIfAbsent(path, k -> field.defaultValue());
return (T) field.type().cast(value);
}
throw new IllegalArgumentException("No field found for path: " + path);
}
public void reload() {
if (!configFile.exists()) {
if (!configFile.mkdirs()) {
logger.error("Failed to create directories for config file: " + configFile.getAbsolutePath());
return;
}
try {
if (!configFile.createNewFile()) {
logger.error("Failed to create config file: " + configFile.getAbsolutePath());
return;
}
} catch (IOException e) {
logger.error("Error creating config file: " + configFile.getAbsolutePath());
logger.error(e);
return;
}
YamlConfiguration yaml = YamlConfiguration.loadConfiguration(configFile);
for (ConfigField<?> field : fields.values()) {
setDefault(yaml, field);
}
saveYaml(yaml);
return;
}
YamlConfiguration yaml = YamlConfiguration.loadConfiguration(configFile);
boolean dirty = false;
for (Map.Entry<String, ConfigField<?>> entry : fields.entrySet()) {
String path = entry.getKey();
ConfigField<?> field = entry.getValue();
if (yaml.isSet(path)) {
Object value = yaml.get(path);
if (field.type().isInstance(value)) {
values.put(path, value);
} else {
logger.warn("Value for path '" + path + "' is not of type '" + field.type().getSimpleName());
setDefault(yaml, field);
dirty = true;
}
} else {
logger.debug("Path '" + path + "' not found in config");
setDefault(yaml, field);
dirty = true;
}
}
if (dirty) {
saveYaml(yaml);
}
}
private void setDefault(YamlConfiguration yaml, ConfigField<?> field) {
logger.debug("Setting default value for path '" + field.path() + "': " + field.defaultValue());
yaml.set(field.path(), field.defaultValue());
yaml.setInlineComments(field.path(), List.of(field.description()));
}
private void saveYaml(YamlConfiguration yaml) {
try {
yaml.save(configFile);
} catch (IOException e) {
logger.error("Error saving config file: " + configFile.getAbsolutePath());
logger.error(e);
}
}
}

View File

@@ -0,0 +1,9 @@
package com.fancyinnovations.config;
public record ConfigField<T>(
String path,
String description,
T defaultValue,
Class<T> type
) {
}

View File

@@ -31,6 +31,8 @@ val supportedVersions =
"1.21.4", "1.21.4",
"1.21.5", "1.21.5",
"1.21.6", "1.21.6",
"1.21.7",
"1.21.8",
) )
allprojects { allprojects {
@@ -69,6 +71,7 @@ dependencies {
implementation(project(":libraries:common")) implementation(project(":libraries:common"))
implementation(project(":libraries:plugin-tests")) implementation(project(":libraries:plugin-tests"))
implementation(project(":libraries:jdb")) implementation(project(":libraries:jdb"))
implementation(project(":libraries:config"))
implementation(project(":libraries:packets")) implementation(project(":libraries:packets"))
implementation(project(":libraries:packets:packets-api")) implementation(project(":libraries:packets:packets-api"))
implementation("de.oliver.FancyAnalytics:java-sdk:0.0.3") implementation("de.oliver.FancyAnalytics:java-sdk:0.0.3")
@@ -119,12 +122,12 @@ paper {
tasks { tasks {
runServer { runServer {
minecraftVersion("1.21.7") minecraftVersion("1.21.8")
downloadPlugins { downloadPlugins {
modrinth("fancynpcs", "2.5.0") modrinth("fancynpcs", "2.7.0")
hangar("ViaVersion", "5.3.2") // hangar("ViaVersion", "5.3.2")
hangar("ViaBackwards", "5.3.2") // hangar("ViaBackwards", "5.3.2")
// modrinth("multiverse-core", "4.3.11") // modrinth("multiverse-core", "4.3.11")
hangar("PlaceholderAPI", "2.11.6") hangar("PlaceholderAPI", "2.11.6")
// modrinth("DecentHolograms", "2.8.12") // modrinth("DecentHolograms", "2.8.12")