From 14d5642335bc046af4cb8c3f5c94b4efbb3c5d06 Mon Sep 17 00:00:00 2001 From: riking Date: Sat, 5 Oct 2013 15:18:10 -0700 Subject: [PATCH] Do not mark a PlayerProfile as clean if it failed to save DatabaseManager.saveUser() now returns a success value, which is used by PlayerProfile to determine whether or not it should be considered clean. --- .../nossr50/database/DatabaseManager.java | 3 +- .../database/FlatfileDatabaseManager.java | 9 ++--- .../nossr50/database/SQLDatabaseManager.java | 35 ++++++++++++------- .../datatypes/player/PlayerProfile.java | 6 ++-- 4 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/database/DatabaseManager.java b/src/main/java/com/gmail/nossr50/database/DatabaseManager.java index 9ff732c92..d918d2373 100644 --- a/src/main/java/com/gmail/nossr50/database/DatabaseManager.java +++ b/src/main/java/com/gmail/nossr50/database/DatabaseManager.java @@ -34,8 +34,9 @@ public interface DatabaseManager { * Save a user to the database. * * @param profile The profile of the player to save + * @return true if successful, false on failure */ - public void saveUser(PlayerProfile profile); + public boolean saveUser(PlayerProfile profile); /** * Retrieve leaderboard info. diff --git a/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java b/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java index 28097a5d3..f98825d45 100644 --- a/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java +++ b/src/main/java/com/gmail/nossr50/database/FlatfileDatabaseManager.java @@ -203,7 +203,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager { return worked; } - public void saveUser(PlayerProfile profile) { + public boolean saveUser(PlayerProfile profile) { String playerName = profile.getPlayerName(); BufferedReader in = null; @@ -272,9 +272,11 @@ public final class FlatfileDatabaseManager implements DatabaseManager { // Write the new file out = new FileWriter(usersFilePath); out.write(writer.toString()); + return true; } catch (Exception e) { e.printStackTrace(); + return false; } finally { tryClose(in); @@ -434,11 +436,6 @@ public final class FlatfileDatabaseManager implements DatabaseManager { } } - public boolean checkConnected() { - // Not implemented - return true; - } - public List getStoredUsers() { ArrayList users = new ArrayList(); BufferedReader in = null; diff --git a/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java b/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java index 46e589aa5..8d6311c87 100644 --- a/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java +++ b/src/main/java/com/gmail/nossr50/database/SQLDatabaseManager.java @@ -116,9 +116,9 @@ public final class SQLDatabaseManager implements DatabaseManager { return success; } - public void saveUser(PlayerProfile profile) { + public boolean saveUser(PlayerProfile profile) { if (!checkConnected()) { - return; + return false; } int userId = readId(profile.getPlayerName()); @@ -126,15 +126,15 @@ public final class SQLDatabaseManager implements DatabaseManager { newUser(profile.getPlayerName()); userId = readId(profile.getPlayerName()); if (userId == -1) { - mcMMO.p.getLogger().log(Level.WARNING, "Failed to save user " + profile.getPlayerName()); - return; + return false; } } + boolean success = true; MobHealthbarType mobHealthbarType = profile.getMobHealthbarType(); - saveLogin(userId, ((int) (System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR))); - saveHuds(userId, (mobHealthbarType == null ? Config.getInstance().getMobHealthbarDefault().toString() : mobHealthbarType.toString())); - saveLongs( + success &= saveLogin(userId, ((int) (System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR))); + success &= saveHuds(userId, (mobHealthbarType == null ? Config.getInstance().getMobHealthbarDefault().toString() : mobHealthbarType.toString())); + success &= saveLongs( "UPDATE " + tablePrefix + "cooldowns SET " + " mining = ?, woodcutting = ?, unarmed = ?" + ", herbalism = ?, excavation = ?, swords = ?" @@ -148,7 +148,7 @@ public final class SQLDatabaseManager implements DatabaseManager { profile.getSkillDATS(AbilityType.SERRATED_STRIKES), profile.getSkillDATS(AbilityType.SKULL_SPLITTER), profile.getSkillDATS(AbilityType.BLAST_MINING)); - saveIntegers( + success &= saveIntegers( "UPDATE " + tablePrefix + "skills SET " + " taming = ?, mining = ?, repair = ?, woodcutting = ?" + ", unarmed = ?, herbalism = ?, excavation = ?" @@ -167,7 +167,7 @@ public final class SQLDatabaseManager implements DatabaseManager { profile.getSkillLevel(SkillType.ACROBATICS), profile.getSkillLevel(SkillType.FISHING), userId); - saveIntegers( + success &= saveIntegers( "UPDATE " + tablePrefix + "experience SET " + " taming = ?, mining = ?, repair = ?, woodcutting = ?" + ", unarmed = ?, herbalism = ?, excavation = ?" @@ -186,6 +186,7 @@ public final class SQLDatabaseManager implements DatabaseManager { profile.getSkillXpLevel(SkillType.ACROBATICS), profile.getSkillXpLevel(SkillType.FISHING), userId); + return success; } public List readLeaderboard(String skillName, int pageNumber, int statsPerPage) { @@ -1023,7 +1024,7 @@ public final class SQLDatabaseManager implements DatabaseManager { } } - private void saveIntegers(String sql, int... args) { + private boolean saveIntegers(String sql, int... args) { PreparedStatement statement = null; try { @@ -1035,9 +1036,11 @@ public final class SQLDatabaseManager implements DatabaseManager { } statement.execute(); + return true; } catch (SQLException ex) { printErrors(ex); + return false; } finally { if (statement != null) { @@ -1051,7 +1054,7 @@ public final class SQLDatabaseManager implements DatabaseManager { } } - private void saveLongs(String sql, int id, long... args) { + private boolean saveLongs(String sql, int id, long... args) { PreparedStatement statement = null; try { @@ -1064,9 +1067,11 @@ public final class SQLDatabaseManager implements DatabaseManager { statement.setInt(i++, id); statement.execute(); + return true; } catch (SQLException ex) { printErrors(ex); + return false; } finally { if (statement != null) { @@ -1101,7 +1106,7 @@ public final class SQLDatabaseManager implements DatabaseManager { return id; } - private void saveLogin(int id, long login) { + private boolean saveLogin(int id, long login) { PreparedStatement statement = null; try { @@ -1109,9 +1114,11 @@ public final class SQLDatabaseManager implements DatabaseManager { statement.setLong(1, login); statement.setInt(2, id); statement.execute(); + return true; } catch (SQLException ex) { printErrors(ex); + return false; } finally { if (statement != null) { @@ -1125,7 +1132,7 @@ public final class SQLDatabaseManager implements DatabaseManager { } } - private void saveHuds(int userId, String mobHealthBar) { + private boolean saveHuds(int userId, String mobHealthBar) { PreparedStatement statement = null; try { @@ -1133,9 +1140,11 @@ public final class SQLDatabaseManager implements DatabaseManager { statement.setString(1, mobHealthBar); statement.setInt(2, userId); statement.execute(); + return true; } catch (SQLException ex) { printErrors(ex); + return false; } finally { if (statement != null) { diff --git a/src/main/java/com/gmail/nossr50/datatypes/player/PlayerProfile.java b/src/main/java/com/gmail/nossr50/datatypes/player/PlayerProfile.java index 30d74a628..e0a2138d8 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/player/PlayerProfile.java +++ b/src/main/java/com/gmail/nossr50/datatypes/player/PlayerProfile.java @@ -70,8 +70,10 @@ public class PlayerProfile { return; } - mcMMO.getDatabaseManager().saveUser(this); - changed = false; + changed = !mcMMO.getDatabaseManager().saveUser(this); + if (changed) { + mcMMO.p.getLogger().warning("PlayerProfile for " + playerName + " failed to save"); + } } public String getPlayerName() {