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!" unknown-player: "&cUnknown player!"
general: "&cThat command is not ready yet - contact admin" general: "&cThat command is not ready yet - contact admin"
warp-not-safe: "&cThat warp is not safe right now!" warp-not-safe: "&cThat warp is not safe right now!"
wrong-world: "&cYou are not in the right world to do that!"
commands: commands:
help: help:

View File

@ -106,7 +106,7 @@ public abstract class Addon implements AddonInterface {
yamlConfig = new YamlConfiguration(); yamlConfig = new YamlConfiguration();
yamlConfig.load(yamlFile); yamlConfig.load(yamlFile);
} catch (Exception e) { } 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; return yamlConfig;

View File

@ -47,7 +47,7 @@ public class AddonClassLoader extends URLClassLoader {
throw new InvalidAddonFormatException("Packages declaration cannot start with 'us.tastybento'"); throw new InvalidAddonFormatException("Packages declaration cannot start with 'us.tastybento'");
} }
} catch (ClassNotFoundException e) { } 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"); 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") @SuppressWarnings("deprecation")
public void updateInventory() { public void updateInventory() {
player.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

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