Implement asynchronous NBT saving

This commit is contained in:
DoggySazHi
2024-06-24 21:47:56 -07:00
parent e84af35838
commit a0794eaab9
4 changed files with 96 additions and 1 deletions

View File

@@ -0,0 +1,32 @@
package net.gensokyoreimagined.nitori.core;
import net.minecraft.Util;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.storage.PlayerDataStorage;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import java.util.concurrent.CompletableFuture;
@Mixin(CraftPlayer.class)
public class MixinCraftPlayer {
@Redirect(method = "saveData", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/storage/PlayerDataStorage;save(Lnet/minecraft/world/entity/player/Player;)V"))
private void gensouHacks$savePlayerData(PlayerDataStorage instance, Player path) {
Runnable writeRunnable = () -> {
var craftPlayer = (CraftPlayer) (Object) this;
var server = craftPlayer.getServer();
if (server instanceof CraftServer craftServer) {
var handle = craftPlayer.getHandle();
var playerIo = craftServer.getHandle().playerIo;
playerIo.save(handle);
}
};
var ioExecutor = Util.backgroundExecutor();
CompletableFuture.runAsync(writeRunnable, ioExecutor);
}
}

View File

@@ -0,0 +1,32 @@
package net.gensokyoreimagined.nitori.core;
import net.minecraft.Util;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.storage.LevelStorageSource;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import java.util.concurrent.CompletableFuture;
@Mixin(LevelStorageSource.LevelStorageAccess.class)
public abstract class MixinLevelStorageAccess {
@Shadow protected abstract void saveLevelData(CompoundTag nbt);
@Redirect(method = "modifyLevelDataWithoutDatafix", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/storage/LevelStorageSource$LevelStorageAccess;saveLevelData(Lnet/minecraft/nbt/CompoundTag;)V"))
private void gensouHacks$saveLevelData(LevelStorageSource.LevelStorageAccess levelStorageAccess, CompoundTag compoundTag) {
Runnable writeRunnable = () -> saveLevelData(compoundTag);
var ioExecutor = Util.backgroundExecutor();
CompletableFuture.runAsync(writeRunnable, ioExecutor);
}
@Redirect(method = "saveDataTag(Lnet/minecraft/core/RegistryAccess;Lnet/minecraft/world/level/storage/WorldData;Lnet/minecraft/nbt/CompoundTag;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/storage/LevelStorageSource$LevelStorageAccess;saveLevelData(Lnet/minecraft/nbt/CompoundTag;)V"))
private void gensouHacks$saveLevelData2(LevelStorageSource.LevelStorageAccess levelStorageAccess, CompoundTag compoundTag) {
Runnable writeRunnable = () -> saveLevelData(compoundTag);
var ioExecutor = Util.backgroundExecutor();
CompletableFuture.runAsync(writeRunnable, ioExecutor);
}
}

View File

@@ -0,0 +1,28 @@
package net.gensokyoreimagined.nitori.core;
import net.minecraft.Util;
import net.minecraft.server.players.PlayerList;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.storage.PlayerDataStorage;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import java.util.concurrent.CompletableFuture;
@Mixin(PlayerList.class)
public class MixinPlayerList {
@Final
@Shadow
public PlayerDataStorage playerIo;
@Redirect(method = "save", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/storage/PlayerDataStorage;save(Lnet/minecraft/world/entity/player/Player;)V"))
private void gensouHacks$savePlayerData(PlayerDataStorage instance, Player player) {
Runnable writeRunnable = () -> playerIo.save(player);
var ioExecutor = Util.backgroundExecutor();
CompletableFuture.runAsync(writeRunnable, ioExecutor);
}
}

View File

@@ -26,6 +26,9 @@
"MixinWorldGenRegion",
"ChunkMapMixin",
"ChunkMapMixin$TrackedEntity",
"MixinReobfServer"
"MixinReobfServer",
"MixinLevelStorageAccess",
"MixinPlayerList",
"MixinCraftPlayer"
]
}