Save users asynchronously

This commit is contained in:
riking 2014-06-03 20:52:22 -07:00 committed by TfT_02
parent 6d719988bf
commit 4fb4d6fc0c
10 changed files with 21 additions and 12 deletions

View File

@ -422,12 +422,12 @@ public final class ExperienceAPI {
profile.addLevels(parentSkill, (levels / parentSkills.size())); profile.addLevels(parentSkill, (levels / parentSkills.size()));
} }
profile.save(); profile.scheduleAsyncSave();
return; return;
} }
profile.addLevels(skill, levels); profile.addLevels(skill, levels);
profile.save(); profile.scheduleAsyncSave();
} }
/** /**
@ -656,7 +656,7 @@ public final class ExperienceAPI {
PlayerProfile profile = getOfflineProfile(playerName); PlayerProfile profile = getOfflineProfile(playerName);
profile.addXp(skill, XP); profile.addXp(skill, XP);
profile.save(); profile.scheduleAsyncSave();
} }
private static PlayerProfile getOfflineProfile(String playerName) { private static PlayerProfile getOfflineProfile(String playerName) {

View File

@ -27,7 +27,7 @@ public class AddlevelsCommand extends ExperienceCommand {
profile.addLevels(skill, value); profile.addLevels(skill, value);
if (player == null) { if (player == null) {
profile.save(); profile.scheduleAsyncSave();
return; return;
} }

View File

@ -28,7 +28,7 @@ public class AddxpCommand extends ExperienceCommand {
} }
else { else {
profile.addXp(skill, value); profile.addXp(skill, value);
profile.save(); profile.scheduleAsyncSave();
} }
} }

View File

@ -29,7 +29,7 @@ public class MmoeditCommand extends ExperienceCommand {
profile.modifySkill(skill, value); profile.modifySkill(skill, value);
if (player == null) { if (player == null) {
profile.save(); profile.scheduleAsyncSave();
return; return;
} }

View File

@ -110,7 +110,7 @@ public class SkillresetCommand implements TabExecutor {
profile.modifySkill(skill, 0); profile.modifySkill(skill, 0);
if (player == null) { if (player == null) {
profile.save(); profile.scheduleAsyncSave();
return; return;
} }

View File

@ -11,6 +11,7 @@ import com.gmail.nossr50.datatypes.MobHealthbarType;
import com.gmail.nossr50.datatypes.experience.FormulaType; import com.gmail.nossr50.datatypes.experience.FormulaType;
import com.gmail.nossr50.datatypes.skills.AbilityType; import com.gmail.nossr50.datatypes.skills.AbilityType;
import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.runnables.player.PlayerProfileSaveTask;
import com.gmail.nossr50.skills.child.FamilyTree; import com.gmail.nossr50.skills.child.FamilyTree;
import com.gmail.nossr50.util.player.UserManager; import com.gmail.nossr50.util.player.UserManager;
@ -19,7 +20,7 @@ import com.google.common.collect.ImmutableMap;
public class PlayerProfile { public class PlayerProfile {
private final String playerName; private final String playerName;
private boolean loaded; private boolean loaded;
private boolean changed; private volatile boolean changed;
/* HUDs */ /* HUDs */
private MobHealthbarType mobHealthbarType; private MobHealthbarType mobHealthbarType;
@ -60,12 +61,18 @@ public class PlayerProfile {
loaded = true; loaded = true;
} }
public void scheduleAsyncSave() {
new PlayerProfileSaveTask(this).runTaskAsynchronously(mcMMO.p);
}
public void save() { public void save() {
if (!changed || !loaded) { if (!changed || !loaded) {
return; return;
} }
changed = !mcMMO.getDatabaseManager().saveUser(new PlayerProfile(playerName, ImmutableMap.copyOf(skills), ImmutableMap.copyOf(skillsXp), ImmutableMap.copyOf(abilityDATS), mobHealthbarType)); // TODO should this part be synchronized?
PlayerProfile profileCopy = new PlayerProfile(playerName, ImmutableMap.copyOf(skills), ImmutableMap.copyOf(skillsXp), ImmutableMap.copyOf(abilityDATS), mobHealthbarType);
changed = !mcMMO.getDatabaseManager().saveUser(profileCopy);
if (changed) { if (changed) {
mcMMO.p.getLogger().warning("PlayerProfile for " + playerName + " failed to save"); mcMMO.p.getLogger().warning("PlayerProfile for " + playerName + " failed to save");

View File

@ -363,7 +363,7 @@ public class PlayerListener implements Listener {
mcMMOPlayer.resetAbilityMode(); mcMMOPlayer.resetAbilityMode();
BleedTimerTask.bleedOut(player); BleedTimerTask.bleedOut(player);
mcMMOPlayer.getProfile().save(); mcMMOPlayer.getProfile().scheduleAsyncSave();
UserManager.remove(player); UserManager.remove(player);
ScoreboardManager.teardownPlayer(player); ScoreboardManager.teardownPlayer(player);
} }

View File

@ -205,6 +205,7 @@ public class mcMMO extends JavaPlugin {
try { try {
Alchemy.finishAllBrews(); // Finish all partially complete AlchemyBrewTasks to prevent vanilla brewing continuation on restart Alchemy.finishAllBrews(); // Finish all partially complete AlchemyBrewTasks to prevent vanilla brewing continuation on restart
UserManager.saveAll(); // Make sure to save player information if the server shuts down UserManager.saveAll(); // Make sure to save player information if the server shuts down
UserManager.clearAll();
PartyManager.saveParties(); // Save our parties PartyManager.saveParties(); // Save our parties
ScoreboardManager.teardownAll(); ScoreboardManager.teardownAll();
formulaManager.saveFormula(); formulaManager.saveFormula();

View File

@ -41,7 +41,8 @@ public class FormulaConversionTask extends BukkitRunnable {
} }
editValues(profile); editValues(profile);
profile.save(); // Since this is a temporary profile, we save it here. // Since this is a temporary profile, we save it here.
profile.scheduleAsyncSave();
} }
else { else {
profile = mcMMOPlayer.getProfile(); profile = mcMMOPlayer.getProfile();

View File

@ -47,7 +47,7 @@ public final class UserManager {
} }
/** /**
* Save all users. * Save all users ON THIS THREAD.
*/ */
public static void saveAll() { public static void saveAll() {
Player[] onlinePlayers = mcMMO.p.getServer().getOnlinePlayers(); Player[] onlinePlayers = mcMMO.p.getServer().getOnlinePlayers();