Made the config abstract (so it can be used by other mods using our api)

This commit is contained in:
coolGi
2022-09-09 23:12:20 +09:30
parent 1b0c97ffca
commit 050d88dd13
4 changed files with 49 additions and 31 deletions
@@ -21,7 +21,13 @@ import java.util.Map;
// Init the config after singletons have been blinded
public class ConfigBase
{
public static ConfigBase INSTANCE;
public ConfigFileHandling configFileINSTANCE;
public static final Logger LOGGER = LogManager.getLogger(ConfigBase.class.getSimpleName());
public final String modID;
public final String modName;
public final int configVersion;
/*
What the config works with
@@ -50,20 +56,24 @@ public class ConfigBase
}
/** Disables the minimum and maximum of a variable */
public static boolean disableMinMax = false; // Very fun to use
public static final List<AbstractConfigType<?, ?>> entries = new ArrayList<>();
public boolean disableMinMax = false; // Very fun to use
public final List<AbstractConfigType<?, ?>> entries = new ArrayList<>();
public static final int configVersion = 1;
public static void init(Class<?> config) {
public ConfigBase(String modID, String modName, Class<?> config, int configVersion) {
this.modID = modID;
this.modName = modName;
this.configVersion = configVersion;
addAcceptableInputs(); // Add all the acceptable stuff to the acceptableInputs list
initNestedClass(config, ""); // Init root category
// File handling (load from file)
ConfigFileHandling.loadFromFile();
this.configFileINSTANCE = new ConfigFileHandling(this);
this.configFileINSTANCE.loadFromFile();
}
private static void initNestedClass(Class<?> config, String category) {
private void initNestedClass(Class<?> config, String category) {
// Put all the entries in entries
for (Field field : config.getFields()) {
@@ -77,6 +87,7 @@ public class ConfigBase
AbstractConfigType<?, ?> entry = entries.get(entries.size() - 1);
entry.category = category;
entry.name = field.getName();
entry.configBase = this;
if (ConfigEntry.class.isAssignableFrom(field.getType())) { // If item is type ConfigEntry
if (!isAcceptableType(((ConfigEntry<?>) entry).get().getClass())) {
@@ -22,17 +22,22 @@ import java.nio.file.Path;
* Handles reading and writing config files.
*
* @author coolGi
* @version 2022-5-26
* @version 2022-9-9
*/
public class ConfigFileHandling
{
private static final Logger LOGGER = LogManager.getLogger(ConfigBase.class.getSimpleName());
public static final Path ConfigPath = SingletonInjector.INSTANCE.get(IMinecraftSharedWrapper.class)
.getInstallationDirectory().toPath().resolve("config").resolve(ModInfo.NAME+".toml");
public class ConfigFileHandling {
private static final Logger LOGGER = ConfigBase.LOGGER;
public final ConfigBase configBase;
public final Path ConfigPath;
public ConfigFileHandling(ConfigBase configBase) {
this.configBase = configBase;
ConfigPath = SingletonInjector.INSTANCE.get(IMinecraftSharedWrapper.class)
.getInstallationDirectory().toPath().resolve("config").resolve(this.configBase.modName+".toml");
}
/** Saves the config to the file */
public static void saveToFile() {
public void saveToFile() {
CommentedFileConfig config = CommentedFileConfig.builder(ConfigPath.toFile()).build();
if (!Files.exists(ConfigPath)) // Try to check if the config exists
try {
@@ -43,7 +48,7 @@ public class ConfigFileHandling
loadConfig(config);
for (AbstractConfigType<?, ?> entry : ConfigBase.entries) {
for (AbstractConfigType<?, ?> entry : this.configBase.entries) {
if (ConfigEntry.class.isAssignableFrom(entry.getClass())) {
createComment((ConfigEntry<?>) entry, config);
saveEntry((ConfigEntry<?>) entry, config);
@@ -54,7 +59,7 @@ public class ConfigFileHandling
config.close();
}
/** Loads the config from the file */
public static void loadFromFile() {
public void loadFromFile() {
CommentedFileConfig config = CommentedFileConfig.builder(ConfigPath.toFile()).build();
// Attempt to load the file and if it fails then save config to file
try {
@@ -71,7 +76,7 @@ public class ConfigFileHandling
}
// Load all the entries
for (AbstractConfigType<?, ?> entry : ConfigBase.entries) {
for (AbstractConfigType<?, ?> entry : this.configBase.entries) {
if (ConfigEntry.class.isAssignableFrom(entry.getClass())) {
createComment((ConfigEntry<?>) entry, config);
loadEntry((ConfigEntry<?>) entry, config);
@@ -86,7 +91,7 @@ public class ConfigFileHandling
// Save an entry when only given the entry
public static void saveEntry(ConfigEntry<?> entry) {
public void saveEntry(ConfigEntry<?> entry) {
CommentedFileConfig config = CommentedFileConfig.builder(ConfigPath.toFile()).build();
loadConfig(config);
saveEntry(entry, config);
@@ -95,7 +100,7 @@ public class ConfigFileHandling
}
// Save an entry
@SuppressWarnings("unchecked")
public static void saveEntry(ConfigEntry<?> entry, CommentedFileConfig workConfig) {
public void saveEntry(ConfigEntry<?> entry, CommentedFileConfig workConfig) {
if (!entry.getAppearance().showInFile) return;
if (ConfigTypeConverters.convertObjects.containsKey(entry.getType())) {
@@ -106,7 +111,7 @@ public class ConfigFileHandling
}
// Loads an entry when only given the entry
public static void loadEntry(ConfigEntry<?> entry) {
public void loadEntry(ConfigEntry<?> entry) {
CommentedFileConfig config = CommentedFileConfig.builder(ConfigPath.toFile()).autosave().build();
loadConfig(config);
loadEntry(entry, config);
@@ -115,7 +120,7 @@ public class ConfigFileHandling
}
// Loads an entry
@SuppressWarnings("unchecked") // Suppress due to its always safe
public static <T> void loadEntry(ConfigEntry<T> entry, CommentedFileConfig workConfig) {
public <T> void loadEntry(ConfigEntry<T> entry, CommentedFileConfig workConfig) {
if (!entry.getAppearance().showInFile) return;
if (workConfig.contains(entry.getNameWCategory())) {
@@ -147,14 +152,14 @@ public class ConfigFileHandling
}
// Creates the comment for an entry when only given the entry
public static void createComment(ConfigEntry<?> entry) {
public void createComment(ConfigEntry<?> entry) {
CommentedFileConfig config = CommentedFileConfig.builder(ConfigPath.toFile()).autosave().build();
loadConfig(config);
createComment(entry, config);
config.close();
}
// Creates a comment for an entry
public static void createComment(ConfigEntry<?> entry, CommentedFileConfig workConfig) {
public void createComment(ConfigEntry<?> entry, CommentedFileConfig workConfig) {
if (!entry.getAppearance().showInFile)
return;
if (entry.getComment() != null) {
@@ -166,7 +171,7 @@ public class ConfigFileHandling
/** Does config.load(); but with error checking */
public static void loadConfig(CommentedFileConfig config) {
public void loadConfig(CommentedFileConfig config) {
try {
config.load();
} catch (Exception e) {
@@ -188,8 +193,10 @@ public class ConfigFileHandling
// ========== API (server) STUFF ========== //
@SuppressWarnings("unchecked")
/** ALWAYS CLEAR WHEN NOT ON SERVER!!!! */
// We are not using this stuff, so comment it out for now (if we ever do need it then we can uncomment it)
/*
@SuppressWarnings("unchecked")
public static void clearApiValues() {
for (AbstractConfigType<?, ?> entry : ConfigBase.entries) {
if (ConfigEntry.class.isAssignableFrom(entry.getClass()) && ((ConfigEntry) entry).allowApiOverride) {
@@ -235,4 +242,5 @@ public class ConfigFileHandling
}
}
}
*/
}
@@ -1,6 +1,6 @@
package com.seibel.lod.core.config.types;
import com.seibel.lod.core.config.types.ConfigEntryAppearance;
import com.seibel.lod.core.config.ConfigBase;
/**
* The class where all config options should extend
@@ -12,6 +12,7 @@ public abstract class AbstractConfigType<T, S> { // The S is the class that is e
public String name; // This should only be set once in the init
protected T value;
protected final Listener listener;
public ConfigBase configBase;
public Object guiValue; // This is a storage variable something like the gui can use
@@ -1,7 +1,5 @@
package com.seibel.lod.core.config.types;
import com.seibel.lod.core.config.ConfigBase;
import com.seibel.lod.core.config.file.ConfigFileHandling;
/**
* Use for making the config variables
@@ -120,7 +118,7 @@ public class ConfigEntry<T> extends AbstractConfigType<T, ConfigEntry<T>>
}
/** Checks if a value is valid */
public byte isValid(T value) {
if (ConfigBase.disableMinMax)
if (this.configBase.disableMinMax)
return 0;
if (Number.class.isAssignableFrom(value.getClass())) { // Only check min max if it is a number
if (this.max != null && Double.valueOf(value.toString()) > Double.valueOf(max.toString()))
@@ -135,11 +133,11 @@ public class ConfigEntry<T> extends AbstractConfigType<T, ConfigEntry<T>>
/** This should normally not be called since set() automatically calls this */
public void save() {
ConfigFileHandling.saveEntry(this);
configBase.configFileINSTANCE.saveEntry(this);
}
/** This should normally not be called except for special circumstances */
public void load() {
ConfigFileHandling.loadEntry(this);
configBase.configFileINSTANCE.loadEntry(this);
}
/** Is the value of this equal to another */