Ver código fonte

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.
riking 11 anos atrás
pai
commit
c9858dfd49

+ 2 - 1
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.

+ 3 - 6
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<String> getStoredUsers() {
         ArrayList<String> users = new ArrayList<String>();
         BufferedReader in = null;

+ 22 - 13
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<PlayerStat> 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) {

+ 4 - 2
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() {