From 3f25472437279efbc8b24ad0228b242c97418d5e Mon Sep 17 00:00:00 2001 From: James Seibel Date: Sat, 29 Jul 2023 17:33:11 -0500 Subject: [PATCH] Remove HashMap from Config and add runtime Config type checking HashMap isn't supported by NightConfig and can cause the game to crash in some situations --- .../distanthorizons/core/config/Config.java | 4 --- .../core/config/types/ConfigEntry.java | 27 +++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/com/seibel/distanthorizons/core/config/Config.java b/core/src/main/java/com/seibel/distanthorizons/core/config/Config.java index d27282404..3180916c4 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/config/Config.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/config/Config.java @@ -1125,10 +1125,6 @@ public class Config .set(new ArrayList(Arrays.asList("option 1", "option 2", "option 3"))) .build(); - public static ConfigEntry> mapTest = new ConfigEntry.Builder>() - .set(new HashMap()) - .build(); - public static ConfigCategory categoryTest = new ConfigCategory.Builder().set(CategoryTest.class).build(); public static ConfigEntry linkableTest = new ConfigEntry.Builder() diff --git a/core/src/main/java/com/seibel/distanthorizons/core/config/types/ConfigEntry.java b/core/src/main/java/com/seibel/distanthorizons/core/config/types/ConfigEntry.java index 6cc64d209..4d1908490 100644 --- a/core/src/main/java/com/seibel/distanthorizons/core/config/types/ConfigEntry.java +++ b/core/src/main/java/com/seibel/distanthorizons/core/config/types/ConfigEntry.java @@ -7,8 +7,12 @@ import com.seibel.distanthorizons.core.config.types.enums.EConfigEntryAppearance import com.seibel.distanthorizons.core.config.types.enums.EConfigEntryPerformance; import com.seibel.distanthorizons.coreapi.interfaces.config.IConfigEntry; +import java.time.temporal.Temporal; import java.util.ArrayList; import java.util.Arrays; +import java.util.List; + +import static com.electronwill.nightconfig.core.NullObject.NULL_OBJECT; /** * Use for making the config variables @@ -40,6 +44,29 @@ public class ConfigEntry extends AbstractConfigType> implem private ConfigEntry(EConfigEntryAppearance appearance, T value, String comment, T min, T max, boolean allowApiOverride, EConfigEntryPerformance performance, ArrayList listenerList) { super(appearance, value); + + // runtime check to make sure the config value is supported by NightConfig, + // unfortunately we can't do this type of check statically without Java 16 + if (value == null) + { + throw new IllegalArgumentException("ConfigEntry setup failure. TOML doesn't support saving null values."); + } + // all of these types were taken from NightConfig's writing code + else if (!(value instanceof List) + && !(value instanceof CharSequence) // String + && !(value instanceof Enum) + && !(value instanceof Temporal) // Date or DateTime + && !(value instanceof Float || value instanceof Double) + && !(value instanceof Number) + && !(value instanceof Boolean) + // optional type that is specific to NightConfig, currently commented out so we aren't tied quite so tightly to NightConfig + //&& !(value instanceof Config) // NightConfig interface + ) + { + throw new IllegalArgumentException("ConfigEntry setup failure. Value type ["+value.getClass()+"] is not supported by NightConfig."); + } + + this.defaultValue = value; this.comment = comment; this.min = min;