Skip to content

Commit

Permalink
fix: properly capture saving future for vanilla chunk serializer
Browse files Browse the repository at this point in the history
Fixes #371
  • Loading branch information
ishland committed Oct 22, 2024
1 parent f71c462 commit c8ce82c
Showing 1 changed file with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
package com.ishland.c2me.rewrites.chunksystem.mixin.serialization_sync;

import com.ishland.c2me.rewrites.chunksystem.common.NewChunkHolderVanillaInterface;
import com.ishland.c2me.base.mixin.access.IChunkHolder;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.world.ChunkHolder;
import net.minecraft.server.world.ServerChunkLoadingManager;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.util.math.ChunkPos;
import org.jetbrains.annotations.Nullable;
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.ModifyVariable;

import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;

@Mixin(ServerChunkLoadingManager.class)
public abstract class MixinThreadedAnvilChunkStorage {

@Shadow protected abstract @Nullable ChunkHolder getCurrentChunkHolder(long pos);

// it is the responsibility of the caller to make sure the chunk is accessible or in a safe state
@ModifyVariable(method = "save(Lnet/minecraft/world/chunk/Chunk;)Z", at = @At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/server/world/ServerChunkLoadingManager;setNbt(Lnet/minecraft/util/math/ChunkPos;Ljava/util/function/Supplier;)Ljava/util/concurrent/CompletableFuture;"))
private CompletableFuture<Void> submitSavingFuture(CompletableFuture<Void> future, Chunk chunk) {
ChunkHolder holder = this.getCurrentChunkHolder(chunk.getPos().toLong());
if (holder instanceof NewChunkHolderVanillaInterface vif) {
vif.getBackingHolder().submitOp(future);
@WrapOperation(method = "save(Lnet/minecraft/world/chunk/Chunk;)Z", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/world/ServerChunkLoadingManager;setNbt(Lnet/minecraft/util/math/ChunkPos;Ljava/util/function/Supplier;)Ljava/util/concurrent/CompletableFuture;"))
private CompletableFuture<Void> submitSavingFuture(ServerChunkLoadingManager instance, ChunkPos chunkPos, Supplier<NbtCompound> supplier, Operation<CompletableFuture<Void>> original) {
CompletableFuture<Void> ret = original.call(instance, chunkPos, supplier);
ChunkHolder holder = this.getCurrentChunkHolder(chunkPos.toLong());
if (holder != null) {
((IChunkHolder) holder).invokeCombineSavingFuture(ret);
}
return future;
return ret;
}

}

0 comments on commit c8ce82c

Please sign in to comment.