Skip to content

Commit

Permalink
Initial support for Moonrise
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Aug 27, 2024
1 parent 2af3389 commit db928a4
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ public interface SquaremapPlatform {
void stopCallback();

String version();

default boolean hasMod(final String id) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ServerTickEvent.Post> {
private long tick = 0;

Expand Down

0 comments on commit db928a4

Please sign in to comment.