diff --git a/src/main/java/us/tastybento/bskyblock/database/RunTest.java b/src/main/java/us/tastybento/bskyblock/database/RunTest.java index d2abaa326..d24abd50f 100644 --- a/src/main/java/us/tastybento/bskyblock/database/RunTest.java +++ b/src/main/java/us/tastybento/bskyblock/database/RunTest.java @@ -1,9 +1,17 @@ package us.tastybento.bskyblock.database; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; + import us.tastybento.bskyblock.BSkyBlock; +import us.tastybento.bskyblock.database.flatfile.FlatFileDatabaseConnecter; import us.tastybento.bskyblock.database.flatfile.FlatFileDatabaseInserter; import us.tastybento.bskyblock.database.flatfile.FlatFileDatabaseSelecter; -import us.tastybento.bskyblock.database.flatfile.FlatFileDatabaseConnecter; public class RunTest { @@ -19,6 +27,14 @@ public class RunTest { Test test = new Test(); test.setId(34); test.setName("testname"); + HashMap homes = new HashMap(); + homes.put(1, new Location(plugin.getServer().getWorld("world"), 1, 2, 3, 1, 1)); + homes.put(2, new Location(plugin.getServer().getWorld("world"), 3, 3, 3, 3, 3)); + test.setHomeLocations(homes); + List items = new ArrayList(); + items.add(new ItemStack(Material.ACTIVATOR_RAIL, 2)); + items.add(new ItemStack(Material.FEATHER,5)); + test.setInventory(items); FlatFileDatabaseInserter inserter = new FlatFileDatabaseInserter(plugin, Test.class, connecter); @@ -30,6 +46,10 @@ public class RunTest { plugin.getLogger().info("DEBUG: name = " + test.getName()); plugin.getLogger().info("DEBUG: id = " + test.getId()); + homes = test.getHomeLocations(); + plugin.getLogger().info("DEBUG: homes = " + homes); + items = test.getInventory(); + plugin.getLogger().info("DEBUG: items = " + items); } catch (Exception e) { e.printStackTrace(); diff --git a/src/main/java/us/tastybento/bskyblock/database/Test.java b/src/main/java/us/tastybento/bskyblock/database/Test.java index e35b73708..1fb43e4bd 100644 --- a/src/main/java/us/tastybento/bskyblock/database/Test.java +++ b/src/main/java/us/tastybento/bskyblock/database/Test.java @@ -1,8 +1,16 @@ package us.tastybento.bskyblock.database; +import java.util.HashMap; +import java.util.List; + +import org.bukkit.Location; +import org.bukkit.inventory.ItemStack; + public class Test { private int id; private String name; + private HashMap homeLocations; + private List inventory; public Test() {} @@ -28,6 +36,34 @@ public class Test { this.name = name; } + /** + * @return the homeLocations + */ + public HashMap getHomeLocations() { + return homeLocations; + } + + /** + * @param homeLocations the homeLocations to set + */ + public void setHomeLocations(HashMap homeLocations) { + this.homeLocations = homeLocations; + } + + /** + * @return the inventory + */ + public List getInventory() { + return inventory; + } + + /** + * @param inventory the inventory to set + */ + public void setInventory(List inventory) { + this.inventory = inventory; + } + public String toString() { final String TAB = " \n"; diff --git a/src/main/java/us/tastybento/bskyblock/database/flatfile/FlatFileDatabaseInserter.java b/src/main/java/us/tastybento/bskyblock/database/flatfile/FlatFileDatabaseInserter.java index 2ad3a7341..75eb69199 100644 --- a/src/main/java/us/tastybento/bskyblock/database/flatfile/FlatFileDatabaseInserter.java +++ b/src/main/java/us/tastybento/bskyblock/database/flatfile/FlatFileDatabaseInserter.java @@ -5,7 +5,13 @@ import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; import us.tastybento.bskyblock.BSkyBlock; @@ -54,10 +60,57 @@ public class FlatFileDatabaseInserter extends AbstractDatabaseHandler { // TODO: depending on the type, it'll need serializing config.set(field.getName(), value); + } databaseConnecter.saveYamlFile(config, type.getSimpleName()); } + /** + * Saves a Map at the specified ConfigurationSection. + * @param section the ConfigurationSection + * @param map the Map, note that the String parameter in the map refers to the keys the objects are saved in + * @throws IllegalArgumentException when either the ConfigurationSection or the Map is null + */ + @SuppressWarnings("unchecked") + public static void saveMap(final ConfigurationSection section, final Map map) throws IllegalArgumentException { + if (section == null || map == null) + throw new IllegalArgumentException("Both the configuration section and the map to save must not be null"); + final Iterator> iter = map.entrySet().iterator(); + + while (iter.hasNext()) { + final Entry entry = iter.next(); + final Object value = entry.getValue(); + final String key = entry.getKey(); + + if (value instanceof Map) { + saveMap(section.createSection(key), (Map) value); + } else if (value instanceof Collection) { + saveCollection(section, (Collection) value); + } else { + section.set(key, value); + } + } + } + + /** + * Saves a Collection at the specified ConfigurationSection. + * @param section the ConfigurationSection + * @param collection the Collection + * @throws IllegalArgumentException when either the ConfigurationSection or the Collection is null + */ + public static void saveCollection(final ConfigurationSection section, final Collection collection) throws IllegalArgumentException { + if (section == null || collection == null) + throw new IllegalArgumentException("Both the configuration section and the iterable object to save must not be null"); + + final Iterator iter = collection.iterator(); + final String currentSectionPath = section.getCurrentPath(); + + while (iter.hasNext()) { + final Object value = iter.next(); + + section.set(currentSectionPath, value); + } + } } diff --git a/src/main/java/us/tastybento/bskyblock/database/flatfile/FlatFileDatabaseSelecter.java b/src/main/java/us/tastybento/bskyblock/database/flatfile/FlatFileDatabaseSelecter.java index 8c750f298..a4eefdb39 100644 --- a/src/main/java/us/tastybento/bskyblock/database/flatfile/FlatFileDatabaseSelecter.java +++ b/src/main/java/us/tastybento/bskyblock/database/flatfile/FlatFileDatabaseSelecter.java @@ -5,6 +5,7 @@ import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.HashMap; import org.bukkit.configuration.file.YamlConfiguration; @@ -66,7 +67,7 @@ public class FlatFileDatabaseSelecter extends AbstractDatabaseHandler { */ private T createObject(YamlConfiguration config) throws InstantiationException, IllegalAccessException, IntrospectionException, IllegalArgumentException, InvocationTargetException - { + { T instance = type.newInstance(); @@ -74,14 +75,29 @@ public class FlatFileDatabaseSelecter extends AbstractDatabaseHandler { /* We assume the table-column-names exactly match the variable-names of T */ // TODO: depending on the data type, it'll need deserializing - Object value = config.get(field.getName()); + try { - PropertyDescriptor propertyDescriptor = new PropertyDescriptor( - field.getName(), type); + PropertyDescriptor propertyDescriptor = new PropertyDescriptor( + field.getName(), type); - Method method = propertyDescriptor.getWriteMethod(); + Method method = propertyDescriptor.getWriteMethod(); + plugin.getLogger().info("DEBUG: " + propertyDescriptor.getPropertyType().getTypeName()); + if (propertyDescriptor.getPropertyType().equals(HashMap.class)) { + plugin.getLogger().info("DEBUG: is HashMap"); + // TODO: this may not work with all keys. Further serialization may be required. + HashMap value = new HashMap(); + for (String key : config.getConfigurationSection(field.getName()).getKeys(false)) { + value.put(key, config.get(field.getName() + "." + key)); + } + method.invoke(instance, value); + } else { + Object value = config.get(field.getName()); + method.invoke(instance, value); + } + } catch (Exception e) { + e.printStackTrace(); + } - method.invoke(instance, value); } return instance;