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
This commit is contained in:
James Seibel
2023-07-29 17:33:11 -05:00
parent 3415db58a6
commit 3f25472437
2 changed files with 27 additions and 4 deletions
@@ -1125,10 +1125,6 @@ public class Config
.set(new ArrayList<String>(Arrays.asList("option 1", "option 2", "option 3")))
.build();
public static ConfigEntry<Map<String, String>> mapTest = new ConfigEntry.Builder<Map<String, String>>()
.set(new HashMap<String, String>())
.build();
public static ConfigCategory categoryTest = new ConfigCategory.Builder().set(CategoryTest.class).build();
public static ConfigEntry<Integer> linkableTest = new ConfigEntry.Builder<Integer>()
@@ -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<T> extends AbstractConfigType<T, ConfigEntry<T>> implem
private ConfigEntry(EConfigEntryAppearance appearance, T value, String comment, T min, T max, boolean allowApiOverride, EConfigEntryPerformance performance, ArrayList<IConfigListener> 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;