Fixed MySQL bug for loading individual objects.

The code was not loading the uniqueId object.

Added a lot of debug code in to help debug.
This commit is contained in:
tastybento 2017-08-26 18:21:42 -07:00
parent f109c956bb
commit fe6159e87b
9 changed files with 119 additions and 39 deletions

View File

@ -46,6 +46,8 @@ public abstract class AbstractCommand implements CommandExecutor, TabCompleter {
private final boolean help;
private static final int MAX_PER_PAGE = 7;
private static final boolean DEBUG = false;
protected AbstractCommand(BSkyBlock plugin, String label, String[] aliases, boolean help) {
this.plugin = plugin;
this.argumentsMap = new LinkedHashMap<>();
@ -252,6 +254,8 @@ public abstract class AbstractCommand implements CommandExecutor, TabCompleter {
* @param sender
*/
private void checkForPlayer(CommandSender sender) {
if (DEBUG)
plugin.getLogger().info("DEBUG: checkForPlayer");
// Check if the command sender is a player or not
if (sender instanceof Player) {
isPlayer = true;
@ -262,9 +266,19 @@ public abstract class AbstractCommand implements CommandExecutor, TabCompleter {
}
// Check if the player is in a team or not and if so, grab the team leader's UUID
if (plugin.getPlayers().inTeam(playerUUID)) {
if (DEBUG)
plugin.getLogger().info("DEBUG: player in team");
inTeam = true;
teamLeaderUUID = plugin.getIslands().getTeamLeader(playerUUID);
if (DEBUG)
plugin.getLogger().info("DEBUG: team leader UUID = " + teamLeaderUUID);
teamMembers = plugin.getIslands().getMembers(teamLeaderUUID);
if (DEBUG) {
plugin.getLogger().info("DEBUG: teammembers = ");
for (UUID member: teamMembers) {
plugin.getLogger().info("DEBUG: " + member);
}
}
} else {
inTeam = false;
}

View File

@ -501,6 +501,8 @@ public class IslandCommand extends AbstractCommand {
Util.sendMessage(player, getLocale(sender).get("team.listingMembers"));
// Display members in the list
for (UUID m : teamMembers) {
if (DEBUG)
plugin.getLogger().info("DEBUG: member " + m);
if (teamLeaderUUID.equals(m)) {
Util.sendMessage(player, getLocale(sender).get("team.leader-color") + getPlayers().getName(m) + getLocale(sender).get("team.leader"));
} else {

View File

@ -49,7 +49,7 @@ import us.tastybento.bskyblock.util.Util;
*/
public class IslandsManager {
private static final boolean DEBUG = true;
private static final boolean DEBUG = false;
private static final boolean DEBUG2 = false;
private BSkyBlock plugin;
private BSBDatabase database;
@ -92,10 +92,20 @@ public class IslandsManager {
islandsByUUID.clear();
spawn = null;
try {
if (DEBUG)
plugin.getLogger().info("DEBUG: loading grid");
for (Island island : handler.loadObjects()) {
if (DEBUG)
plugin.getLogger().info("DEBUG: addin island at "+ island.getCenter());
islandsByLocation.put(island.getCenter(), island);
if (DEBUG)
plugin.getLogger().info("DEBUG: owner = " + island.getOwner());
islandsByUUID.put(island.getOwner(), island);
if (DEBUG)
plugin.getLogger().info("DEBUG: island has " + island.getMembers().size() + " members");
for (UUID member: island.getMembers()) {
if (DEBUG)
plugin.getLogger().info("DEBUG: " + member);
islandsByUUID.put(member, island);
}
addToGrid(island);

View File

@ -66,8 +66,12 @@ public class PlayersManager{
* @param async - if true, save async
*/
public void save(boolean async){
if (DEBUG)
plugin.getLogger().info("DEBUG: saving " + async);
Runnable save = () -> {
for(Players player : playerCache.values()){
if (DEBUG)
plugin.getLogger().info("DEBUG: saving player " + player.getPlayerName() + " "+ player.getUniqueId());
try {
handler.saveObject(player);
} catch (Exception e) {
@ -331,8 +335,10 @@ public class PlayersManager{
* @param name
*/
public void setPlayerName(UUID uniqueId, String name) {
if (DEBUG)
plugin.getLogger().info("DEBUG: Setting player name to " + name + " for " + uniqueId);
addPlayer(uniqueId);
playerCache.get(uniqueId).setPlayerN(name);
playerCache.get(uniqueId).setPlayerName(name);
//database.savePlayerName(name, uniqueId);
}
@ -344,10 +350,14 @@ public class PlayersManager{
* @return String - playerName
*/
public String getName(UUID playerUUID) {
if (DEBUG)
plugin.getLogger().info("DEBUG: Geting player name");
if (playerUUID == null) {
return "";
}
addPlayer(playerUUID);
if (DEBUG)
plugin.getLogger().info("DEBUG: name is " + playerCache.get(playerUUID).getPlayerName());
return playerCache.get(playerUUID).getPlayerName();
}
@ -595,11 +605,12 @@ public class PlayersManager{
*/
public void save(UUID playerUUID) {
if (playerCache.containsKey(playerUUID)) {
Players player = playerCache.get(playerUUID);
final Players player = playerCache.get(playerUUID);
try {
handler.saveObject(player);
if (DEBUG)
plugin.getLogger().info("DEBUG: " + playerUUID + " saved");
plugin.getLogger().info("DEBUG: saving player by uuid " + player.getPlayerName() + " " + playerUUID + " saved");
handler.saveObject(player);
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException | SecurityException
| InstantiationException | NoSuchMethodException

View File

@ -45,6 +45,7 @@ import us.tastybento.bskyblock.util.Util;
*/
public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
private static final boolean DEBUG = false;
/**
* Connection to the database
*/
@ -85,6 +86,9 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
mySQLmapping.put(HashMap.class.getTypeName(), "BOOL");
mySQLmapping.put(ArrayList.class.getTypeName(), "BOOL");
// Enums
mySQLmapping.put(Enum.class.getTypeName(), "VARCHAR(254)");
}
/**
@ -132,7 +136,11 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
// The SQL column name is the name of the field
String columnName = field.getName();
// Get the mapping for this field from the hashmap
String mapping = mySQLmapping.get(propertyDescriptor.getPropertyType().getTypeName());
String typeName = propertyDescriptor.getPropertyType().getTypeName();
if (propertyDescriptor.getPropertyType().isEnum()) {
typeName = "Enum";
}
String mapping = mySQLmapping.get(typeName);
// If it exists, then create the SQL
if (mapping != null) {
// Note that the column name must be enclosed in `'s because it may include reserved words.
@ -153,6 +161,8 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
//plugin.getLogger().info(setSql);
// Execute the statement
PreparedStatement collections = connection.prepareStatement(setSql);
if (DEBUG)
plugin.getLogger().info("DEBUG: collections prepared statement = " + collections.toString());
collections.executeUpdate();
}
} else {
@ -160,6 +170,7 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
// This should NOT be used in general because every type should be in the hashmap
sql += field.getName() + " VARCHAR(254),";
plugin.getLogger().severe("Unknown type! Hoping it'll fit in a string!");
plugin.getLogger().severe(propertyDescriptor.getPropertyType().getTypeName());
}
}
//plugin.getLogger().info("DEBUG: SQL before trim string = " + sql);
@ -168,6 +179,8 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
//plugin.getLogger().info("DEBUG: SQL string = " + sql);
// Prepare and execute the database statements
pstmt = connection.prepareStatement(sql);
if (DEBUG)
plugin.getLogger().info("DEBUG: pstmt = " + pstmt.toString());
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
@ -234,7 +247,6 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
if (createSchema) {
// If this is the schema, then guess the mapping type
columns += " VARCHAR(254)";
plugin.getLogger().warning("Unknown type! Hoping it'll fit in a string!");
}
}
}
@ -318,33 +330,40 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
Connection connection = null;
PreparedStatement preparedStatement = null;
if (DEBUG)
plugin.getLogger().info("DEBUG: saveObject ");
try {
// Try to connect to the database
connection = databaseConnecter.createConnection();
// insertQuery is created in super from the createInsertQuery() method
preparedStatement = connection.prepareStatement(insertQuery);
// Get the uniqueId. As each class extends DataObject, it must have this method in it.
Method getUniqueId = type.getMethod("getUniqueId");
String uniqueId = (String) getUniqueId.invoke(instance);
//plugin.getLogger().info("DEBUG: Unique Id = " + uniqueId);
PropertyDescriptor propertyDescriptor = new PropertyDescriptor("uniqueId", type);
Method getUniqueId = propertyDescriptor.getReadMethod();
final String uniqueId = (String) getUniqueId.invoke(instance);
if (DEBUG) {
plugin.getLogger().info("DEBUG: Unique Id = " + uniqueId);
}
if (uniqueId.isEmpty()) {
throw new SQLException("uniqueId is blank");
}
// Create the insertion
int i = 0;
//plugin.getLogger().info("DEBUG: insert Query " + insertQuery);
if (DEBUG)
plugin.getLogger().info("DEBUG: insert Query " + insertQuery);
// Run through the fields in the class using introspection
for (Field field : type.getDeclaredFields()) {
// Get the field's property descriptor
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(field.getName(), type);
propertyDescriptor = new PropertyDescriptor(field.getName(), type);
// Get the read method for this field
Method method = propertyDescriptor.getReadMethod();
//plugin.getLogger().info("DEBUG: Field = " + field.getName() + "(" + propertyDescriptor.getPropertyType().getTypeName() + ")");
if (DEBUG)
plugin.getLogger().info("DEBUG: Field = " + field.getName() + "(" + propertyDescriptor.getPropertyType().getTypeName() + ")");
//sql += "`" + field.getName() + "` " + mapping + ",";
// Invoke the read method to obtain the value from the class - this is the value we need to store in the database
Object value = method.invoke(instance);
if (DEBUG)
plugin.getLogger().info("DEBUG: value = " + value);
// Create set and map table inserts if this is a Collection
if (propertyDescriptor.getPropertyType().equals(Set.class) ||
propertyDescriptor.getPropertyType().equals(Map.class) ||
@ -356,6 +375,8 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
PreparedStatement collStatement = connection.prepareStatement(clearTableSql);
collStatement.setString(1, uniqueId);
collStatement.execute();
if (DEBUG)
plugin.getLogger().info("DEBUG: collStatement " + collStatement.toString());
// Insert into the table
String setSql = "INSERT INTO `" + type.getCanonicalName() + "." + field.getName() + "` (uniqueId, ";
// Get the columns we are going to insert, just the names of them
@ -364,7 +385,8 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
setSql += "VALUES ('" + uniqueId + "'," + getCollectionColumns(propertyDescriptor.getWriteMethod(), true, false) + ")";
// Prepare the statement
collStatement = connection.prepareStatement(setSql);
//plugin.getLogger().info("DEBUG: collection insert =" + setSql);
if (DEBUG)
plugin.getLogger().info("DEBUG: collection insert =" + setSql);
// Do single dimension types (set and list)
if (propertyDescriptor.getPropertyType().equals(Set.class) ||
propertyDescriptor.getPropertyType().equals(ArrayList.class)) {
@ -381,7 +403,8 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
//}
// Set the value from ? to whatever it is
collStatement.setObject(1, setValue);
//plugin.getLogger().info("DEBUG: " + collStatement.toString());
if (DEBUG)
plugin.getLogger().info("DEBUG: " + collStatement.toString());
// Execute the SQL in the database
collStatement.execute();
}
@ -400,7 +423,8 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
// Write the objects into prepared statement
collStatement.setObject(1, key);
collStatement.setObject(2, mapValue);
//plugin.getLogger().info("DEBUG: " + collStatement.toString());
if (DEBUG)
plugin.getLogger().info("DEBUG: " + collStatement.toString());
// Write to database
collStatement.execute();
}
@ -419,6 +443,8 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
// Add the statements to a batch
preparedStatement.addBatch();
// Execute
if (DEBUG)
plugin.getLogger().info("DEBUG: prepared statement = " + preparedStatement.toString());
preparedStatement.executeBatch();
} finally {
@ -497,6 +523,8 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
try {
connection = databaseConnecter.createConnection();
statement = connection.createStatement();
if (DEBUG)
plugin.getLogger().info("DEBUG: selectQuery = " + selectQuery);
resultSet = statement.executeQuery(selectQuery);
return createObjects(resultSet);
@ -518,11 +546,16 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
if (DEBUG)
plugin.getLogger().info("DEBUG: loading object for " + uniqueId);
try {
connection = databaseConnecter.createConnection();
statement = connection.createStatement();
resultSet = statement.executeQuery(selectQuery);
String query = "SELECT " + super.getColumns(false) + " FROM `" + type.getCanonicalName() + "` WHERE uniqueId = ? LIMIT 1";
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, uniqueId);
if (DEBUG)
plugin.getLogger().info("DEBUG: load Object query = " + preparedStatement.toString());
resultSet = preparedStatement.executeQuery();
List<T> result = createObjects(resultSet);
if (!result.isEmpty()) {
@ -605,22 +638,25 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
PreparedStatement collStatement = connection.prepareStatement(setSql);
// Set the unique ID
collStatement.setObject(1, uniqueId);
//plugin.getLogger().info("DEBUG: collStatement = " + collStatement.toString());
if (DEBUG)
plugin.getLogger().info("DEBUG: collStatement = " + collStatement.toString());
ResultSet collectionResultSet = collStatement.executeQuery();
//plugin.getLogger().info("DEBUG: collectionResultSet = " + collectionResultSet.toString());
// Do single dimension types (set and list)
if (propertyDescriptor.getPropertyType().equals(Set.class)) {
//plugin.getLogger().info("DEBUG: adding a set");
if (DEBUG)
plugin.getLogger().info("DEBUG: adding a set");
// Loop through the collection resultset
// Note that we have no idea what type this is
List<Type> collectionTypes = Util.getCollectionParameterTypes(method);
// collectionTypes should be only 1 long
Type setType = collectionTypes.get(0);
value = new HashSet<Object>();
//plugin.getLogger().info("DEBUG: collection type argument = " + collectionTypes);
//plugin.getLogger().info("DEBUG: setType = " + setType.getTypeName());
if (DEBUG) {
plugin.getLogger().info("DEBUG: collection type argument = " + collectionTypes);
plugin.getLogger().info("DEBUG: setType = " + setType.getTypeName());
}
while (collectionResultSet.next()) {
//plugin.getLogger().info("DEBUG: collectionResultSet size = " + collectionResultSet.getFetchSize());
((Set<Object>) value).add(deserialize(collectionResultSet.getObject(1),Class.forName(setType.getTypeName())));
}
} else if (propertyDescriptor.getPropertyType().equals(ArrayList.class)) {
@ -750,7 +786,8 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
// Second is the unique ID
preparedStatement.setString(1, uniqueId);
preparedStatement.addBatch();
plugin.getLogger().info("DEBUG: DELETE Query " + preparedStatement.toString());
if (DEBUG)
plugin.getLogger().info("DEBUG: DELETE Query " + preparedStatement.toString());
preparedStatement.executeBatch();
// Delete from any sub tables created from the object
// Run through the fields in the class using introspection
@ -768,7 +805,8 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
preparedStatement.setString(1, uniqueId);
preparedStatement.addBatch();
// Execute
plugin.getLogger().info("DEBUG: " + preparedStatement.toString());
if (DEBUG)
plugin.getLogger().info("DEBUG: " + preparedStatement.toString());
preparedStatement.executeBatch();
}
}
@ -784,16 +822,25 @@ public class MySQLDatabaseHandler<T> extends AbstractDatabaseHandler<T> {
*/
@Override
public boolean objectExits(String key) {
if (DEBUG)
plugin.getLogger().info("DEBUG: checking if " + key + " exists in the database");
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String query = "SELECT * FROM `" + type.getCanonicalName() + "` WHERE uniqueId = ?";
String query = "SELECT IF ( EXISTS( SELECT * FROM `" + type.getCanonicalName() + "` WHERE `uniqueId` = ?), 1, 0)";
//String query = "SELECT * FROM `" + type.getCanonicalName() + "` WHERE uniqueId = ?";
try {
connection = databaseConnecter.createConnection();
preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, key);
resultSet = preparedStatement.executeQuery();
return resultSet.next();
if (DEBUG)
plugin.getLogger().info("DEBUG: object exists sql " + preparedStatement.toString());
if (resultSet.next()) {
if (DEBUG)
plugin.getLogger().info("DEBUG: result is " + resultSet.getBoolean(1));
return resultSet.getBoolean(1);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {

View File

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

View File

@ -135,10 +135,6 @@ public class Players extends DataObject {
return playerName;
}
public void setPlayerN(String playerName) {
this.playerName = playerName;
}
/**
* @return the resetsLeft
*/

View File

@ -2,7 +2,6 @@ package us.tastybento.bskyblock.listeners;
import java.util.UUID;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;

View File

@ -65,12 +65,12 @@ public class SchematicsMgr {
// built-in island generation
schematics.put("default",new Schematic(plugin));
}
plugin.getLogger().info("Loaded default nether schematic");
//plugin.getLogger().info("Loaded default nether schematic");
} else {
// It exists, so load it
try {
schematics.put("default",new Schematic(plugin, schematicFile));
plugin.getLogger().info("Loaded default island schematic.");
//plugin.getLogger().info("Loaded default island schematic.");
} catch (IOException e) {
plugin.getLogger().severe("Could not load default schematic!");
e.printStackTrace();
@ -86,7 +86,7 @@ public class SchematicsMgr {
Schematic netherIsland = new Schematic(plugin, netherFile);
netherIsland.setVisible(false);
schematics.put("nether", netherIsland);
plugin.getLogger().info("Loaded default nether schematic.");
//plugin.getLogger().info("Loaded default nether schematic.");
} catch (IOException e) {
plugin.getLogger().severe("Could not load default nether schematic!");
e.printStackTrace();
@ -100,7 +100,7 @@ public class SchematicsMgr {
Schematic netherIsland = new Schematic(plugin, netherFile);
netherIsland.setVisible(false);
schematics.put("nether", netherIsland);
plugin.getLogger().info("Loaded default nether schematic.");
//plugin.getLogger().info("Loaded default nether schematic.");
} catch (IOException e) {
plugin.getLogger().severe("Could not load default nether schematic!");
e.printStackTrace();