Skip to content

Commit

Permalink
Consider below zero retrogen target status for chunk generated check (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla authored Sep 22, 2023
1 parent f9fafbe commit e078e68
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

import java.util.EnumMap;
import java.util.Map;
import java.util.Set;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelHeightAccessor;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.biome.BiomeManager;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.chunk.LevelChunkSection;
import net.minecraft.world.level.chunk.PalettedContainer;
import net.minecraft.world.level.dimension.DimensionType;
Expand All @@ -33,7 +35,7 @@ public interface ChunkSnapshot extends LevelHeightAccessor, BiomeManager.NoiseBi
boolean sectionEmpty(int sectionIndex);

@SuppressWarnings({"unchecked", "rawtypes"})
static ChunkSnapshot snapshot(final LevelChunk chunk, final boolean biomesOnly) {
static ChunkSnapshot snapshot(final Level level, final ChunkAccess chunk, final boolean biomesOnly) {
final LevelHeightAccessor heightAccessor = LevelHeightAccessor.create(chunk.getMinBuildHeight(), chunk.getHeight());
final int sectionCount = heightAccessor.getSectionsCount();
final LevelChunkSection[] sections = chunk.getSections();
Expand All @@ -44,7 +46,11 @@ static ChunkSnapshot snapshot(final LevelChunk chunk, final boolean biomesOnly)
Map<Heightmap.Types, HeightmapSnapshot> heightmaps = ChunkSnapshotImpl.EMPTY_HEIGHTMAPS;
if (!biomesOnly) {
if (!chunk.hasPrimedHeightmap(Heightmap.Types.WORLD_SURFACE)) {
throw new IllegalStateException("Expected WORLD_SURFACE heightmap to be present, but it wasn't! " + chunk.getPos());
if (chunk.getBelowZeroRetrogen() == null) {
throw new IllegalStateException("Expected WORLD_SURFACE heightmap to be present, but it wasn't! " + chunk.getPos());
} else {
Heightmap.primeHeightmaps(chunk, Set.of(Heightmap.Types.WORLD_SURFACE));
}
}
heightmaps = new EnumMap<>(ChunkSnapshotImpl.EMPTY_HEIGHTMAPS);
heightmaps.put(Heightmap.Types.WORLD_SURFACE, new HeightmapSnapshot(chunk, heightAccessor, Heightmap.Types.WORLD_SURFACE));
Expand Down Expand Up @@ -73,7 +79,7 @@ static ChunkSnapshot snapshot(final LevelChunk chunk, final boolean biomesOnly)
biomes,
heightmaps,
empty,
chunk.getLevel().dimensionType(),
level.dimensionType(),
chunk.getPos()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.chunk.ChunkStatus;
import net.minecraft.world.level.chunk.ImposterProtoChunk;
import net.minecraft.world.level.chunk.LevelChunk;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
Expand All @@ -24,38 +23,38 @@ record VanillaChunkSnapshotProvider(ServerLevel level) implements ChunkSnapshotP
@Override
public CompletableFuture<@Nullable ChunkSnapshot> asyncSnapshot(final int x, final int z) {
return CompletableFuture.supplyAsync(() -> {
final @Nullable LevelChunk chunk = fullChunkIfGenerated(this.level, x, z);
if (chunk == null || chunk.isEmpty()) {
final @Nullable ChunkAccess chunk = chunkIfGenerated(this.level, x, z);
if (chunk == null) {
return null;
}
return ChunkSnapshot.snapshot(chunk, false);
return ChunkSnapshot.snapshot(this.level, chunk, false);
}, this.level.getServer());
}

private static @Nullable LevelChunk fullChunkIfGenerated(final ServerLevel level, final int x, final int z) {
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;

final ChunkHolder visibleChunk = chunkMap.squaremap$getVisibleChunkIfPresent(chunkPos.toLong());
if (visibleChunk != null) {
final @Nullable LevelChunk chunk = fullIfPresent(visibleChunk);
final @Nullable ChunkAccess chunk = fullIfPresent(visibleChunk);
if (chunk != null) {
return chunk;
}
}

final ChunkHolder unloadingChunk = chunkMap.squaremap$pendingUnloads().get(chunkPos.toLong());
if (unloadingChunk != null) {
final @Nullable LevelChunk chunk = fullIfPresent(unloadingChunk);
final @Nullable ChunkAccess chunk = fullIfPresent(unloadingChunk);
if (chunk != null) {
return chunk;
}
}

final @Nullable CompoundTag chunkTag = chunkMap.squaremap$readChunk(chunkPos).join().orElse(null);
if (chunkTag != null && chunkTag.contains("Status", Tag.TAG_STRING)) {
if (isFullStatus(chunkTag)) {
@Nullable ChunkAccess chunk = level.getChunkSource()
if (isFullStatus(chunkTag) || preHeightChangeFullChunk(chunkTag)) {
final @Nullable ChunkAccess chunk = level.getChunkSource()
.getChunkFuture(x, z, ChunkStatus.EMPTY, true)
.join()
.left()
Expand All @@ -71,17 +70,40 @@ private static boolean isFullStatus(final CompoundTag chunkTag) {
return FULL.equals(ResourceLocation.tryParse(chunkTag.getString("Status")));
}

private static @Nullable LevelChunk fullIfPresent(final ChunkHolder chunkHolder) {
private static @Nullable ChunkAccess fullIfPresent(final ChunkHolder chunkHolder) {
return unwrap(chunkHolder.getLastAvailable());
}

private static @Nullable LevelChunk unwrap(@Nullable ChunkAccess chunk) {
if (chunk == null || !chunk.getStatus().isOrAfter(ChunkStatus.FULL)) {
private static @Nullable ChunkAccess unwrap(@Nullable ChunkAccess chunk) {
if (chunk == null) {
return null;
}
if (chunk instanceof ImposterProtoChunk imposter) {
chunk = imposter.getWrapped();
}
return (LevelChunk) chunk;
if (!chunk.getStatus().isOrAfter(ChunkStatus.FULL) && !preHeightChangeFullChunk(chunk)) {
return null;
}
return chunk;
}

private static boolean preHeightChangeFullChunk(final ChunkAccess chunk) {
return chunk.getBelowZeroRetrogen() != null && chunk.getBelowZeroRetrogen().targetStatus().isOrAfter(ChunkStatus.SPAWN);
}

private static boolean preHeightChangeFullChunk(final CompoundTag chunkTag) {
final CompoundTag belowZeroRetrogen = chunkTag.getCompound("below_zero_retrogen");
if (belowZeroRetrogen.isEmpty()) {
return false;
}
final String targetStatusStr = belowZeroRetrogen.getString("target_status");
if (targetStatusStr.isEmpty()) {
return false;
}
final @Nullable ResourceLocation targetStatus = ResourceLocation.tryParse(targetStatusStr);
if (targetStatus == null) {
return false;
}
return BuiltInRegistries.CHUNK_STATUS.get(targetStatus).isOrAfter(ChunkStatus.SPAWN);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.chunk.ChunkStatus;
import net.minecraft.world.level.chunk.ImposterProtoChunk;
import net.minecraft.world.level.chunk.LevelChunk;
import org.bukkit.Server;
import org.bukkit.plugin.java.JavaPlugin;
import org.checkerframework.checker.nullness.qual.NonNull;
Expand Down Expand Up @@ -46,16 +45,18 @@ record PaperChunkSnapshotProvider(
);
return load;
}, this.executor(x, z)).thenCompose(chunkFuture -> chunkFuture.thenApplyAsync(chunk -> {
if (chunk == null || !chunk.getStatus().isOrAfter(ChunkStatus.FULL)) {
if (chunk == null) {
return null;
}
if (chunk instanceof ImposterProtoChunk imposter) {
chunk = imposter.getWrapped();
}
if (chunk instanceof LevelChunk levelChunk && !levelChunk.isEmpty()) {
return ChunkSnapshot.snapshot(levelChunk, false);
if (!chunk.getStatus().isOrAfter(ChunkStatus.FULL)) {
if (chunk.getBelowZeroRetrogen() == null || !chunk.getBelowZeroRetrogen().targetStatus().isOrAfter(ChunkStatus.SPAWN)) {
return null;
}
}
return null;
return ChunkSnapshot.snapshot(this.level, chunk, false);
}, this.executor(x, z)));
}

Expand Down

0 comments on commit e078e68

Please sign in to comment.