OptimizeWorldScreen .java

net.minecraft.client.gui.screens.worldselection.OptimizeWorldScreen (class): members: DIMENSION_COLORS, upgrader, levelSourceAccess, levelSource, callback.

net.minecraft.client.gui.screens.worldselectionclass148 linesclientextends Screen6.1 KB

API Surface (6)

void init()
void tick()
boolean shouldCloseOnEsc()
void onClose()
void removed()
void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a)

Fields & Constants (9)

LOGGER
DIMENSION_COLORS
callback
upgrader
levelSourceAccess
dataFixer
levelSource
worldData
eraseCache

String literals (7)

"Failed to load datapacks, can't optimize world"
"optimizeWorld.title"
"optimizeWorld.info.converted"
"optimizeWorld.info.skipped"
"optimizeWorld.info.total"
"optimizeWorld.progress.counter"
"optimizeWorld.progress.percentage"

Full Source

OptimizeWorldScreen.java148 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui.screens.worldselection;
2
3
import com.mojang.datafixers.DataFixer;
4
import com.mojang.logging.LogUtils;
5
import com.mojang.serialization.Dynamic;
6
import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
7
import it.unimi.dsi.fastutil.objects.Reference2IntOpenHashMap;
8
import java.util.function.ToIntFunction;
9
import net.fabricmc.api.EnvType;
10
import net.fabricmc.api.Environment;
11
import net.minecraft.client.Minecraft;
12
import net.minecraft.client.gui.GuiGraphicsExtractor;
13
import net.minecraft.client.gui.components.Button;
14
import net.minecraft.client.gui.screens.Screen;
15
import net.minecraft.core.RegistryAccess;
16
import net.minecraft.core.RegistryAccess.Frozen;
17
import net.minecraft.nbt.NbtUtils;
18
import net.minecraft.network.chat.CommonComponents;
19
import net.minecraft.network.chat.Component;
20
import net.minecraft.resources.ResourceKey;
21
import net.minecraft.server.WorldStem;
22
import net.minecraft.server.packs.repository.PackRepository;
23
import net.minecraft.server.packs.repository.ServerPacksSource;
24
import net.minecraft.util.Mth;
25
import net.minecraft.util.Util;
26
import net.minecraft.util.datafix.DataFixTypes;
27
import net.minecraft.util.datafix.DataFixers;
28
import net.minecraft.util.worldupdate.WorldUpgrader;
29
import net.minecraft.world.level.Level;
30
import net.minecraft.world.level.storage.WorldData;
31
import net.minecraft.world.level.storage.LevelStorageSource.LevelStorageAccess;
32
import org.jspecify.annotations.Nullable;
33
import org.slf4j.Logger;
34
35
@Environment(EnvType.CLIENT)
36
public class OptimizeWorldScreen extends Screen {
37
	private static final Logger LOGGER = LogUtils.getLogger();
38
	private static final ToIntFunction<ResourceKey<Level>> DIMENSION_COLORS = Util.make(new Reference2IntOpenHashMap<>(), map -> {
39
		map.put(Level.OVERWORLD, -13408734);
40
		map.put(Level.NETHER, -10075085);
41
		map.put(Level.END, -8943531);
42
		map.defaultReturnValue(-2236963);
43
	});
44
	private final BooleanConsumer callback;
45
	private final WorldUpgrader upgrader;
46
47
	public static @Nullable OptimizeWorldScreen create(
48
		final Minecraft minecraft, final BooleanConsumer callback, final DataFixer dataFixer, final LevelStorageAccess levelSourceAccess, final boolean eraseCache
49
	) {
50
		try {
51
			WorldOpenFlows worldOpenFlows = minecraft.createWorldOpenFlows();
52
			PackRepository packRepository = ServerPacksSource.createPackRepository(levelSourceAccess);
53
			Dynamic<?> unfixedDataTag = levelSourceAccess.getUnfixedDataTagWithFallback();
54
			int dataVersion = NbtUtils.getDataVersion(unfixedDataTag);
55
			if (DataFixers.getFileFixer().requiresFileFixing(dataVersion)) {
56
				throw new IllegalStateException("Can't optimize world before file fixing; shouldn't be able to get here");
57
			}
58
59
			Dynamic<?> dataTag = DataFixTypes.LEVEL.updateToCurrentVersion(DataFixers.getDataFixer(), unfixedDataTag, dataVersion);
60
61
			try (WorldStem worldStem = worldOpenFlows.loadWorldStem(levelSourceAccess, dataTag, false, packRepository)) {
62
				WorldData worldData = worldStem.worldDataAndGenSettings().data();
63
				Frozen registryAccess = worldStem.registries().compositeAccess();
64
				levelSourceAccess.saveDataTag(worldData);
65
				return new OptimizeWorldScreen(callback, dataFixer, levelSourceAccess, worldData, eraseCache, registryAccess);
66
			}
67
		} catch (Exception e) {
68
			LOGGER.warn("Failed to load datapacks, can't optimize world", e);
69
			return null;
70
		}
71
	}
72
73
	private OptimizeWorldScreen(
74
		final BooleanConsumer callback,
75
		final DataFixer dataFixer,
76
		final LevelStorageAccess levelSource,
77
		final WorldData worldData,
78
		final boolean eraseCache,
79
		final RegistryAccess registryAccess
80
	) {
81
		super(Component.translatable("optimizeWorld.title", worldData.getLevelSettings().levelName()));
82
		this.callback = callback;
83
		this.upgrader = new WorldUpgrader(levelSource, dataFixer, registryAccess, eraseCache, false);
84
	}
85
86
	@Override
87
	protected void init() {
88
		super.init();
89
		this.addRenderableWidget(Button.builder(CommonComponents.GUI_CANCEL, button -> {
90
			this.upgrader.cancel();
91
			this.callback.accept(false);
92
		}).bounds(this.width / 2 - 100, this.height / 4 + 150, 200, 20).build());
93
	}
94
95
	@Override
96
	public void tick() {
97
		if (this.upgrader.isFinished()) {
98
			this.callback.accept(true);
99
		}
100
	}
101
102
	@Override
103
	public boolean shouldCloseOnEsc() {
104
		return false;
105
	}
106
107
	@Override
108
	public void onClose() {
109
		this.callback.accept(false);
110
	}
111
112
	@Override
113
	public void removed() {
114
		this.upgrader.cancel();
115
		this.upgrader.close();
116
	}
117
118
	@Override
119
	public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
120
		super.extractRenderState(graphics, mouseX, mouseY, a);
121
		graphics.centeredText(this.font, this.title, this.width / 2, 20, -1);
122
		int x0 = this.width / 2 - 150;
123
		int x1 = this.width / 2 + 150;
124
		int y0 = this.height / 4 + 100;
125
		int y1 = y0 + 10;
126
		graphics.centeredText(this.font, this.upgrader.getStatus(), this.width / 2, y0 - 9 - 2, -6250336);
127
		if (this.upgrader.getTotalChunks() > 0) {
128
			graphics.fill(x0 - 1, y0 - 1, x1 + 1, y1 + 1, -16777216);
129
			graphics.text(this.font, Component.translatable("optimizeWorld.info.converted", this.upgrader.getConverted()), x0, 40, -6250336);
130
			graphics.text(this.font, Component.translatable("optimizeWorld.info.skipped", this.upgrader.getSkipped()), x0, 40 + 9 + 3, -6250336);
131
			graphics.text(this.font, Component.translatable("optimizeWorld.info.total", this.upgrader.getTotalChunks()), x0, 40 + (9 + 3) * 2, -6250336);
132
			int progress = 0;
133
134
			for (ResourceKey<Level> dimension : this.upgrader.levels()) {
135
				int length = Mth.floor(this.upgrader.dimensionProgress(dimension) * (x1 - x0));
136
				graphics.fill(x0 + progress, y0, x0 + progress + length, y1, DIMENSION_COLORS.applyAsInt(dimension));
137
				progress += length;
138
			}
139
140
			int totalProgress = this.upgrader.getConverted() + this.upgrader.getSkipped();
141
			Component countStr = Component.translatable("optimizeWorld.progress.counter", totalProgress, this.upgrader.getTotalChunks());
142
			Component progressStr = Component.translatable("optimizeWorld.progress.percentage", Mth.floor(this.upgrader.getTotalProgress() * 100.0F));
143
			graphics.centeredText(this.font, countStr, this.width / 2, y0 + 2 * 9 + 2, -6250336);
144
			graphics.centeredText(this.font, progressStr, this.width / 2, y0 + (y1 - y0) / 2 - 9 / 2, -6250336);
145
		}
146
	}
147
}
148

Decompiled Minecraft 26.3 source of OptimizeWorldScreen (net/minecraft/client/gui/screens/worldselection/OptimizeWorldScreen.java) with Mojang official mappings, stored verbatim on the site.