Added some serialization to the read/write to MySQL tables.

Still a WIP. Needs to have the collections added and needs to be tested.
This commit is contained in:
tastybento 2017-05-27 16:55:53 -07:00
parent 6be730f747
commit 3cff4277b3
2 changed files with 105 additions and 2 deletions

View File

@ -30,6 +30,7 @@ import org.bukkit.World;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.database.AbstractDatabaseHandler;
import us.tastybento.bskyblock.database.DatabaseConnecter;
import us.tastybento.bskyblock.util.Util;
/**
*
@ -246,8 +247,34 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
Method method = propertyDescriptor
.getReadMethod();
plugin.getLogger().info("DEBUG: Field = " + field.getName() + "(" + propertyDescriptor.getPropertyType().getTypeName() + ")");
//sql += "`" + field.getName() + "` " + mapping + ",";
Object value = method.invoke(instance);
// Create set and map tables.
if (propertyDescriptor.getPropertyType().equals(Set.class) ||
propertyDescriptor.getPropertyType().equals(Map.class) ||
propertyDescriptor.getPropertyType().equals(HashMap.class) ||
propertyDescriptor.getPropertyType().equals(ArrayList.class)) {
// Collection
// TODO Set the values in the subsidiary tables.
value = true;
}
// Types that need to be serialized
if (propertyDescriptor.getPropertyType().equals(UUID.class)) {
value = ((UUID)value).toString();
}
// Bukkit Types
if (propertyDescriptor.getPropertyType().equals(Location.class)) {
// Serialize
value = Util.getStringLocation(((Location)value));
}
if (propertyDescriptor.getPropertyType().equals(World.class)) {
// Serialize - get the name
value = ((World)value).getName();
}
preparedStatement.setObject(++i, value);
}
@ -349,7 +376,29 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
field.getName(), type);
Method method = propertyDescriptor.getWriteMethod();
// Create set and map tables.
if (propertyDescriptor.getPropertyType().equals(Set.class) ||
propertyDescriptor.getPropertyType().equals(Map.class) ||
propertyDescriptor.getPropertyType().equals(HashMap.class) ||
propertyDescriptor.getPropertyType().equals(ArrayList.class)) {
// Collection
// TODO Set the values in the subsidiary tables.
value = null;
}
// Types that need to be serialized
if (propertyDescriptor.getPropertyType().equals(UUID.class)) {
value = UUID.fromString((String)value);
}
// Bukkit Types
if (propertyDescriptor.getPropertyType().equals(Location.class)) {
// Serialize
value = Util.getLocationString(((String)value));
}
if (propertyDescriptor.getPropertyType().equals(World.class)) {
// Serialize - get the name
value = plugin.getServer().getWorld((String)value);
}
method.invoke(instance, value);
}

View File

@ -2,7 +2,10 @@ package us.tastybento.bskyblock.util;
import java.lang.reflect.InvocationTargetException;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import us.tastybento.bskyblock.BSkyBlock;
@ -56,4 +59,55 @@ public class Util {
throw new IllegalStateException("Class " + clazz.getName() + " does not implement NMSAbstraction");
}
}
/**
* Converts a serialized location to a Location. Returns null if string is
* empty
*
* @param s
* - serialized location in format "world:x:y:z"
* @return Location
*/
static public Location getLocationString(final String s) {
if (s == null || s.trim() == "") {
return null;
}
final String[] parts = s.split(":");
if (parts.length == 4) {
final World w = Bukkit.getServer().getWorld(parts[0]);
if (w == null) {
return null;
}
final int x = Integer.parseInt(parts[1]);
final int y = Integer.parseInt(parts[2]);
final int z = Integer.parseInt(parts[3]);
return new Location(w, x, y, z);
} else if (parts.length == 6) {
final World w = Bukkit.getServer().getWorld(parts[0]);
if (w == null) {
return null;
}
final int x = Integer.parseInt(parts[1]);
final int y = Integer.parseInt(parts[2]);
final int z = Integer.parseInt(parts[3]);
final float yaw = Float.intBitsToFloat(Integer.parseInt(parts[4]));
final float pitch = Float.intBitsToFloat(Integer.parseInt(parts[5]));
return new Location(w, x, y, z, yaw, pitch);
}
return null;
}
/**
* Converts a location to a simple string representation
* If location is null, returns empty string
*
* @param location
* @return String of location
*/
static public String getStringLocation(final Location location) {
if (location == null || location.getWorld() == null) {
return "";
}
return location.getWorld().getName() + ":" + location.getBlockX() + ":" + location.getBlockY() + ":" + location.getBlockZ() + ":" + Float.floatToIntBits(location.getYaw()) + ":" + Float.floatToIntBits(location.getPitch());
}
}