| 1 | package net.minecraft.world.level.chunk.status;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import java.util.Arrays;
|
| 5 | import java.util.concurrent.CompletableFuture;
|
| 6 | import net.minecraft.server.level.GenerationChunkHolder;
|
| 7 | import net.minecraft.util.StaticCache2D;
|
| 8 | import net.minecraft.util.profiling.Profiler;
|
| 9 | import net.minecraft.util.profiling.Zone;
|
| 10 | import net.minecraft.util.profiling.jfr.JvmProfiler;
|
| 11 | import net.minecraft.util.profiling.jfr.callback.ProfiledDuration;
|
| 12 | import net.minecraft.world.level.chunk.ChunkAccess;
|
| 13 | import net.minecraft.world.level.chunk.ProtoChunk;
|
| 14 | import org.jspecify.annotations.Nullable;
|
| 15 |
|
| 16 | public record ChunkStep(
|
| 17 | ChunkStatus targetStatus, ChunkDependencies directDependencies, ChunkDependencies accumulatedDependencies, int blockStateWriteRadius, ChunkStatusTask task
|
| 18 | ) {
|
| 19 | public int getAccumulatedRadiusOf(final ChunkStatus status) {
|
| 20 | return status == this.targetStatus ? 0 : this.accumulatedDependencies.getRadiusOf(status);
|
| 21 | }
|
| 22 |
|
| 23 | public CompletableFuture<ChunkAccess> apply(final WorldGenContext context, final StaticCache2D<GenerationChunkHolder> cache, final ChunkAccess chunk) {
|
| 24 | if (chunk.getPersistedStatus().isBefore(this.targetStatus)) {
|
| 25 | String stepName = this.targetStatus.getName();
|
| 26 |
|
| 27 | try (Zone var5 = Profiler.get().zone(stepName)) {
|
| 28 | ProfiledDuration profiledDuration = JvmProfiler.INSTANCE.onChunkGenerate(chunk.getPos(), context.level().dimension(), stepName);
|
| 29 | return this.task.doWork(context, this, cache, chunk).thenApply(newCenterChunk -> this.completeChunkGeneration(newCenterChunk, profiledDuration));
|
| 30 | }
|
| 31 | } else {
|
| 32 | return this.task.doWork(context, this, cache, chunk);
|
| 33 | }
|
| 34 | }
|
| 35 |
|
| 36 | private ChunkAccess completeChunkGeneration(final ChunkAccess newCenterChunk, final @Nullable ProfiledDuration profiledDuration) {
|
| 37 | if (newCenterChunk instanceof ProtoChunk protochunk && protochunk.getPersistedStatus().isBefore(this.targetStatus)) {
|
| 38 | protochunk.setPersistedStatus(this.targetStatus);
|
| 39 | }
|
| 40 |
|
| 41 | if (profiledDuration != null) {
|
| 42 | profiledDuration.finish(true);
|
| 43 | }
|
| 44 |
|
| 45 | return newCenterChunk;
|
| 46 | }
|
| 47 |
|
| 48 | public static class Builder {
|
| 49 | private final ChunkStatus status;
|
| 50 | private final @Nullable ChunkStep parent;
|
| 51 | private ChunkStatus[] directDependenciesByRadius;
|
| 52 | private int blockStateWriteRadius = -1;
|
| 53 | private ChunkStatusTask task = ChunkStatusTasks::passThrough;
|
| 54 |
|
| 55 | protected Builder(final ChunkStatus status) {
|
| 56 | if (status.getParent() != status) {
|
| 57 | throw new IllegalArgumentException("Not starting with the first status: " + status);
|
| 58 | }
|
| 59 |
|
| 60 | this.status = status;
|
| 61 | this.parent = null;
|
| 62 | this.directDependenciesByRadius = new ChunkStatus[0];
|
| 63 | }
|
| 64 |
|
| 65 | protected Builder(final ChunkStatus status, final ChunkStep parent) {
|
| 66 | if (parent.targetStatus.getIndex() != status.getIndex() - 1) {
|
| 67 | throw new IllegalArgumentException("Out of order status: " + status);
|
| 68 | }
|
| 69 |
|
| 70 | this.status = status;
|
| 71 | this.parent = parent;
|
| 72 | this.directDependenciesByRadius = new ChunkStatus[]{parent.targetStatus};
|
| 73 | }
|
| 74 |
|
| 75 | public ChunkStep.Builder addRequirement(final ChunkStatus status, final int radius) {
|
| 76 | if (status.isOrAfter(this.status)) {
|
| 77 | throw new IllegalArgumentException("Status " + status + " can not be required by " + this.status);
|
| 78 | }
|
| 79 |
|
| 80 | ChunkStatus[] previous = this.directDependenciesByRadius;
|
| 81 | int newLength = radius + 1;
|
| 82 | if (newLength > previous.length) {
|
| 83 | this.directDependenciesByRadius = new ChunkStatus[newLength];
|
| 84 | Arrays.fill(this.directDependenciesByRadius, status);
|
| 85 | }
|
| 86 |
|
| 87 | for (int i = 0; i < Math.min(newLength, previous.length); i++) {
|
| 88 | this.directDependenciesByRadius[i] = ChunkStatus.max(previous[i], status);
|
| 89 | }
|
| 90 |
|
| 91 | return this;
|
| 92 | }
|
| 93 |
|
| 94 | public ChunkStep.Builder blockStateWriteRadius(final int radius) {
|
| 95 | this.blockStateWriteRadius = radius;
|
| 96 | return this;
|
| 97 | }
|
| 98 |
|
| 99 | public ChunkStep.Builder setTask(final ChunkStatusTask task) {
|
| 100 | this.task = task;
|
| 101 | return this;
|
| 102 | }
|
| 103 |
|
| 104 | public ChunkStep build() {
|
| 105 | return new ChunkStep(
|
| 106 | this.status,
|
| 107 | new ChunkDependencies(ImmutableList.copyOf(this.directDependenciesByRadius)),
|
| 108 | new ChunkDependencies(ImmutableList.copyOf(this.buildAccumulatedDependencies())),
|
| 109 | this.blockStateWriteRadius,
|
| 110 | this.task
|
| 111 | );
|
| 112 | }
|
| 113 |
|
| 114 | private ChunkStatus[] buildAccumulatedDependencies() {
|
| 115 | if (this.parent == null) {
|
| 116 | return this.directDependenciesByRadius;
|
| 117 | }
|
| 118 |
|
| 119 | int radiusOfParent = this.getRadiusOfParent(this.parent.targetStatus);
|
| 120 | ChunkDependencies parentDependencies = this.parent.accumulatedDependencies;
|
| 121 | ChunkStatus[] accumulatedDependencies = new ChunkStatus[Math.max(radiusOfParent + parentDependencies.size(), this.directDependenciesByRadius.length)];
|
| 122 |
|
| 123 | for (int distance = 0; distance < accumulatedDependencies.length; distance++) {
|
| 124 | int distanceInParent = distance - radiusOfParent;
|
| 125 | if (distanceInParent < 0 || distanceInParent >= parentDependencies.size()) {
|
| 126 | accumulatedDependencies[distance] = this.directDependenciesByRadius[distance];
|
| 127 | } else if (distance >= this.directDependenciesByRadius.length) {
|
| 128 | accumulatedDependencies[distance] = parentDependencies.get(distanceInParent);
|
| 129 | } else {
|
| 130 | accumulatedDependencies[distance] = ChunkStatus.max(this.directDependenciesByRadius[distance], parentDependencies.get(distanceInParent));
|
| 131 | }
|
| 132 | }
|
| 133 |
|
| 134 | return accumulatedDependencies;
|
| 135 | }
|
| 136 |
|
| 137 | private int getRadiusOfParent(final ChunkStatus status) {
|
| 138 | for (int i = this.directDependenciesByRadius.length - 1; i >= 0; i--) {
|
| 139 | if (this.directDependenciesByRadius[i].isOrAfter(status)) {
|
| 140 | return i;
|
| 141 | }
|
| 142 | }
|
| 143 |
|
| 144 | return 0;
|
| 145 | }
|
| 146 | }
|
| 147 | }
|
| 148 |
|