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..91c7bafe45ac9247b26d9781dcb6b80b92fad2b8 --- /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 = false; + + @Override + public void onLoaded() { + config.addComment(getBasePath(), "Make PlayerData saving asynchronously."); + + enabled = config().getBoolean(getBasePath() + ".enabled", enabled); + } +}