ソースを参照

Save users asynchronously

riking 11 年 前
コミット
4fb4d6fc0c

+ 3 - 3
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) {

+ 1 - 1
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;
         }
 

+ 1 - 1
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();
         }
     }
 

+ 1 - 1
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;
         }
 

+ 1 - 1
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;
         }
 

+ 9 - 2
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");

+ 1 - 1
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);
     }

+ 1 - 0
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();

+ 2 - 1
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();

+ 1 - 1
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();