MySQL is now working for saving and loading islands.

Next step is to fix flat file enum storage.
This commit is contained in:
tastybento 2017-06-10 08:23:12 -07:00
parent 6f842a9b29
commit a541ea3e25
4 changed files with 83 additions and 72 deletions

View File

@ -47,13 +47,14 @@ public class BSkyBlock extends JavaPlugin{
// Load configuration and locales. If there are no errors, load the plugin. // Load configuration and locales. If there are no errors, load the plugin.
if(PluginConfig.loadPluginConfig(this)){ if(PluginConfig.loadPluginConfig(this)){
// TEMP DEBUG DATABASE // TEMP DEBUG DATABASE
/*
Settings.databaseType = DatabaseType.MYSQL; Settings.databaseType = DatabaseType.MYSQL;
Settings.dbHost = "localhost"; Settings.dbHost = "localhost";
Settings.dbPort = 3306; Settings.dbPort = 3306;
Settings.dbName = "ASkyBlock"; Settings.dbName = "ASkyBlock";
Settings.dbUsername = "username"; Settings.dbUsername = "username";
Settings.dbPassword = "password"; Settings.dbPassword = "password";
*/
playersManager = new PlayersManager(this); playersManager = new PlayersManager(this);
islandsManager = new IslandsManager(this); islandsManager = new IslandsManager(this);
// Only load metrics if set to true in config // Only load metrics if set to true in config
@ -82,10 +83,12 @@ public class BSkyBlock extends JavaPlugin{
// Test: Create a random island and save it // Test: Create a random island and save it
// TODO: ideally this should be in a test class! // TODO: ideally this should be in a test class!
/* UUID owner = UUID.fromString("ddf561c5-72b6-4ec6-a7ea-8b50a893beb2");
Island island = islandsManager.createIsland(new Location(getServer().getWorld("world"),0,0,0,0,0), UUID.randomUUID());
Island island = islandsManager.createIsland(new Location(getServer().getWorld("world"),0,0,0,0,0), owner);
// Add members // Add members
Set<UUID> randomSet = new HashSet<UUID>(); Set<UUID> randomSet = new HashSet<UUID>();
island.addMember(owner);
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
randomSet.add(UUID.randomUUID()); randomSet.add(UUID.randomUUID());
island.addMember(UUID.randomUUID()); island.addMember(UUID.randomUUID());
@ -105,12 +108,17 @@ public class BSkyBlock extends JavaPlugin{
getLogger().info("DEBUG: ************ Finished saving, now loading *************"); getLogger().info("DEBUG: ************ Finished saving, now loading *************");
// TODO: Write loading code for MySQL
*
*/
playersManager.load(); playersManager.load();
islandsManager.load(); islandsManager.load();
Island loadedIsland = islandsManager.getIsland(owner);
getLogger().info("Island name = " + loadedIsland.getName());
getLogger().info("Island locked = " + loadedIsland.getLocked());
//getLogger().info("Random set = " + randomSet);
getLogger().info("Island coops = " + loadedIsland.getCoops());
// Save islands & players data asynchronously every X minutes // Save islands & players data asynchronously every X minutes
Settings.databaseBackupPeriod = 10 * 60 * 20; Settings.databaseBackupPeriod = 10 * 60 * 20;

View File

@ -48,7 +48,13 @@ public class IslandsManager {
spawn = null; spawn = null;
} }
/**
* Clear and reload all islands from database
*/
public void load(){ public void load(){
islands.clear();
islandsByUUID.clear();
spawn = null;
try { try {
for (Object island : handler.selectObjects()) { for (Object island : handler.selectObjects()) {
if (island instanceof Island) { if (island instanceof Island) {

View File

@ -210,7 +210,7 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
int index = 0; int index = 0;
boolean first = true; boolean first = true;
for (Type type : parameters) { for (Type type : parameters) {
plugin.getLogger().info("DEBUG: set type = " + type.getTypeName()); //plugin.getLogger().info("DEBUG: set type = " + type.getTypeName());
// first is used to add commas in the right place // first is used to add commas in the right place
if (first) if (first)
first = false; first = false;
@ -355,7 +355,7 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
} }
// Create the insertion // Create the insertion
int i = 0; int i = 0;
plugin.getLogger().info("DEBUG: insert Query " + insertQuery); //plugin.getLogger().info("DEBUG: insert Query " + insertQuery);
// Run through the fields in the class using introspection // Run through the fields in the class using introspection
for (Field field : type.getDeclaredFields()) { for (Field field : type.getDeclaredFields()) {
// Get the field's property descriptor // Get the field's property descriptor
@ -373,16 +373,19 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
propertyDescriptor.getPropertyType().equals(HashMap.class) || propertyDescriptor.getPropertyType().equals(HashMap.class) ||
propertyDescriptor.getPropertyType().equals(ArrayList.class)) { propertyDescriptor.getPropertyType().equals(ArrayList.class)) {
// Collection // Collection
// The table is cleared everytime the data is stored again // The table is cleared for this uniqueId every time the data is stored
String setSql = "DELETE FROM `" + type.getCanonicalName() + "." + field.getName() + "`;"; String clearTableSql = "DELETE FROM `" + type.getCanonicalName() + "." + field.getName() + "` WHERE uniqueId = ?";
PreparedStatement collStatement = connection.prepareStatement(clearTableSql);
collStatement.setString(1, uniqueId);
collStatement.execute();
// Insert into the table // Insert into the table
setSql += "INSERT INTO `" + type.getCanonicalName() + "." + field.getName() + "` (uniqueId, "; String setSql = "INSERT INTO `" + type.getCanonicalName() + "." + field.getName() + "` (uniqueId, ";
// Get the columns we are going to insert, just the names of them // Get the columns we are going to insert, just the names of them
setSql += getCollectionColumns(propertyDescriptor.getWriteMethod(), false, false) + ") "; setSql += getCollectionColumns(propertyDescriptor.getWriteMethod(), false, false) + ") ";
// Get all the ?'s for the columns // Get all the ?'s for the columns
setSql += "VALUES ('" + uniqueId + "'," + getCollectionColumns(propertyDescriptor.getWriteMethod(), true, false) + ")"; setSql += "VALUES ('" + uniqueId + "'," + getCollectionColumns(propertyDescriptor.getWriteMethod(), true, false) + ")";
// Prepare the statement // Prepare the statement
PreparedStatement collStatement = connection.prepareStatement(setSql); collStatement = connection.prepareStatement(setSql);
//plugin.getLogger().info("DEBUG: collection insert =" + setSql); //plugin.getLogger().info("DEBUG: collection insert =" + setSql);
// Do single dimension types (set and list) // Do single dimension types (set and list)
if (propertyDescriptor.getPropertyType().equals(Set.class) || if (propertyDescriptor.getPropertyType().equals(Set.class) ||
@ -400,7 +403,7 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
//} //}
// Set the value from ? to whatever it is // Set the value from ? to whatever it is
collStatement.setObject(1, setValue); collStatement.setObject(1, setValue);
plugin.getLogger().info("DEBUG: " + collStatement.toString()); //plugin.getLogger().info("DEBUG: " + collStatement.toString());
// Execute the SQL in the database // Execute the SQL in the database
collStatement.execute(); collStatement.execute();
} }
@ -455,7 +458,7 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
* @return the object to write to the database * @return the object to write to the database
*/ */
private Object serialize(Object value, Class<? extends Object> clazz) { private Object serialize(Object value, Class<? extends Object> clazz) {
plugin.getLogger().info("DEBUG: serialize - class is " + clazz.getTypeName()); //plugin.getLogger().info("DEBUG: serialize - class is " + clazz.getTypeName());
if (value == null) { if (value == null) {
// If the value is null to start, return null as a string // If the value is null to start, return null as a string
return "null"; return "null";
@ -576,6 +579,7 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
* @throws IntrospectionException * @throws IntrospectionException
* @throws InvocationTargetException * @throws InvocationTargetException
*/ */
@SuppressWarnings("unchecked")
private List<T> createObjects(ResultSet resultSet) private List<T> createObjects(ResultSet resultSet)
throws SecurityException, IllegalArgumentException, throws SecurityException, IllegalArgumentException,
SQLException, InstantiationException, SQLException, InstantiationException,
@ -608,91 +612,84 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
propertyDescriptor.getPropertyType().equals(HashMap.class) || propertyDescriptor.getPropertyType().equals(HashMap.class) ||
propertyDescriptor.getPropertyType().equals(ArrayList.class)) { propertyDescriptor.getPropertyType().equals(ArrayList.class)) {
// Collection // Collection
plugin.getLogger().info("DEBUG: Collection"); //plugin.getLogger().info("DEBUG: Collection");
// TODO Get the values from the subsidiary tables. // TODO Get the values from the subsidiary tables.
// value is just of type boolean right now // value is just of type boolean right now
String setSql = "SELECT ("; String setSql = "SELECT ";
// Get the columns, just the names of them, no ?'s or types // Get the columns, just the names of them, no ?'s or types
setSql += getCollectionColumns(method, false, false) + ") "; setSql += getCollectionColumns(method, false, false) + " ";
setSql += "FROM `" + type.getCanonicalName() + "." + field.getName() + "` "; setSql += "FROM `" + type.getCanonicalName() + "." + field.getName() + "` ";
// We will need to fill in the ? later with the unique id of the class from the database // We will need to fill in the ? later with the unique id of the class from the database
setSql += "WHERE uniqueID = ?"; setSql += "WHERE uniqueId = ?";
// Prepare the statement // Prepare the statement
PreparedStatement collStatement = connection.prepareStatement(setSql); PreparedStatement collStatement = connection.prepareStatement(setSql);
// Set the unique ID // Set the unique ID
collStatement.setObject(1, uniqueId); collStatement.setObject(1, uniqueId);
plugin.getLogger().info("DEBUG: collStatement = " + collStatement.toString()); //plugin.getLogger().info("DEBUG: collStatement = " + collStatement.toString());
ResultSet collectionResultSet = collStatement.executeQuery(); ResultSet collectionResultSet = collStatement.executeQuery();
plugin.getLogger().info("DEBUG: collectionResultSet = " + collectionResultSet); //plugin.getLogger().info("DEBUG: collectionResultSet = " + collectionResultSet.toString());
// Do single dimension types (set and list) // Do single dimension types (set and list)
if (propertyDescriptor.getPropertyType().equals(Set.class)) { if (propertyDescriptor.getPropertyType().equals(Set.class)) {
plugin.getLogger().info("DEBUG: adding a set"); //plugin.getLogger().info("DEBUG: adding a set");
// Loop through the collection resultset // Loop through the collection resultset
// Note that we have no idea what type this is // Note that we have no idea what type this is
List<Type> collectionTypes = getCollectionParameterTypes(method); List<Type> collectionTypes = getCollectionParameterTypes(method);
// collectionTypes should be only 1 long // collectionTypes should be only 1 long
Type setType = collectionTypes.get(0); Type setType = collectionTypes.get(0);
Collection<Object> collection = new HashSet<Object>(); value = new HashSet<Object>();
plugin.getLogger().info("DEBUG: collection type argument = " + collectionTypes); //plugin.getLogger().info("DEBUG: collection type argument = " + collectionTypes);
int i = 0; //plugin.getLogger().info("DEBUG: setType = " + setType.getTypeName());
while (collectionResultSet.next()) { while (collectionResultSet.next()) {
plugin.getLogger().info("DEBUG: adding to the collection"); //plugin.getLogger().info("DEBUG: collectionResultSet size = " + collectionResultSet.getFetchSize());
collection.add(deserialize(collectionResultSet.getObject(i++),setType.getClass())); ((Set<Object>) value).add(deserialize(collectionResultSet.getObject(1),setType.getClass()));
} }
value.getClass().cast(HashSet.class);
value = collection;
} else if (propertyDescriptor.getPropertyType().equals(ArrayList.class)) { } else if (propertyDescriptor.getPropertyType().equals(ArrayList.class)) {
plugin.getLogger().info("DEBUG: Adding a list "); //plugin.getLogger().info("DEBUG: Adding a list ");
// Loop through the collection resultset // Loop through the collection resultset
// Note that we have no idea what type this is // Note that we have no idea what type this is
List<Type> collectionTypes = getCollectionParameterTypes(method); List<Type> collectionTypes = getCollectionParameterTypes(method);
// collectionTypes should be only 1 long // collectionTypes should be only 1 long
Type setType = collectionTypes.get(0); Type setType = collectionTypes.get(0);
Collection<Object> collection = new ArrayList<Object>(); value = new ArrayList<Object>();
plugin.getLogger().info("DEBUG: collection type argument = " + collectionTypes); //plugin.getLogger().info("DEBUG: collection type argument = " + collectionTypes);
int i = 0;
while (collectionResultSet.next()) { while (collectionResultSet.next()) {
plugin.getLogger().info("DEBUG: adding to the collection"); //plugin.getLogger().info("DEBUG: adding to the list");
collection.add(deserialize(collectionResultSet.getObject(i++),setType.getClass())); //plugin.getLogger().info("DEBUG: collectionResultSet size = " + collectionResultSet.getFetchSize());
((List<Object>) value).add(deserialize(collectionResultSet.getObject(1),setType.getClass()));
} }
value = collection;
} else if (propertyDescriptor.getPropertyType().equals(Map.class) || } else if (propertyDescriptor.getPropertyType().equals(Map.class) ||
propertyDescriptor.getPropertyType().equals(HashMap.class)) { propertyDescriptor.getPropertyType().equals(HashMap.class)) {
// Loop through the map //plugin.getLogger().info("DEBUG: Adding a map ");
/* // Loop through the collection resultset
Map<?,?> collection = (Map<?,?>)value; // Note that we have no idea what type this is
Iterator<?> it = collection.entrySet().iterator(); List<Type> collectionTypes = getCollectionParameterTypes(method);
while (it.hasNext()) { // collectionTypes should be 2 long
Entry<?,?> en = (Entry<?, ?>) it.next(); Type keyType = collectionTypes.get(0);
// Get the key and serialize it Type valueType = collectionTypes.get(1);
Object key = serialize(en.getKey(), en.getKey().getClass()); value = new HashMap<Object, Object>();
//plugin.getLogger().info("DEBUG: key class = " + en.getKey().getClass().getTypeName()); //plugin.getLogger().info("DEBUG: collection type argument = " + collectionTypes);
// Get the value and serialize it while (collectionResultSet.next()) {
Object mapValue = serialize(en.getValue(), en.getValue().getClass()); //plugin.getLogger().info("DEBUG: adding to the map");
// Write the objects into prepared statement //plugin.getLogger().info("DEBUG: collectionResultSet size = " + collectionResultSet.getFetchSize());
collStatement.setObject(1, key); // Work through the columns
collStatement.setObject(2, mapValue); // Key
//plugin.getLogger().info("DEBUG: " + collStatement.toString()); Object key = (deserialize(collectionResultSet.getObject(1),keyType.getClass()));
// Write to database //plugin.getLogger().info("DEBUG: key = " + key);
collStatement.execute(); Object mapValue = (deserialize(collectionResultSet.getObject(2),valueType.getClass()));
//plugin.getLogger().info("DEBUG: value = " + mapValue);
}*/ ((Map<Object,Object>) value).put(key,mapValue);
} }
} else {
// Set value for the main insert. For collections, this is just a dummy value because the real values are in the // Set value for the main insert. For collections, this is just a dummy value because the real values are in the
// additional table. // additional table.
value = true; value = true;
}
} else { } else {
plugin.getLogger().info("DEBUG: regular type"); //plugin.getLogger().info("DEBUG: regular type");
value = deserialize(value, propertyDescriptor.getPropertyType()); value = deserialize(value, propertyDescriptor.getPropertyType());
} }
plugin.getLogger().info("DEBUG: invoking method " + method.getName()); //plugin.getLogger().info("DEBUG: invoking method " + method.getName());
plugin.getLogger().info("DEBUG: value class = " + value.getClass().getName()); //plugin.getLogger().info("DEBUG: value class = " + value.getClass().getName());
// Write the value to the class // Write the value to the class
method.invoke(instance, value); method.invoke(instance, value);
} }
@ -709,9 +706,9 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
* @param clazz * @param clazz
* @return the deserialized value * @return the deserialized value
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings({ "unchecked", "rawtypes" })
private Object deserialize(Object value, Class<? extends Object> clazz) { private Object deserialize(Object value, Class<? extends Object> clazz) {
plugin.getLogger().info("DEBUG: deserialize - class is " + clazz.getTypeName()); //plugin.getLogger().info("DEBUG: deserialize - class is " + clazz.getTypeName());
if (value instanceof String && value.equals("null")) { if (value instanceof String && value.equals("null")) {
// If the value is null as a string, return null // If the value is null as a string, return null
return null; return null;

View File

@ -694,7 +694,7 @@ public class Island extends DataObject {
* @param members - the members to set * @param members - the members to set
*/ */
public void setMembers(Set<UUID> members){ public void setMembers(Set<UUID> members){
Bukkit.getLogger().info("DEBUG: setting members = " + members); //Bukkit.getLogger().info("DEBUG: setting members = " + members);
this.members = members; this.members = members;
} }