Made improvements while writing challenges addon

Improved enum error handling in flat file.
This commit is contained in:
Tastybento 2018-02-24 11:28:21 -08:00
parent 84f10d6790
commit 5a3a557cf2
5 changed files with 47 additions and 13 deletions

View File

@ -28,6 +28,7 @@ general:
unknown-player: "&cUnknown player!"
general: "&cThat command is not ready yet - contact admin"
warp-not-safe: "&cThat warp is not safe right now!"
wrong-world: "&cYou are not in the right world to do that!"
commands:
help:

View File

@ -106,7 +106,7 @@ public abstract class Addon implements AddonInterface {
yamlConfig = new YamlConfiguration();
yamlConfig.load(yamlFile);
} catch (Exception e) {
Bukkit.getLogger().severe("Could not load YAML file: " + file);
Bukkit.getLogger().severe("Could not load YAML file: " + file + " " + e.getMessage());
}
}
return yamlConfig;

View File

@ -47,7 +47,7 @@ public class AddonClassLoader extends URLClassLoader {
throw new InvalidAddonFormatException("Packages declaration cannot start with 'us.tastybento'");
}
} catch (ClassNotFoundException e) {
BSkyBlock.getInstance().getLogger().severe("Could not load '" + path.getName() + "' in folder '" + path.getParent() + "'");
BSkyBlock.getInstance().getLogger().severe("Could not load '" + path.getName() + "' in folder '" + path.getParent() + "' - invalid addon.yml");
throw new InvalidDescriptionException("Invalid addon.yml");
}

View File

@ -272,9 +272,23 @@ public class User {
}
/**
* Forces an update of the user's complete inventory.
* Deprecated, but there is no current alternative.
*/
@SuppressWarnings("deprecation")
public void updateInventory() {
player.updateInventory();
}
/**
* Performs a command as the player
* @param cmd
* @return true if the command was successful, otherwise false
*/
public boolean performCommand(String cmd) {
return player.performCommand(cmd);
}
}

View File

@ -245,9 +245,11 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
// collectionTypes should be only 1 long
Type setType = collectionTypes.get(0);
List<Object> value = new ArrayList<>();
if (config.getList(storageLocation) != null) {
for (Object listValue: config.getList(storageLocation)) {
value.add(deserialize(listValue,Class.forName(setType.getTypeName())));
}
}
// TODO: this may not work with all keys. Further serialization may be required.
method.invoke(instance, value);
} else {
@ -289,6 +291,8 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
@SuppressWarnings("unchecked")
@Override
public void saveObject(T instance) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException {
// This is the Yaml Configuration that will be used and saved at the end
YamlConfiguration config = new YamlConfiguration();
@ -315,7 +319,9 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
Method method = propertyDescriptor.getReadMethod();
// Invoke the read method to get the value. We have no idea what type of value it is.
Object value = method.invoke(instance);
if (DEBUG) {
plugin.getLogger().info("DEBUG: field = " + field.getName() + " value = " + value);
}
String storageLocation = field.getName();
// Check if there is an annotation on the field
ConfigEntry configEntry = field.getAnnotation(ConfigEntry.class);
@ -350,12 +356,12 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
continue fields;
}
//plugin.getLogger().info("DEBUG: property desc = " + propertyDescriptor.getPropertyType().getTypeName());
// Depending on the vale type, it'll need serializing differenty
plugin.getLogger().info("DEBUG: property desc = " + propertyDescriptor.getPropertyType().getTypeName());
// Depending on the vale type, it'll need serializing differently
// Check if this field is the mandatory UniqueId field. This is used to identify this instantiation of the class
if (method.getName().equals("getUniqueId")) {
// If the object does not have a unique name assigned to it already, one is created at random
//plugin.getLogger().info("DEBUG: uniqueId = " + value);
plugin.getLogger().info("DEBUG: flat file db uniqueId = " + value);
String id = (String)value;
if (value == null || id.isEmpty()) {
id = databaseConnecter.getUniqueId(dataObject.getSimpleName());
@ -399,6 +405,9 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
if (filename.isEmpty()) {
throw new IllegalArgumentException("No uniqueId in class");
}
if (DEBUG) {
plugin.getLogger().info("DEBUG: Saving YAML file : " + path + " " + filename);
}
databaseConnecter.saveYamlFile(config, path, filename);
}
@ -469,13 +478,23 @@ public class FlatFileDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
if (Enum.class.isAssignableFrom(clazz)) {
//Custom enums are a child of the Enum class.
// Find out the value
try {
Class<Enum> enumClass = (Class<Enum>)clazz;
try {
value = Enum.valueOf(enumClass, (String)value);
} catch (Exception e) {
// Maybe this value does not exist?
// TODO return something?
plugin.getLogger().severe(() -> "Could not deserialize enum: " + clazz.getCanonicalName());
// This value does not exist - probably admin typed it wrongly
// Show what is available and pick one at random
plugin.getLogger().severe("Error in YML file: " + value + " is not a valid value in the enum " + clazz.getCanonicalName() + "!");
plugin.getLogger().severe("Options are : ");
boolean isSet = false;
for (Field fields : enumClass.getFields()) {
plugin.getLogger().severe(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.getLogger().severe("Setting to " + fields.getName() + " because it starts with the same letter");
isSet = true;
}
}
}
}
return value;