TitleScreen .java

net.minecraft.client.gui.screens.TitleScreen (class, 367 lines): members: realmsNotificationsScreen, COPYRIGHT_TEXT, DEMO_LEVEL_ID, fading, splash, added.

net.minecraft.client.gui.screensclass367 linesclientextends Screen13.7 KB

API Surface (11)

void tick()
void registerTextures(final TextureManager textureManager)
boolean isPauseScreen()
boolean shouldCloseOnEsc()
void init()
void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a)
void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a)
boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick)
void removed()
void added()
boolean canInterruptWithAnotherScreen()

Fields & Constants (10)

LOGGER
TITLE
COPYRIGHT_TEXT
DEMO_LEVEL_ID
splash
realmsNotificationsScreen
friends
fading
fadeInStart
logoRenderer

String literals (23)

"narrator.screen.title"
"title.credits"
"Demo_World"
"menu.options"
"menu.quit"
"menu.singleplayer"
"menu.multiplayer"
"menu.online"
"title.multiplayer.disabled.banned.name"
"title.multiplayer.disabled.banned.temporary"
"title.multiplayer.disabled.banned.permanent"
"title.multiplayer.disabled"
"menu.playdemo"
"menu.resetdemo"
"selectWorld.deleteQuestion"
"selectWorld.deleteWarning"
"selectWorld.deleteButton"
"Failed to access demo world"
"Failed to read demo world data"
"Minecraft "
" Demo"
"menu.modded"
"Failed to delete demo world"

Full Source

TitleScreen.java367 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui.screens;
2
3
import com.mojang.authlib.minecraft.BanDetails;
4
import com.mojang.logging.LogUtils;
5
import com.mojang.realmsclient.RealmsMainScreen;
6
import com.mojang.realmsclient.gui.screens.RealmsNotificationsScreen;
7
import java.io.IOException;
8
import java.util.Objects;
9
import net.fabricmc.api.EnvType;
10
import net.fabricmc.api.Environment;
11
import net.minecraft.SharedConstants;
12
import net.minecraft.client.Minecraft;
13
import net.minecraft.client.gui.GuiGraphicsExtractor;
14
import net.minecraft.client.gui.components.Button;
15
import net.minecraft.client.gui.components.CommonButtons;
16
import net.minecraft.client.gui.components.FriendsButton;
17
import net.minecraft.client.gui.components.LogoRenderer;
18
import net.minecraft.client.gui.components.PlainTextButton;
19
import net.minecraft.client.gui.components.SplashRenderer;
20
import net.minecraft.client.gui.components.SpriteIconButton;
21
import net.minecraft.client.gui.components.Tooltip;
22
import net.minecraft.client.gui.components.toasts.SystemToast;
23
import net.minecraft.client.gui.screens.friends.FriendsOverlayScreen;
24
import net.minecraft.client.gui.screens.multiplayer.JoinMultiplayerScreen;
25
import net.minecraft.client.gui.screens.multiplayer.SafetyScreen;
26
import net.minecraft.client.gui.screens.options.AccessibilityOptionsScreen;
27
import net.minecraft.client.gui.screens.options.LanguageSelectScreen;
28
import net.minecraft.client.gui.screens.options.OnlineOptionsScreen;
29
import net.minecraft.client.gui.screens.options.OptionsScreen;
30
import net.minecraft.client.gui.screens.worldselection.CreateWorldScreen;
31
import net.minecraft.client.gui.screens.worldselection.SelectWorldScreen;
32
import net.minecraft.client.input.MouseButtonEvent;
33
import net.minecraft.client.renderer.Panorama;
34
import net.minecraft.client.renderer.texture.TextureManager;
35
import net.minecraft.client.resources.language.I18n;
36
import net.minecraft.network.chat.CommonComponents;
37
import net.minecraft.network.chat.Component;
38
import net.minecraft.server.MinecraftServer;
39
import net.minecraft.util.ARGB;
40
import net.minecraft.util.Mth;
41
import net.minecraft.util.Util;
42
import net.minecraft.world.level.levelgen.WorldOptions;
43
import net.minecraft.world.level.levelgen.presets.WorldPresets;
44
import net.minecraft.world.level.storage.LevelStorageSource;
45
import net.minecraft.world.level.storage.LevelStorageSource.LevelStorageAccess;
46
import org.jspecify.annotations.Nullable;
47
import org.slf4j.Logger;
48
49
@Environment(EnvType.CLIENT)
50
public class TitleScreen extends Screen {
51
	private static final Logger LOGGER = LogUtils.getLogger();
52
	private static final Component TITLE = Component.translatable("narrator.screen.title");
53
	private static final Component COPYRIGHT_TEXT = Component.translatable("title.credits");
54
	private static final String DEMO_LEVEL_ID = "Demo_World";
55
	private @Nullable SplashRenderer splash;
56
	private @Nullable RealmsNotificationsScreen realmsNotificationsScreen;
57
	private @Nullable FriendsButton friends;
58
	private boolean fading;
59
	private long fadeInStart;
60
	private final LogoRenderer logoRenderer;
61
62
	public TitleScreen() {
63
		this(false);
64
	}
65
66
	public TitleScreen(final boolean fading) {
67
		this(fading, null);
68
	}
69
70
	public TitleScreen(final boolean fading, final @Nullable LogoRenderer logoRenderer) {
71
		super(TITLE);
72
		this.fading = fading;
73
		this.logoRenderer = Objects.requireNonNullElseGet(logoRenderer, () -> new LogoRenderer(false));
74
		this.minecraft.gameRenderer.panorama().startSpin();
75
	}
76
77
	private boolean realmsNotificationsEnabled() {
78
		return this.realmsNotificationsScreen != null;
79
	}
80
81
	@Override
82
	public void tick() {
83
		if (this.realmsNotificationsEnabled()) {
84
			this.realmsNotificationsScreen.tick();
85
		}
86
87
		if (this.minecraft.getPlayerSocialManager().isFriendListEnabled() && this.friends != null) {
88
			this.friends.refreshIncomingRequestCount();
89
		}
90
	}
91
92
	public static void registerTextures(final TextureManager textureManager) {
93
		textureManager.registerForNextReload(LogoRenderer.MINECRAFT_LOGO);
94
		textureManager.registerForNextReload(LogoRenderer.MINECRAFT_EDITION);
95
		textureManager.registerForNextReload(Panorama.PANORAMA_OVERLAY);
96
	}
97
98
	@Override
99
	public boolean isPauseScreen() {
100
		return false;
101
	}
102
103
	@Override
104
	public boolean shouldCloseOnEsc() {
105
		return false;
106
	}
107
108
	@Override
109
	protected void init() {
110
		if (this.splash == null) {
111
			this.splash = this.minecraft.gui.splashManager().getSplash();
112
		}
113
114
		int copyrightWidth = this.font.width(COPYRIGHT_TEXT);
115
		int copyrightX = this.width - copyrightWidth - 2;
116
		int spacing = 24;
117
		int topPos = this.height / 4 + 48;
118
		if (this.minecraft.isDemo()) {
119
			topPos = this.createDemoMenuOptions(topPos, 24);
120
		} else {
121
			topPos = this.createNormalMenuOptions(topPos, 24);
122
		}
123
124
		int numberOfButtons = 3;
125
		int currentButton = 0;
126
		topPos += 24;
127
		this.friends = this.addRenderableWidget(
128
			CommonButtons.friends(
129
				20,
130
				var1x -> OnlineOptionsScreen.confirmFriendsListEnabled(this.minecraft, () -> this.minecraft.gui.setScreen(new FriendsOverlayScreen(this)), this),
131
				!this.minecraft.isDemo() && !this.minecraft.isOfflineDeveloperMode()
132
			)
133
		);
134
		this.friends.setPosition(this.getHorizontalPosition(++currentButton, 3, 20), topPos);
135
		SpriteIconButton language = this.addRenderableWidget(
136
			CommonButtons.language(
137
				20, var1x -> this.minecraft.gui.setScreen(new LanguageSelectScreen(this, this.minecraft.options, this.minecraft.getLanguageManager())), true
138
			)
139
		);
140
		language.setPosition(this.getHorizontalPosition(++currentButton, 3, 20), topPos);
141
		SpriteIconButton accessibility = this.addRenderableWidget(
142
			CommonButtons.accessibility(20, var1x -> this.minecraft.gui.setScreen(new AccessibilityOptionsScreen(this, this.minecraft.options)), true)
143
		);
144
		accessibility.setPosition(this.getHorizontalPosition(++currentButton, 3, 20), topPos);
145
		Button.Builder var10001 = Button.builder(
146
			Component.translatable("menu.options"), var1x -> this.minecraft.gui.setScreen(new OptionsScreen(this, this.minecraft.options))
147
		);
148
		int var10002 = this.width / 2 - 100;
149
		topPos += 24;
150
		this.addRenderableWidget(var10001.bounds(var10002, topPos, 98, 20).build());
151
		this.addRenderableWidget(
152
			Button.builder(Component.translatable("menu.quit"), var1x -> this.minecraft.stop()).bounds(this.width / 2 + 2, topPos, 98, 20).build()
153
		);
154
		this.addRenderableWidget(
155
			new PlainTextButton(
156
				copyrightX, this.height - 10, copyrightWidth, 10, COPYRIGHT_TEXT, var1x -> this.minecraft.gui.setScreen(new CreditsAndAttributionScreen(this)), this.font
157
			)
158
		);
159
		if (this.realmsNotificationsScreen == null) {
160
			this.realmsNotificationsScreen = new RealmsNotificationsScreen();
161
		}
162
163
		if (this.realmsNotificationsEnabled()) {
164
			this.realmsNotificationsScreen.init(this.width, this.height);
165
		}
166
	}
167
168
	private int getHorizontalPosition(final int currentButton, final int numberOfButtons, final int buttonWidth) {
169
		int totalWidth = numberOfButtons * buttonWidth + (numberOfButtons - 1) * 4;
170
		return this.width / 2 - totalWidth / 2 + (currentButton - 1) * (buttonWidth + 4);
171
	}
172
173
	private int createNormalMenuOptions(int topPos, final int spacing) {
174
		Button singleplayerButton = this.addRenderableWidget(
175
			Button.builder(Component.translatable("menu.singleplayer"), var1 -> this.minecraft.gui.setScreen(new SelectWorldScreen(this)))
176
				.bounds(this.width / 2 - 100, topPos, 200, 20)
177
				.build()
178
		);
179
		if (SharedConstants.IS_RUNNING_IN_IDE) {
180
			this.addRenderableWidget(
181
				Button.builder(Component.literal("TW"), var1 -> CreateWorldScreen.testWorld(this.minecraft, () -> this.minecraft.gui.setScreen(this)))
182
					.bounds(singleplayerButton.getX() + singleplayerButton.getWidth() + 2, topPos, 20, 20)
183
					.build()
184
			);
185
		}
186
187
		Component multiplayerDisabledReason = this.getMultiplayerDisabledReason();
188
		boolean multiplayerAllowed = multiplayerDisabledReason == null;
189
		Tooltip tooltip = multiplayerDisabledReason != null ? Tooltip.create(multiplayerDisabledReason) : null;
190
		int var7;
191
		this.addRenderableWidget(Button.builder(Component.translatable("menu.multiplayer"), button -> {
192
			Screen screen = this.minecraft.options.skipMultiplayerWarning ? new JoinMultiplayerScreen(this) : new SafetyScreen(this);
193
			this.minecraft.gui.setScreen(screen);
194
		}).bounds(this.width / 2 - 100, var7 = topPos + spacing, 200, 20).tooltip(tooltip).build()).active = multiplayerAllowed;
195
		this.addRenderableWidget(
196
				Button.builder(Component.translatable("menu.online"), var1 -> this.minecraft.gui.setScreen(new RealmsMainScreen(this)))
197
					.bounds(this.width / 2 - 100, topPos = var7 + spacing, 200, 20)
198
					.tooltip(tooltip)
199
					.build()
200
			)
201
			.active = multiplayerAllowed;
202
		return topPos;
203
	}
204
205
	private @Nullable Component getMultiplayerDisabledReason() {
206
		if (this.minecraft.allowsMultiplayer()) {
207
			return null;
208
		} else if (this.minecraft.isNameBanned()) {
209
			return Component.translatable("title.multiplayer.disabled.banned.name");
210
		} else {
211
			BanDetails multiplayerBan = this.minecraft.multiplayerBan();
212
			if (multiplayerBan != null) {
213
				return multiplayerBan.expires() != null
214
					? Component.translatable("title.multiplayer.disabled.banned.temporary")
215
					: Component.translatable("title.multiplayer.disabled.banned.permanent");
216
			} else {
217
				return Component.translatable("title.multiplayer.disabled");
218
			}
219
		}
220
	}
221
222
	private int createDemoMenuOptions(int topPos, final int spacing) {
223
		boolean demoWorldPresent = this.checkDemoWorldPresence();
224
		this.addRenderableWidget(
225
			Button.builder(
226
					Component.translatable("menu.playdemo"),
227
					button -> {
228
						if (demoWorldPresent) {
229
							this.minecraft.createWorldOpenFlows().openWorld("Demo_World", () -> this.minecraft.gui.setScreen(this));
230
						} else {
231
							this.minecraft
232
								.createWorldOpenFlows()
233
								.createFreshLevel("Demo_World", MinecraftServer.DEMO_SETTINGS, WorldOptions.DEMO_OPTIONS, WorldPresets::createNormalWorldDimensions, this);
234
						}
235
					}
236
				)
237
				.bounds(this.width / 2 - 100, topPos, 200, 20)
238
				.build()
239
		);
240
		int var5;
241
		Button resetDemoButton = this.addRenderableWidget(
242
			Button.builder(
243
					Component.translatable("menu.resetdemo"),
244
					button -> {
245
						LevelStorageSource levelSource = this.minecraft.getLevelSource();
246
247
						try (LevelStorageAccess levelAccess = levelSource.createAccess("Demo_World")) {
248
							if (levelAccess.hasWorldData()) {
249
								this.minecraft
250
									.gui
251
									.setScreen(
252
										new ConfirmScreen(
253
											this::confirmDemo,
254
											Component.translatable("selectWorld.deleteQuestion"),
255
											Component.translatable("selectWorld.deleteWarning", MinecraftServer.DEMO_SETTINGS.levelName()),
256
											Component.translatable("selectWorld.deleteButton"),
257
											CommonComponents.GUI_CANCEL
258
										)
259
									);
260
							}
261
						} catch (IOException e) {
262
							SystemToast.onWorldAccessFailure(this.minecraft, "Demo_World");
263
							LOGGER.warn("Failed to access demo world", e);
264
						}
265
					}
266
				)
267
				.bounds(this.width / 2 - 100, var5 = topPos + spacing, 200, 20)
268
				.build()
269
		);
270
		resetDemoButton.active = demoWorldPresent;
271
		return var5;
272
	}
273
274
	private boolean checkDemoWorldPresence() {
275
		try (LevelStorageAccess levelSource = this.minecraft.getLevelSource().createAccess("Demo_World")) {
276
			return levelSource.hasWorldData();
277
		} catch (IOException e) {
278
			SystemToast.onWorldAccessFailure(this.minecraft, "Demo_World");
279
			LOGGER.warn("Failed to read demo world data", e);
280
			return false;
281
		}
282
	}
283
284
	@Override
285
	public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
286
		if (this.fadeInStart == 0L && this.fading) {
287
			this.fadeInStart = Util.getMillis();
288
		}
289
290
		float widgetFade = 1.0F;
291
		if (this.fading) {
292
			float fade = (float)(Util.getMillis() - this.fadeInStart) / 2000.0F;
293
			if (fade > 1.0F) {
294
				this.fading = false;
295
			} else {
296
				fade = Mth.clamp(fade, 0.0F, 1.0F);
297
				widgetFade = Mth.clampedMap(fade, 0.5F, 1.0F, 0.0F, 1.0F);
298
			}
299
300
			this.fadeWidgets(widgetFade);
301
		}
302
303
		this.extractPanorama(graphics, a);
304
		super.extractRenderState(graphics, mouseX, mouseY, a);
305
		this.logoRenderer.extractRenderState(graphics, this.width, this.logoRenderer.keepLogoThroughFade() ? 1.0F : widgetFade);
306
		if (this.splash != null && !this.minecraft.options.hideSplashTexts().get()) {
307
			this.splash.extractRenderState(graphics, this.width, this.font, widgetFade);
308
		}
309
310
		String versionString = "Minecraft " + SharedConstants.getCurrentVersion().name();
311
		if (this.minecraft.isDemo()) {
312
			versionString = versionString + " Demo";
313
		}
314
315
		if (Minecraft.checkModStatus().shouldReportAsModified()) {
316
			versionString = versionString + I18n.get("menu.modded");
317
		}
318
319
		graphics.text(this.font, versionString, 2, this.height - 10, ARGB.white(widgetFade));
320
		if (this.realmsNotificationsEnabled() && widgetFade >= 1.0F) {
321
			this.realmsNotificationsScreen.extractRenderState(graphics, mouseX, mouseY, a);
322
		}
323
	}
324
325
	@Override
326
	public void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
327
	}
328
329
	@Override
330
	public boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick) {
331
		return super.mouseClicked(event, doubleClick) ? true : this.realmsNotificationsEnabled() && this.realmsNotificationsScreen.mouseClicked(event, doubleClick);
332
	}
333
334
	@Override
335
	public void removed() {
336
		if (this.realmsNotificationsScreen != null) {
337
			this.realmsNotificationsScreen.removed();
338
		}
339
	}
340
341
	@Override
342
	public void added() {
343
		super.added();
344
		if (this.realmsNotificationsScreen != null) {
345
			this.realmsNotificationsScreen.added();
346
		}
347
	}
348
349
	private void confirmDemo(final boolean result) {
350
		if (result) {
351
			try (LevelStorageAccess levelSource = this.minecraft.getLevelSource().createAccess("Demo_World")) {
352
				levelSource.deleteLevel();
353
			} catch (IOException e) {
354
				SystemToast.onWorldDeleteFailure(this.minecraft, "Demo_World");
355
				LOGGER.warn("Failed to delete demo world", e);
356
			}
357
		}
358
359
		this.minecraft.gui.setScreen(this);
360
	}
361
362
	@Override
363
	public boolean canInterruptWithAnotherScreen() {
364
		return true;
365
	}
366
}
367

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