Proper fix for checking if the config is loaded
This commit is contained in:
@@ -60,14 +60,6 @@ public class Config
|
||||
|
||||
public static ConfigCategory client = new ConfigCategory.Builder().set(Client.class).build();
|
||||
|
||||
/**
|
||||
* <strong>False</strong> if the config hasn't been loaded in from file yet.
|
||||
* While in this state the config shouldn't be modified since it may cause file corruption. <br><br>
|
||||
*
|
||||
* <strong>True</strong> if the config has been loaded and is ready to use.
|
||||
*/
|
||||
public static boolean loaded = false;
|
||||
|
||||
|
||||
|
||||
public static class Client
|
||||
|
||||
@@ -26,11 +26,13 @@ public class ConfigBase
|
||||
public static ConfigBase INSTANCE;
|
||||
public ConfigFileHandling configFileINSTANCE;
|
||||
|
||||
public static final Logger LOGGER = LogManager.getLogger(ConfigBase.class.getSimpleName());
|
||||
private final Logger LOGGER;
|
||||
public final String modID;
|
||||
public final String modName;
|
||||
public final int configVersion;
|
||||
|
||||
public boolean isLoaded = false;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
@@ -78,6 +80,8 @@ public class ConfigBase
|
||||
|
||||
public ConfigBase(String modID, String modName, Class<?> config, int configVersion)
|
||||
{
|
||||
this.LOGGER = LogManager.getLogger(this.getClass().getSimpleName() + ", " + modID);
|
||||
|
||||
LOGGER.info("Initialising config for " + modName);
|
||||
this.modID = modID;
|
||||
this.modName = modName;
|
||||
@@ -88,6 +92,8 @@ public class ConfigBase
|
||||
// File handling (load from file)
|
||||
this.configFileINSTANCE = new ConfigFileHandling(this);
|
||||
this.configFileINSTANCE.loadFromFile();
|
||||
|
||||
isLoaded = true;
|
||||
LOGGER.info("Config for " + modName + " initialised");
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -1,6 +1,7 @@
|
||||
package com.seibel.distanthorizons.core.config.eventHandlers.presets;
|
||||
|
||||
import com.seibel.distanthorizons.core.config.Config;
|
||||
import com.seibel.distanthorizons.core.config.ConfigBase;
|
||||
import com.seibel.distanthorizons.core.config.ConfigEntryWithPresetOptions;
|
||||
import com.seibel.distanthorizons.core.config.listeners.IConfigListener;
|
||||
import com.seibel.distanthorizons.core.dependencyInjection.SingletonInjector;
|
||||
@@ -64,7 +65,7 @@ public abstract class AbstractPresetConfigEventHandler<TPresetEnum extends Enum<
|
||||
public void onConfigValueSet()
|
||||
{
|
||||
// don't try modifying the config before it's been loaded from file
|
||||
if (!Config.loaded)
|
||||
if (!ConfigBase.INSTANCE.isLoaded)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
+15
-6
@@ -1,13 +1,13 @@
|
||||
package com.seibel.distanthorizons.core.config.file;
|
||||
|
||||
import com.electronwill.nightconfig.core.file.CommentedFileConfig;
|
||||
import com.seibel.distanthorizons.core.config.Config;
|
||||
import com.seibel.distanthorizons.core.dependencyInjection.SingletonInjector;
|
||||
import com.seibel.distanthorizons.core.config.ConfigBase;
|
||||
import com.seibel.distanthorizons.core.config.types.AbstractConfigType;
|
||||
import com.seibel.distanthorizons.core.config.types.ConfigEntry;
|
||||
import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftClientWrapper;
|
||||
import com.seibel.distanthorizons.core.wrapperInterfaces.minecraft.IMinecraftSharedWrapper;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -27,11 +27,20 @@ public class ConfigFileHandling
|
||||
public final ConfigBase configBase;
|
||||
public final Path configPath;
|
||||
|
||||
private final Logger LOGGER;
|
||||
|
||||
/** This is the object for night-config */
|
||||
private final CommentedFileConfig configFileConfig;
|
||||
|
||||
public ConfigFileHandling(ConfigBase configBase)
|
||||
{
|
||||
this.LOGGER = LogManager.getLogger(this.getClass().getSimpleName() + ", " + configBase.modID);
|
||||
this.configBase = configBase;
|
||||
|
||||
configPath = SingletonInjector.INSTANCE.get(IMinecraftSharedWrapper.class)
|
||||
.getInstallationDirectory().toPath().resolve("config").resolve(this.configBase.modName + ".toml");
|
||||
|
||||
this.configFileConfig = CommentedFileConfig.builder(configPath.toFile()).build();
|
||||
}
|
||||
|
||||
/** Saves the entire config to the file */
|
||||
@@ -229,7 +238,11 @@ public class ConfigFileHandling
|
||||
|
||||
|
||||
|
||||
/** Does config.load(); but with error checking */
|
||||
/**
|
||||
* Does config.load(); but with error checking
|
||||
*
|
||||
* @apiNote This overwrites any value currently stored in the config
|
||||
*/
|
||||
public void loadConfig(CommentedFileConfig config)
|
||||
{
|
||||
try
|
||||
@@ -261,10 +274,6 @@ public class ConfigFileHandling
|
||||
SingletonInjector.INSTANCE.get(IMinecraftClientWrapper.class).crashMinecraft("Loading file and resetting config file failed at path [" + configPath + "]. Please check the file is ok and you have the permissions", ex);
|
||||
}
|
||||
}
|
||||
|
||||
// if the config is modified before having been loaded it can cause file corruption
|
||||
// and permissions errors
|
||||
Config.loaded = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user