Flat file database is now writing and reading correctly with various

data types.
This commit is contained in:
tastybento 2017-05-21 18:18:02 -07:00
parent 9ef9d931af
commit 960993108d
4 changed files with 132 additions and 7 deletions

View File

@ -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<Integer, Location> homes = new HashMap<Integer, Location>();
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<ItemStack> items = new ArrayList<ItemStack>();
items.add(new ItemStack(Material.ACTIVATOR_RAIL, 2));
items.add(new ItemStack(Material.FEATHER,5));
test.setInventory(items);
FlatFileDatabaseInserter<Test> inserter = new FlatFileDatabaseInserter<Test>(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();

View File

@ -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<Integer, Location> homeLocations;
private List<ItemStack> inventory;
public Test() {}
@ -28,6 +36,34 @@ public class Test {
this.name = name;
}
/**
* @return the homeLocations
*/
public HashMap<Integer, Location> getHomeLocations() {
return homeLocations;
}
/**
* @param homeLocations the homeLocations to set
*/
public void setHomeLocations(HashMap<Integer, Location> homeLocations) {
this.homeLocations = homeLocations;
}
/**
* @return the inventory
*/
public List<ItemStack> getInventory() {
return inventory;
}
/**
* @param inventory the inventory to set
*/
public void setInventory(List<ItemStack> inventory) {
this.inventory = inventory;
}
public String toString() {
final String TAB = " \n";

View File

@ -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<T> extends AbstractDatabaseHandler<T> {
// 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<String, Object> 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<Entry<String, Object>> iter = map.entrySet().iterator();
while (iter.hasNext()) {
final Entry<String, Object> entry = iter.next();
final Object value = entry.getValue();
final String key = entry.getKey();
if (value instanceof Map) {
saveMap(section.createSection(key), (Map<String, Object>) value);
} else if (value instanceof Collection) {
saveCollection(section, (Collection<Object>) 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<Object> 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<Object> iter = collection.iterator();
final String currentSectionPath = section.getCurrentPath();
while (iter.hasNext()) {
final Object value = iter.next();
section.set(currentSectionPath, value);
}
}
}

View File

@ -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<T> extends AbstractDatabaseHandler<T> {
*/
private T createObject(YamlConfiguration config) throws InstantiationException, IllegalAccessException,
IntrospectionException, IllegalArgumentException, InvocationTargetException
{
{
T instance = type.newInstance();
@ -74,14 +75,29 @@ public class FlatFileDatabaseSelecter<T> extends AbstractDatabaseHandler<T> {
/* 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<Object,Object> value = new HashMap<Object, Object>();
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;