Refactor config system and introduce ConfigRegistry.

Moved configuration setup to a dedicated ConfigRegistry for better organization and modularity. Simplified config management and added support for multiple namespaces/configurations.

Also added hardcoded option for folder or file configs
This commit is contained in:
Chris Toph 2025-02-22 23:23:03 -05:00
parent 7549757ce3
commit 9136660de7
7 changed files with 395 additions and 377 deletions

View file

@ -1,6 +1,7 @@
package cc.toph.simplycompat; package cc.toph.simplycompat;
import cc.toph.simplycompat.config.Config; import cc.toph.simplycompat.config.Config;
import cc.toph.simplycompat.registry.ConfigRegistry;
import cc.toph.simplycompat.registry.ItemsRegistry; import cc.toph.simplycompat.registry.ItemsRegistry;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -25,7 +26,7 @@ public final class SimplyCompat {
public static void init() { public static void init() {
// TABS.register(); // TABS.register();
Config.registerConfigs(); ConfigRegistry.registerConfigs();
ItemsRegistry.registerAllSwords(); ItemsRegistry.registerAllSwords();
} }
} }

View file

@ -3,73 +3,46 @@ package cc.toph.simplycompat.config;
import cc.toph.simplycompat.SimplyCompat; import cc.toph.simplycompat.SimplyCompat;
public class Config { public class Config {
private SimpleConfig config;
private final ConfigProvider provider;
// public static final String CONFIG_NAME = SimplyCompat.MOD_ID + SimpleConfig.FILE_EXTENSION; public Config(ConfigProvider provider) {
public static SimpleConfig CONFIG; this.provider = provider;
private static final ConfigProvider PROVIDER = new ConfigProvider(); config = SimpleConfig.of(provider.getNamespace()).provider(provider).request();
// TODO: Move some of this stuff into ConfigRegistry
public static final class WeaponAttributes {
public static float COPPER;
public static float STEEL;
public static float TEST;
} }
public void register() {
public static void registerConfigs() { config.update();
// Load config 'simplycompat.config', if it isn't present create one
// Temp Default Empty Provider to load config
CONFIG = SimpleConfig.of(SimplyCompat.MOD_ID).provider(PROVIDER).request();
initializeConfigs();
CONFIG.update();
} }
// Overloaded registerConfig for float // Overloaded registerProp for float
private static float registerConfig(String key, float defaultValue, String comment) { public float registerProp(String key, float defaultValue, String comment) {
PROVIDER.addKeyValuePair(key, defaultValue, comment); provider.addKeyValuePair(key, defaultValue, comment);
// Cast needed because getOrDefault for numbers returns double // Cast needed because getOrDefault for numbers returns double
return (float) CONFIG.getOrDefault(key, defaultValue); return (float) config.getOrDefault(key, defaultValue);
} }
// Overloaded registerConfig for int // Overloaded registerProp for int
private static int registerConfig(String key, int defaultValue, String comment) { public int registerProp(String key, int defaultValue, String comment) {
PROVIDER.addKeyValuePair(key, defaultValue, comment); provider.addKeyValuePair(key, defaultValue, comment);
return CONFIG.getOrDefault(key, defaultValue); return config.getOrDefault(key, defaultValue);
} }
// Overloaded registerConfig for double // Overloaded registerProp for double
private static double registerConfig(String key, double defaultValue, String comment) { public double registerProp(String key, double defaultValue, String comment) {
PROVIDER.addKeyValuePair(key, defaultValue, comment); provider.addKeyValuePair(key, defaultValue, comment);
return CONFIG.getOrDefault(key, defaultValue); return config.getOrDefault(key, defaultValue);
} }
// Overloaded registerConfig for String // Overloaded registerProp for String
private static String registerConfig(String key, String defaultValue, String comment) { public String registerProp(String key, String defaultValue, String comment) {
PROVIDER.addKeyValuePair(key, defaultValue, comment); provider.addKeyValuePair(key, defaultValue, comment);
return CONFIG.getOrDefault(key, defaultValue); return config.getOrDefault(key, defaultValue);
} }
// Overloaded registerConfig for boolean // Overloaded registerProp for boolean
private static boolean registerConfig(String key, boolean defaultValue, String comment) { public boolean registerProp(String key, boolean defaultValue, String comment) {
PROVIDER.addKeyValuePair(key, defaultValue, comment); provider.addKeyValuePair(key, defaultValue, comment);
return CONFIG.getOrDefault(key, defaultValue); return config.getOrDefault(key, defaultValue);
}
// Initialize config fields in one step
private static void initializeConfigs() {
WeaponAttributes.COPPER = registerConfig(
"copper_damageModifier",
3.0f,
"Copper Damage Modifier"
);
WeaponAttributes.STEEL = registerConfig(
"steel_damageModifier",
5.0f,
"Steel Damage Modifier"
);
} }
} }

View file

@ -5,8 +5,7 @@ import cc.toph.simplycompat.SimplyCompatExpectPlatform;
import java.util.HashMap; import java.util.HashMap;
public class ConfigProvider implements SimpleConfig.DefaultConfig { public class ConfigProvider implements SimpleConfig.DefaultConfig {
// Key: config key (e.g. "copper_damageModifier") private final String namespace;
// Value: Pair(value, comment)
private final HashMap<String, String> configMap = new HashMap<>(); private final HashMap<String, String> configMap = new HashMap<>();
private String contents = String.format(""" private String contents = String.format("""
## Simply Compat Config ## Simply Compat Config
@ -14,6 +13,11 @@ public class ConfigProvider implements SimpleConfig.DefaultConfig {
""", SimplyCompatExpectPlatform.getVersion()); """, SimplyCompatExpectPlatform.getVersion());
public ConfigProvider(String namespace) {
this.namespace = namespace;
}
public void addKeyValuePair(String key, Object value, String comment) { public void addKeyValuePair(String key, Object value, String comment) {
String stringValue = String.valueOf(value); // safely handle `null` too String stringValue = String.valueOf(value); // safely handle `null` too
configMap.put(key, stringValue); configMap.put(key, stringValue);
@ -21,12 +25,6 @@ public class ConfigProvider implements SimpleConfig.DefaultConfig {
# %s # %s
%s = %s %s = %s
""", comment, key, value); """, comment, key, value);
}
@Override
public HashMap<String, String> getMap() {
return configMap;
} }
@Override @Override
@ -34,5 +32,13 @@ public class ConfigProvider implements SimpleConfig.DefaultConfig {
return contents; return contents;
} }
@Override
public HashMap<String, String> getMap() {
return configMap;
}
public String getNamespace() {
return namespace;
}
} }

View file

@ -21,6 +21,7 @@ package cc.toph.simplycompat.config;
* THE SOFTWARE. * THE SOFTWARE.
*/ */
import cc.toph.simplycompat.SimplyCompat;
import cc.toph.simplycompat.SimplyCompatExpectPlatform; import cc.toph.simplycompat.SimplyCompatExpectPlatform;
import java.io.File; import java.io.File;
@ -47,12 +48,12 @@ public class SimpleConfig {
String identifier = "Config '" + request.filename + "'"; String identifier = "Config '" + request.filename + "'";
if (!request.file.exists()) { if (!request.file.exists()) {
LOGGER.info(identifier + " is missing, generating default one..."); LOGGER.info("{} is missing, generating default one...", identifier);
try { try {
createConfig(); createConfig();
} catch (IOException e) { } catch (IOException e) {
LOGGER.error(identifier + " failed to generate!"); LOGGER.error("{} failed to generate!", identifier);
LOGGER.trace(e); LOGGER.trace(e);
broken = true; broken = true;
} }
@ -62,7 +63,7 @@ public class SimpleConfig {
try { try {
loadConfig(); loadConfig();
} catch (Exception e) { } catch (Exception e) {
LOGGER.error(identifier + " failed to load!"); LOGGER.error("{} failed to load!", identifier);
LOGGER.trace(e); LOGGER.trace(e);
broken = true; broken = true;
} }
@ -78,8 +79,10 @@ public class SimpleConfig {
* @return new config request object * @return new config request object
*/ */
public static ConfigRequest of(String filename) { public static ConfigRequest of(String filename) {
// Small edit for use in common, super cool! :D // File
Path path = SimplyCompatExpectPlatform.getConfigDirectory().resolve(filename + FILE_EXTENSION); // Path path = SimplyCompatExpectPlatform.getConfigDirectory().resolve(filename + FILE_EXTENSION);
// Folder
Path path = SimplyCompatExpectPlatform.getConfigDirectory().resolve(String.format("%s/%s%s", SimplyCompat.MOD_ID, filename, FILE_EXTENSION));
return new ConfigRequest(path.toFile(), filename); return new ConfigRequest(path.toFile(), filename);
} }
@ -220,12 +223,10 @@ public class SimpleConfig {
} }
/** /**
* deletes the config file from the filesystem * Deletes the config file from the filesystem
*
* @return true if the operation was successful
*/ */
public boolean delete() { public boolean delete() {
LOGGER.warn("Config '" + request.filename + "' was removed from existence! Restart the game to regenerate it."); LOGGER.warn("Config '{}' will rise from the ashes! Please restart game.", request.filename);
return request.file.delete(); return request.file.delete();
} }

View file

@ -1,6 +1,6 @@
package cc.toph.simplycompat.item; package cc.toph.simplycompat.item;
import cc.toph.simplycompat.config.Config.WeaponAttributes; import cc.toph.simplycompat.registry.ConfigRegistry.WEAPON_ATTRIBUTES;
import com.google.common.base.Suppliers; import com.google.common.base.Suppliers;
import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.item.Item; import net.minecraft.world.item.Item;
@ -12,8 +12,8 @@ import org.jetbrains.annotations.NotNull;
import java.util.function.Supplier; import java.util.function.Supplier;
public enum SimplyCompatToolMaterials implements Tier { public enum SimplyCompatToolMaterials implements Tier {
COPPER(1, 125, 4.5F, 1.0F, WeaponAttributes.COPPER, 8, Items.COPPER_INGOT), COPPER(1, 125, 4.5F, 1.0F, WEAPON_ATTRIBUTES.COPPER, 8, Items.COPPER_INGOT),
STEEL(2, 600, 6.5F, 2.5F, WeaponAttributes.STEEL, 12, Items.DIAMOND); STEEL(2, 600, 6.5F, 2.5F, WEAPON_ATTRIBUTES.STEEL, 12, Items.DIAMOND);
private final int level; private final int level;
private final int uses; private final int uses;

View file

@ -0,0 +1,37 @@
package cc.toph.simplycompat.registry;
import cc.toph.simplycompat.SimplyCompat;
import cc.toph.simplycompat.config.Config;
import cc.toph.simplycompat.config.ConfigProvider;
public class ConfigRegistry {
public static final class WEAPON_ATTRIBUTES {
public static float COPPER;
public static float STEEL;
public static final ConfigProvider PROVIDER = new ConfigProvider("weapon_attributes");
public static void register() {
Config common = new Config(PROVIDER);
COPPER = common.registerProp(
"copper_damageModifier",
3.0f,
"Copper Damage Modifier"
);
STEEL = common.registerProp(
"steel_damageModifier",
5.0f,
"Steel Damage Modifier"
);
common.register();
}
}
public static void registerConfigs() {
WEAPON_ATTRIBUTES.register();
}
}