Change ConfigValue#get() to throw if called before config loaded (#8236)

This prevents silent issues where a mod gets the value of the setting
before configs are loaded, which means the default value is always
returned.

As there may be situations where the getting the config setting before
configs are loaded is needed, and it is not preferable to hardcode the
default value, the original behavior is made available through #getRaw.

Implements and closes #7716

* Remove getRaw() method

This is effectively replaced with the expression `spec.isLoaded() ?
configValue.get() : configValue.getDefault()`.

* Remove forceSystemNanoTime config setting

As implemented, it never had any effect as any place where the config
value would be queried happens before the configs are loaded.
This commit is contained in:
sciwhiz12 authored and GitHub committed 2022-06-17 08:31:45 +01:00
1 parent 915d570b17
commit 4f95a20c5e
3 files changed
+28 -15

No files matched your search

@@ -126,8 +126,6 @@ public class ForgeConfig {
public final BooleanValue useCombinedDepthStencilAttachment;
public final BooleanValue forceSystemNanoTime;
Client(ForgeConfigSpec.Builder builder) {
builder.comment("Client only settings, mostly things related to rendering")
.push("client");
@@ -154,11 +152,6 @@ public class ForgeConfig {
.translation("forge.configgui.useCombinedDepthStencilAttachment")
.define("useCombinedDepthStencilAttachment", false);
forceSystemNanoTime = builder
.comment("Forces the use of System.nanoTime instead of glfwGetTime, as the main Util time provider")
.translation("forge.configgui.forceSystemNanoTime")
.define("forceSystemNanoTime", false);
builder.pop();
}
}
@@ -834,9 +834,29 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper<UnmodifiableConfi
return Lists.newArrayList(path);
}
/**
* Returns the actual value for the configuration setting, throwing if the config has not yet been loaded.
*
* @return the actual value for the setting
* @throws NullPointerException if the {@link ForgeConfigSpec config spec} object that will contain this has
* not yet been built
* @throws IllegalStateException if the associated config has not yet been loaded
*/
public T get()
{
Preconditions.checkNotNull(spec, "Cannot get config value before spec is built");
// TODO: Remove this dev-time check so this errors out on both production and dev
// This is dev-time-only in 1.18.2, to avoid breaking already published mods while forcing devs to fix their errors
if (!FMLEnvironment.production)
{
// When the above if-check is removed, change message to "Cannot get config value before config is loaded"
Preconditions.checkState(spec.childConfig != null, """
Cannot get config value before config is loaded.
This error is currently only thrown in the development environment, to avoid breaking published mods.
In a future version, this will also throw in the production environment.
""");
}
if (spec.childConfig == null)
return defaultSupplier.get();
@@ -853,6 +873,14 @@ public class ForgeConfigSpec extends UnmodifiableConfigWrapper<UnmodifiableConfi
return config.getOrElse(path, defaultSupplier);
}
/**
* {@return the default value for the configuration setting}
*/
public T getDefault()
{
return defaultSupplier.get();
}
public Builder next()
{
return parent;