PlayerSocialManager .java

net.minecraft.client.gui.screens.social.PlayerSocialManager (class, 241 lines): members: FRIEND_ACTION_RATE_LIMITED_MESSAGE, FRIEND_ACTION_UNKNOWN_PROFILE.

net.minecraft.client.gui.screens.socialclass241 linesclient8.5 KB

API Surface (30)

void addFriendListUpdateListener(final Runnable listener)
void removeFriendListUpdateListener(final Runnable listener)
List<PlayerSocialManager.PlayerData> getFriends()
List<PlayerSocialManager.PlayerData> getIncomingRequests()
List<PlayerSocialManager.PlayerData> getOutgoingRequests()
RemoteFriendListUpdateHandler.State getFriendListState()
void hidePlayer(final UUID id)
void showPlayer(final UUID id)
boolean shouldHideMessageFrom(final UUID id)
boolean isHidden(final UUID id)
void startOnlineMode()
void stopOnlineMode()
boolean isBlocked(final UUID id)
Set<UUID> getHiddenPlayers()
UUID getDiscoveredUUID(final String name)
void addPlayer(final PlayerInfo info)
CompletableFuture<ResultCode> sendFriendRequest(final String name)
void removePlayer(final UUID id)
CompletableFuture<ResultCode> removeFriend(final UUID id)
CompletableFuture<ResultCode> acceptIncomingFriendRequest(final UUID id)
CompletableFuture<ResultCode> declineIncomingFriendRequest(final UUID id)
CompletableFuture<ResultCode> revokeOutgoingFriendRequest(final UUID id)
CompletableFuture<ResultCode> updateFriendSettings(final boolean friendsListEnabled, final boolean allowInvites)
boolean isFriendListEnabled()
void setFriendListEnabled(final boolean friendListEnabled)
boolean isAllowFriendRequests()
void setAllowFriendRequests(final boolean allowFriendRequests)
PresenceHandler getPresenceHandler()
boolean isFriend(final UUID uuid)
record PlayerData(UUID id, String name)

Fields & Constants (17)

FRIEND_ACTION_FAILED_MESSAGE
FRIEND_ACTION_RATE_LIMITED_MESSAGE
FRIEND_ACTION_FORBIDDEN_MESSAGE
FRIEND_ACTION_UNKNOWN_PROFILE
FRIEND_ACTION_UNAUTHORIZED
FRIEND_ACTION_UNAVAILABLE_MESSAGE
minecraft
hiddenPlayers
service
friendsService
presenceHandler
discoveredNamesToUUID
onlineMode
pendingBlockListRefresh
remoteFriendListUpdateHandler
friendListEnabled
allowFriendRequests

String literals (6)

"gui.friends.error.failed.message"
"gui.friends.error.rateLimited.message"
"gui.friends.error.forbidden.message"
"gui.friends.error.user_may_lack_active_profile"
"gui.friends.error.unauthorized"
"gui.friends.error.unavailable.message"

Full Source

PlayerSocialManager.java241 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui.screens.social;
2
3
import com.google.common.collect.Maps;
4
import com.google.common.collect.Sets;
5
import com.mojang.authlib.GameProfile;
6
import com.mojang.authlib.minecraft.UserApiService;
7
import com.mojang.authlib.services.FriendsService;
8
import com.mojang.authlib.services.FriendsService.ResultCode;
9
import com.mojang.authlib.services.response.FriendDto;
10
import java.util.List;
11
import java.util.Map;
12
import java.util.Set;
13
import java.util.UUID;
14
import java.util.concurrent.CompletableFuture;
15
import java.util.function.Supplier;
16
import net.fabricmc.api.EnvType;
17
import net.fabricmc.api.Environment;
18
import net.minecraft.client.Minecraft;
19
import net.minecraft.client.gui.components.toasts.SystemToast;
20
import net.minecraft.client.multiplayer.PlayerInfo;
21
import net.minecraft.network.chat.Component;
22
import net.minecraft.util.Util;
23
24
@Environment(EnvType.CLIENT)
25
public class PlayerSocialManager {
26
	private static final Component FRIEND_ACTION_FAILED_MESSAGE = Component.translatable("gui.friends.error.failed.message");
27
	private static final Component FRIEND_ACTION_RATE_LIMITED_MESSAGE = Component.translatable("gui.friends.error.rateLimited.message");
28
	private static final Component FRIEND_ACTION_FORBIDDEN_MESSAGE = Component.translatable("gui.friends.error.forbidden.message");
29
	private static final Component FRIEND_ACTION_UNKNOWN_PROFILE = Component.translatable("gui.friends.error.user_may_lack_active_profile");
30
	private static final Component FRIEND_ACTION_UNAUTHORIZED = Component.translatable("gui.friends.error.unauthorized");
31
	private static final Component FRIEND_ACTION_UNAVAILABLE_MESSAGE = Component.translatable("gui.friends.error.unavailable.message");
32
	private final Minecraft minecraft;
33
	private final Set<UUID> hiddenPlayers = Sets.newHashSet();
34
	private final UserApiService service;
35
	private final FriendsService friendsService;
36
	private final PresenceHandler presenceHandler;
37
	private final Map<String, UUID> discoveredNamesToUUID = Maps.newHashMap();
38
	private boolean onlineMode;
39
	private CompletableFuture<?> pendingBlockListRefresh = CompletableFuture.completedFuture(null);
40
	private final RemoteFriendListUpdateHandler remoteFriendListUpdateHandler;
41
	private boolean friendListEnabled;
42
	private boolean allowFriendRequests;
43
44
	public PlayerSocialManager(
45
		final Minecraft minecraft,
46
		final UserApiService service,
47
		final FriendsService friendsService,
48
		final RemoteFriendListUpdateHandler remoteFriendListUpdateHandler
49
	) {
50
		this.minecraft = minecraft;
51
		this.service = service;
52
		this.friendsService = friendsService;
53
		this.remoteFriendListUpdateHandler = remoteFriendListUpdateHandler;
54
		this.friendListEnabled = minecraft.friendsEnabled();
55
		this.allowFriendRequests = minecraft.allowFriendRequests();
56
		this.presenceHandler = new PresenceHandler(minecraft, friendsService);
57
	}
58
59
	public void addFriendListUpdateListener(final Runnable listener) {
60
		this.remoteFriendListUpdateHandler.addUpdateListener(listener);
61
	}
62
63
	public void removeFriendListUpdateListener(final Runnable listener) {
64
		this.remoteFriendListUpdateHandler.removeUpdateListener(listener);
65
	}
66
67
	public List<PlayerSocialManager.PlayerData> getFriends() {
68
		return remap(this.remoteFriendListUpdateHandler.getLatestFriendData().friends());
69
	}
70
71
	public List<PlayerSocialManager.PlayerData> getIncomingRequests() {
72
		return remap(this.remoteFriendListUpdateHandler.getLatestFriendData().incomingRequests());
73
	}
74
75
	public List<PlayerSocialManager.PlayerData> getOutgoingRequests() {
76
		return remap(this.remoteFriendListUpdateHandler.getLatestFriendData().outgoingRequests());
77
	}
78
79
	public RemoteFriendListUpdateHandler.State getFriendListState() {
80
		return this.remoteFriendListUpdateHandler.getState();
81
	}
82
83
	public void hidePlayer(final UUID id) {
84
		this.hiddenPlayers.add(id);
85
	}
86
87
	public void showPlayer(final UUID id) {
88
		this.hiddenPlayers.remove(id);
89
	}
90
91
	public boolean shouldHideMessageFrom(final UUID id) {
92
		return this.isHidden(id) || this.isBlocked(id);
93
	}
94
95
	public boolean isHidden(final UUID id) {
96
		return this.hiddenPlayers.contains(id);
97
	}
98
99
	public void startOnlineMode() {
100
		this.onlineMode = true;
101
		this.pendingBlockListRefresh = this.pendingBlockListRefresh.thenRunAsync(this.service::refreshBlockList, Util.nonCriticalIoPool());
102
	}
103
104
	public void stopOnlineMode() {
105
		this.onlineMode = false;
106
	}
107
108
	public boolean isBlocked(final UUID id) {
109
		if (!this.onlineMode) {
110
			return false;
111
		}
112
113
		this.pendingBlockListRefresh.join();
114
		return this.service.isBlockedPlayer(id);
115
	}
116
117
	public Set<UUID> getHiddenPlayers() {
118
		return this.hiddenPlayers;
119
	}
120
121
	public UUID getDiscoveredUUID(final String name) {
122
		return this.discoveredNamesToUUID.getOrDefault(name, Util.NIL_UUID);
123
	}
124
125
	public void addPlayer(final PlayerInfo info) {
126
		GameProfile gameProfile = info.getProfile();
127
		this.discoveredNamesToUUID.put(gameProfile.name(), gameProfile.id());
128
		if (this.minecraft.gui.screen() instanceof SocialInteractionsScreen screen) {
129
			screen.onAddPlayer(info);
130
		}
131
	}
132
133
	public CompletableFuture<ResultCode> sendFriendRequest(final String name) {
134
		return this.runAction(() -> this.friendsService.sendFriendRequest(name));
135
	}
136
137
	public void removePlayer(final UUID id) {
138
		if (this.minecraft.gui.screen() instanceof SocialInteractionsScreen screen) {
139
			screen.onRemovePlayer(id);
140
		}
141
	}
142
143
	public CompletableFuture<ResultCode> removeFriend(final UUID id) {
144
		return this.runAction(() -> this.friendsService.removeFriend(id));
145
	}
146
147
	public CompletableFuture<ResultCode> acceptIncomingFriendRequest(final UUID id) {
148
		return this.runAction(() -> this.friendsService.acceptIncomingFriendRequest(id));
149
	}
150
151
	public CompletableFuture<ResultCode> declineIncomingFriendRequest(final UUID id) {
152
		return this.runAction(() -> this.friendsService.declineIncomingFriendRequest(id));
153
	}
154
155
	public CompletableFuture<ResultCode> revokeOutgoingFriendRequest(final UUID id) {
156
		return this.runAction(() -> this.friendsService.revokeOutgoingFriendRequest(id));
157
	}
158
159
	public CompletableFuture<ResultCode> updateFriendSettings(final boolean friendsListEnabled, final boolean allowInvites) {
160
		return this.runAction(() -> this.friendsService.updateFriendSettings(friendsListEnabled, allowInvites));
161
	}
162
163
	private CompletableFuture<ResultCode> runAction(final Supplier<ResultCode> action) {
164
		return CompletableFuture.<ResultCode>supplyAsync(() -> {
165
				ResultCode result = action.get();
166
				this.handleResult(result);
167
				return result;
168
			}, Util.ioPool())
169
			.thenComposeAsync(
170
				result -> result == ResultCode.SUCCESS
171
					? this.remoteFriendListUpdateHandler.forceUpdate().thenApply(var1x -> result)
172
					: CompletableFuture.completedFuture(result),
173
				Util.ioPool()
174
			);
175
	}
176
177
	private void handleResult(final ResultCode result) {
178
		if (result != ResultCode.SUCCESS) {
179
			this.showFailureToast(result);
180
		}
181
	}
182
183
	private void showFailureToast(final ResultCode resultCode) {
184
		Component title = switch (resultCode) {
185
			case TOO_MANY_REQUESTS -> FRIEND_ACTION_RATE_LIMITED_MESSAGE;
186
			case UNKNOWN_PROFILE -> FRIEND_ACTION_UNKNOWN_PROFILE;
187
			case UNAUTHORIZED -> FRIEND_ACTION_UNAUTHORIZED;
188
			case FORBIDDEN -> FRIEND_ACTION_FORBIDDEN_MESSAGE;
189
			case SERVICE_NOT_AVAILABLE -> FRIEND_ACTION_UNAVAILABLE_MESSAGE;
190
			case ERROR -> FRIEND_ACTION_FAILED_MESSAGE;
191
			case SUCCESS, UPGRADE_NEEDED, CONNECTION_ISSUE, TEMPORARY_UNAVAILABLE, GENERIC_ERROR -> null;
192
		};
193
		if (title != null) {
194
			this.minecraft.execute(() -> SystemToast.addOrUpdate(this.minecraft.gui.toastManager(), SystemToast.SystemToastId.FRIEND_SYSTEM_NOTIFICATION, title, null));
195
		}
196
	}
197
198
	public boolean isFriendListEnabled() {
199
		return this.friendListEnabled;
200
	}
201
202
	public void setFriendListEnabled(final boolean friendListEnabled) {
203
		this.friendListEnabled = friendListEnabled;
204
		if (friendListEnabled) {
205
			this.remoteFriendListUpdateHandler.start();
206
		} else {
207
			this.remoteFriendListUpdateHandler.stop();
208
		}
209
	}
210
211
	public boolean isAllowFriendRequests() {
212
		return this.allowFriendRequests;
213
	}
214
215
	public void setAllowFriendRequests(final boolean allowFriendRequests) {
216
		this.allowFriendRequests = allowFriendRequests;
217
	}
218
219
	public PresenceHandler getPresenceHandler() {
220
		return this.presenceHandler;
221
	}
222
223
	public boolean isFriend(final UUID uuid) {
224
		for (PlayerSocialManager.PlayerData playerData : this.getFriends()) {
225
			if (playerData.id.equals(uuid)) {
226
				return true;
227
			}
228
		}
229
230
		return false;
231
	}
232
233
	private static List<PlayerSocialManager.PlayerData> remap(final List<FriendDto> friends) {
234
		return friends.stream().map(friend -> new PlayerSocialManager.PlayerData(friend.profileId(), friend.name())).toList();
235
	}
236
237
	@Environment(EnvType.CLIENT)
238
	public record PlayerData(UUID id, String name) {
239
	}
240
}
241

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