| 1 | package net.minecraft.client.gui.screens;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
| 4 | import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.GameNarrator;
|
| 8 | import net.minecraft.client.Minecraft;
|
| 9 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 10 | import net.minecraft.client.gui.components.debug.DebugScreenEntries;
|
| 11 | import net.minecraft.client.gui.narration.NarratedElementType;
|
| 12 | import net.minecraft.client.gui.narration.NarrationElementOutput;
|
| 13 | import net.minecraft.client.gui.render.TextureSetup;
|
| 14 | import net.minecraft.client.multiplayer.LevelLoadTracker;
|
| 15 | import net.minecraft.client.renderer.RenderPipelines;
|
| 16 | import net.minecraft.client.renderer.blockentity.AbstractEndPortalRenderer;
|
| 17 | import net.minecraft.client.renderer.texture.AbstractTexture;
|
| 18 | import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
| 19 | import net.minecraft.client.renderer.texture.TextureManager;
|
| 20 | import net.minecraft.network.chat.Component;
|
| 21 | import net.minecraft.server.level.progress.ChunkLoadStatusView;
|
| 22 | import net.minecraft.util.ARGB;
|
| 23 | import net.minecraft.util.Mth;
|
| 24 | import net.minecraft.util.Util;
|
| 25 | import net.minecraft.world.level.block.Blocks;
|
| 26 | import net.minecraft.world.level.chunk.status.ChunkStatus;
|
| 27 | import org.jspecify.annotations.Nullable;
|
| 28 |
|
| 29 | @Environment(EnvType.CLIENT)
|
| 30 | public class LevelLoadingScreen extends Screen {
|
| 31 | private static final Component DOWNLOADING_TERRAIN_TEXT = Component.translatable("multiplayer.downloadingTerrain");
|
| 32 | private static final Component READY_TO_PLAY_TEXT = Component.translatable("narrator.ready_to_play");
|
| 33 | private static final long NARRATION_DELAY_MS = 2000L;
|
| 34 | private static final int PROGRESS_BAR_WIDTH = 200;
|
| 35 | private LevelLoadTracker loadTracker;
|
| 36 | private float smoothedProgress;
|
| 37 | private long lastNarration = -1L;
|
| 38 | private LevelLoadingScreen.Reason reason;
|
| 39 | private @Nullable TextureAtlasSprite cachedNetherPortalSprite;
|
| 40 | private static final Object2IntMap<ChunkStatus> COLORS = Util.make(new Object2IntOpenHashMap<>(), map -> {
|
| 41 | map.defaultReturnValue(0);
|
| 42 | map.put(ChunkStatus.EMPTY, 5526612);
|
| 43 | map.put(ChunkStatus.STRUCTURE_STARTS, 10066329);
|
| 44 | map.put(ChunkStatus.STRUCTURE_REFERENCES, 6250897);
|
| 45 | map.put(ChunkStatus.BIOMES, 8434258);
|
| 46 | map.put(ChunkStatus.TERRAIN, 3159410);
|
| 47 | map.put(ChunkStatus.FEATURES, 2213376);
|
| 48 | map.put(ChunkStatus.INITIALIZE_LIGHT, 13421772);
|
| 49 | map.put(ChunkStatus.LIGHT, 16769184);
|
| 50 | map.put(ChunkStatus.SPAWN, 15884384);
|
| 51 | map.put(ChunkStatus.FULL, 16777215);
|
| 52 | });
|
| 53 |
|
| 54 | public LevelLoadingScreen(final LevelLoadTracker loadTracker, final LevelLoadingScreen.Reason reason) {
|
| 55 | super(GameNarrator.NO_TITLE);
|
| 56 | this.loadTracker = loadTracker;
|
| 57 | this.reason = reason;
|
| 58 | }
|
| 59 |
|
| 60 | public void update(final LevelLoadTracker loadTracker, final LevelLoadingScreen.Reason reason) {
|
| 61 | this.loadTracker = loadTracker;
|
| 62 | this.reason = reason;
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | public boolean shouldCloseOnEsc() {
|
| 67 | return false;
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | protected boolean shouldNarrateNavigation() {
|
| 72 | return false;
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | protected void updateNarratedWidget(final NarrationElementOutput output) {
|
| 77 | if (this.loadTracker.hasProgress()) {
|
| 78 | output.add(NarratedElementType.TITLE, Component.translatable("loading.progress", Mth.floor(this.loadTracker.serverProgress() * 100.0F)));
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | public void tick() {
|
| 84 | super.tick();
|
| 85 | this.smoothedProgress = this.smoothedProgress + (this.loadTracker.serverProgress() - this.smoothedProgress) * 0.2F;
|
| 86 | if (this.loadTracker.isLevelReady()) {
|
| 87 | this.onClose();
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 93 | super.extractRenderState(graphics, mouseX, mouseY, a);
|
| 94 | long current = Util.getMillis();
|
| 95 | if (current - this.lastNarration > 2000L) {
|
| 96 | this.lastNarration = current;
|
| 97 | this.triggerImmediateNarration(true);
|
| 98 | }
|
| 99 |
|
| 100 | int xCenter = this.width / 2;
|
| 101 | int yCenter = this.height / 2;
|
| 102 | ChunkLoadStatusView statusView = this.loadTracker.statusView();
|
| 103 | int textTop;
|
| 104 | if (statusView != null) {
|
| 105 | int size = 2;
|
| 106 | extractChunksForRendering(graphics, xCenter, yCenter, 2, 0, statusView);
|
| 107 | textTop = yCenter - statusView.radius() * 2 - 9 * 3;
|
| 108 | } else {
|
| 109 | textTop = yCenter - 50;
|
| 110 | }
|
| 111 |
|
| 112 | graphics.centeredText(this.font, DOWNLOADING_TERRAIN_TEXT, xCenter, textTop, -1);
|
| 113 | if (this.loadTracker.hasProgress()) {
|
| 114 | this.drawProgressBar(graphics, xCenter - 100, textTop + 9 + 3, 200, 2, this.smoothedProgress);
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | private void drawProgressBar(final GuiGraphicsExtractor graphics, final int left, final int top, final int width, final int height, final float progress) {
|
| 119 | graphics.fill(left, top, left + width, top + height, -16777216);
|
| 120 | graphics.fill(left, top, left + Math.round(progress * width), top + height, -16711936);
|
| 121 | }
|
| 122 |
|
| 123 | public static void extractChunksForRendering(
|
| 124 | final GuiGraphicsExtractor graphics, final int xCenter, final int yCenter, final int size, final int margin, final ChunkLoadStatusView statusView
|
| 125 | ) {
|
| 126 | int width = size + margin;
|
| 127 | int diameter = statusView.radius() * 2 + 1;
|
| 128 | int totalWidth = diameter * width - margin;
|
| 129 | int xStart = xCenter - totalWidth / 2;
|
| 130 | int yStart = yCenter - totalWidth / 2;
|
| 131 | if (Minecraft.getInstance().debugEntries.isCurrentlyEnabled(DebugScreenEntries.VISUALIZE_CHUNKS_ON_SERVER)) {
|
| 132 | int centerWidth = width / 2 + 1;
|
| 133 | graphics.fill(xCenter - centerWidth, yCenter - centerWidth, xCenter + centerWidth, yCenter + centerWidth, -65536);
|
| 134 | }
|
| 135 |
|
| 136 | for (int x = 0; x < diameter; x++) {
|
| 137 | for (int z = 0; z < diameter; z++) {
|
| 138 | ChunkStatus status = statusView.get(x, z);
|
| 139 | int xCellStart = xStart + x * width;
|
| 140 | int yCellStart = yStart + z * width;
|
| 141 | graphics.fill(xCellStart, yCellStart, xCellStart + size, yCellStart + size, ARGB.opaque(COLORS.getInt(status)));
|
| 142 | }
|
| 143 | }
|
| 144 | }
|
| 145 |
|
| 146 | @Override
|
| 147 | public void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 148 | switch (this.reason) {
|
| 149 | case NETHER_PORTAL:
|
| 150 | graphics.blitSprite(RenderPipelines.GUI_OPAQUE_TEXTURED_BACKGROUND, this.getNetherPortalSprite(), 0, 0, graphics.guiWidth(), graphics.guiHeight());
|
| 151 | break;
|
| 152 | case END_PORTAL:
|
| 153 | TextureManager textureManager = Minecraft.getInstance().getTextureManager();
|
| 154 | AbstractTexture skyTexture = textureManager.getTexture(AbstractEndPortalRenderer.END_SKY_LOCATION);
|
| 155 | AbstractTexture portalTexture = textureManager.getTexture(AbstractEndPortalRenderer.END_PORTAL_LOCATION);
|
| 156 | TextureSetup textureSetup = TextureSetup.doubleTexture(
|
| 157 | skyTexture.getTextureView(), skyTexture.getSampler(), portalTexture.getTextureView(), portalTexture.getSampler()
|
| 158 | );
|
| 159 | graphics.fill(RenderPipelines.END_PORTAL, textureSetup, 0, 0, this.width, this.height);
|
| 160 | break;
|
| 161 | case OTHER:
|
| 162 | this.extractPanorama(graphics, a);
|
| 163 | this.extractBlurredBackground(graphics);
|
| 164 | this.extractMenuBackground(graphics);
|
| 165 | }
|
| 166 | }
|
| 167 |
|
| 168 | private TextureAtlasSprite getNetherPortalSprite() {
|
| 169 | if (this.cachedNetherPortalSprite != null) {
|
| 170 | return this.cachedNetherPortalSprite;
|
| 171 | }
|
| 172 |
|
| 173 | this.cachedNetherPortalSprite = this.minecraft
|
| 174 | .getModelManager()
|
| 175 | .getBlockStateModelSet()
|
| 176 | .getParticleMaterial(Blocks.NETHER_PORTAL.defaultBlockState())
|
| 177 | .sprite();
|
| 178 | return this.cachedNetherPortalSprite;
|
| 179 | }
|
| 180 |
|
| 181 | @Override
|
| 182 | public void onClose() {
|
| 183 | this.minecraft.getNarrator().saySystemNow(READY_TO_PLAY_TEXT);
|
| 184 | super.onClose();
|
| 185 | }
|
| 186 |
|
| 187 | @Override
|
| 188 | public boolean isPauseScreen() {
|
| 189 | return false;
|
| 190 | }
|
| 191 |
|
| 192 | @Environment(EnvType.CLIENT)
|
| 193 | public enum Reason {
|
| 194 | NETHER_PORTAL,
|
| 195 | END_PORTAL,
|
| 196 | OTHER;
|
| 197 | }
|
| 198 | }
|
| 199 |
|