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.
This commit is contained in:
riking 2013-10-05 15:18:10 -07:00 committed by TfT_02
parent f75fe3cc89
commit c9858dfd49
4 changed files with 31 additions and 22 deletions

View File

@ -34,8 +34,9 @@ public interface DatabaseManager {
* Save a user to the database. * Save a user to the database.
* *
* @param profile The profile of the player to save * @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. * Retrieve leaderboard info.

View File

@ -203,7 +203,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
return worked; return worked;
} }
public void saveUser(PlayerProfile profile) { public boolean saveUser(PlayerProfile profile) {
String playerName = profile.getPlayerName(); String playerName = profile.getPlayerName();
BufferedReader in = null; BufferedReader in = null;
@ -272,9 +272,11 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
// Write the new file // Write the new file
out = new FileWriter(usersFilePath); out = new FileWriter(usersFilePath);
out.write(writer.toString()); out.write(writer.toString());
return true;
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return false;
} }
finally { finally {
tryClose(in); tryClose(in);
@ -434,11 +436,6 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
} }
} }
public boolean checkConnected() {
// Not implemented
return true;
}
public List<String> getStoredUsers() { public List<String> getStoredUsers() {
ArrayList<String> users = new ArrayList<String>(); ArrayList<String> users = new ArrayList<String>();
BufferedReader in = null; BufferedReader in = null;

View File

@ -116,9 +116,9 @@ public final class SQLDatabaseManager implements DatabaseManager {
return success; return success;
} }
public void saveUser(PlayerProfile profile) { public boolean saveUser(PlayerProfile profile) {
if (!checkConnected()) { if (!checkConnected()) {
return; return false;
} }
int userId = readId(profile.getPlayerName()); int userId = readId(profile.getPlayerName());
@ -126,15 +126,15 @@ public final class SQLDatabaseManager implements DatabaseManager {
newUser(profile.getPlayerName()); newUser(profile.getPlayerName());
userId = readId(profile.getPlayerName()); userId = readId(profile.getPlayerName());
if (userId == -1) { if (userId == -1) {
mcMMO.p.getLogger().log(Level.WARNING, "Failed to save user " + profile.getPlayerName()); return false;
return;
} }
} }
boolean success = true;
MobHealthbarType mobHealthbarType = profile.getMobHealthbarType(); MobHealthbarType mobHealthbarType = profile.getMobHealthbarType();
saveLogin(userId, ((int) (System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR))); success &= saveLogin(userId, ((int) (System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR)));
saveHuds(userId, (mobHealthbarType == null ? Config.getInstance().getMobHealthbarDefault().toString() : mobHealthbarType.toString())); success &= saveHuds(userId, (mobHealthbarType == null ? Config.getInstance().getMobHealthbarDefault().toString() : mobHealthbarType.toString()));
saveLongs( success &= saveLongs(
"UPDATE " + tablePrefix + "cooldowns SET " "UPDATE " + tablePrefix + "cooldowns SET "
+ " mining = ?, woodcutting = ?, unarmed = ?" + " mining = ?, woodcutting = ?, unarmed = ?"
+ ", herbalism = ?, excavation = ?, swords = ?" + ", herbalism = ?, excavation = ?, swords = ?"
@ -148,7 +148,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
profile.getSkillDATS(AbilityType.SERRATED_STRIKES), profile.getSkillDATS(AbilityType.SERRATED_STRIKES),
profile.getSkillDATS(AbilityType.SKULL_SPLITTER), profile.getSkillDATS(AbilityType.SKULL_SPLITTER),
profile.getSkillDATS(AbilityType.BLAST_MINING)); profile.getSkillDATS(AbilityType.BLAST_MINING));
saveIntegers( success &= saveIntegers(
"UPDATE " + tablePrefix + "skills SET " "UPDATE " + tablePrefix + "skills SET "
+ " taming = ?, mining = ?, repair = ?, woodcutting = ?" + " taming = ?, mining = ?, repair = ?, woodcutting = ?"
+ ", unarmed = ?, herbalism = ?, excavation = ?" + ", unarmed = ?, herbalism = ?, excavation = ?"
@ -167,7 +167,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
profile.getSkillLevel(SkillType.ACROBATICS), profile.getSkillLevel(SkillType.ACROBATICS),
profile.getSkillLevel(SkillType.FISHING), profile.getSkillLevel(SkillType.FISHING),
userId); userId);
saveIntegers( success &= saveIntegers(
"UPDATE " + tablePrefix + "experience SET " "UPDATE " + tablePrefix + "experience SET "
+ " taming = ?, mining = ?, repair = ?, woodcutting = ?" + " taming = ?, mining = ?, repair = ?, woodcutting = ?"
+ ", unarmed = ?, herbalism = ?, excavation = ?" + ", unarmed = ?, herbalism = ?, excavation = ?"
@ -186,6 +186,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
profile.getSkillXpLevel(SkillType.ACROBATICS), profile.getSkillXpLevel(SkillType.ACROBATICS),
profile.getSkillXpLevel(SkillType.FISHING), profile.getSkillXpLevel(SkillType.FISHING),
userId); userId);
return success;
} }
public List<PlayerStat> readLeaderboard(String skillName, int pageNumber, int statsPerPage) { 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; PreparedStatement statement = null;
try { try {
@ -1035,9 +1036,11 @@ public final class SQLDatabaseManager implements DatabaseManager {
} }
statement.execute(); statement.execute();
return true;
} }
catch (SQLException ex) { catch (SQLException ex) {
printErrors(ex); printErrors(ex);
return false;
} }
finally { finally {
if (statement != null) { 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; PreparedStatement statement = null;
try { try {
@ -1064,9 +1067,11 @@ public final class SQLDatabaseManager implements DatabaseManager {
statement.setInt(i++, id); statement.setInt(i++, id);
statement.execute(); statement.execute();
return true;
} }
catch (SQLException ex) { catch (SQLException ex) {
printErrors(ex); printErrors(ex);
return false;
} }
finally { finally {
if (statement != null) { if (statement != null) {
@ -1101,7 +1106,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
return id; return id;
} }
private void saveLogin(int id, long login) { private boolean saveLogin(int id, long login) {
PreparedStatement statement = null; PreparedStatement statement = null;
try { try {
@ -1109,9 +1114,11 @@ public final class SQLDatabaseManager implements DatabaseManager {
statement.setLong(1, login); statement.setLong(1, login);
statement.setInt(2, id); statement.setInt(2, id);
statement.execute(); statement.execute();
return true;
} }
catch (SQLException ex) { catch (SQLException ex) {
printErrors(ex); printErrors(ex);
return false;
} }
finally { finally {
if (statement != null) { 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; PreparedStatement statement = null;
try { try {
@ -1133,9 +1140,11 @@ public final class SQLDatabaseManager implements DatabaseManager {
statement.setString(1, mobHealthBar); statement.setString(1, mobHealthBar);
statement.setInt(2, userId); statement.setInt(2, userId);
statement.execute(); statement.execute();
return true;
} }
catch (SQLException ex) { catch (SQLException ex) {
printErrors(ex); printErrors(ex);
return false;
} }
finally { finally {
if (statement != null) { if (statement != null) {

View File

@ -70,8 +70,10 @@ public class PlayerProfile {
return; return;
} }
mcMMO.getDatabaseManager().saveUser(this); changed = !mcMMO.getDatabaseManager().saveUser(this);
changed = false; if (changed) {
mcMMO.p.getLogger().warning("PlayerProfile for " + playerName + " failed to save");
}
} }
public String getPlayerName() { public String getPlayerName() {