Fixed some config stuff. Only 1 more thing to go

This commit is contained in:
coolGi2007
2021-12-25 20:52:46 +10:30
parent c721c14e27
commit 87486f6825
7 changed files with 63 additions and 100 deletions
+2 -2
View File
@@ -29,8 +29,8 @@ publishing {
}
dependencies {
implementation("com.moandjiezana.toml:toml4j:${rootProject.toml_version}")
shadowMe("com.moandjiezana.toml:toml4j:${rootProject.toml_version}") {
implementation("com.electronwill.night-config:toml:${rootProject.toml_version}")
shadowMe("com.electronwill.night-config:toml:${rootProject.toml_version}") {
exclude(module: "gson")
}
}
@@ -21,12 +21,14 @@ import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import com.electronwill.nightconfig.core.file.CommentedFileConfig;
import com.electronwill.nightconfig.core.file.CommentedFileConfigBuilder;
import org.apache.logging.log4j.Logger;
// Uses https://github.com/mwanji/toml4j for toml
import com.moandjiezana.toml.Toml;
import com.moandjiezana.toml.TomlWriter;
import com.electronwill.nightconfig.toml.*;
import com.electronwill.nightconfig.core.file.FileConfig;
import com.mojang.blaze3d.vertex.PoseStack;
// Gets info from our own mod
@@ -90,7 +92,7 @@ public abstract class ConfigGui
// Chainge these to your own mod
private static final String MOD_NAME = ModInfo.NAME; // For file saving and identifying
private static final String MOD_NAME_READABLE = ModInfo.READABLE_NAME; // For logs
private static Logger LOGGER = ClientApi.LOGGER; // For logs
// private static Logger LOGGER; // For logs
private static TomlWriter tomlWriter = new TomlWriter();
@@ -144,31 +146,6 @@ public abstract class ConfigGui
initNestedClass(config, "");
loadFromFile();
// Save and read the file
try
{
new Toml().read(Files.newBufferedReader(configFilePath)).to(config);
}
catch (Exception e)
{
saveToFile();
}
for (EntryInfo info : entries)
{
if (info.field.isAnnotationPresent(Entry.class))
{
try
{
info.value = info.field.get(null);
info.tempValue = info.value.toString();
}
catch (IllegalAccessException ignored)
{
}
}
}
}
private static void initNestedClass(Class<?> config, String category)
@@ -334,21 +311,34 @@ public abstract class ConfigGui
/** Grabs what is in the config and puts it in modid.toml */
public static void saveToFile()
{
// If this line fails then delete the modid.toml and start the mod again
CommentedFileConfig config = CommentedFileConfig.builder(configFilePath.toFile()).build();
// First try to create a config file
try
{
try {
if (!Files.exists(configFilePath))
Files.createFile(configFilePath);
}
catch (Exception e)
{
LOGGER.info("Failed creating config file for " + MOD_NAME_READABLE + " at the path [" + configFilePath.toString() + "].");
catch (Exception e) {
// ClientApi.LOGGER.info("Failed creating config file for " + MOD_NAME_READABLE + " at the path [" + configFilePath.toString() + "].");
e.printStackTrace();
}
// If this line fails then delete the modid.toml and start the mod again
Toml toml = new Toml().read(configFilePath.toFile());
LOGGER.info("TomlWriter stuff not made yet");
config.load();
for (EntryInfo info : entries) {
if (info.field.isAnnotationPresent(Entry.class)) {
try {
info.value = info.field.get(null);
info.tempValue = info.value.toString();
} catch (IllegalAccessException ignored) {}
config.set((info.category != "" ? info.category + "." : "") + info.field.getName(), info.value);
}
}
config.save();
config.close();
}
/**
@@ -357,32 +347,27 @@ public abstract class ConfigGui
*/
public static void loadFromFile()
{
Toml toml;
try
{
toml = new Toml().read(configFilePath.toFile());
}
catch (Exception e)
{
LOGGER.info("Config file not found for " + MOD_NAME_READABLE + ". Creating config...");
CommentedFileConfig config = CommentedFileConfig.builder(configFilePath.toFile()).build();
// First checks if the config file was already made
if (!Files.exists(configFilePath)) {
// ClientApi.LOGGER.info("Config file not found for " + MOD_NAME_READABLE + ". Creating config...");
saveToFile();
return;
}
/*
for (EntryInfo info : entries) {
if (info.widget instanceof Map.Entry) { // For enum
info.value = toml.getList((info.category != "" ? info.category + "." : "") + info.field.getName());
} else if (info.field.getType() == String.class) {
info.value = toml.getString((info.category != "" ? info.category + "." : "") + info.field.getName());
} else if (info.field.getType() == Double.class) {
info.value = toml.getDouble((info.category != "" ? info.category + "." : "") + info.field.getName());
} else if (info.field.getType() == Long.class) {
info.value = toml.getLong((info.category != "" ? info.category + "." : "") + info.field.getName());
} else if (info.field.getType() == List.class) {
info.value = toml.getList((info.category != "" ? info.category + "." : "") + info.field.getName());
config.load();
for (EntryInfo info : entries) {
if (info.field.isAnnotationPresent(Entry.class)) {
// if (info.field.getType().isEnum())
// info.value = Enum.valueOf(info.field.getType(), config.get((info.category != "" ? info.category + "." : "") + info.field.getName()).toString());
// else
// info.value = config.get((info.category != "" ? info.category + "." : "") + info.field.getName());
}
}*/
}
config.close();
}
@@ -415,34 +400,12 @@ public abstract class ConfigGui
public void tick()
{
super.tick();
for (EntryInfo info : entries)
{
try
{
info.field.set(null, info.value);
}
catch (IllegalAccessException ignored)
{
}
}
}
private void loadValues()
{
loadFromFile();
for (EntryInfo info : entries)
{
if (info.field.isAnnotationPresent(Entry.class))
try
{
info.value = info.field.get(null);
info.tempValue = info.value.toString();
}
catch (IllegalAccessException ignored)
{
}
}
// for (EntryInfo info : entries)
// {
// try {
// info.field.set(null, info.value);
// } catch (IllegalAccessException ignored) {}
// }
}
@Override
@@ -450,10 +413,10 @@ public abstract class ConfigGui
{
super.init();
if (!reload)
loadValues();
loadFromFile();
this.addRenderableWidget(new Button(this.width / 2 - 154, this.height - 28, 150, 20, CommonComponents.GUI_CANCEL, button -> {
loadValues();
loadFromFile();
Objects.requireNonNull(minecraft).setScreen(parent);
}));
+2 -2
View File
@@ -35,8 +35,8 @@ dependencies {
exclude(group: "net.fabricmc.fabric-api")
}
implementation("com.moandjiezana.toml:toml4j:${rootProject.toml_version}")
shadowMe("com.moandjiezana.toml:toml4j:${rootProject.toml_version}") {
implementation("com.electronwill.night-config:toml:${rootProject.toml_version}")
shadowMe("com.electronwill.night-config:toml:${rootProject.toml_version}") {
exclude(module: "gson")
}
@@ -54,9 +54,9 @@ public class Main implements ClientModInitializer
// This loads the mod after minecraft loads which doesn't causes a lot of issues
public static void init() {
LodCommonMain.initConfig();
LodCommonMain.startup(null, false);
DependencySetup.createInitialBindings();
LodCommonMain.initConfig();
ClientApi.LOGGER.info(ModInfo.READABLE_NAME + ", Version: " + ModInfo.VERSION);
// Check if this works
@@ -65,9 +65,9 @@ public class Main implements ClientModInitializer
}
public static void initServer() {
LodCommonMain.initConfig();
LodCommonMain.startup(null, true);
DependencySetup.createInitialBindings();
LodCommonMain.initConfig();
ClientApi.LOGGER.info(ModInfo.READABLE_NAME + ", Version: " + ModInfo.VERSION);
}
}
+4 -4
View File
@@ -29,10 +29,10 @@ dependencies {
common(project(path: ":common", configuration: "namedElements")) { transitive false }
shadowMe(project(path: ":common", configuration: "transformProductionForge")) { transitive = false }
implementation("com.moandjiezana.toml:toml4j:${rootProject.toml_version}")
forgeDependencies(shadowMe("com.moandjiezana.toml:toml4j:${rootProject.toml_version}") {
exclude(module: "gson")
})
// implementation("com.electronwill.night-config:toml:${rootProject.toml_version}")
// forgeDependencies(shadowMe("com.electronwill.night-config:toml:${rootProject.toml_version}") {
// exclude(module: "gson")
// })
// forgeDependencies(project(":core")) { transitive false }
@@ -59,9 +59,9 @@ public class ForgeMain implements LodForgeMethodCaller
private void init(final FMLCommonSetupEvent event)
{
// make sure the dependencies are set up before the mod needs them
LodCommonMain.initConfig();
LodCommonMain.startup(this, !FMLLoader.getDist().isClient());
ForgeDependencySetup.createInitialBindings();
LodCommonMain.initConfig();
}
+1 -1
View File
@@ -5,7 +5,7 @@ minecraft_version=1.18.1
archives_base_name=DistantHorizons
mod_version=1.5.4a
maven_group=com.seibel.lod
toml_version=0.7.2
toml_version=3.6.0
fabric_loader_version=0.12.12
fabric_api_version=0.44.0+1.18