| 1 | package net.minecraft.world.level.chunk.status;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
|
| 5 | import java.util.EnumSet;
|
| 6 | import java.util.Set;
|
| 7 | import java.util.concurrent.CompletableFuture;
|
| 8 | import net.minecraft.SharedConstants;
|
| 9 | import net.minecraft.core.Holder;
|
| 10 | import net.minecraft.server.level.GenerationChunkHolder;
|
| 11 | import net.minecraft.server.level.ServerLevel;
|
| 12 | import net.minecraft.server.level.ThreadedLevelLightEngine;
|
| 13 | import net.minecraft.server.level.WorldGenRegion;
|
| 14 | import net.minecraft.util.ProblemReporter;
|
| 15 | import net.minecraft.util.StaticCache2D;
|
| 16 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 17 | import net.minecraft.world.entity.EntityType;
|
| 18 | import net.minecraft.world.level.ChunkPos;
|
| 19 | import net.minecraft.world.level.biome.Biome;
|
| 20 | import net.minecraft.world.level.chunk.ChunkAccess;
|
| 21 | import net.minecraft.world.level.chunk.ImposterProtoChunk;
|
| 22 | import net.minecraft.world.level.chunk.LevelChunk;
|
| 23 | import net.minecraft.world.level.chunk.ProtoChunk;
|
| 24 | import net.minecraft.world.level.levelgen.BelowZeroRetrogen;
|
| 25 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 26 | import net.minecraft.world.level.levelgen.blending.Blender;
|
| 27 | import net.minecraft.world.level.storage.TagValueInput;
|
| 28 | import net.minecraft.world.level.storage.ValueInput;
|
| 29 | import org.slf4j.Logger;
|
| 30 |
|
| 31 | public class ChunkStatusTasks {
|
| 32 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 33 |
|
| 34 | private static boolean isLighted(final ChunkAccess chunk) {
|
| 35 | return chunk.getPersistedStatus().isOrAfter(ChunkStatus.LIGHT) && chunk.isLightCorrect();
|
| 36 | }
|
| 37 |
|
| 38 | public static CompletableFuture<ChunkAccess> passThrough(
|
| 39 | final WorldGenContext context, final ChunkStep step, final StaticCache2D<GenerationChunkHolder> chunks, final ChunkAccess chunk
|
| 40 | ) {
|
| 41 | return CompletableFuture.completedFuture(chunk);
|
| 42 | }
|
| 43 |
|
| 44 | public static CompletableFuture<ChunkAccess> generateStructureStarts(
|
| 45 | final WorldGenContext context, final ChunkStep step, final StaticCache2D<GenerationChunkHolder> chunks, final ChunkAccess chunk
|
| 46 | ) {
|
| 47 | ServerLevel level = context.level();
|
| 48 | if (level.getServer().getWorldGenSettings().options().generateStructures()) {
|
| 49 | context.generator()
|
| 50 | .createStructures(
|
| 51 | level.registryAccess(), level.getChunkSource().getGeneratorState(), level.structureManager(), chunk, context.structureManager(), level.dimension()
|
| 52 | );
|
| 53 | }
|
| 54 |
|
| 55 | level.onStructureStartsAvailable(chunk);
|
| 56 | return CompletableFuture.completedFuture(chunk);
|
| 57 | }
|
| 58 |
|
| 59 | public static CompletableFuture<ChunkAccess> loadStructureStarts(
|
| 60 | final WorldGenContext context, final ChunkStep step, final StaticCache2D<GenerationChunkHolder> cache, final ChunkAccess chunk
|
| 61 | ) {
|
| 62 | context.level().onStructureStartsAvailable(chunk);
|
| 63 | return CompletableFuture.completedFuture(chunk);
|
| 64 | }
|
| 65 |
|
| 66 | public static CompletableFuture<ChunkAccess> generateStructureReferences(
|
| 67 | final WorldGenContext context, final ChunkStep step, final StaticCache2D<GenerationChunkHolder> chunks, final ChunkAccess chunk
|
| 68 | ) {
|
| 69 | ServerLevel level = context.level();
|
| 70 | WorldGenRegion region = new WorldGenRegion(level, chunks, step, chunk);
|
| 71 | context.generator().createReferences(region, level.structureManager().forWorldGenRegion(region), chunk);
|
| 72 | return CompletableFuture.completedFuture(chunk);
|
| 73 | }
|
| 74 |
|
| 75 | public static CompletableFuture<ChunkAccess> generateBiomes(
|
| 76 | final WorldGenContext context, final ChunkStep step, final StaticCache2D<GenerationChunkHolder> chunks, final ChunkAccess chunk
|
| 77 | ) {
|
| 78 | ServerLevel level = context.level();
|
| 79 | WorldGenRegion region = new WorldGenRegion(level, chunks, step, chunk);
|
| 80 | return context.generator().createBiomes(level.getChunkSource().randomState(), Blender.of(region), level.structureManager().forWorldGenRegion(region), chunk);
|
| 81 | }
|
| 82 |
|
| 83 | private static Set<Holder<Biome>> collectPossibleBiomes(final WorldGenRegion region, final int chunkRadius) {
|
| 84 | Set<Holder<Biome>> chunkBiomes = new ObjectOpenHashSet<>();
|
| 85 | ChunkPos center = region.getCenter();
|
| 86 |
|
| 87 | for (int z = center.z() - chunkRadius; z <= center.z() + chunkRadius; z++) {
|
| 88 | for (int x = center.x() - chunkRadius; x <= center.x() + chunkRadius; x++) {
|
| 89 | region.getChunk(x, z).collectBiomesInPalette(chunkBiomes);
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | return chunkBiomes;
|
| 94 | }
|
| 95 |
|
| 96 | public static CompletableFuture<ChunkAccess> buildTerrain(
|
| 97 | final WorldGenContext context, final ChunkStep step, final StaticCache2D<GenerationChunkHolder> chunks, final ChunkAccess chunk
|
| 98 | ) {
|
| 99 | ServerLevel level = context.level();
|
| 100 | WorldGenRegion region = new WorldGenRegion(level, chunks, step, chunk);
|
| 101 | Set<Holder<Biome>> possibleBiomes = collectPossibleBiomes(region, 1);
|
| 102 | return context.generator()
|
| 103 | .buildTerrain(
|
| 104 | chunk,
|
| 105 | Blender.of(region),
|
| 106 | level.getChunkSource().randomState(),
|
| 107 | level.structureManager().forWorldGenRegion(region),
|
| 108 | region.getBiomeManager(),
|
| 109 | region,
|
| 110 | possibleBiomes
|
| 111 | )
|
| 112 | .thenApply(
|
| 113 | generatedChunk -> {
|
| 114 | if (generatedChunk instanceof ProtoChunk protoChunk) {
|
| 115 | BelowZeroRetrogen belowZeroRetrogen = protoChunk.getBelowZeroRetrogen();
|
| 116 | if (belowZeroRetrogen != null) {
|
| 117 | BelowZeroRetrogen.replaceOldBedrock(protoChunk);
|
| 118 | if (belowZeroRetrogen.hasBedrockHoles()) {
|
| 119 | belowZeroRetrogen.applyBedrockMask(protoChunk);
|
| 120 | }
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | Heightmap.primeHeightmaps(
|
| 125 | generatedChunk,
|
| 126 | EnumSet.of(Heightmap.Types.MOTION_BLOCKING, Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, Heightmap.Types.OCEAN_FLOOR, Heightmap.Types.WORLD_SURFACE)
|
| 127 | );
|
| 128 | return (ChunkAccess)generatedChunk;
|
| 129 | }
|
| 130 | );
|
| 131 | }
|
| 132 |
|
| 133 | public static CompletableFuture<ChunkAccess> generateFeatures(
|
| 134 | final WorldGenContext context, final ChunkStep step, final StaticCache2D<GenerationChunkHolder> chunks, final ChunkAccess chunk
|
| 135 | ) {
|
| 136 | ServerLevel level = context.level();
|
| 137 | WorldGenRegion region = new WorldGenRegion(level, chunks, step, chunk);
|
| 138 | if (!SharedConstants.DEBUG_DISABLE_FEATURES) {
|
| 139 | context.generator().applyBiomeDecoration(region, chunk, level.structureManager().forWorldGenRegion(region));
|
| 140 | }
|
| 141 |
|
| 142 | Blender.generateBorderTicks(region, chunk);
|
| 143 | return CompletableFuture.completedFuture(chunk);
|
| 144 | }
|
| 145 |
|
| 146 | public static CompletableFuture<ChunkAccess> initializeLight(
|
| 147 | final WorldGenContext context, final ChunkStep step, final StaticCache2D<GenerationChunkHolder> chunks, final ChunkAccess chunk
|
| 148 | ) {
|
| 149 | ThreadedLevelLightEngine lightEngine = context.lightEngine();
|
| 150 | chunk.initializeLightSources();
|
| 151 | ((ProtoChunk)chunk).setLightEngine(lightEngine);
|
| 152 | boolean lighted = isLighted(chunk);
|
| 153 | return lightEngine.initializeLight(chunk, lighted);
|
| 154 | }
|
| 155 |
|
| 156 | public static CompletableFuture<ChunkAccess> light(
|
| 157 | final WorldGenContext context, final ChunkStep step, final StaticCache2D<GenerationChunkHolder> chunks, final ChunkAccess chunk
|
| 158 | ) {
|
| 159 | boolean lighted = isLighted(chunk);
|
| 160 | return context.lightEngine().lightChunk(chunk, lighted);
|
| 161 | }
|
| 162 |
|
| 163 | public static CompletableFuture<ChunkAccess> generateSpawn(
|
| 164 | final WorldGenContext context, final ChunkStep step, final StaticCache2D<GenerationChunkHolder> chunks, final ChunkAccess chunk
|
| 165 | ) {
|
| 166 | if (!chunk.isUpgrading()) {
|
| 167 | context.generator().spawnOriginalMobs(new WorldGenRegion(context.level(), chunks, step, chunk));
|
| 168 | }
|
| 169 |
|
| 170 | return CompletableFuture.completedFuture(chunk);
|
| 171 | }
|
| 172 |
|
| 173 | public static CompletableFuture<ChunkAccess> full(
|
| 174 | final WorldGenContext context, final ChunkStep step, final StaticCache2D<GenerationChunkHolder> chunks, final ChunkAccess chunk
|
| 175 | ) {
|
| 176 | ChunkPos pos = chunk.getPos();
|
| 177 | GenerationChunkHolder holder = chunks.get(pos.x(), pos.z());
|
| 178 | return CompletableFuture.supplyAsync(() -> {
|
| 179 | ProtoChunk protoChunk = (ProtoChunk)chunk;
|
| 180 | ServerLevel level = context.level();
|
| 181 | LevelChunk levelChunk;
|
| 182 | if (protoChunk instanceof ImposterProtoChunk imposter) {
|
| 183 | levelChunk = imposter.getWrapped();
|
| 184 | } else {
|
| 185 | levelChunk = new LevelChunk(level, protoChunk, lc -> {
|
| 186 | try (ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(chunk.problemPath(), LOGGER)) {
|
| 187 | postLoadProtoChunk(level, TagValueInput.create(reporter, level.registryAccess(), protoChunk.getEntities()));
|
| 188 | }
|
| 189 | });
|
| 190 | holder.replaceProtoChunk(new ImposterProtoChunk(levelChunk, false));
|
| 191 | }
|
| 192 |
|
| 193 | levelChunk.setFullStatus(holder::getFullStatus);
|
| 194 | levelChunk.runPostLoad();
|
| 195 | levelChunk.setLoaded(true);
|
| 196 | levelChunk.registerAllBlockEntitiesAfterLevelLoad();
|
| 197 | levelChunk.registerTickContainerInLevel(level);
|
| 198 | levelChunk.setUnsavedListener(context.unsavedListener());
|
| 199 | return levelChunk;
|
| 200 | }, context.mainThreadExecutor());
|
| 201 | }
|
| 202 |
|
| 203 | private static void postLoadProtoChunk(final ServerLevel level, final ValueInput.ValueInputList entities) {
|
| 204 | if (!entities.isEmpty()) {
|
| 205 | level.addWorldGenChunkEntities(EntityType.loadEntitiesRecursive(entities, level, EntitySpawnReason.LOAD));
|
| 206 | }
|
| 207 | }
|
| 208 | }
|
| 209 |
|