Skip to content

Commit

Permalink
Uses default enum values if invalid one used in a config.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Oct 31, 2018
1 parent 935704d commit a6357ed
Showing 1 changed file with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,13 @@ private T createObject(YamlConfiguration config) throws InstantiationException,
Object value = config.get(storageLocation);
// If the value is a yml MemorySection then something is wrong, so ignore it. Maybe an admin did some bad editing
if (value != null && !value.getClass().equals(MemorySection.class)) {
method.invoke(instance, deserialize(value,propertyDescriptor.getPropertyType()));
Object setTo = deserialize(value,propertyDescriptor.getPropertyType());
if (!(Enum.class.isAssignableFrom(propertyDescriptor.getPropertyType()) && setTo == null)) {
// Do not invoke null on Enums
method.invoke(instance, setTo);
} else {
plugin.logError("Default setting value will be used: " + propertyDescriptor.getReadMethod().invoke(instance));
}
}
}
}
Expand Down Expand Up @@ -520,15 +526,10 @@ private Object deserialize(Object value, Class<?> clazz) {
// Show what is available and pick one at random
plugin.logError("Error in YML file: " + value + " is not a valid value in the enum " + clazz.getCanonicalName() + "!");
plugin.logError("Options are : ");
boolean isSet = false;
for (Field fields : enumClass.getFields()) {
plugin.logError(fields.getName());
if (!isSet && !((String)value).isEmpty() && fields.getName().substring(0, 1).equals(((String)value).substring(0, 1))) {
value = Enum.valueOf(enumClass, fields.getName());
plugin.logError("Setting to " + fields.getName() + " because it starts with the same letter");
isSet = true;
}
}
value = null;
}
}
return value;
Expand Down

0 comments on commit a6357ed

Please sign in to comment.