Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial support for Moonrise on Fabric and NeoForge #283

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Initial support for Moonrise
  • Loading branch information
jpenilla committed Sep 4, 2024
commit 106f60a9353edd49c927cd475cd10ad469f0c0aa
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