FriendsOverlayScreen .java

net.minecraft.client.gui.screens.friends.FriendsOverlayScreen (class, 361 lines): extends Screen. members: ERROR_USER_MAY_LACK_ACTIVE_PROFILE, friendsTab.

net.minecraft.client.gui.screens.friendsclass361 linesclientextends Screen13.1 KB

API Surface (12)

void added()
void removed()
void init()
int getOverlayWidth()
void repositionElements()
void tick()
void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a)
boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick)
boolean keyPressed(final KeyEvent event)
void onClose()
void refreshLists()
void applyPresenceUpdate()

Fields & Constants (26)

LOGGER
TITLE
BACKGROUND_SPRITE
LOADING_FRIENDS
LOADING_REQUESTS
ERROR_UPGRADE_NEEDED
ERROR_CONNECTION_ISSUE
ERROR_TEMPORARY_UNAVAILABLE
ERROR_USER_MAY_LACK_ACTIVE_PROFILE
ERROR_UNAUTHORIZED
ERROR_GENERIC
ERROR_TOAST_GENERIC
BG_BORDER_WIDTH
OVERLAY_WIDTH
TAB_BUTTON_WIDTH
TAB_BUTTON_HEIGHT
backgroundScreen
friendsTab
pendingTab
tabNavigationBar
layout
contentLayout
pendingTabButton
tabManager
friendListUpdateListener
pendingFriendRemovals

String literals (13)

"gui.friends.open"
"friends/background"
"gui.friends.loading_friends"
"gui.friends.loading_requests"
"gui.friends.error.upgrade_needed"
"gui.friends.error.connection_issue"
"gui.friends.error.temporary_unavailable"
"gui.friends.error.user_may_lack_active_profile"
"gui.friends.error.unauthorized"
"gui.friends.error.generic"
"gui.friends.toast.generic_error"
"gui.friends.requests_count"
"Friend action failed"

Full Source

FriendsOverlayScreen.java361 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui.screens.friends;
2
3
import com.mojang.authlib.services.response.PresenceResponse;
4
import com.mojang.authlib.services.response.PresenceStatusDto;
5
import com.mojang.logging.LogUtils;
6
import java.util.ArrayList;
7
import java.util.Comparator;
8
import java.util.HashSet;
9
import java.util.List;
10
import java.util.Set;
11
import java.util.UUID;
12
import net.fabricmc.api.EnvType;
13
import net.fabricmc.api.Environment;
14
import net.minecraft.client.gui.GuiGraphicsExtractor;
15
import net.minecraft.client.gui.components.LoadingDotsWidget;
16
import net.minecraft.client.gui.components.tabs.Tab;
17
import net.minecraft.client.gui.components.tabs.TabManager;
18
import net.minecraft.client.gui.components.tabs.TabNavigationBar;
19
import net.minecraft.client.gui.components.toasts.SystemToast;
20
import net.minecraft.client.gui.layouts.FrameLayout;
21
import net.minecraft.client.gui.layouts.LinearLayout;
22
import net.minecraft.client.gui.screens.Screen;
23
import net.minecraft.client.gui.screens.social.PlayerSocialManager;
24
import net.minecraft.client.gui.screens.social.RemoteFriendListUpdateHandler;
25
import net.minecraft.client.input.KeyEvent;
26
import net.minecraft.client.input.MouseButtonEvent;
27
import net.minecraft.client.renderer.RenderPipelines;
28
import net.minecraft.network.chat.Component;
29
import net.minecraft.resources.Identifier;
30
import org.jspecify.annotations.Nullable;
31
import org.slf4j.Logger;
32
33
@Environment(EnvType.CLIENT)
34
public class FriendsOverlayScreen extends Screen {
35
	private static final Logger LOGGER = LogUtils.getLogger();
36
	private static final Component TITLE = Component.translatable("gui.friends.open");
37
	private static final Identifier BACKGROUND_SPRITE = Identifier.withDefaultNamespace("friends/background");
38
	private static final Component LOADING_FRIENDS = Component.translatable("gui.friends.loading_friends");
39
	private static final Component LOADING_REQUESTS = Component.translatable("gui.friends.loading_requests");
40
	private static final Component ERROR_UPGRADE_NEEDED = Component.translatable("gui.friends.error.upgrade_needed");
41
	private static final Component ERROR_CONNECTION_ISSUE = Component.translatable("gui.friends.error.connection_issue");
42
	private static final Component ERROR_TEMPORARY_UNAVAILABLE = Component.translatable("gui.friends.error.temporary_unavailable");
43
	private static final Component ERROR_USER_MAY_LACK_ACTIVE_PROFILE = Component.translatable("gui.friends.error.user_may_lack_active_profile");
44
	private static final Component ERROR_UNAUTHORIZED = Component.translatable("gui.friends.error.unauthorized");
45
	private static final Component ERROR_GENERIC = Component.translatable("gui.friends.error.generic");
46
	private static final Component ERROR_TOAST_GENERIC = Component.translatable("gui.friends.toast.generic_error");
47
	private static final int BG_BORDER_WIDTH = 8;
48
	private static final int OVERLAY_WIDTH = 220;
49
	public static final int TAB_BUTTON_WIDTH = 110;
50
	private static final int TAB_BUTTON_HEIGHT = 20;
51
	private final @Nullable Screen backgroundScreen;
52
	private @Nullable FriendsTab friendsTab;
53
	private @Nullable PendingTab pendingTab;
54
	private @Nullable TabNavigationBar tabNavigationBar;
55
	private @Nullable LinearLayout layout;
56
	private @Nullable LinearLayout contentLayout;
57
	private @Nullable FriendsOverlayTabButton pendingTabButton;
58
	private final TabManager tabManager;
59
	private final Runnable friendListUpdateListener = this::onFriendListUpdate;
60
	private final Set<UUID> pendingFriendRemovals = new HashSet<>();
61
62
	public FriendsOverlayScreen(final @Nullable Screen backgroundScreen) {
63
		super(TITLE);
64
		this.backgroundScreen = backgroundScreen;
65
		this.tabManager = new TabManager(x$0 -> this.addRenderableWidget(x$0), x$0 -> this.removeWidget(x$0), this::selectTab, this::deselectTab);
66
	}
67
68
	@Override
69
	public void added() {
70
		super.added();
71
		if (this.backgroundScreen != null) {
72
			this.backgroundScreen.clearFocus();
73
		}
74
75
		this.minecraft.getPlayerSocialManager().addFriendListUpdateListener(this.friendListUpdateListener);
76
	}
77
78
	@Override
79
	public void removed() {
80
		this.minecraft.getPlayerSocialManager().removeFriendListUpdateListener(this.friendListUpdateListener);
81
		super.removed();
82
	}
83
84
	private void onFriendListUpdate() {
85
		if (this.minecraft.gui.screen() == this && this.minecraft.getPlayerSocialManager().isFriendListEnabled()) {
86
			this.refreshLists();
87
		}
88
	}
89
90
	@Override
91
	protected void init() {
92
		if (this.backgroundScreen != null) {
93
			this.backgroundScreen.init(this.width, this.height);
94
		}
95
96
		this.layout = LinearLayout.vertical();
97
		int scrollableMaxHeight = this.height - 80;
98
		this.friendsTab = new FriendsTab(this.minecraft, new LoadingDotsWidget(this.font, LOADING_FRIENDS), this, 220, scrollableMaxHeight);
99
		this.pendingTab = new PendingTab(this.minecraft, new LoadingDotsWidget(this.font, LOADING_REQUESTS), this, 220, scrollableMaxHeight);
100
		this.pendingTabButton = new FriendsOverlayTabButton(this.tabManager, this.pendingTab, 110, 20);
101
		this.tabNavigationBar = TabNavigationBar.builder(this.tabManager, 0, 0, 220, 20)
102
			.addTab(new FriendsOverlayTabButton(this.tabManager, this.friendsTab, 110, 20), this.friendsTab)
103
			.addTab(this.pendingTabButton, this.pendingTab)
104
			.build();
105
		this.addRenderableWidget(this.tabNavigationBar);
106
		this.contentLayout = this.layout.addChild(LinearLayout.vertical());
107
		this.tabManager.setCurrentTab(this.friendsTab, false, false);
108
		this.layout.visitWidgets(x$0 -> this.addRenderableWidget(x$0));
109
		this.repositionElements();
110
		this.refreshLists();
111
	}
112
113
	public int getOverlayWidth() {
114
		return 220;
115
	}
116
117
	@Override
118
	protected void repositionElements() {
119
		if (this.backgroundScreen != null) {
120
			this.backgroundScreen.resize(this.width, this.height);
121
		}
122
123
		this.friendsTab.setHeight(this.height - 80);
124
		this.pendingTab.setHeight(this.height - 80);
125
		this.layout.arrangeElements();
126
		FrameLayout.alignInRectangle(this.layout, this.getRectangle(), 0.5F, 0.5F);
127
		this.tabNavigationBar.setPosition(this.layout.getX(), this.layout.getY() - 20 - 7);
128
		this.tabNavigationBar.arrangeElements(this.width);
129
	}
130
131
	@Override
132
	public void tick() {
133
		super.tick();
134
		this.minecraft.getPlayerSocialManager().getPresenceHandler().tryUpdatePresence();
135
	}
136
137
	@Override
138
	public void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
139
		if (this.backgroundScreen != null) {
140
			this.backgroundScreen.extractBackground(graphics, mouseX, mouseY, a);
141
			graphics.nextStratum();
142
			this.backgroundScreen.extractRenderState(graphics, -1, -1, a);
143
			graphics.nextStratum();
144
			this.extractBlurredBackground(graphics);
145
		} else {
146
			super.extractBackground(graphics, mouseX, mouseY, a);
147
		}
148
149
		graphics.blitSprite(
150
			RenderPipelines.GUI_TEXTURED,
151
			BACKGROUND_SPRITE,
152
			this.layout.getX() - 8,
153
			this.layout.getY() - 8,
154
			this.layout.getWidth() + 16,
155
			this.layout.getHeight() + 16 + 1
156
		);
157
	}
158
159
	private void selectTab(final Tab tab) {
160
		if (this.contentLayout != null) {
161
			this.contentLayout.addChild(tab.getLayout());
162
			this.repositionElements();
163
		}
164
	}
165
166
	private void deselectTab(final Tab tab) {
167
		if (this.contentLayout != null) {
168
			this.contentLayout.removeChildren();
169
		}
170
	}
171
172
	@Override
173
	public boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick) {
174
		int panelLeft = this.layout.getX() - 8;
175
		int panelRight = this.layout.getX() + this.layout.getWidth() + 8;
176
		int panelTop = this.tabNavigationBar.getY();
177
		int panelBottom = this.layout.getY() + this.layout.getHeight() + 16;
178
		if (!(event.x() < panelLeft) && !(event.x() > panelRight) && !(event.y() < panelTop) && !(event.y() > panelBottom)) {
179
			return super.mouseClicked(event, doubleClick);
180
		}
181
182
		this.minecraft.gui.setScreen(this.backgroundScreen);
183
		return true;
184
	}
185
186
	@Override
187
	public boolean keyPressed(final KeyEvent event) {
188
		return this.tabNavigationBar != null && this.tabNavigationBar.keyPressed(event) ? true : super.keyPressed(event);
189
	}
190
191
	@Override
192
	public void onClose() {
193
		this.minecraft.gui.setScreen(this.backgroundScreen);
194
	}
195
196
	public void refreshLists() {
197
		PlayerSocialManager playerSocialManager = this.minecraft.getPlayerSocialManager();
198
		RemoteFriendListUpdateHandler.State state = playerSocialManager.getFriendListState();
199
		switch (state) {
200
			case LOADING:
201
				this.friendsTab.showLoading();
202
				this.pendingTab.showLoading();
203
				break;
204
			case UPGRADE_NEEDED:
205
				this.showError(ERROR_UPGRADE_NEEDED);
206
				break;
207
			case CONNECTION_ISSUE:
208
				this.showError(ERROR_CONNECTION_ISSUE);
209
				break;
210
			case TEMPORARY_UNAVAILABLE:
211
				this.showError(ERROR_TEMPORARY_UNAVAILABLE);
212
				break;
213
			case USER_MAY_LACK_ACTIVE_PROFILE:
214
				this.showError(ERROR_USER_MAY_LACK_ACTIVE_PROFILE);
215
				break;
216
			case UNAUTHORIZED:
217
				this.showError(ERROR_UNAUTHORIZED);
218
				break;
219
			case GENERIC_ERROR:
220
				this.showError(ERROR_GENERIC);
221
				break;
222
			case SUCCESS:
223
				this.populateLists(playerSocialManager);
224
		}
225
226
		if (this.layout != null) {
227
			this.layout.arrangeElements();
228
			FrameLayout.alignInRectangle(this.layout, this.getRectangle(), 0.5F, 0.5F);
229
		}
230
	}
231
232
	public void applyPresenceUpdate() {
233
		if (this.friendsTab != null) {
234
			this.friendsTab.applyPresenceUpdate(this.minecraft.getPlayerSocialManager().getPresenceHandler().getLatestPresence());
235
		}
236
	}
237
238
	private void showError(final Component message) {
239
		this.friendsTab.showError(message);
240
		this.pendingTab.showError(message);
241
	}
242
243
	private void populateLists(final PlayerSocialManager playerSocialManager) {
244
		List<PlayerSocialManager.PlayerData> friends = playerSocialManager.getFriends();
245
		List<PlayerSocialManager.PlayerData> incomingRequests = playerSocialManager.getIncomingRequests();
246
		List<PlayerSocialManager.PlayerData> outgoingRequests = playerSocialManager.getOutgoingRequests();
247
		PresenceResponse latestPresence = this.minecraft.getPlayerSocialManager().getPresenceHandler().getLatestPresence();
248
		if (friends.isEmpty()) {
249
			this.friendsTab.showEmpty();
250
		} else {
251
			List<FriendEntry> entries = new ArrayList<>(friends.size());
252
253
			for (PlayerSocialManager.PlayerData friend : friends) {
254
				PresenceStatusDto presence = null;
255
256
				for (PresenceStatusDto presenceStatusDto : latestPresence.presence()) {
257
					if (presenceStatusDto.profileId().equals(friend.id())) {
258
						presence = presenceStatusDto;
259
						break;
260
					}
261
				}
262
263
				UUID friendId = friend.id();
264
				boolean removalPending = this.pendingFriendRemovals.contains(friendId);
265
				entries.add(
266
					new FriendEntry(
267
						this.minecraft,
268
						this,
269
						friend,
270
						presence,
271
						removalPending,
272
						() -> {
273
							this.pendingFriendRemovals.add(friendId);
274
							this.minecraft
275
								.getPlayerSocialManager()
276
								.removeFriend(friendId)
277
								.whenCompleteAsync((var2x, var3x) -> this.pendingFriendRemovals.remove(friendId), this.minecraft)
278
								.thenAcceptAsync(var1x -> this.refreshLists(), this.minecraft)
279
								.exceptionally(this::onActionFailed);
280
						}
281
					)
282
				);
283
			}
284
285
			entries.sort(Comparator.comparingInt(FriendEntry::presenceStatusSortOrder));
286
			this.friendsTab.updateEntries(entries);
287
		}
288
289
		int incomingRequestsCount = incomingRequests.size();
290
		if (this.pendingTabButton != null) {
291
			this.pendingTabButton.setMessage(Component.translatable("gui.friends.requests_count", incomingRequestsCount));
292
		}
293
294
		int totalRequestsCount = incomingRequestsCount + outgoingRequests.size();
295
		if (totalRequestsCount == 0) {
296
			this.pendingTab.showEmpty();
297
		} else {
298
			List<IncomingEntry> incomingEntries = new ArrayList<>(incomingRequests.size());
299
			if (!incomingRequests.isEmpty()) {
300
				for (PlayerSocialManager.PlayerData incomingRequest : incomingRequests) {
301
					incomingEntries.add(
302
						new IncomingEntry(
303
							this.minecraft,
304
							this,
305
							incomingRequest,
306
							() -> this.minecraft
307
								.getPlayerSocialManager()
308
								.acceptIncomingFriendRequest(incomingRequest.id())
309
								.thenAcceptAsync(var1x -> this.refreshLists(), this.minecraft)
310
								.exceptionally(this::onActionFailed),
311
							() -> this.minecraft
312
								.getPlayerSocialManager()
313
								.declineIncomingFriendRequest(incomingRequest.id())
314
								.thenRunAsync(this::refreshLists, this.minecraft)
315
								.exceptionally(this::onActionFailed)
316
						)
317
					);
318
				}
319
			}
320
321
			List<OutgoingEntry> outgoingEntries = new ArrayList<>(outgoingRequests.size());
322
			if (!outgoingRequests.isEmpty()) {
323
				for (PlayerSocialManager.PlayerData outgoingRequest : outgoingRequests) {
324
					outgoingEntries.add(
325
						new OutgoingEntry(
326
							this.minecraft,
327
							this,
328
							outgoingRequest,
329
							() -> this.minecraft
330
								.getPlayerSocialManager()
331
								.revokeOutgoingFriendRequest(outgoingRequest.id())
332
								.thenRunAsync(this::refreshLists, this.minecraft)
333
								.exceptionally(this::onActionFailed)
334
						)
335
					);
336
				}
337
			}
338
339
			this.pendingTab.updateEntries(incomingEntries, outgoingEntries);
340
			if (this.layout != null) {
341
				this.layout.arrangeElements();
342
				FrameLayout.alignInRectangle(this.layout, this.getRectangle(), 0.5F, 0.5F);
343
			}
344
		}
345
	}
346
347
	void startFriendAction() {
348
		this.friendsTab.disable();
349
		this.pendingTab.disable();
350
	}
351
352
	private @Nullable Void onActionFailed(final Throwable ex) {
353
		LOGGER.error("Friend action failed", ex);
354
		this.minecraft.execute(() -> {
355
			SystemToast.addOrUpdate(this.minecraft.gui.toastManager(), SystemToast.SystemToastId.FRIEND_SYSTEM_NOTIFICATION, ERROR_TOAST_GENERIC, null);
356
			this.refreshLists();
357
		});
358
		return null;
359
	}
360
}
361

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