Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
Save Json list asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
404Setup committed Aug 30, 2024
1 parent 395f3bc commit b6243a4
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions patches/server/0078-Akarin-Save-Json-list-asynchronously.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: 404Setup <153366651+404Setup@users.noreply.github.com>
Date: Fri, 30 Aug 2024 16:30:18 +0800
Subject: [PATCH] Akarin: Save Json list asynchronously

You can find the original code on https://github.com/Akarin-project/Akarin

diff --git a/src/main/java/net/minecraft/server/players/StoredUserList.java b/src/main/java/net/minecraft/server/players/StoredUserList.java
index c038da20b76c0b7b1c18471b20be01e849d29f3a..c5d000ac7595a78de98464aa689ad90ebe7ff94d 100644
--- a/src/main/java/net/minecraft/server/players/StoredUserList.java
+++ b/src/main/java/net/minecraft/server/players/StoredUserList.java
@@ -103,37 +103,43 @@ public abstract class StoredUserList<K, V extends StoredUserEntry<K>> {
}

public void save() throws IOException {
- this.removeExpired(); // Paper - remove expired values before saving
- JsonArray jsonarray = new JsonArray();
- Stream<JsonObject> stream = this.map.values().stream().map((jsonlistentry) -> { // CraftBukkit - decompile error
- JsonObject jsonobject = new JsonObject();
+ Runnable run = () -> {// Akarin - Save json list async
+ this.removeExpired(); // Paper - remove expired values before saving
+ JsonArray jsonarray = new JsonArray();
+ Stream<JsonObject> stream = this.map.values().stream().map((jsonlistentry) -> { // CraftBukkit - decompile error
+ JsonObject jsonobject = new JsonObject();

- Objects.requireNonNull(jsonlistentry);
- return (JsonObject) Util.make(jsonobject, jsonlistentry::serialize);
- });
+ Objects.requireNonNull(jsonlistentry);
+ return (JsonObject) Util.make(jsonobject, jsonlistentry::serialize);
+ });

- Objects.requireNonNull(jsonarray);
- stream.forEach(jsonarray::add);
- BufferedWriter bufferedwriter = Files.newWriter(this.file, StandardCharsets.UTF_8);
+ Objects.requireNonNull(jsonarray);
+ stream.forEach(jsonarray::add);
+ try {// Akarin - Save json list async
+ BufferedWriter bufferedwriter = Files.newWriter(this.file, StandardCharsets.UTF_8);

- try {
- StoredUserList.GSON.toJson(jsonarray, StoredUserList.GSON.newJsonWriter(bufferedwriter));
- } catch (Throwable throwable) {
- if (bufferedwriter != null) {
try {
+ StoredUserList.GSON.toJson(jsonarray, StoredUserList.GSON.newJsonWriter(bufferedwriter));
+ } catch (Throwable throwable) {
+ if (bufferedwriter != null) {
+ try {
+ bufferedwriter.close();
+ } catch (Throwable throwable1) {
+ throwable.addSuppressed(throwable1);
+ }
+ }
+ throw throwable;
+ }
+ if (bufferedwriter != null) {
bufferedwriter.close();
- } catch (Throwable throwable1) {
- throwable.addSuppressed(throwable1);
}
+ // Leaf start - Akarin - Save json list async
+ } catch (Exception e) {
+ StoredUserList.LOGGER.warn("Failed to async save " + this.file, e);
}
-
- throw throwable;
- }
-
- if (bufferedwriter != null) {
- bufferedwriter.close();
- }
-
+ };
+ io.papermc.paper.util.MCUtil.scheduleAsyncTask(run);
+ // Akarin end - Save json list async
}

public void load() throws IOException {

0 comments on commit b6243a4

Please sign in to comment.