9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-22 08:29:28 +00:00
Files
Leaf/patches/server/0101-Nitori-Async-playerdata-Save.patch
Dreeam 43fae827ad Add Mutltithreaded Tracker & Async playerdata saving (#109)
* init Multithreaded Tracker

* Rebase & Clean up

* Some clean up

* Some work

* Checked some petal issues

* Fix tracker

* Unify thread name again

* Nitori: Async playerdata Save

* Rebase

* Fix Citizens player type NPC tracking issue (WIP)

Temporary move sendChanges to off-main only. This can fix Citizens's player type NPC visible issue. But still working on making updatePlayer async too, since it also takes big part of performance, and also need to be compat with Citizens.

* Drop useless patch

* Adjust comments

* Optimize tracker, batch processing sendChanges tasks

* Clean up and fix

* Rebase

* Partial update player asynchronously & Fix citizens player type NPC visual issue

This made async tracker compat with CItizens, but still need to further optimize

* Optimize and update config

* Fix realPlayer detect condition & Made more async & Update patch comment

* Add compat mode for tracker

By isolating Citizens compat logic into compat mode, it can gain more performance if Citizens is not installed.

* Update comment
2024-09-06 13:44:53 -04:00

112 lines
4.8 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com>
Date: Fri, 23 Aug 2024 22:04:20 -0400
Subject: [PATCH] Nitori: Async playerdata Save
Original license: GPL v3
Original project: https://github.com/Gensokyo-Reimagined/Nitori
diff --git a/src/main/java/net/minecraft/world/level/storage/LevelStorageSource.java b/src/main/java/net/minecraft/world/level/storage/LevelStorageSource.java
index 85ba843ce7e1f62971e736fa2cc028c47b274ce4..7d018095f9cafbe727be41655742875bee2c028b 100644
--- a/src/main/java/net/minecraft/world/level/storage/LevelStorageSource.java
+++ b/src/main/java/net/minecraft/world/level/storage/LevelStorageSource.java
@@ -605,7 +605,11 @@ public class LevelStorageSource {
CompoundTag nbttagcompound2 = new CompoundTag();
nbttagcompound2.put("Data", nbttagcompound1);
- this.saveLevelData(nbttagcompound2);
+
+ // Leaf start - Nitori - Async playerdata save
+ Runnable runnable = () -> this.saveLevelData(nbttagcompound2);
+ org.dreeam.leaf.async.AsyncPlayerDataSaving.saveAsync(runnable);
+ // Leaf end - Nitori - Async playerdata save
}
private void saveLevelData(CompoundTag nbt) {
@@ -702,7 +706,11 @@ public class LevelStorageSource {
CompoundTag nbttagcompound = LevelStorageSource.readLevelDataTagRaw(this.levelDirectory.dataFile());
nbtProcessor.accept(nbttagcompound.getCompound("Data"));
- this.saveLevelData(nbttagcompound);
+
+ // Leaf start - Nitori - Async playerdata save
+ Runnable runnable = () -> this.saveLevelData(nbttagcompound);
+ org.dreeam.leaf.async.AsyncPlayerDataSaving.saveAsync(runnable);
+ // Leaf end - Nitori - Async playerdata save
}
public long makeWorldBackup() throws IOException {
diff --git a/src/main/java/net/minecraft/world/level/storage/PlayerDataStorage.java b/src/main/java/net/minecraft/world/level/storage/PlayerDataStorage.java
index b148cf247acdd36f856d0495cde4cc5ad32b5a2f..e825d9e573a38531f5a3b3f9cdccc24570953015 100644
--- a/src/main/java/net/minecraft/world/level/storage/PlayerDataStorage.java
+++ b/src/main/java/net/minecraft/world/level/storage/PlayerDataStorage.java
@@ -36,6 +36,13 @@ public class PlayerDataStorage {
public void save(Player player) {
if (org.spigotmc.SpigotConfig.disablePlayerDataSaving) return; // Spigot
+
+ // Leaf start - Nitori - Async playerdata save
+ Runnable runnable = () -> save0(player);
+ org.dreeam.leaf.async.AsyncPlayerDataSaving.saveAsync(runnable);
+ }
+ private void save0(Player player) {
+ // Leaf end - Nitori - Async playerdata save
try {
CompoundTag nbttagcompound = player.saveWithoutId(new CompoundTag());
Path path = this.playerDir.toPath();
diff --git a/src/main/java/org/dreeam/leaf/async/AsyncPlayerDataSaving.java b/src/main/java/org/dreeam/leaf/async/AsyncPlayerDataSaving.java
new file mode 100644
index 0000000000000000000000000000000000000000..6f74ca2f5bdae24434255976ec24f28c4980ac17
--- /dev/null
+++ b/src/main/java/org/dreeam/leaf/async/AsyncPlayerDataSaving.java
@@ -0,0 +1,23 @@
+package org.dreeam.leaf.async;
+
+import net.minecraft.Util;
+import org.dreeam.leaf.config.modules.async.AsyncPlayerDataSave;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+
+public class AsyncPlayerDataSaving {
+
+ private AsyncPlayerDataSaving() {
+ }
+
+ public static void saveAsync(Runnable runnable) {
+ if (!AsyncPlayerDataSave.enabled) {
+ runnable.run();
+ return;
+ }
+
+ ExecutorService ioExecutor = Util.backgroundExecutor();
+ CompletableFuture.runAsync(runnable, ioExecutor);
+ }
+}
diff --git a/src/main/java/org/dreeam/leaf/config/modules/async/AsyncPlayerDataSave.java b/src/main/java/org/dreeam/leaf/config/modules/async/AsyncPlayerDataSave.java
new file mode 100644
index 0000000000000000000000000000000000000000..f1d715723a641dc042a7f645398169e6f483700c
--- /dev/null
+++ b/src/main/java/org/dreeam/leaf/config/modules/async/AsyncPlayerDataSave.java
@@ -0,0 +1,20 @@
+package org.dreeam.leaf.config.modules.async;
+
+import org.dreeam.leaf.config.ConfigModules;
+import org.dreeam.leaf.config.EnumConfigCategory;
+
+public class AsyncPlayerDataSave extends ConfigModules {
+
+ public String getBasePath() {
+ return EnumConfigCategory.ASYNC.getBaseKeyName() + ".async-playerdata-save";
+ }
+
+ public static boolean enabled = true;
+
+ @Override
+ public void onLoaded() {
+ config.addComment(getBasePath(), "Make PlayerData saving asynchronously.");
+
+ enabled = config().getBoolean(getBasePath() + ".enabled", enabled);
+ }
+}