Gui .java

net.minecraft.client.gui.Gui (class): members: SOCIAL_INTERACTIONS_NOT_AVAILABLE, setClientLevelTeardownInProgress, clientLevelTeardownInProgress, hud.

net.minecraft.client.guiclass479 linesclient16.5 KB

API Surface (20)

void registerReloadListeners(final ReloadableResourceManager resourceManager)
void tick()
void update()
void extractRenderState(final DeltaTracker deltaTracker, final boolean shouldRenderLevel, final boolean resourcesLoaded)
void setScreen(@Nullable Screen screen)
void setOverlay(final @Nullable Overlay overlay)
boolean isPausing()
SplashManager splashManager()
ToastManager toastManager()
ChatListener chatListener()
void addSocialInteractionsToast()
void setPauseScreen(final boolean suppressPauseMenuIfWeReallyArePausing, final boolean canGameReallyBePaused)
void handleKeybinds()
void openChatScreen(final ChatComponent.ChatMethod chatMethod)
void openChatAndAddText(final ChatComponent.ChatMethod chatMethod, final String text)
Runnable buildInitialScreens(final @Nullable GameLoadCookie cookie)
boolean canInterruptScreen()
void setClientLevelTeardownInProgress(final boolean clientLevelTeardownInProgress)
void acceptGlyph(final TextRenderable.Styled glyph)
void acceptEmptyArea(final EmptyArea empty)

Fields & Constants (15)

LOGGER
TRACY_CURRENT_SCREEN
SOCIAL_INTERACTIONS_NOT_AVAILABLE
SAVING_LEVEL
minecraft
hud
guiRenderState
screenTracySection
screen
overlay
clientLevelTeardownInProgress
splashManager
toastManager
chatListener
socialInteractionsToast

String literals (18)

"Current Screen"
"multiplayer.socialInteractions.not_available"
"menu.savingLevel"
"screen"
"Narrating screen"
"Screen details"
"Screen name"
"overlay"
"Extracting overlay render state"
"Overlay details"
"Overlay name"
"Rendering screen"
"Screen render details"
"toasts"
"setScreen called from non-game thread"
"tutorial.socialInteractions.title"
"tutorial.socialInteractions.description"
"socialInteractions"

Full Source

Gui.java479 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui;
2
3
import com.google.common.collect.Lists;
4
import com.mojang.authlib.GameProfile;
5
import com.mojang.authlib.minecraft.BanDetails;
6
import com.mojang.authlib.services.ProfileActionType;
7
import com.mojang.authlib.services.ProfileResult;
8
import com.mojang.blaze3d.Blaze3D;
9
import com.mojang.jtracy.Section;
10
import com.mojang.jtracy.SectionCategory;
11
import com.mojang.jtracy.TracyClient;
12
import com.mojang.logging.LogUtils;
13
import java.util.ArrayList;
14
import java.util.List;
15
import java.util.Set;
16
import java.util.function.Function;
17
import net.fabricmc.api.EnvType;
18
import net.fabricmc.api.Environment;
19
import net.minecraft.CrashReport;
20
import net.minecraft.CrashReportCategory;
21
import net.minecraft.ReportedException;
22
import net.minecraft.SharedConstants;
23
import net.minecraft.client.DeltaTracker;
24
import net.minecraft.client.GameLoadCookie;
25
import net.minecraft.client.InputType;
26
import net.minecraft.client.KeyMapping;
27
import net.minecraft.client.Minecraft;
28
import net.minecraft.client.Options;
29
import net.minecraft.client.gui.components.ChatComponent;
30
import net.minecraft.client.gui.components.LogoRenderer;
31
import net.minecraft.client.gui.components.toasts.ToastManager;
32
import net.minecraft.client.gui.components.toasts.TutorialToast;
33
import net.minecraft.client.gui.font.ActiveArea;
34
import net.minecraft.client.gui.font.EmptyArea;
35
import net.minecraft.client.gui.font.TextRenderable;
36
import net.minecraft.client.gui.render.TextureSetup;
37
import net.minecraft.client.gui.screens.AccessibilityOnboardingScreen;
38
import net.minecraft.client.gui.screens.BanNoticeScreens;
39
import net.minecraft.client.gui.screens.ChatScreen;
40
import net.minecraft.client.gui.screens.DeathScreen;
41
import net.minecraft.client.gui.screens.InBedChatScreen;
42
import net.minecraft.client.gui.screens.Overlay;
43
import net.minecraft.client.gui.screens.PauseScreen;
44
import net.minecraft.client.gui.screens.Screen;
45
import net.minecraft.client.gui.screens.TitleScreen;
46
import net.minecraft.client.gui.screens.advancements.AdvancementsScreen;
47
import net.minecraft.client.gui.screens.debug.DebugOptionsScreen;
48
import net.minecraft.client.gui.screens.social.SocialInteractionsScreen;
49
import net.minecraft.client.multiplayer.chat.ChatListener;
50
import net.minecraft.client.player.LocalPlayer;
51
import net.minecraft.client.quickplay.QuickPlay;
52
import net.minecraft.client.renderer.RenderPipelines;
53
import net.minecraft.client.renderer.state.gui.ColoredRectangleRenderState;
54
import net.minecraft.client.renderer.state.gui.GuiRenderState;
55
import net.minecraft.client.resources.SplashManager;
56
import net.minecraft.client.tutorial.Tutorial;
57
import net.minecraft.network.chat.Component;
58
import net.minecraft.network.chat.Style;
59
import net.minecraft.server.packs.resources.ReloadableResourceManager;
60
import net.minecraft.util.ARGB;
61
import net.minecraft.util.CommonLinks;
62
import net.minecraft.util.profiling.Profiler;
63
import net.minecraft.util.profiling.ProfilerFiller;
64
import net.minecraft.util.profiling.Zone;
65
import org.jetbrains.annotations.Contract;
66
import org.jspecify.annotations.Nullable;
67
import org.slf4j.Logger;
68
69
@Environment(EnvType.CLIENT)
70
public class Gui {
71
	private static final Logger LOGGER = LogUtils.getLogger();
72
	private static final SectionCategory TRACY_CURRENT_SCREEN = TracyClient.createSectionCategory("Current Screen");
73
	private static final Component SOCIAL_INTERACTIONS_NOT_AVAILABLE = Component.translatable("multiplayer.socialInteractions.not_available");
74
	public static final Component SAVING_LEVEL = Component.translatable("menu.savingLevel");
75
	private final Minecraft minecraft;
76
	public final Hud hud;
77
	private final GuiRenderState guiRenderState;
78
	private @Nullable Section screenTracySection;
79
	private @Nullable Screen screen;
80
	private @Nullable Overlay overlay;
81
	private boolean clientLevelTeardownInProgress;
82
	private final SplashManager splashManager;
83
	private final ToastManager toastManager;
84
	private final ChatListener chatListener;
85
	private @Nullable TutorialToast socialInteractionsToast;
86
87
	public Gui(final Minecraft minecraft, final Hud hud, final GuiRenderState guiRenderState) {
88
		this.minecraft = minecraft;
89
		this.hud = hud;
90
		this.splashManager = new SplashManager(minecraft.getUser());
91
		this.toastManager = new ToastManager(minecraft, minecraft.options);
92
		this.chatListener = new ChatListener(minecraft);
93
		this.chatListener.setMessageDelay(minecraft.options.chatDelay().get());
94
		this.guiRenderState = guiRenderState;
95
	}
96
97
	public void registerReloadListeners(final ReloadableResourceManager resourceManager) {
98
		resourceManager.registerReloadListener(this.splashManager);
99
		this.hud.registerReloadListeners(resourceManager);
100
	}
101
102
	public void tick() {
103
		ProfilerFiller profiler = Profiler.get();
104
		this.chatListener.tick();
105
		this.hud.tick(this.minecraft.isPaused());
106
		profiler.push("screen");
107
		LocalPlayer player = this.minecraft.player;
108
		if (this.screen != null || player == null) {
109
			if (this.screen instanceof InBedChatScreen inBedScreen && !player.isSleeping()) {
110
				inBedScreen.onPlayerWokeUp();
111
			}
112
		} else if (player.isDeadOrDying() && !(this.screen instanceof DeathScreen)) {
113
			this.setScreen(null);
114
		} else if (player.isSleeping() && this.minecraft.level != null) {
115
			this.hud.getChat().openScreen(ChatComponent.ChatMethod.MESSAGE, InBedChatScreen::new);
116
		}
117
118
		if (this.screen != null) {
119
			this.screen.tick();
120
		}
121
122
		profiler.pop();
123
		if (this.overlay != null) {
124
			this.overlay.tick();
125
		}
126
127
		if (!this.minecraft.getDebugOverlay().showDebugScreen()) {
128
			this.hud.clearCache();
129
		}
130
	}
131
132
	public void update() {
133
		this.toastManager.update();
134
135
		try {
136
			if (this.screen != null) {
137
				this.screen.handleDelayedNarration();
138
			}
139
		} catch (Throwable t) {
140
			CrashReport report = CrashReport.forThrowable(t, "Narrating screen");
141
			CrashReportCategory category = report.addCategory("Screen details");
142
			category.setDetail("Screen name", () -> this.screen.getClass().getCanonicalName());
143
			throw new ReportedException(report);
144
		}
145
	}
146
147
	public void extractRenderState(final DeltaTracker deltaTracker, final boolean shouldRenderLevel, final boolean resourcesLoaded) {
148
		ProfilerFiller profiler = Profiler.get();
149
		int xMouse = (int)this.minecraft.mouseHandler.getScaledXPos(this.minecraft.getWindow());
150
		int yMouse = (int)this.minecraft.mouseHandler.getScaledYPos(this.minecraft.getWindow());
151
		profiler.push("gui");
152
		this.guiRenderState.reset();
153
		GuiGraphicsExtractor graphics = new GuiGraphicsExtractor(this.minecraft, this.guiRenderState, xMouse, yMouse);
154
		if (shouldRenderLevel) {
155
			profiler.push("hud");
156
			this.hud.extractRenderState(graphics, deltaTracker);
157
			profiler.pop();
158
		}
159
160
		if (this.overlay != null) {
161
			profiler.push("overlay");
162
163
			try {
164
				this.overlay.extractRenderState(graphics, xMouse, yMouse, deltaTracker.getGameTimeDeltaTicks());
165
			} catch (Throwable t) {
166
				CrashReport report = CrashReport.forThrowable(t, "Extracting overlay render state");
167
				CrashReportCategory category = report.addCategory("Overlay details");
168
				category.setDetail("Overlay name", () -> this.overlay.getClass().getCanonicalName());
169
				throw new ReportedException(report);
170
			}
171
172
			profiler.pop();
173
		} else if (resourcesLoaded && this.screen != null) {
174
			profiler.push("screen");
175
176
			try {
177
				this.screen.extractRenderStateWithTooltipAndSubtitles(graphics, xMouse, yMouse, deltaTracker.getGameTimeDeltaTicks());
178
			} catch (Throwable t) {
179
				CrashReport report = CrashReport.forThrowable(t, "Rendering screen");
180
				CrashReportCategory category = report.addCategory("Screen render details");
181
				category.setDetail("Screen name", () -> this.screen.getClass().getCanonicalName());
182
				this.minecraft.mouseHandler.fillMousePositionDetails(category, this.minecraft.getWindow());
183
				throw new ReportedException(report);
184
			}
185
186
			if (SharedConstants.DEBUG_CURSOR_POS) {
187
				this.minecraft.mouseHandler.drawDebugMouseInfo(this.minecraft.font, graphics);
188
			}
189
190
			profiler.pop();
191
		}
192
193
		if (shouldRenderLevel) {
194
			this.hud.extractSavingIndicator(graphics, deltaTracker);
195
		}
196
197
		if (resourcesLoaded) {
198
			try (Zone ignored = profiler.zone("toasts")) {
199
				this.toastManager().extractRenderState(graphics);
200
			}
201
		}
202
203
		if (!(this.screen instanceof DebugOptionsScreen)) {
204
			this.hud.extractDebugOverlay(graphics);
205
		}
206
207
		this.hud.extractDeferredSubtitles();
208
		if (SharedConstants.DEBUG_ACTIVE_TEXT_AREAS) {
209
			this.renderActiveTextDebug();
210
		}
211
212
		profiler.pop();
213
		graphics.applyCursor(this.minecraft.getWindow());
214
	}
215
216
	@Contract(pure = true)
217
	public @Nullable Screen screen() {
218
		return this.screen;
219
	}
220
221
	public void setScreen(@Nullable Screen screen) {
222
		if (SharedConstants.IS_RUNNING_IN_IDE && Thread.currentThread() != this.minecraft.getRunningThread()) {
223
			LOGGER.error("setScreen called from non-game thread");
224
		}
225
226
		if (this.screenTracySection != null) {
227
			this.screenTracySection.close();
228
			this.screenTracySection = null;
229
		}
230
231
		if (this.screen != null) {
232
			this.screen.clearFocus();
233
			this.minecraft.textInputManager().stopTextInput();
234
			this.screen.removed();
235
		} else {
236
			this.minecraft.setLastInputType(InputType.NONE);
237
		}
238
239
		if (screen == null) {
240
			if (this.clientLevelTeardownInProgress) {
241
				throw new IllegalStateException("Trying to return to in-game GUI during disconnection");
242
			}
243
244
			if (this.minecraft.level == null) {
245
				screen = new TitleScreen();
246
			} else if (this.minecraft.player.isDeadOrDying()) {
247
				if (this.minecraft.player.shouldShowDeathScreen()) {
248
					screen = new DeathScreen(null, this.minecraft.level.getLevelData().isHardcore(), this.minecraft.player);
249
				} else {
250
					this.minecraft.player.respawn();
251
				}
252
			} else {
253
				screen = this.hud.getChat().restoreChatScreen();
254
			}
255
		}
256
257
		this.screen = screen;
258
		if (this.screen != null) {
259
			this.screen.added();
260
			String name = this.screen.getTitle().getString();
261
			this.screenTracySection = TRACY_CURRENT_SCREEN.enterSection(name.isBlank() ? this.screen.getClass().getSimpleName() : name);
262
		}
263
264
		if (screen != null) {
265
			this.minecraft.mouseHandler.releaseMouse();
266
			KeyMapping.releaseAll();
267
			screen.init(this.minecraft.getWindow().getGuiScaledWidth(), this.minecraft.getWindow().getGuiScaledHeight());
268
		} else {
269
			this.minecraft.textInputManager().stopTextInput();
270
			if (this.minecraft.level != null) {
271
				KeyMapping.restoreToggleStatesOnScreenClosed();
272
			}
273
274
			this.minecraft.getSoundManager().resume();
275
			this.minecraft.mouseHandler.grabMouse();
276
		}
277
278
		this.minecraft.mouseHandler.resyncMousePosition();
279
		this.minecraft.updateTitle();
280
	}
281
282
	@Contract(pure = true)
283
	public @Nullable Overlay overlay() {
284
		return this.overlay;
285
	}
286
287
	public void setOverlay(final @Nullable Overlay overlay) {
288
		this.overlay = overlay;
289
	}
290
291
	public boolean isPausing() {
292
		return this.screen != null && this.screen.isPauseScreen() || this.overlay != null && this.overlay.isPausing();
293
	}
294
295
	public SplashManager splashManager() {
296
		return this.splashManager;
297
	}
298
299
	public ToastManager toastManager() {
300
		return this.toastManager;
301
	}
302
303
	public ChatListener chatListener() {
304
		return this.chatListener;
305
	}
306
307
	public void addSocialInteractionsToast() {
308
		Component title = Component.translatable("tutorial.socialInteractions.title");
309
		Component message = Component.translatable("tutorial.socialInteractions.description", Tutorial.key("socialInteractions"));
310
		this.socialInteractionsToast = new TutorialToast(this.minecraft.font, TutorialToast.Icons.SOCIAL_INTERACTIONS, title, message, true, 8000);
311
		this.toastManager.addToast(this.socialInteractionsToast);
312
	}
313
314
	public void setPauseScreen(final boolean suppressPauseMenuIfWeReallyArePausing, final boolean canGameReallyBePaused) {
315
		if (this.screen == null) {
316
			if (canGameReallyBePaused) {
317
				this.setScreen(new PauseScreen(!suppressPauseMenuIfWeReallyArePausing));
318
			} else {
319
				this.setScreen(new PauseScreen(true));
320
			}
321
		}
322
	}
323
324
	public void handleKeybinds() {
325
		Options options = this.minecraft.options;
326
327
		while (options.keyToggleGui.consumeClick()) {
328
			this.hud.toggle();
329
		}
330
331
		while (options.keyAdvancements.consumeClick()) {
332
			this.setScreen(new AdvancementsScreen(this.minecraft.player.connection.getAdvancements()));
333
		}
334
335
		while (options.keySocialInteractions.consumeClick()) {
336
			if (!this.minecraft.isMultiplayerServer() && !SharedConstants.DEBUG_SOCIAL_INTERACTIONS) {
337
				this.chatListener.handleOverlay(SOCIAL_INTERACTIONS_NOT_AVAILABLE);
338
				this.minecraft.getNarrator().saySystemNow(SOCIAL_INTERACTIONS_NOT_AVAILABLE);
339
			} else {
340
				if (this.socialInteractionsToast != null) {
341
					this.socialInteractionsToast.hide();
342
					this.socialInteractionsToast = null;
343
				}
344
345
				this.setScreen(new SocialInteractionsScreen());
346
			}
347
		}
348
349
		while (options.keyChat.consumeClick()) {
350
			this.openChatScreen(ChatComponent.ChatMethod.MESSAGE);
351
		}
352
353
		if (this.screen == null && this.overlay == null && options.keyCommand.consumeClick()) {
354
			this.openChatScreen(ChatComponent.ChatMethod.COMMAND);
355
		}
356
	}
357
358
	public void openChatScreen(final ChatComponent.ChatMethod chatMethod) {
359
		if (this.minecraft.player != null) {
360
			this.hud.getChat().openScreen(chatMethod, ChatScreen::new);
361
		}
362
	}
363
364
	public void openChatAndAddText(final ChatComponent.ChatMethod chatMethod, final String text) {
365
		this.openChatScreen(chatMethod);
366
		if (this.screen instanceof ChatScreen chatScreen) {
367
			chatScreen.insertText(text, false);
368
		}
369
	}
370
371
	public Runnable buildInitialScreens(final @Nullable GameLoadCookie cookie) {
372
		List<Function<Runnable, Screen>> screens = new ArrayList<>();
373
		boolean onboardingScreenAdded = this.addInitialScreens(screens);
374
		Runnable nextStep = () -> {
375
			if (cookie != null && cookie.quickPlayData().isEnabled()) {
376
				QuickPlay.connect(this.minecraft, cookie.quickPlayData().variant(), cookie.realmsClient());
377
			} else {
378
				this.setScreen(new TitleScreen(true, new LogoRenderer(onboardingScreenAdded)));
379
			}
380
		};
381
382
		for (Function<Runnable, Screen> function : Lists.reverse(screens)) {
383
			Screen screen = function.apply(nextStep);
384
			nextStep = () -> this.setScreen(screen);
385
		}
386
387
		return nextStep;
388
	}
389
390
	private boolean addInitialScreens(final List<Function<Runnable, Screen>> screens) {
391
		boolean onboardingScreenAdded = false;
392
		if (this.minecraft.options.onboardAccessibility || SharedConstants.DEBUG_FORCE_ONBOARDING_SCREEN) {
393
			screens.add(next -> new AccessibilityOnboardingScreen(this.minecraft.options, next));
394
			onboardingScreenAdded = true;
395
		}
396
397
		BanDetails multiplayerBan = this.minecraft.multiplayerBan();
398
		if (multiplayerBan != null) {
399
			screens.add(next -> BanNoticeScreens.create(result -> {
400
				if (result) {
401
					Blaze3D.openUri(CommonLinks.SUSPENSION_HELP);
402
				}
403
404
				next.run();
405
			}, multiplayerBan));
406
		}
407
408
		ProfileResult profileResult = this.minecraft.getProfileResult();
409
		if (profileResult != null) {
410
			GameProfile profile = profileResult.profile();
411
			Set<ProfileActionType> actions = profileResult.actions();
412
			if (actions.contains(ProfileActionType.FORCED_NAME_CHANGE)) {
413
				screens.add(onClose -> BanNoticeScreens.createNameBan(profile.name(), onClose));
414
			}
415
416
			if (actions.contains(ProfileActionType.USING_BANNED_SKIN)) {
417
				screens.add(BanNoticeScreens::createSkinBan);
418
			}
419
		}
420
421
		return onboardingScreenAdded;
422
	}
423
424
	public boolean canInterruptScreen() {
425
		return (this.screen == null || this.screen.canInterruptWithAnotherScreen()) && !this.clientLevelTeardownInProgress;
426
	}
427
428
	public void setClientLevelTeardownInProgress(final boolean clientLevelTeardownInProgress) {
429
		this.clientLevelTeardownInProgress = clientLevelTeardownInProgress;
430
	}
431
432
	private void renderActiveTextDebug() {
433
		this.guiRenderState.nextStratum();
434
		this.guiRenderState
435
			.forEachText(
436
				text -> text.ensurePrepared()
437
					.visit(
438
						new Font.GlyphVisitor() {
439
							private int index;
440
441
							@Override
442
							public void acceptGlyph(final TextRenderable.Styled glyph) {
443
								this.renderDebugMarkers(glyph, false);
444
							}
445
446
							@Override
447
							public void acceptEmptyArea(final EmptyArea empty) {
448
								this.renderDebugMarkers(empty, true);
449
							}
450
451
							private void renderDebugMarkers(final ActiveArea glyph, final boolean isEmpty) {
452
								int intensity = (isEmpty ? 128 : 255) - (this.index++ & 1) * 64;
453
								Style style = glyph.style();
454
								int red = style.getClickEvent() != null ? intensity : 0;
455
								int green = style.getHoverEvent() != null ? intensity : 0;
456
								int blue = red != 0 && green != 0 ? 0 : intensity;
457
								int color = ARGB.color(128, red, green, blue);
458
								Gui.this.guiRenderState
459
									.addGuiElement(
460
										new ColoredRectangleRenderState(
461
											RenderPipelines.GUI,
462
											TextureSetup.noTexture(),
463
											text.pose,
464
											(int)glyph.activeLeft(),
465
											(int)glyph.activeTop(),
466
											(int)glyph.activeRight(),
467
											(int)glyph.activeBottom(),
468
											color,
469
											color,
470
											text.scissor
471
										)
472
									);
473
							}
474
						}
475
					)
476
			);
477
	}
478
}
479

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