Fix config handling failing for Doubles and add additional logging
This commit is contained in:
+14
-2
@@ -224,9 +224,21 @@ public class ConfigFileHandling
|
||||
return;
|
||||
}
|
||||
|
||||
entry.pureSet((T) ConfigTypeConverters.attemptToConvertFromString(entry.getType(), nightConfig.get(entry.getNameWCategory())));
|
||||
// try converting the value if necessary
|
||||
Class<?> expectedValueClass = entry.getType();
|
||||
Object value = nightConfig.get(entry.getNameWCategory());
|
||||
Object convertedValue = ConfigTypeConverters.attemptToConvertFromString(expectedValueClass, value);
|
||||
if (!convertedValue.getClass().equals(expectedValueClass))
|
||||
{
|
||||
LOGGER.error("Unable to convert config value ["+value+"] from ["+(value != null ? value.getClass() : "NULL")+"] to ["+expectedValueClass+"] for config ["+entry.name+"], " +
|
||||
"the default config value will be used instead ["+entry.getDefaultValue()+"]. " +
|
||||
"Make sure a converter is defined in ["+ConfigTypeConverters.class.getSimpleName()+"].");
|
||||
convertedValue = entry.getDefaultValue();
|
||||
}
|
||||
entry.pureSet((T) convertedValue);
|
||||
|
||||
if (entry.getTrueValue() == null) {
|
||||
if (entry.getTrueValue() == null)
|
||||
{
|
||||
LOGGER.warn("Entry [" + entry.getNameWCategory() + "] returned as null from the config. Using default value.");
|
||||
entry.pureSet(entry.getDefaultValue());
|
||||
}
|
||||
|
||||
+17
-9
@@ -37,12 +37,13 @@ public class ConfigTypeConverters
|
||||
// Once you've made a converter add it to here where the first value is the type you want to convert and the 2nd value is the converter
|
||||
public static final Map<Class<?>, ConverterBase> convertObjects = new HashMap<Class<?>, ConverterBase>()
|
||||
{{
|
||||
put(Short.class, new ShortConverter());
|
||||
put(Long.class, new LongConverter());
|
||||
put(Float.class, new FloatConverter());
|
||||
put(Byte.class, new ByteConverter());
|
||||
this.put(Short.class, new ShortConverter());
|
||||
this.put(Long.class, new LongConverter());
|
||||
this.put(Float.class, new FloatConverter());
|
||||
this.put(Double.class, new DoubleConverter());
|
||||
this.put(Byte.class, new ByteConverter());
|
||||
|
||||
put(Map.class, new MapConverter());
|
||||
this.put(Map.class, new MapConverter());
|
||||
}};
|
||||
|
||||
public static Class<?> isClassConvertable(Class<?> clazz)
|
||||
@@ -73,10 +74,12 @@ public class ConfigTypeConverters
|
||||
{
|
||||
return attemptToConvertFromString(value.getClass(), value);
|
||||
}
|
||||
public static Object attemptToConvertFromString(Class<?> clazz, Object value)
|
||||
public static Object attemptToConvertFromString(Class<?> outputClass, Object value)
|
||||
{
|
||||
Class<?> convertablClass = isClassConvertable(clazz);
|
||||
if (convertablClass != null) {
|
||||
boolean valueNeedsConverting = (value == null || value.getClass().equals(String.class));
|
||||
Class<?> convertablClass = isClassConvertable(outputClass);
|
||||
if (valueNeedsConverting && convertablClass != null)
|
||||
{
|
||||
return convertFromString(convertablClass, (String) value);
|
||||
}
|
||||
return value;
|
||||
@@ -140,7 +143,12 @@ public class ConfigTypeConverters
|
||||
{
|
||||
@Override public String convertToString(Object item) { return ((Float) item).toString(); }
|
||||
@Override public Float convertFromString(String s) { return Float.valueOf(s); }
|
||||
|
||||
}
|
||||
|
||||
public static class DoubleConverter extends ConverterBase
|
||||
{
|
||||
@Override public String convertToString(Object item) { return ((Double) item).toString(); }
|
||||
@Override public Double convertFromString(String s) { return Double.valueOf(s); }
|
||||
}
|
||||
|
||||
public static class ByteConverter extends ConverterBase
|
||||
|
||||
Reference in New Issue
Block a user