From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Mon, 16 May 2016 20:47:41 -0400
Subject: [PATCH] Async GameProfileCache saving


diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index 1766cd6d83aee31800364402dbe4a439c56e63d0..b46cffe141921e499a47dac08318f167f21a5509 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -914,7 +914,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
         } catch (java.lang.InterruptedException ignored) {} // Paper
         if (org.spigotmc.SpigotConfig.saveUserCacheOnStopOnly) {
             MinecraftServer.LOGGER.info("Saving usercache.json");
-            this.getProfileCache().save();
+            this.getProfileCache().save(false); // Paper
         }
         // Spigot end
         io.papermc.paper.chunk.system.io.RegionFileIOThread.close(true); // Paper // Paper - rewrite chunk system
diff --git a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
index b77cbdd926cd54a4bae89e0d5a21e7d139bc0c63..f17e1621b498e70c8787dfe96dd819dc6e4b621f 100644
--- a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
+++ b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
@@ -242,7 +242,7 @@ public class DedicatedServer extends MinecraftServer implements ServerInterface
         }
 
         if (this.convertOldUsers()) {
-            this.getProfileCache().save();
+            this.getProfileCache().save(false); // Paper
         }
 
         if (!OldUsersConverter.serverReadyAfterUserconversion(this)) {
diff --git a/src/main/java/net/minecraft/server/players/GameProfileCache.java b/src/main/java/net/minecraft/server/players/GameProfileCache.java
index 7170f8cfd733708089495b51647659237bd10385..322e07adc556ee8131d40d89856574b185ee4c94 100644
--- a/src/main/java/net/minecraft/server/players/GameProfileCache.java
+++ b/src/main/java/net/minecraft/server/players/GameProfileCache.java
@@ -115,7 +115,7 @@ public class GameProfileCache {
         GameProfileCache.GameProfileInfo usercache_usercacheentry = new GameProfileCache.GameProfileInfo(profile, date);
 
         this.safeAdd(usercache_usercacheentry);
-        if( !org.spigotmc.SpigotConfig.saveUserCacheOnStopOnly ) this.save(); // Spigot - skip saving if disabled
+        if( !org.spigotmc.SpigotConfig.saveUserCacheOnStopOnly ) this.save(true); // Spigot - skip saving if disabled // Paper - async
     }
 
     private long getNextOperation() {
@@ -148,7 +148,7 @@ public class GameProfileCache {
         }
 
         if (flag && !org.spigotmc.SpigotConfig.saveUserCacheOnStopOnly) { // Spigot - skip saving if disabled
-            this.save();
+            this.save(true); // Paper
         }
 
         return optional;
@@ -260,7 +260,7 @@ public class GameProfileCache {
         return arraylist;
     }
 
-    public void save() {
+    public void save(boolean asyncSave) { // Paper
         JsonArray jsonarray = new JsonArray();
         DateFormat dateformat = GameProfileCache.createDateFormat();
 
@@ -268,6 +268,7 @@ public class GameProfileCache {
             jsonarray.add(GameProfileCache.writeGameProfile(usercache_usercacheentry, dateformat));
         });
         String s = this.gson.toJson(jsonarray);
+        Runnable save = () -> { // Paper
 
         try {
             BufferedWriter bufferedwriter = Files.newWriter(this.file, StandardCharsets.UTF_8);
@@ -292,6 +293,14 @@ public class GameProfileCache {
         } catch (IOException ioexception) {
             ;
         }
+        // Paper start
+        };
+        if (asyncSave) {
+            io.papermc.paper.util.MCUtil.scheduleAsyncTask(save);
+        } else {
+            save.run();
+        }
+        // Paper end
 
     }