Skip to content

Commit

Permalink
Implement dummy IO worker for compat with mods that directly call loa…
Browse files Browse the repository at this point in the history
…dAsync

This change is targeting Distant Horizons compatibility, however further changes are needed on their side to read starlight data.
  • Loading branch information
jpenilla committed Aug 22, 2024
1 parent 3f80f35 commit be74b67
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package ca.spottedleaf.moonrise.mixin.chunk_system;

import ca.spottedleaf.moonrise.patches.chunk_system.io.MoonriseRegionFileIO;
import ca.spottedleaf.moonrise.patches.chunk_system.storage.ChunkSystemChunkStorage;
import com.mojang.datafixers.DataFixer;
import com.mojang.logging.LogUtils;
import java.nio.file.Path;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.StreamTagVisitor;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.chunk.storage.ChunkScanAccess;
import net.minecraft.world.level.chunk.storage.ChunkStorage;
import net.minecraft.world.level.chunk.storage.IOWorker;
import net.minecraft.world.level.chunk.storage.RegionFileStorage;
import net.minecraft.world.level.chunk.storage.RegionStorageInfo;
import net.minecraft.world.level.levelgen.structure.LegacyStructureDataHandler;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
Expand All @@ -35,6 +41,14 @@ abstract class ChunkStorageMixin implements ChunkSystemChunkStorage, AutoCloseab
@Unique
private RegionFileStorage storage;

@Unique
private ServerLevel level;

@Override
public void moonrise$setLevel(final ServerLevel level) {
this.level = level;
}

/**
* @reason Destroy old IO worker field after retrieving region storage from it
* @author Spottedleaf
Expand All @@ -45,9 +59,53 @@ abstract class ChunkStorageMixin implements ChunkSystemChunkStorage, AutoCloseab
value = "RETURN"
)
)
private void initHook(final CallbackInfo ci) {
private void initHook(final RegionStorageInfo arg, final Path path, final DataFixer dataFixer, final boolean bl, final CallbackInfo ci) {
this.storage = this.worker.storage;
this.worker = null;
// Dummy impl for mods that try to loadAsync directly
this.worker = new IOWorker(arg, path, bl) {
@Override
public boolean isOldChunkAround(final ChunkPos chunkPos, final int i) {
throw new UnsupportedOperationException();
}

@Override
public CompletableFuture<Void> store(final ChunkPos chunkPos, final @Nullable CompoundTag compoundTag) {
throw new UnsupportedOperationException();
}

@Override
public CompletableFuture<Optional<CompoundTag>> loadAsync(final ChunkPos chunkPos) {
final CompletableFuture<Optional<CompoundTag>> future = new CompletableFuture<>();
MoonriseRegionFileIO.loadDataAsync(ChunkStorageMixin.this.level, chunkPos.x, chunkPos.z, MoonriseRegionFileIO.RegionFileType.CHUNK_DATA, (tag, throwable) -> {
if (throwable != null) {
future.completeExceptionally(throwable);
} else {
future.complete(Optional.ofNullable(tag));
}
}, false);
return future;
}

@Override
public CompletableFuture<Void> synchronize(final boolean bl) {
throw new UnsupportedOperationException();
}

@Override
public CompletableFuture<Void> scanChunk(final ChunkPos chunkPos, final StreamTagVisitor streamTagVisitor) {
throw new UnsupportedOperationException();
}

@Override
public void close() throws IOException {
throw new UnsupportedOperationException();
}

@Override
public RegionStorageInfo storageInfo() {
throw new UnsupportedOperationException();
}
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public final class ChunkDataController extends MoonriseRegionFileIO.RegionDataCo
public ChunkDataController(final ServerLevel world, final ChunkTaskScheduler taskScheduler) {
super(MoonriseRegionFileIO.RegionFileType.CHUNK_DATA, taskScheduler.ioExecutor, taskScheduler.compressionExecutor);
this.world = world;
((ChunkSystemChunkStorage)this.world.getChunkSource().chunkMap).moonrise$setLevel(world);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package ca.spottedleaf.moonrise.patches.chunk_system.storage;

import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.chunk.storage.RegionFileStorage;

public interface ChunkSystemChunkStorage {

public RegionFileStorage moonrise$getRegionStorage();

public void moonrise$setLevel(final ServerLevel level);

}

0 comments on commit be74b67

Please sign in to comment.