Minor changes to deserialization in Yaml handler

This commit is contained in:
Florian CUNY 2018-12-01 15:39:41 +01:00
parent 6977a31830
commit bce906d98e

View File

@ -491,30 +491,19 @@ public class YamlDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
return Long.valueOf((Integer) value);
}
if (value.getClass().equals(String.class)) {
if (clazz.equals(Integer.class)) {
return Integer.valueOf((String) value);
}
if (clazz.equals(Long.class)) {
return Long.valueOf((String) value);
}
if (clazz.equals(Double.class)) {
return Double.valueOf((String) value);
}
if (clazz.equals(Float.class)) {
return Float.valueOf((String) value);
}
return deserializeGenericTypes((String) value, clazz);
}
if (clazz.equals(UUID.class)) {
value = UUID.fromString((String)value);
return UUID.fromString((String)value);
}
// Bukkit Types
if (clazz.equals(Location.class)) {
// Get Location from String - may be null...
value = Util.getLocationString(((String)value));
return Util.getLocationString(((String)value));
}
if (clazz.equals(World.class)) {
// Get world by name - may be null...
value = plugin.getServer().getWorld((String)value);
return plugin.getServer().getWorld((String)value);
}
// Enums
if (Enum.class.isAssignableFrom(clazz)) {
@ -522,7 +511,7 @@ public class YamlDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
// Find out the value
Class<Enum> enumClass = (Class<Enum>)clazz;
try {
value = Enum.valueOf(enumClass, (String)value);
return Enum.valueOf(enumClass, (String)value);
} catch (Exception e) {
// This value does not exist - probably admin typed it wrongly
// Show what is available and pick one at random
@ -531,12 +520,28 @@ public class YamlDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
for (Field fields : enumClass.getFields()) {
plugin.logError(fields.getName());
}
value = null;
}
}
// Could not deserialize the value.
return value;
}
private Object deserializeGenericTypes(String value, Class<?> clazz) {
if (clazz.equals(Integer.class)) {
return Integer.valueOf(value);
}
if (clazz.equals(Long.class)) {
return Long.valueOf(value);
}
if (clazz.equals(Double.class)) {
return Double.valueOf(value);
}
if (clazz.equals(Float.class)) {
return Float.valueOf(value);
}
return null;
}
/* (non-Javadoc)
* @see world.bentobox.bentobox.database.AbstractDatabaseHandler#deleteObject(java.lang.Object)
*/