| 1 | package net.minecraft.client.gui.screens.social;
|
| 2 |
|
| 3 | import com.google.common.base.Strings;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import com.mojang.authlib.GameProfile;
|
| 6 | import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
|
| 7 | import java.util.Collection;
|
| 8 | import java.util.Comparator;
|
| 9 | import java.util.HashMap;
|
| 10 | import java.util.List;
|
| 11 | import java.util.Locale;
|
| 12 | import java.util.Map;
|
| 13 | import java.util.UUID;
|
| 14 | import net.fabricmc.api.EnvType;
|
| 15 | import net.fabricmc.api.Environment;
|
| 16 | import net.minecraft.client.Minecraft;
|
| 17 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 18 | import net.minecraft.client.gui.components.ContainerObjectSelectionList;
|
| 19 | import net.minecraft.client.multiplayer.ClientPacketListener;
|
| 20 | import net.minecraft.client.multiplayer.PlayerInfo;
|
| 21 | import net.minecraft.client.multiplayer.chat.ChatLog;
|
| 22 | import net.minecraft.client.multiplayer.chat.LoggedChatMessage;
|
| 23 | import org.jspecify.annotations.Nullable;
|
| 24 |
|
| 25 | @Environment(EnvType.CLIENT)
|
| 26 | public class SocialInteractionsPlayerList extends ContainerObjectSelectionList<PlayerEntry> {
|
| 27 | private final SocialInteractionsScreen socialInteractionsScreen;
|
| 28 | private final List<PlayerEntry> players = Lists.newArrayList();
|
| 29 | private @Nullable String filter;
|
| 30 |
|
| 31 | public SocialInteractionsPlayerList(
|
| 32 | final SocialInteractionsScreen socialInteractionsScreen, final Minecraft minecraft, final int width, final int height, final int y, final int itemHeight
|
| 33 | ) {
|
| 34 | super(minecraft, width, height, y, itemHeight);
|
| 35 | this.socialInteractionsScreen = socialInteractionsScreen;
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | protected void extractListBackground(final GuiGraphicsExtractor graphics) {
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | protected void extractListSeparators(final GuiGraphicsExtractor graphics) {
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | protected void enableScissor(final GuiGraphicsExtractor graphics) {
|
| 48 | graphics.enableScissor(this.getX(), this.getY() + 4, this.getRight(), this.getBottom());
|
| 49 | }
|
| 50 |
|
| 51 | public void updatePlayerList(final Collection<UUID> playersToAdd, final double scrollAmount, final boolean addOfflineEntries) {
|
| 52 | Map<UUID, PlayerEntry> newEntries = new HashMap<>();
|
| 53 | this.addOnlinePlayers(playersToAdd, newEntries);
|
| 54 | if (addOfflineEntries) {
|
| 55 | this.addSeenPlayers(newEntries);
|
| 56 | }
|
| 57 |
|
| 58 | this.updatePlayersFromChatLog(newEntries, addOfflineEntries);
|
| 59 | this.updateFiltersAndScroll(newEntries.values(), scrollAmount);
|
| 60 | }
|
| 61 |
|
| 62 | private void addOnlinePlayers(final Collection<UUID> playersToAdd, final Map<UUID, PlayerEntry> output) {
|
| 63 | ClientPacketListener connection = this.minecraft.player.connection;
|
| 64 |
|
| 65 | for (UUID id : playersToAdd) {
|
| 66 | PlayerInfo playerInfo = connection.getPlayerInfo(id);
|
| 67 | if (playerInfo != null) {
|
| 68 | PlayerEntry player = this.makePlayerEntry(id, playerInfo);
|
| 69 | output.put(id, player);
|
| 70 | }
|
| 71 | }
|
| 72 | }
|
| 73 |
|
| 74 | private void addSeenPlayers(final Map<UUID, PlayerEntry> newEntries) {
|
| 75 | Map<UUID, PlayerInfo> seenPlayers = this.minecraft.player.connection.getSeenPlayers();
|
| 76 |
|
| 77 | for (Map.Entry<UUID, PlayerInfo> entry : seenPlayers.entrySet()) {
|
| 78 | newEntries.computeIfAbsent(entry.getKey(), uuid -> {
|
| 79 | PlayerEntry player = this.makePlayerEntry(uuid, entry.getValue());
|
| 80 | player.setRemoved(true);
|
| 81 | return player;
|
| 82 | });
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | private PlayerEntry makePlayerEntry(final UUID id, final PlayerInfo playerInfo) {
|
| 87 | return new PlayerEntry(this.minecraft, this.socialInteractionsScreen, id, playerInfo.getProfile().name(), playerInfo::getSkin, playerInfo.hasVerifiableChat());
|
| 88 | }
|
| 89 |
|
| 90 | private void updatePlayersFromChatLog(final Map<UUID, PlayerEntry> entries, final boolean addOfflineEntries) {
|
| 91 | Map<UUID, GameProfile> gameProfiles = collectProfilesFromChatLog(this.minecraft.getReportingContext().chatLog());
|
| 92 | gameProfiles.forEach(
|
| 93 | (id, gameProfile) -> {
|
| 94 | PlayerEntry entry;
|
| 95 | if (addOfflineEntries) {
|
| 96 | entry = entries.computeIfAbsent(
|
| 97 | id,
|
| 98 | uuid -> {
|
| 99 | PlayerEntry player = new PlayerEntry(
|
| 100 | this.minecraft,
|
| 101 | this.socialInteractionsScreen,
|
| 102 | gameProfile.id(),
|
| 103 | gameProfile.name(),
|
| 104 | this.minecraft.getSkinManager().createLookup(gameProfile, true),
|
| 105 | true
|
| 106 | );
|
| 107 | player.setRemoved(true);
|
| 108 | return player;
|
| 109 | }
|
| 110 | );
|
| 111 | } else {
|
| 112 | entry = entries.get(id);
|
| 113 | if (entry == null) {
|
| 114 | return;
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | entry.setHasRecentMessages(true);
|
| 119 | }
|
| 120 | );
|
| 121 | }
|
| 122 |
|
| 123 | private static Map<UUID, GameProfile> collectProfilesFromChatLog(final ChatLog chatLog) {
|
| 124 | Map<UUID, GameProfile> gameProfiles = new Object2ObjectLinkedOpenHashMap<>();
|
| 125 |
|
| 126 | for (int id = chatLog.end(); id >= chatLog.start(); id--) {
|
| 127 | if (chatLog.lookup(id) instanceof LoggedChatMessage.Player message && message.message().hasSignature()) {
|
| 128 | gameProfiles.put(message.profileId(), message.profile());
|
| 129 | }
|
| 130 | }
|
| 131 |
|
| 132 | return gameProfiles;
|
| 133 | }
|
| 134 |
|
| 135 | private void sortPlayerEntries() {
|
| 136 | this.players
|
| 137 | .sort(
|
| 138 | Comparator.<PlayerEntry, Integer>comparing(e -> {
|
| 139 | if (this.minecraft.isLocalPlayer(e.getPlayerId())) {
|
| 140 | return 0;
|
| 141 | } else if (this.minecraft.getReportingContext().hasDraftReportFor(e.getPlayerId())) {
|
| 142 | return 1;
|
| 143 | } else if (e.getPlayerId().version() == 2) {
|
| 144 | return 4;
|
| 145 | } else {
|
| 146 | return e.hasRecentMessages() ? 2 : 3;
|
| 147 | }
|
| 148 | })
|
| 149 | .thenComparing(
|
| 150 | e -> {
|
| 151 | if (!e.getPlayerName().isBlank()) {
|
| 152 | int firstCodepoint = e.getPlayerName().codePointAt(0);
|
| 153 | if (firstCodepoint == 95
|
| 154 | || firstCodepoint >= 97 && firstCodepoint <= 122
|
| 155 | || firstCodepoint >= 65 && firstCodepoint <= 90
|
| 156 | || firstCodepoint >= 48 && firstCodepoint <= 57) {
|
| 157 | return 0;
|
| 158 | }
|
| 159 | }
|
| 160 |
|
| 161 | return 1;
|
| 162 | }
|
| 163 | )
|
| 164 | .thenComparing(PlayerEntry::getPlayerName, String::compareToIgnoreCase)
|
| 165 | );
|
| 166 | }
|
| 167 |
|
| 168 | private void updateFiltersAndScroll(final Collection<PlayerEntry> newEntries, final double scrollAmount) {
|
| 169 | this.players.clear();
|
| 170 | this.players.addAll(newEntries);
|
| 171 | this.sortPlayerEntries();
|
| 172 | this.updateFilteredPlayers();
|
| 173 | this.replaceEntries(this.players);
|
| 174 | this.setScrollAmount(scrollAmount);
|
| 175 | }
|
| 176 |
|
| 177 | private void updateFilteredPlayers() {
|
| 178 | if (this.filter != null) {
|
| 179 | this.players.removeIf(p -> !p.getPlayerName().toLowerCase(Locale.ROOT).contains(this.filter));
|
| 180 | this.replaceEntries(this.players);
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | public void setFilter(final String filter) {
|
| 185 | this.filter = filter;
|
| 186 | }
|
| 187 |
|
| 188 | public boolean isEmpty() {
|
| 189 | return this.players.isEmpty();
|
| 190 | }
|
| 191 |
|
| 192 | public void addPlayer(final PlayerInfo player, final SocialInteractionsScreen.Page page) {
|
| 193 | UUID playerId = player.getProfile().id();
|
| 194 |
|
| 195 | for (PlayerEntry playerEntry : this.players) {
|
| 196 | if (playerEntry.getPlayerId().equals(playerId)) {
|
| 197 | playerEntry.setRemoved(false);
|
| 198 | return;
|
| 199 | }
|
| 200 | }
|
| 201 |
|
| 202 | if ((page == SocialInteractionsScreen.Page.ALL || this.minecraft.getPlayerSocialManager().shouldHideMessageFrom(playerId))
|
| 203 | && (Strings.isNullOrEmpty(this.filter) || player.getProfile().name().toLowerCase(Locale.ROOT).contains(this.filter))) {
|
| 204 | boolean chatReportable = player.hasVerifiableChat();
|
| 205 | PlayerEntry playerEntry = new PlayerEntry(
|
| 206 | this.minecraft, this.socialInteractionsScreen, player.getProfile().id(), player.getProfile().name(), player::getSkin, chatReportable
|
| 207 | );
|
| 208 | this.addEntry(playerEntry);
|
| 209 | this.players.add(playerEntry);
|
| 210 | }
|
| 211 | }
|
| 212 |
|
| 213 | public void removePlayer(final UUID id) {
|
| 214 | for (PlayerEntry playerEntry : this.players) {
|
| 215 | if (playerEntry.getPlayerId().equals(id)) {
|
| 216 | playerEntry.setRemoved(true);
|
| 217 | return;
|
| 218 | }
|
| 219 | }
|
| 220 | }
|
| 221 |
|
| 222 | public void refreshHasDraftReport() {
|
| 223 | this.players.forEach(playerEntry -> playerEntry.refreshHasDraftReport(this.minecraft.getReportingContext()));
|
| 224 | }
|
| 225 | }
|
| 226 |
|