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

View file

@ -3,73 +3,46 @@ package cc.toph.simplycompat.config;
import cc.toph.simplycompat.SimplyCompat;
public class Config {
private SimpleConfig config;
private final ConfigProvider provider;
// public static final String CONFIG_NAME = SimplyCompat.MOD_ID + SimpleConfig.FILE_EXTENSION;
public static SimpleConfig CONFIG;
private static final ConfigProvider PROVIDER = new ConfigProvider();
// 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 Config(ConfigProvider provider) {
this.provider = provider;
config = SimpleConfig.of(provider.getNamespace()).provider(provider).request();
}
public static void registerConfigs() {
// 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();
public void register() {
config.update();
}
// Overloaded registerConfig for float
private static float registerConfig(String key, float defaultValue, String comment) {
PROVIDER.addKeyValuePair(key, defaultValue, comment);
// Overloaded registerProp for float
public float registerProp(String key, float defaultValue, String comment) {
provider.addKeyValuePair(key, defaultValue, comment);
// Cast needed because getOrDefault for numbers returns double
return (float) CONFIG.getOrDefault(key, defaultValue);
return (float) config.getOrDefault(key, defaultValue);
}
// Overloaded registerConfig for int
private static int registerConfig(String key, int defaultValue, String comment) {
PROVIDER.addKeyValuePair(key, defaultValue, comment);
return CONFIG.getOrDefault(key, defaultValue);
// Overloaded registerProp for int
public int registerProp(String key, int defaultValue, String comment) {
provider.addKeyValuePair(key, defaultValue, comment);
return config.getOrDefault(key, defaultValue);
}
// Overloaded registerConfig for double
private static double registerConfig(String key, double defaultValue, String comment) {
PROVIDER.addKeyValuePair(key, defaultValue, comment);
return CONFIG.getOrDefault(key, defaultValue);
// Overloaded registerProp for double
public double registerProp(String key, double defaultValue, String comment) {
provider.addKeyValuePair(key, defaultValue, comment);
return config.getOrDefault(key, defaultValue);
}
// Overloaded registerConfig for String
private static String registerConfig(String key, String defaultValue, String comment) {
PROVIDER.addKeyValuePair(key, defaultValue, comment);
return CONFIG.getOrDefault(key, defaultValue);
// Overloaded registerProp for String
public String registerProp(String key, String defaultValue, String comment) {
provider.addKeyValuePair(key, defaultValue, comment);
return config.getOrDefault(key, defaultValue);
}
// Overloaded registerConfig for boolean
private static boolean registerConfig(String key, boolean defaultValue, String comment) {
PROVIDER.addKeyValuePair(key, defaultValue, comment);
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"
);
// Overloaded registerProp for boolean
public boolean registerProp(String key, boolean defaultValue, String comment) {
provider.addKeyValuePair(key, defaultValue, comment);
return config.getOrDefault(key, defaultValue);
}
}

View file

@ -5,8 +5,7 @@ import cc.toph.simplycompat.SimplyCompatExpectPlatform;
import java.util.HashMap;
public class ConfigProvider implements SimpleConfig.DefaultConfig {
// Key: config key (e.g. "copper_damageModifier")
// Value: Pair(value, comment)
private final String namespace;
private final HashMap<String, String> configMap = new HashMap<>();
private String contents = String.format("""
## Simply Compat Config
@ -14,6 +13,11 @@ public class ConfigProvider implements SimpleConfig.DefaultConfig {
""", SimplyCompatExpectPlatform.getVersion());
public ConfigProvider(String namespace) {
this.namespace = namespace;
}
public void addKeyValuePair(String key, Object value, String comment) {
String stringValue = String.valueOf(value); // safely handle `null` too
configMap.put(key, stringValue);
@ -21,12 +25,6 @@ public class ConfigProvider implements SimpleConfig.DefaultConfig {
# %s
%s = %s
""", comment, key, value);
}
@Override
public HashMap<String, String> getMap() {
return configMap;
}
@Override
@ -34,5 +32,13 @@ public class ConfigProvider implements SimpleConfig.DefaultConfig {
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.
*/
import cc.toph.simplycompat.SimplyCompat;
import cc.toph.simplycompat.SimplyCompatExpectPlatform;
import java.io.File;
@ -47,12 +48,12 @@ public class SimpleConfig {
String identifier = "Config '" + request.filename + "'";
if (!request.file.exists()) {
LOGGER.info(identifier + " is missing, generating default one...");
LOGGER.info("{} is missing, generating default one...", identifier);
try {
createConfig();
} catch (IOException e) {
LOGGER.error(identifier + " failed to generate!");
LOGGER.error("{} failed to generate!", identifier);
LOGGER.trace(e);
broken = true;
}
@ -62,7 +63,7 @@ public class SimpleConfig {
try {
loadConfig();
} catch (Exception e) {
LOGGER.error(identifier + " failed to load!");
LOGGER.error("{} failed to load!", identifier);
LOGGER.trace(e);
broken = true;
}
@ -78,8 +79,10 @@ public class SimpleConfig {
* @return new config request object
*/
public static ConfigRequest of(String filename) {
// Small edit for use in common, super cool! :D
Path path = SimplyCompatExpectPlatform.getConfigDirectory().resolve(filename + FILE_EXTENSION);
// File
// 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);
}
@ -220,12 +223,10 @@ public class SimpleConfig {
}
/**
* deletes the config file from the filesystem
*
* @return true if the operation was successful
* Deletes the config file from the filesystem
*/
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();
}

View file

@ -1,6 +1,6 @@
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 net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.item.Item;
@ -12,8 +12,8 @@ import org.jetbrains.annotations.NotNull;
import java.util.function.Supplier;
public enum SimplyCompatToolMaterials implements Tier {
COPPER(1, 125, 4.5F, 1.0F, WeaponAttributes.COPPER, 8, Items.COPPER_INGOT),
STEEL(2, 600, 6.5F, 2.5F, WeaponAttributes.STEEL, 12, Items.DIAMOND);
COPPER(1, 125, 4.5F, 1.0F, WEAPON_ATTRIBUTES.COPPER, 8, Items.COPPER_INGOT),
STEEL(2, 600, 6.5F, 2.5F, WEAPON_ATTRIBUTES.STEEL, 12, Items.DIAMOND);
private final int level;
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();
}
}