Started more work on the new config system (file handling)
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
package com.seibel.lod.core.config;
|
||||
|
||||
import com.seibel.lod.core.ModInfo;
|
||||
import com.seibel.lod.core.config.file.ConfigFileHandling;
|
||||
import com.seibel.lod.core.util.SingletonHandler;
|
||||
import com.seibel.lod.core.wrapperInterfaces.minecraft.IMinecraftWrapper;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -8,10 +14,9 @@ import java.util.List;
|
||||
* Indexes and sets everything up for the file handling and gui
|
||||
*
|
||||
* @author coolGi2007
|
||||
* @author Ran
|
||||
*/
|
||||
public class ConfigBase {
|
||||
public static final List<ConfigEntry<?>> entries = new ArrayList<ConfigEntry<?>>();
|
||||
public static final List<ConfigEntry> entries = new ArrayList<>();
|
||||
public static final List<String> categories = new ArrayList<>();
|
||||
|
||||
public static void init(Class<?> config) {
|
||||
@@ -19,7 +24,7 @@ public class ConfigBase {
|
||||
initNestedClass(config, ""); // Init root category
|
||||
|
||||
// File handling (load from file)
|
||||
|
||||
ConfigFileHandling.loadFromFile();
|
||||
|
||||
// Temporary to see stuff
|
||||
System.out.println(entries);
|
||||
@@ -32,13 +37,9 @@ public class ConfigBase {
|
||||
for (Field field : config.getFields())
|
||||
{
|
||||
if (ConfigEntry.class.isAssignableFrom(field.getType())) { // If item is type ConfigEntry
|
||||
try {
|
||||
entries.add((ConfigEntry<?>) field.get(field.getType())); // Add to entries
|
||||
} catch (IllegalAccessException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
entries.get(entries.size() - 1).category = (category.isEmpty() ? "" : category + ".");
|
||||
entries.get(entries.size() - 1).category = field.getName();
|
||||
// entries.add(ConfigEntry.class.cast(field));
|
||||
// entries.get(entries.size() - 1).category = (category.isEmpty() ? "" : category + ".");
|
||||
// entries.get(entries.size() - 1).category = field.getName();
|
||||
}
|
||||
|
||||
if (field.isAnnotationPresent(ConfigAnnotations.Category.class)) { // If it's a category then init the stuff inside it and put it in the category list
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
package com.seibel.lod.core.config;
|
||||
|
||||
import com.seibel.lod.core.config.file.ConfigFileHandling;
|
||||
|
||||
/**
|
||||
* Use for making the config variables
|
||||
*
|
||||
* @author coolGi2007
|
||||
* @version 02-06-2022
|
||||
*/
|
||||
public class ConfigEntry<T> {
|
||||
public String category = ""; // This should only be set once in the init
|
||||
@@ -105,15 +106,17 @@ public class ConfigEntry<T> {
|
||||
|
||||
/** This should normally not be called since set() automatically calls this */
|
||||
public void save() {
|
||||
// Call to something to save option
|
||||
ConfigFileHandling.saveEntry(this);
|
||||
}
|
||||
/** This should normally not be called except for special circumstances */
|
||||
public void load() {
|
||||
// Call something to load option
|
||||
ConfigFileHandling.loadEntry(this);
|
||||
}
|
||||
|
||||
/** Is the value of this equal to another */
|
||||
public boolean equals(ConfigEntry<?> obj) {
|
||||
// Can all of this just be "return this.value.equals(obj.value)"?
|
||||
|
||||
if (this.value.getClass() != obj.value.getClass())
|
||||
return false;
|
||||
if (Number.class.isAssignableFrom(this.value.getClass())) {
|
||||
|
||||
@@ -1,10 +1,15 @@
|
||||
package com.seibel.lod.core.config.file;
|
||||
|
||||
import com.electronwill.nightconfig.core.file.CommentedFileConfig;
|
||||
import com.seibel.lod.core.ModInfo;
|
||||
import com.seibel.lod.core.config.ConfigEntry;
|
||||
import com.seibel.lod.core.util.SingletonHandler;
|
||||
import com.seibel.lod.core.wrapperInterfaces.minecraft.IMinecraftWrapper;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
/**
|
||||
* Handles all stuff to do with the files
|
||||
@@ -12,13 +17,64 @@ import java.io.File;
|
||||
* @author coolGi2007
|
||||
*/
|
||||
public class ConfigFileHandling {
|
||||
public static final File ConfigPath = SingletonHandler.get(IMinecraftWrapper.class).getGameDirectory().toPath().resolve("config").resolve(ModInfo.NAME+".toml").toFile();
|
||||
public static final Path ConfigPath = SingletonHandler.get(IMinecraftWrapper.class).getGameDirectory().toPath().resolve("config").resolve(ModInfo.NAME+".toml");
|
||||
public static final CommentedFileConfig config = CommentedFileConfig.builder(ConfigPath.toFile()).autosave().build();
|
||||
|
||||
public static void saveToFile() {
|
||||
if (!Files.exists(ConfigPath))
|
||||
try {
|
||||
Files.createFile(ConfigPath);
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
loadConfig(config);
|
||||
|
||||
config.close();
|
||||
}
|
||||
|
||||
public static void loadFromFile() {
|
||||
try {
|
||||
config.load();
|
||||
} catch (Exception e) {
|
||||
saveToFile();
|
||||
return;
|
||||
}
|
||||
|
||||
config.close();
|
||||
}
|
||||
|
||||
public static void saveEntry(ConfigEntry<?> entry) {
|
||||
loadConfig(config);
|
||||
saveEntry(entry, config);
|
||||
config.close();
|
||||
}
|
||||
public static void saveEntry(ConfigEntry<?> entry, CommentedFileConfig workConfig) {
|
||||
|
||||
}
|
||||
public static void loadEntry(ConfigEntry<?> entry) {
|
||||
loadConfig(config);
|
||||
loadEntry(entry, config);
|
||||
config.close();
|
||||
|
||||
}
|
||||
public static void loadEntry(ConfigEntry<?> entry, CommentedFileConfig workConfig) {
|
||||
|
||||
}
|
||||
|
||||
public static void loadConfig(CommentedFileConfig config) {
|
||||
try {
|
||||
config.load();
|
||||
} catch (Exception e) {
|
||||
System.out.println("Loading file failed because of this expectation:\n"+e);
|
||||
try { // Now try remaking the file
|
||||
Files.deleteIfExists(ConfigPath);
|
||||
Files.createFile(ConfigPath);
|
||||
config.load();
|
||||
} catch (IOException ex) {
|
||||
System.out.println("Creating file failed");
|
||||
ex.printStackTrace();
|
||||
SingletonHandler.get(IMinecraftWrapper.class).crashMinecraft("Loading file and resetting config file failed at path ["+ConfigPath+"]. Please check the file is ok and you have the permissions", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user