| 1 | package net.minecraft.util.worldupdate;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import it.unimi.dsi.fastutil.objects.Reference2FloatMap;
|
| 5 | import it.unimi.dsi.fastutil.objects.Reference2FloatMaps;
|
| 6 | import it.unimi.dsi.fastutil.objects.Reference2FloatOpenHashMap;
|
| 7 | import java.util.concurrent.atomic.AtomicInteger;
|
| 8 | import java.util.concurrent.atomic.AtomicLong;
|
| 9 | import net.minecraft.network.chat.Component;
|
| 10 | import net.minecraft.resources.ResourceKey;
|
| 11 | import net.minecraft.server.notifications.EmptyNotificationService;
|
| 12 | import net.minecraft.server.notifications.NotificationService;
|
| 13 | import net.minecraft.util.Mth;
|
| 14 | import net.minecraft.util.StringRepresentable;
|
| 15 | import net.minecraft.util.Util;
|
| 16 | import net.minecraft.util.datafix.DataFixTypes;
|
| 17 | import net.minecraft.world.level.Level;
|
| 18 | import org.jspecify.annotations.Nullable;
|
| 19 | import org.slf4j.Logger;
|
| 20 |
|
| 21 | public class UpgradeProgress {
|
| 22 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 23 | private volatile boolean finished;
|
| 24 | private final UpgradeProgress.FileFixStats totalFileFixStats = new UpgradeProgress.FileFixStats();
|
| 25 | private final UpgradeProgress.FileFixStats typeFileFixStats = new UpgradeProgress.FileFixStats();
|
| 26 | private final UpgradeProgress.FileFixStats runningFileFixerStats = new UpgradeProgress.FileFixStats();
|
| 27 | private volatile float totalProgress;
|
| 28 | private final AtomicInteger totalChunks = new AtomicInteger();
|
| 29 | private final AtomicInteger converted = new AtomicInteger();
|
| 30 | private final AtomicInteger skipped = new AtomicInteger();
|
| 31 | private final Reference2FloatMap<ResourceKey<Level>> progressMap = Reference2FloatMaps.synchronize(new Reference2FloatOpenHashMap<>());
|
| 32 | private volatile boolean canceled = false;
|
| 33 | private volatile @Nullable DataFixTypes dataFixType;
|
| 34 | private volatile UpgradeProgress.Status status = UpgradeProgress.Status.COUNTING;
|
| 35 | private volatile UpgradeProgress.@Nullable Type type;
|
| 36 | private final AtomicLong lastLoggedProgressTime = new AtomicLong();
|
| 37 | private final NotificationService notificationService;
|
| 38 |
|
| 39 | public UpgradeProgress() {
|
| 40 | this(new EmptyNotificationService());
|
| 41 | }
|
| 42 |
|
| 43 | public UpgradeProgress(final NotificationService notificationService) {
|
| 44 | this.notificationService = notificationService;
|
| 45 | }
|
| 46 |
|
| 47 | public boolean isFinished() {
|
| 48 | return this.finished;
|
| 49 | }
|
| 50 |
|
| 51 | public void setFinished(final boolean finished) {
|
| 52 | this.finished = finished;
|
| 53 | }
|
| 54 |
|
| 55 | public UpgradeProgress.FileFixStats getTotalFileFixStats() {
|
| 56 | return this.totalFileFixStats;
|
| 57 | }
|
| 58 |
|
| 59 | public UpgradeProgress.FileFixStats getTypeFileFixStats() {
|
| 60 | return this.typeFileFixStats;
|
| 61 | }
|
| 62 |
|
| 63 | public UpgradeProgress.FileFixStats getRunningFileFixerStats() {
|
| 64 | return this.runningFileFixerStats;
|
| 65 | }
|
| 66 |
|
| 67 | public void addTotalFileFixOperations(final int additionalFileFixOperations) {
|
| 68 | this.totalFileFixStats.totalOperations.addAndGet(additionalFileFixOperations);
|
| 69 | this.typeFileFixStats.totalOperations.addAndGet(additionalFileFixOperations);
|
| 70 | }
|
| 71 |
|
| 72 | public float getTotalProgress() {
|
| 73 | return this.totalProgress;
|
| 74 | }
|
| 75 |
|
| 76 | public void setTotalProgress(final float totalProgress) {
|
| 77 | this.totalProgress = totalProgress;
|
| 78 | }
|
| 79 |
|
| 80 | public int getTotalChunks() {
|
| 81 | return this.totalChunks.get();
|
| 82 | }
|
| 83 |
|
| 84 | public void addTotalChunks(final int additionalTotalChunks) {
|
| 85 | this.totalChunks.addAndGet(additionalTotalChunks);
|
| 86 | }
|
| 87 |
|
| 88 | public int getConverted() {
|
| 89 | return this.converted.get();
|
| 90 | }
|
| 91 |
|
| 92 | public void setDimensionProgress(final ResourceKey<Level> dimensionKey, final float currentProgress) {
|
| 93 | this.progressMap.put(dimensionKey, currentProgress);
|
| 94 | }
|
| 95 |
|
| 96 | public float getDimensionProgress(final ResourceKey<Level> dimensionKey) {
|
| 97 | return this.progressMap.getFloat(dimensionKey);
|
| 98 | }
|
| 99 |
|
| 100 | public void incrementConverted() {
|
| 101 | this.converted.incrementAndGet();
|
| 102 | }
|
| 103 |
|
| 104 | public int getSkipped() {
|
| 105 | return this.skipped.get();
|
| 106 | }
|
| 107 |
|
| 108 | public void incrementSkipped() {
|
| 109 | this.skipped.incrementAndGet();
|
| 110 | }
|
| 111 |
|
| 112 | public void incrementFinishedOperations() {
|
| 113 | this.incrementFinishedOperationsBy(1);
|
| 114 | }
|
| 115 |
|
| 116 | public void incrementFinishedOperationsBy(final int count) {
|
| 117 | this.totalFileFixStats.finishedOperations.addAndGet(count);
|
| 118 | this.typeFileFixStats.finishedOperations.addAndGet(count);
|
| 119 | this.logProgress();
|
| 120 | }
|
| 121 |
|
| 122 | public void setCanceled() {
|
| 123 | this.canceled = true;
|
| 124 | }
|
| 125 |
|
| 126 | public boolean isCanceled() {
|
| 127 | return this.canceled;
|
| 128 | }
|
| 129 |
|
| 130 | public UpgradeProgress.Status getStatus() {
|
| 131 | return this.status;
|
| 132 | }
|
| 133 |
|
| 134 | public void setStatus(final UpgradeProgress.Status status) {
|
| 135 | this.status = status;
|
| 136 | }
|
| 137 |
|
| 138 | public @Nullable DataFixTypes getDataFixType() {
|
| 139 | return this.dataFixType;
|
| 140 | }
|
| 141 |
|
| 142 | public void setType(final UpgradeProgress.Type type) {
|
| 143 | this.type = type;
|
| 144 | this.typeFileFixStats.reset();
|
| 145 | }
|
| 146 |
|
| 147 | public UpgradeProgress.@Nullable Type getType() {
|
| 148 | return this.type;
|
| 149 | }
|
| 150 |
|
| 151 | public void setApplicableFixerAmount(final int amount) {
|
| 152 | this.runningFileFixerStats.totalOperations.set(amount);
|
| 153 | }
|
| 154 |
|
| 155 | public void incrementRunningFileFixer() {
|
| 156 | this.runningFileFixerStats.finishedOperations.incrementAndGet();
|
| 157 | }
|
| 158 |
|
| 159 | public void reset(final DataFixTypes dataFixType) {
|
| 160 | this.totalFileFixStats.reset();
|
| 161 | this.typeFileFixStats.reset();
|
| 162 | this.totalChunks.set(0);
|
| 163 | this.converted.set(0);
|
| 164 | this.skipped.set(0);
|
| 165 | this.dataFixType = dataFixType;
|
| 166 | }
|
| 167 |
|
| 168 | public void logProgress() {
|
| 169 | long now = Util.getMillis();
|
| 170 | if (now >= this.lastLoggedProgressTime.get() + 1000L) {
|
| 171 | float progress = (float)this.totalFileFixStats.finishedOperations() / this.totalFileFixStats.totalOperations();
|
| 172 | this.lastLoggedProgressTime.set(now);
|
| 173 | LOGGER.info("Upgrading progress: {}%", (int)(progress * 100.0F));
|
| 174 | this.notificationService.worldUpgradeProgress(progress);
|
| 175 | }
|
| 176 | }
|
| 177 |
|
| 178 | public NotificationService notifications() {
|
| 179 | return this.notificationService;
|
| 180 | }
|
| 181 |
|
| 182 | public static class FileFixStats {
|
| 183 | private final AtomicInteger finishedOperations = new AtomicInteger();
|
| 184 | private final AtomicInteger totalOperations = new AtomicInteger();
|
| 185 |
|
| 186 | public int finishedOperations() {
|
| 187 | return this.finishedOperations.get();
|
| 188 | }
|
| 189 |
|
| 190 | public int totalOperations() {
|
| 191 | return this.totalOperations.get();
|
| 192 | }
|
| 193 |
|
| 194 | public void reset() {
|
| 195 | this.finishedOperations.set(0);
|
| 196 | this.totalOperations.set(0);
|
| 197 | }
|
| 198 |
|
| 199 | public float getProgress() {
|
| 200 | return this.totalOperations() == 0 ? 0.0F : Mth.clamp((float)this.finishedOperations() / this.totalOperations(), 0.0F, 1.0F);
|
| 201 | }
|
| 202 | }
|
| 203 |
|
| 204 | public static class Noop extends UpgradeProgress {
|
| 205 | @Override
|
| 206 | public void setFinished(final boolean finished) {
|
| 207 | }
|
| 208 |
|
| 209 | @Override
|
| 210 | public void addTotalFileFixOperations(final int additionalFileFixOperations) {
|
| 211 | }
|
| 212 |
|
| 213 | @Override
|
| 214 | public void setTotalProgress(final float totalProgress) {
|
| 215 | }
|
| 216 |
|
| 217 | @Override
|
| 218 | public void addTotalChunks(final int additionalTotalChunks) {
|
| 219 | }
|
| 220 |
|
| 221 | @Override
|
| 222 | public void setDimensionProgress(final ResourceKey<Level> dimensionKey, final float currentProgress) {
|
| 223 | }
|
| 224 |
|
| 225 | @Override
|
| 226 | public void incrementConverted() {
|
| 227 | }
|
| 228 |
|
| 229 | @Override
|
| 230 | public void incrementSkipped() {
|
| 231 | }
|
| 232 |
|
| 233 | @Override
|
| 234 | public void incrementFinishedOperations() {
|
| 235 | }
|
| 236 |
|
| 237 | @Override
|
| 238 | public void incrementFinishedOperationsBy(final int count) {
|
| 239 | }
|
| 240 |
|
| 241 | @Override
|
| 242 | public void setCanceled() {
|
| 243 | }
|
| 244 |
|
| 245 | @Override
|
| 246 | public void setStatus(final UpgradeProgress.Status status) {
|
| 247 | }
|
| 248 |
|
| 249 | @Override
|
| 250 | public void setApplicableFixerAmount(final int amount) {
|
| 251 | }
|
| 252 |
|
| 253 | @Override
|
| 254 | public void incrementRunningFileFixer() {
|
| 255 | }
|
| 256 |
|
| 257 | @Override
|
| 258 | public void reset(final DataFixTypes dataFixType) {
|
| 259 | }
|
| 260 | }
|
| 261 |
|
| 262 | public enum Status {
|
| 263 | COUNTING,
|
| 264 | FAILED,
|
| 265 | FINISHED,
|
| 266 | UPGRADING;
|
| 267 | }
|
| 268 |
|
| 269 | public enum Type implements StringRepresentable {
|
| 270 | FILES("files"),
|
| 271 | LEGACY_STRUCTURES("legacy_structures"),
|
| 272 | REGIONS("regions");
|
| 273 |
|
| 274 | private final String id;
|
| 275 | private final Component label;
|
| 276 |
|
| 277 | Type(final String id) {
|
| 278 | this.id = id;
|
| 279 | this.label = Component.translatable("upgradeWorld.progress.type." + this.getSerializedName());
|
| 280 | }
|
| 281 |
|
| 282 | @Override
|
| 283 | public String getSerializedName() {
|
| 284 | return this.id;
|
| 285 | }
|
| 286 |
|
| 287 | public Component label() {
|
| 288 | return this.label;
|
| 289 | }
|
| 290 | }
|
| 291 | }
|
| 292 |
|