diff --git a/src/main/java/com/gmail/nossr50/api/ExperienceAPI.java b/src/main/java/com/gmail/nossr50/api/ExperienceAPI.java index 0f15d57fb..3d85b2253 100644 --- a/src/main/java/com/gmail/nossr50/api/ExperienceAPI.java +++ b/src/main/java/com/gmail/nossr50/api/ExperienceAPI.java @@ -422,12 +422,12 @@ public final class ExperienceAPI { profile.addLevels(parentSkill, (levels / parentSkills.size())); } - profile.save(); + profile.scheduleAsyncSave(); return; } profile.addLevels(skill, levels); - profile.save(); + profile.scheduleAsyncSave(); } /** @@ -656,7 +656,7 @@ public final class ExperienceAPI { PlayerProfile profile = getOfflineProfile(playerName); profile.addXp(skill, XP); - profile.save(); + profile.scheduleAsyncSave(); } private static PlayerProfile getOfflineProfile(String playerName) { diff --git a/src/main/java/com/gmail/nossr50/commands/experience/AddlevelsCommand.java b/src/main/java/com/gmail/nossr50/commands/experience/AddlevelsCommand.java index ccd9792e8..85121d3fe 100644 --- a/src/main/java/com/gmail/nossr50/commands/experience/AddlevelsCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/experience/AddlevelsCommand.java @@ -27,7 +27,7 @@ public class AddlevelsCommand extends ExperienceCommand { profile.addLevels(skill, value); if (player == null) { - profile.save(); + profile.scheduleAsyncSave(); return; } diff --git a/src/main/java/com/gmail/nossr50/commands/experience/AddxpCommand.java b/src/main/java/com/gmail/nossr50/commands/experience/AddxpCommand.java index 8fb063cf7..b4358f8be 100644 --- a/src/main/java/com/gmail/nossr50/commands/experience/AddxpCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/experience/AddxpCommand.java @@ -28,7 +28,7 @@ public class AddxpCommand extends ExperienceCommand { } else { profile.addXp(skill, value); - profile.save(); + profile.scheduleAsyncSave(); } } diff --git a/src/main/java/com/gmail/nossr50/commands/experience/MmoeditCommand.java b/src/main/java/com/gmail/nossr50/commands/experience/MmoeditCommand.java index 7b63eb598..6041e0576 100644 --- a/src/main/java/com/gmail/nossr50/commands/experience/MmoeditCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/experience/MmoeditCommand.java @@ -29,7 +29,7 @@ public class MmoeditCommand extends ExperienceCommand { profile.modifySkill(skill, value); if (player == null) { - profile.save(); + profile.scheduleAsyncSave(); return; } diff --git a/src/main/java/com/gmail/nossr50/commands/experience/SkillresetCommand.java b/src/main/java/com/gmail/nossr50/commands/experience/SkillresetCommand.java index bac4c6d39..e4adcd67d 100644 --- a/src/main/java/com/gmail/nossr50/commands/experience/SkillresetCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/experience/SkillresetCommand.java @@ -110,7 +110,7 @@ public class SkillresetCommand implements TabExecutor { profile.modifySkill(skill, 0); if (player == null) { - profile.save(); + profile.scheduleAsyncSave(); return; } 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 af1084249..7d1a3941f 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/player/PlayerProfile.java +++ b/src/main/java/com/gmail/nossr50/datatypes/player/PlayerProfile.java @@ -11,6 +11,7 @@ import com.gmail.nossr50.datatypes.MobHealthbarType; import com.gmail.nossr50.datatypes.experience.FormulaType; import com.gmail.nossr50.datatypes.skills.AbilityType; 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.util.player.UserManager; @@ -19,7 +20,7 @@ import com.google.common.collect.ImmutableMap; public class PlayerProfile { private final String playerName; private boolean loaded; - private boolean changed; + private volatile boolean changed; /* HUDs */ private MobHealthbarType mobHealthbarType; @@ -60,12 +61,18 @@ public class PlayerProfile { loaded = true; } + public void scheduleAsyncSave() { + new PlayerProfileSaveTask(this).runTaskAsynchronously(mcMMO.p); + } + public void save() { if (!changed || !loaded) { 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) { mcMMO.p.getLogger().warning("PlayerProfile for " + playerName + " failed to save"); diff --git a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java index d3cc069c9..6d6b76b80 100644 --- a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java @@ -363,7 +363,7 @@ public class PlayerListener implements Listener { mcMMOPlayer.resetAbilityMode(); BleedTimerTask.bleedOut(player); - mcMMOPlayer.getProfile().save(); + mcMMOPlayer.getProfile().scheduleAsyncSave(); UserManager.remove(player); ScoreboardManager.teardownPlayer(player); } diff --git a/src/main/java/com/gmail/nossr50/mcMMO.java b/src/main/java/com/gmail/nossr50/mcMMO.java index 68e0b5e59..d8f39813d 100644 --- a/src/main/java/com/gmail/nossr50/mcMMO.java +++ b/src/main/java/com/gmail/nossr50/mcMMO.java @@ -205,6 +205,7 @@ public class mcMMO extends JavaPlugin { try { 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.clearAll(); PartyManager.saveParties(); // Save our parties ScoreboardManager.teardownAll(); formulaManager.saveFormula(); diff --git a/src/main/java/com/gmail/nossr50/runnables/database/FormulaConversionTask.java b/src/main/java/com/gmail/nossr50/runnables/database/FormulaConversionTask.java index 801d802ea..ad00520ad 100644 --- a/src/main/java/com/gmail/nossr50/runnables/database/FormulaConversionTask.java +++ b/src/main/java/com/gmail/nossr50/runnables/database/FormulaConversionTask.java @@ -41,7 +41,8 @@ public class FormulaConversionTask extends BukkitRunnable { } 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 { profile = mcMMOPlayer.getProfile(); diff --git a/src/main/java/com/gmail/nossr50/util/player/UserManager.java b/src/main/java/com/gmail/nossr50/util/player/UserManager.java index 67ef76417..51dc76e4c 100644 --- a/src/main/java/com/gmail/nossr50/util/player/UserManager.java +++ b/src/main/java/com/gmail/nossr50/util/player/UserManager.java @@ -47,7 +47,7 @@ public final class UserManager { } /** - * Save all users. + * Save all users ON THIS THREAD. */ public static void saveAll() { Player[] onlinePlayers = mcMMO.p.getServer().getOnlinePlayers();