AddFriendWidget .java

net.minecraft.client.gui.screens.friends.AddFriendWidget (class): members: EMPTY_NICKNAME_MESSAGE, PROFILE_NAME_HEIGHT, PROFILE_NAME_MARGIN, INPUT_SPACING.

net.minecraft.client.gui.screens.friendsclass246 linesclientextends AbstractContainerWidget9.8 KB

API Surface (13)

boolean keyPressed(final KeyEvent event)
MutableComponent createNarrationMessage()
void applyState(final AddFriendWidget.State newState)
EditBox getEditBox()
String getValue()
void setValue(final String value)
int contentHeight()
void setX(final int x)
void setY(final int y)
void extractWidgetRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a)
void updateWidgetNarration(final NarrationElementOutput output)
Collection<? extends NarratableEntry> getNarratables()
List<? extends GuiEventListener> children()

Fields & Constants (19)

ADD_SPRITE
LIST_SEPARATOR_TOP
ENTER_NICKNAME
SEND_REQUEST
EMPTY_NICKNAME_MESSAGE
COPY_TO_CLIPBOARD
PROFILE_NAME_LABEL
WINDOW_MARGIN
SEPARATOR_HEIGHT
INPUT_SPACING
ADD_BUTTON_SIZE
SEPARATOR_PADDING
PROFILE_NAME_HEIGHT
PROFILE_NAME_MARGIN
editBox
addButton
profileNameButton
minecraft
layout

String literals (12)

"friends/send_request"
"friends/list_separator_top"
"gui.friends.enter_nickname"
"gui.friends.send_request"
"gui.friends.empty_nickname"
"gui.friends.copy_to_clipboard"
"gui.friends.my_profile_name"
"gui.friends.my_profile_name.narration"
"gui.friends.validation.cannot_add_self"
"gui.friends.validation.already_friend"
"gui.friends.validation.already_outgoing"
"gui.friends.validation.already_incoming"

Full Source

AddFriendWidget.java246 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui.screens.friends;
2
3
import java.util.Collection;
4
import java.util.List;
5
import net.fabricmc.api.EnvType;
6
import net.fabricmc.api.Environment;
7
import net.minecraft.client.Minecraft;
8
import net.minecraft.client.gui.GuiGraphicsExtractor;
9
import net.minecraft.client.gui.components.AbstractContainerWidget;
10
import net.minecraft.client.gui.components.EditBox;
11
import net.minecraft.client.gui.components.ImageWidget;
12
import net.minecraft.client.gui.components.PlainTextButton;
13
import net.minecraft.client.gui.components.SpriteIconButton;
14
import net.minecraft.client.gui.components.StringWidget;
15
import net.minecraft.client.gui.components.Tooltip;
16
import net.minecraft.client.gui.components.WidgetSprites;
17
import net.minecraft.client.gui.components.events.GuiEventListener;
18
import net.minecraft.client.gui.components.toasts.SystemToast;
19
import net.minecraft.client.gui.layouts.LinearLayout;
20
import net.minecraft.client.gui.narration.NarratableEntry;
21
import net.minecraft.client.gui.narration.NarrationElementOutput;
22
import net.minecraft.client.gui.screens.social.PlayerSocialManager;
23
import net.minecraft.client.gui.screens.social.RemoteFriendListUpdateHandler;
24
import net.minecraft.client.input.KeyEvent;
25
import net.minecraft.network.chat.Component;
26
import net.minecraft.network.chat.MutableComponent;
27
import net.minecraft.resources.Identifier;
28
import org.jspecify.annotations.Nullable;
29
30
@Environment(EnvType.CLIENT)
31
class AddFriendWidget extends AbstractContainerWidget {
32
	private static final WidgetSprites ADD_SPRITE = new WidgetSprites(Identifier.withDefaultNamespace("friends/send_request"));
33
	private static final Identifier LIST_SEPARATOR_TOP = Identifier.withDefaultNamespace("friends/list_separator_top");
34
	private static final Component ENTER_NICKNAME = Component.translatable("gui.friends.enter_nickname");
35
	private static final Component SEND_REQUEST = Component.translatable("gui.friends.send_request");
36
	private static final Component EMPTY_NICKNAME_MESSAGE = Component.translatable("gui.friends.empty_nickname");
37
	private static final Component COPY_TO_CLIPBOARD = Component.translatable("gui.friends.copy_to_clipboard");
38
	private static final Component PROFILE_NAME_LABEL = Component.translatable("gui.friends.my_profile_name").withColor(-6250336);
39
	private static final int WINDOW_MARGIN = 8;
40
	private static final int SEPARATOR_HEIGHT = 2;
41
	private static final int INPUT_SPACING = 3;
42
	private static final int ADD_BUTTON_SIZE = 20;
43
	private static final int SEPARATOR_PADDING = 4;
44
	private static final int PROFILE_NAME_HEIGHT = 9;
45
	private static final int PROFILE_NAME_MARGIN = 6;
46
	private final EditBox editBox;
47
	private final SpriteIconButton addButton;
48
	private final PlainTextButton profileNameButton;
49
	private final Minecraft minecraft = Minecraft.getInstance();
50
	private final LinearLayout layout;
51
52
	AddFriendWidget(final int width, final Runnable afterSend) {
53
		super(0, 0, width, 0, Component.empty());
54
		this.editBox = new EditBox(this.minecraft.font, width - 20 - 3 - 16, 20, ENTER_NICKNAME) {
55
			@Override
56
			public boolean keyPressed(final KeyEvent event) {
57
				boolean enterPressed = event.shortcutKey() == 13 || event.shortcutKey() == 1073741912;
58
				boolean elementsActive = this.isActive() && AddFriendWidget.this.addButton.active;
59
				if (elementsActive && this.isFocused() && enterPressed) {
60
					AddFriendWidget.this.addButton.playDownSound(AddFriendWidget.this.minecraft.getSoundManager());
61
					AddFriendWidget.this.addButton.onPress(event);
62
					return true;
63
				} else {
64
					return super.keyPressed(event);
65
				}
66
			}
67
		};
68
		this.editBox.setHint(ENTER_NICKNAME);
69
		this.editBox.setResponder(this::editBoxResponder);
70
		this.addButton = SpriteIconButton.builder(SEND_REQUEST, var2 -> {
71
			String name = this.getValue();
72
			if (name.isBlank()) {
73
				this.applyState(AddFriendWidget.State.EMPTY_INPUT);
74
			} else {
75
				Component invalidInputReason = this.getInvalidInputReason(name);
76
				if (invalidInputReason != null) {
77
					SystemToast.addOrUpdate(this.minecraft.gui.toastManager(), SystemToast.SystemToastId.FRIEND_SYSTEM_NOTIFICATION, invalidInputReason, null);
78
					this.applyState(AddFriendWidget.State.READY);
79
				} else {
80
					this.applyState(AddFriendWidget.State.SENDING);
81
					this.minecraft.getPlayerSocialManager().sendFriendRequest(name).thenAcceptAsync(var2x -> {
82
						this.editBox.setValue("");
83
						this.applyState(AddFriendWidget.State.EMPTY_INPUT);
84
						afterSend.run();
85
					}, this.minecraft);
86
				}
87
			}
88
		}, true).sprite(ADD_SPRITE, 15, 15).size(20, 20).tooltip(SEND_REQUEST).switchToLoadingAfterPress().build();
89
		this.applyState(AddFriendWidget.State.EMPTY_INPUT);
90
		String profileName = this.minecraft.getUser().getName();
91
		final Component profileNameComponent = Component.literal(profileName);
92
		int profileNameWidth = this.minecraft.font.width(profileNameComponent);
93
		this.profileNameButton = new PlainTextButton(
94
			0, 0, profileNameWidth, 9, profileNameComponent, var2 -> this.minecraft.keyboardHandler.setClipboard(profileName), this.minecraft.font
95
		) {
96
			@Override
97
			protected MutableComponent createNarrationMessage() {
98
				return wrapDefaultNarrationMessage(Component.translatable("gui.friends.my_profile_name.narration", profileNameComponent));
99
			}
100
		};
101
		this.profileNameButton.setTooltip(Tooltip.create(COPY_TO_CLIPBOARD));
102
		this.layout = LinearLayout.vertical();
103
		LinearLayout inputRow = LinearLayout.horizontal().spacing(3);
104
		inputRow.addChild(this.editBox);
105
		inputRow.addChild(this.addButton);
106
		this.layout.addChild(inputRow, settings -> settings.paddingLeft(8).paddingTop(3));
107
		this.layout.addChild(this.createProfileRow(), settings -> settings.paddingLeft(8).paddingTop(6));
108
		this.layout.addChild(ImageWidget.sprite(width, 2, LIST_SEPARATOR_TOP), settings -> settings.paddingTop(4));
109
		this.layout.arrangeElements();
110
		this.setHeight(this.layout.getHeight());
111
	}
112
113
	private LinearLayout createProfileRow() {
114
		LinearLayout profileRow = LinearLayout.horizontal();
115
		StringWidget profileNameLabel = new StringWidget(PROFILE_NAME_LABEL, this.minecraft.font);
116
		if (this.minecraft.font.isBidirectional()) {
117
			profileRow.addChild(this.profileNameButton);
118
			profileRow.addChild(profileNameLabel);
119
		} else {
120
			profileRow.addChild(profileNameLabel);
121
			profileRow.addChild(this.profileNameButton);
122
		}
123
124
		return profileRow;
125
	}
126
127
	private void editBoxResponder(final String value) {
128
		this.applyState(value.trim().isEmpty() ? AddFriendWidget.State.EMPTY_INPUT : AddFriendWidget.State.READY);
129
	}
130
131
	private @Nullable Component getInvalidInputReason(final String name) {
132
		PlayerSocialManager playerSocialManager = this.minecraft.getPlayerSocialManager();
133
		if (this.minecraft.getUser().getName().equalsIgnoreCase(name)) {
134
			return Component.translatable("gui.friends.validation.cannot_add_self");
135
		} else if (contains(playerSocialManager.getFriends(), name)) {
136
			return Component.translatable("gui.friends.validation.already_friend", name);
137
		} else if (contains(playerSocialManager.getOutgoingRequests(), name)) {
138
			return Component.translatable("gui.friends.validation.already_outgoing", name);
139
		} else {
140
			return contains(playerSocialManager.getIncomingRequests(), name) ? Component.translatable("gui.friends.validation.already_incoming", name) : null;
141
		}
142
	}
143
144
	private static boolean contains(final List<PlayerSocialManager.PlayerData> players, final String playerName) {
145
		for (PlayerSocialManager.PlayerData playerData : players) {
146
			if (playerData.name().equalsIgnoreCase(playerName)) {
147
				return true;
148
			}
149
		}
150
151
		return false;
152
	}
153
154
	public void applyState(final AddFriendWidget.State newState) {
155
		switch (newState) {
156
			case EMPTY_INPUT:
157
				this.editBox.setEditable(true);
158
				this.editBox.active = true;
159
				this.addButton.setLoading(false);
160
				this.addButton.active = false;
161
				this.addButton.setTooltip(Tooltip.create(EMPTY_NICKNAME_MESSAGE));
162
				break;
163
			case READY:
164
				RemoteFriendListUpdateHandler.State friendListState = this.minecraft.getPlayerSocialManager().getFriendListState();
165
				boolean listReady = friendListState == RemoteFriendListUpdateHandler.State.SUCCESS;
166
				this.editBox.setEditable(true);
167
				this.editBox.active = true;
168
				this.addButton.setLoading(false);
169
				this.addButton.active = listReady;
170
				this.addButton.setTooltip(Tooltip.create(SEND_REQUEST));
171
				break;
172
			case SENDING:
173
				this.editBox.setEditable(false);
174
				this.editBox.active = false;
175
				this.editBox.setFocused(false);
176
				this.addButton.active = false;
177
				this.addButton.setLoading(true);
178
				break;
179
			case DISABLED:
180
				this.editBox.setEditable(false);
181
				this.editBox.active = false;
182
				this.editBox.setFocused(false);
183
				this.addButton.active = false;
184
				this.addButton.setLoading(false);
185
		}
186
	}
187
188
	public EditBox getEditBox() {
189
		return this.editBox;
190
	}
191
192
	public String getValue() {
193
		return this.editBox.getValue().trim();
194
	}
195
196
	public void setValue(final String value) {
197
		this.editBox.setValue(value);
198
	}
199
200
	@Override
201
	protected int contentHeight() {
202
		return this.height;
203
	}
204
205
	@Override
206
	public void setX(final int x) {
207
		super.setX(x);
208
		this.layout.setX(x);
209
		this.layout.arrangeElements();
210
	}
211
212
	@Override
213
	public void setY(final int y) {
214
		super.setY(y);
215
		this.layout.setY(y);
216
		this.layout.arrangeElements();
217
	}
218
219
	@Override
220
	protected void extractWidgetRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
221
		this.layout.visitWidgets(child -> child.extractRenderState(graphics, mouseX, mouseY, a));
222
	}
223
224
	@Override
225
	protected void updateWidgetNarration(final NarrationElementOutput output) {
226
	}
227
228
	@Override
229
	public Collection<? extends NarratableEntry> getNarratables() {
230
		return List.of(this.editBox, this.addButton, this.profileNameButton);
231
	}
232
233
	@Override
234
	public List<? extends GuiEventListener> children() {
235
		return List.of(this.editBox, this.addButton, this.profileNameButton);
236
	}
237
238
	@Environment(EnvType.CLIENT)
239
	enum State {
240
		EMPTY_INPUT,
241
		READY,
242
		SENDING,
243
		DISABLED;
244
	}
245
}
246

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