From db928a460fa010123a89ac0bb58b8d20b31099dc Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Mon, 26 Aug 2024 18:27:27 -0700 Subject: [PATCH] Initial support for Moonrise --- .../squaremap/common/SquaremapPlatform.java | 4 +++ .../VanillaChunkSnapshotProvider.java | 27 ++++++++++++++++++- .../VanillaChunkSnapshotProviderFactory.java | 8 ++++-- .../squaremap/fabric/SquaremapFabric.java | 5 ++++ .../squaremap/forge/SquaremapForge.java | 6 +++++ 5 files changed, 47 insertions(+), 3 deletions(-) diff --git a/common/src/main/java/xyz/jpenilla/squaremap/common/SquaremapPlatform.java b/common/src/main/java/xyz/jpenilla/squaremap/common/SquaremapPlatform.java index 281344a0..7748b505 100644 --- a/common/src/main/java/xyz/jpenilla/squaremap/common/SquaremapPlatform.java +++ b/common/src/main/java/xyz/jpenilla/squaremap/common/SquaremapPlatform.java @@ -10,4 +10,8 @@ public interface SquaremapPlatform { void stopCallback(); String version(); + + default boolean hasMod(final String id) { + return false; + } } diff --git a/common/src/main/java/xyz/jpenilla/squaremap/common/util/chunksnapshot/VanillaChunkSnapshotProvider.java b/common/src/main/java/xyz/jpenilla/squaremap/common/util/chunksnapshot/VanillaChunkSnapshotProvider.java index b12a3c8a..cb098873 100644 --- a/common/src/main/java/xyz/jpenilla/squaremap/common/util/chunksnapshot/VanillaChunkSnapshotProvider.java +++ b/common/src/main/java/xyz/jpenilla/squaremap/common/util/chunksnapshot/VanillaChunkSnapshotProvider.java @@ -17,11 +17,14 @@ import xyz.jpenilla.squaremap.common.util.ChunkMapAccess; @DefaultQualifier(NonNull.class) -record VanillaChunkSnapshotProvider(ServerLevel level) implements ChunkSnapshotProvider { +record VanillaChunkSnapshotProvider(ServerLevel level, boolean moonrise) implements ChunkSnapshotProvider { private static final ResourceLocation FULL = BuiltInRegistries.CHUNK_STATUS.getKey(ChunkStatus.FULL); @Override public CompletableFuture<@Nullable ChunkSnapshot> asyncSnapshot(final int x, final int z) { + if (this.moonrise) { + return this.moonriseAsyncSnapshot(x, z); + } return CompletableFuture.supplyAsync(() -> { final @Nullable ChunkAccess chunk = chunkIfGenerated(this.level, x, z); if (chunk == null) { @@ -31,6 +34,28 @@ record VanillaChunkSnapshotProvider(ServerLevel level) implements ChunkSnapshotP }, this.level.getServer()); } + private CompletableFuture<@Nullable ChunkSnapshot> moonriseAsyncSnapshot(final int x, final int z) { + return CompletableFuture.supplyAsync(() -> { + final ChunkPos chunkPos = new ChunkPos(x, z); + final ChunkMapAccess chunkMap = (ChunkMapAccess) level.getChunkSource().chunkMap; + + final ChunkHolder visibleChunk = chunkMap.squaremap$getVisibleChunkIfPresent(chunkPos.toLong()); + if (visibleChunk != null) { + final @Nullable ChunkAccess chunk = fullIfPresent(visibleChunk); + if (chunk != null) { + return CompletableFuture.completedFuture(chunk); + } + } + + return this.level.getChunkSource().getChunkFuture(x, z, ChunkStatus.EMPTY, false).thenApply(result -> unwrap(result.orElse(null))); + }, this.level.getServer()).thenCompose(future -> future.thenApplyAsync(chunk -> { + if (chunk == null) { + return null; + } + return ChunkSnapshot.snapshot(this.level, chunk, false); + }, this.level.getServer())); + } + private static @Nullable ChunkAccess chunkIfGenerated(final ServerLevel level, final int x, final int z) { final ChunkPos chunkPos = new ChunkPos(x, z); final ChunkMapAccess chunkMap = (ChunkMapAccess) level.getChunkSource().chunkMap; diff --git a/common/src/main/java/xyz/jpenilla/squaremap/common/util/chunksnapshot/VanillaChunkSnapshotProviderFactory.java b/common/src/main/java/xyz/jpenilla/squaremap/common/util/chunksnapshot/VanillaChunkSnapshotProviderFactory.java index a0aad282..b2412e23 100644 --- a/common/src/main/java/xyz/jpenilla/squaremap/common/util/chunksnapshot/VanillaChunkSnapshotProviderFactory.java +++ b/common/src/main/java/xyz/jpenilla/squaremap/common/util/chunksnapshot/VanillaChunkSnapshotProviderFactory.java @@ -5,16 +5,20 @@ import net.minecraft.server.level.ServerLevel; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.framework.qual.DefaultQualifier; +import xyz.jpenilla.squaremap.common.SquaremapPlatform; @DefaultQualifier(NonNull.class) @Singleton public final class VanillaChunkSnapshotProviderFactory implements ChunkSnapshotProviderFactory { + private final SquaremapPlatform platform; + @Inject - private VanillaChunkSnapshotProviderFactory() { + private VanillaChunkSnapshotProviderFactory(final SquaremapPlatform platform) { + this.platform = platform; } @Override public ChunkSnapshotProvider createChunkSnapshotProvider(final ServerLevel level) { - return new VanillaChunkSnapshotProvider(level); + return new VanillaChunkSnapshotProvider(level, this.platform.hasMod("moonrise")); } } diff --git a/fabric/src/main/java/xyz/jpenilla/squaremap/fabric/SquaremapFabric.java b/fabric/src/main/java/xyz/jpenilla/squaremap/fabric/SquaremapFabric.java index 25a85f82..235faef7 100644 --- a/fabric/src/main/java/xyz/jpenilla/squaremap/fabric/SquaremapFabric.java +++ b/fabric/src/main/java/xyz/jpenilla/squaremap/fabric/SquaremapFabric.java @@ -102,6 +102,11 @@ public String version() { return this.modContainer.getMetadata().getVersion().getFriendlyString(); } + @Override + public boolean hasMod(final String id) { + return FabricLoader.getInstance().isModLoaded(id); + } + private final class TickEndListener implements ServerTickEvents.EndTick { private long tick = 0; diff --git a/neoforge/src/main/java/xyz/jpenilla/squaremap/forge/SquaremapForge.java b/neoforge/src/main/java/xyz/jpenilla/squaremap/forge/SquaremapForge.java index 02bc1567..957a6690 100644 --- a/neoforge/src/main/java/xyz/jpenilla/squaremap/forge/SquaremapForge.java +++ b/neoforge/src/main/java/xyz/jpenilla/squaremap/forge/SquaremapForge.java @@ -8,6 +8,7 @@ import net.neoforged.bus.api.EventPriority; import net.neoforged.bus.api.IEventBus; import net.neoforged.fml.ModContainer; +import net.neoforged.fml.ModList; import net.neoforged.fml.common.Mod; import net.neoforged.neoforge.common.NeoForge; import net.neoforged.neoforge.event.GameShuttingDownEvent; @@ -106,6 +107,11 @@ public String version() { return this.container.getModInfo().getVersion().toString(); } + @Override + public boolean hasMod(final String id) { + return ModList.get().isLoaded(id); + } + private final class TickEndListener implements Consumer { private long tick = 0;