| 1 | package net.minecraft.server.level.progress;
|
| 2 |
|
| 3 | import net.minecraft.resources.ResourceKey;
|
| 4 | import net.minecraft.util.Mth;
|
| 5 | import net.minecraft.world.level.ChunkPos;
|
| 6 | import net.minecraft.world.level.Level;
|
| 7 |
|
| 8 | public class LevelLoadProgressTracker implements LevelLoadListener {
|
| 9 | private static final int PREPARE_SERVER_WEIGHT = 10;
|
| 10 | private static final int EXPECTED_PLAYER_CHUNKS = Mth.square(7);
|
| 11 | private final boolean includePlayerChunks;
|
| 12 | private int totalWeight;
|
| 13 | private int finalizedWeight;
|
| 14 | private int segmentWeight;
|
| 15 | private float segmentFraction;
|
| 16 | private volatile float progress;
|
| 17 |
|
| 18 | public LevelLoadProgressTracker(final boolean includePlayerChunks) {
|
| 19 | this.includePlayerChunks = includePlayerChunks;
|
| 20 | }
|
| 21 |
|
| 22 | @Override
|
| 23 | public void start(final LevelLoadListener.Stage stage, final int totalChunks) {
|
| 24 | if (this.tracksStage(stage)) {
|
| 25 | switch (stage) {
|
| 26 | case LOAD_INITIAL_CHUNKS:
|
| 27 | int playerChunksWeight = this.includePlayerChunks ? EXPECTED_PLAYER_CHUNKS : 0;
|
| 28 | this.totalWeight = 10 + totalChunks + playerChunksWeight;
|
| 29 | this.beginSegment(10);
|
| 30 | this.finishSegment();
|
| 31 | this.beginSegment(totalChunks);
|
| 32 | break;
|
| 33 | case LOAD_PLAYER_CHUNKS:
|
| 34 | this.beginSegment(EXPECTED_PLAYER_CHUNKS);
|
| 35 | }
|
| 36 | }
|
| 37 | }
|
| 38 |
|
| 39 | private void beginSegment(final int weight) {
|
| 40 | this.segmentWeight = weight;
|
| 41 | this.segmentFraction = 0.0F;
|
| 42 | this.updateProgress();
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | public void update(final LevelLoadListener.Stage stage, final int currentChunks, final int totalChunks) {
|
| 47 | if (this.tracksStage(stage)) {
|
| 48 | this.segmentFraction = totalChunks == 0 ? 0.0F : (float)currentChunks / totalChunks;
|
| 49 | this.updateProgress();
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public void finish(final LevelLoadListener.Stage stage) {
|
| 55 | if (this.tracksStage(stage)) {
|
| 56 | this.finishSegment();
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | private void finishSegment() {
|
| 61 | this.finalizedWeight = this.finalizedWeight + this.segmentWeight;
|
| 62 | this.segmentWeight = 0;
|
| 63 | this.updateProgress();
|
| 64 | }
|
| 65 |
|
| 66 | private boolean tracksStage(final LevelLoadListener.Stage stage) {
|
| 67 | return switch (stage) {
|
| 68 | case LOAD_INITIAL_CHUNKS -> true;
|
| 69 | case LOAD_PLAYER_CHUNKS -> this.includePlayerChunks;
|
| 70 | default -> false;
|
| 71 | };
|
| 72 | }
|
| 73 |
|
| 74 | private void updateProgress() {
|
| 75 | if (this.totalWeight == 0) {
|
| 76 | this.progress = 0.0F;
|
| 77 | } else {
|
| 78 | float currentWeight = this.finalizedWeight + this.segmentFraction * this.segmentWeight;
|
| 79 | this.progress = currentWeight / this.totalWeight;
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | public float get() {
|
| 84 | return this.progress;
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public void updateFocus(final ResourceKey<Level> dimension, final ChunkPos chunkPos) {
|
| 89 | }
|
| 90 | }
|
| 91 |
|