| 1 | package net.minecraft.client.gui.screens.social;
|
| 2 |
|
| 3 | import java.util.Collection;
|
| 4 | import java.util.Locale;
|
| 5 | import java.util.Set;
|
| 6 | import java.util.UUID;
|
| 7 | import java.util.stream.Collectors;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.ChatFormatting;
|
| 11 | import net.minecraft.client.GameNarrator;
|
| 12 | import net.minecraft.client.Minecraft;
|
| 13 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 14 | import net.minecraft.client.gui.components.Button;
|
| 15 | import net.minecraft.client.gui.components.EditBox;
|
| 16 | import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
|
| 17 | import net.minecraft.client.gui.screens.ConfirmLinkScreen;
|
| 18 | import net.minecraft.client.gui.screens.Screen;
|
| 19 | import net.minecraft.client.input.KeyEvent;
|
| 20 | import net.minecraft.client.multiplayer.PlayerInfo;
|
| 21 | import net.minecraft.client.multiplayer.ServerData;
|
| 22 | import net.minecraft.client.renderer.RenderPipelines;
|
| 23 | import net.minecraft.network.chat.CommonComponents;
|
| 24 | import net.minecraft.network.chat.Component;
|
| 25 | import net.minecraft.network.chat.MutableComponent;
|
| 26 | import net.minecraft.resources.Identifier;
|
| 27 | import net.minecraft.util.CommonLinks;
|
| 28 | import org.jspecify.annotations.Nullable;
|
| 29 |
|
| 30 | @Environment(EnvType.CLIENT)
|
| 31 | public class SocialInteractionsScreen extends Screen {
|
| 32 | private static final Component TITLE = Component.translatable("gui.socialInteractions.title");
|
| 33 | private static final Identifier BACKGROUND_SPRITE = Identifier.withDefaultNamespace("social_interactions/background");
|
| 34 | private static final Identifier SEARCH_SPRITE = Identifier.withDefaultNamespace("icon/search");
|
| 35 | private static final Component TAB_ALL = Component.translatable("gui.socialInteractions.tab_all");
|
| 36 | private static final Component TAB_HIDDEN = Component.translatable("gui.socialInteractions.tab_hidden");
|
| 37 | private static final Component TAB_BLOCKED = Component.translatable("gui.socialInteractions.tab_blocked");
|
| 38 | private static final Component TAB_ALL_SELECTED = TAB_ALL.plainCopy().withStyle(ChatFormatting.UNDERLINE);
|
| 39 | private static final Component TAB_HIDDEN_SELECTED = TAB_HIDDEN.plainCopy().withStyle(ChatFormatting.UNDERLINE);
|
| 40 | private static final Component TAB_BLOCKED_SELECTED = TAB_BLOCKED.plainCopy().withStyle(ChatFormatting.UNDERLINE);
|
| 41 | private static final Component SEARCH_HINT = Component.translatable("gui.socialInteractions.search_hint").withStyle(EditBox.SEARCH_HINT_STYLE);
|
| 42 | private static final Component EMPTY_SEARCH = Component.translatable("gui.socialInteractions.search_empty").withStyle(ChatFormatting.GRAY);
|
| 43 | private static final Component EMPTY_HIDDEN = Component.translatable("gui.socialInteractions.empty_hidden").withStyle(ChatFormatting.GRAY);
|
| 44 | private static final Component EMPTY_BLOCKED = Component.translatable("gui.socialInteractions.empty_blocked").withStyle(ChatFormatting.GRAY);
|
| 45 | private static final Component BLOCKING_HINT = Component.translatable("gui.socialInteractions.blocking_hint");
|
| 46 | private static final int BG_BORDER_SIZE = 8;
|
| 47 | private static final int BG_WIDTH = 236;
|
| 48 | private static final int SEARCH_HEIGHT = 16;
|
| 49 | private static final int MARGIN_Y = 64;
|
| 50 | public static final int SEARCH_START = 72;
|
| 51 | public static final int LIST_START = 88;
|
| 52 | private static final int IMAGE_WIDTH = 238;
|
| 53 | private static final int BUTTON_HEIGHT = 20;
|
| 54 | private static final int ITEM_HEIGHT = 36;
|
| 55 | private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this);
|
| 56 | private final @Nullable Screen lastScreen;
|
| 57 | private @Nullable SocialInteractionsPlayerList socialInteractionsPlayerList;
|
| 58 | private EditBox searchBox;
|
| 59 | private String lastSearch = "";
|
| 60 | private SocialInteractionsScreen.Page page = SocialInteractionsScreen.Page.ALL;
|
| 61 | private Button allButton;
|
| 62 | private Button hiddenButton;
|
| 63 | private Button blockedButton;
|
| 64 | private Button blockingHintButton;
|
| 65 | private @Nullable Component serverLabel;
|
| 66 | private int playerCount;
|
| 67 |
|
| 68 | public SocialInteractionsScreen() {
|
| 69 | this(null);
|
| 70 | }
|
| 71 |
|
| 72 | public SocialInteractionsScreen(final @Nullable Screen lastScreen) {
|
| 73 | super(TITLE);
|
| 74 | this.lastScreen = lastScreen;
|
| 75 | this.updateServerLabel(Minecraft.getInstance());
|
| 76 | }
|
| 77 |
|
| 78 | private int windowHeight() {
|
| 79 | return Math.max(52, this.height - 128 - 16);
|
| 80 | }
|
| 81 |
|
| 82 | private int listEnd() {
|
| 83 | return 80 + this.windowHeight() - 8;
|
| 84 | }
|
| 85 |
|
| 86 | private int marginX() {
|
| 87 | return (this.width - 238) / 2;
|
| 88 | }
|
| 89 |
|
| 90 | @Override
|
| 91 | public Component getNarrationMessage() {
|
| 92 | return this.serverLabel != null ? CommonComponents.joinForNarration(super.getNarrationMessage(), this.serverLabel) : super.getNarrationMessage();
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | protected void init() {
|
| 97 | this.layout.addTitleHeader(TITLE, this.font);
|
| 98 | this.socialInteractionsPlayerList = new SocialInteractionsPlayerList(this, this.minecraft, this.width, this.listEnd() - 88, 88, 36);
|
| 99 | int buttonWidth = this.socialInteractionsPlayerList.getRowWidth() / 3;
|
| 100 | int buttonLeft = this.socialInteractionsPlayerList.getRowLeft();
|
| 101 | int buttonRight = this.socialInteractionsPlayerList.getRowRight();
|
| 102 | this.allButton = this.addRenderableWidget(
|
| 103 | Button.builder(TAB_ALL, button -> this.showPage(SocialInteractionsScreen.Page.ALL)).bounds(buttonLeft, 45, buttonWidth, 20).build()
|
| 104 | );
|
| 105 | this.hiddenButton = this.addRenderableWidget(
|
| 106 | Button.builder(TAB_HIDDEN, button -> this.showPage(SocialInteractionsScreen.Page.HIDDEN))
|
| 107 | .bounds((buttonLeft + buttonRight - buttonWidth) / 2 + 1, 45, buttonWidth, 20)
|
| 108 | .build()
|
| 109 | );
|
| 110 | this.blockedButton = this.addRenderableWidget(
|
| 111 | Button.builder(TAB_BLOCKED, button -> this.showPage(SocialInteractionsScreen.Page.BLOCKED))
|
| 112 | .bounds(buttonRight - buttonWidth + 1, 45, buttonWidth, 20)
|
| 113 | .build()
|
| 114 | );
|
| 115 | String oldEdit = this.searchBox != null ? this.searchBox.getValue() : "";
|
| 116 | this.searchBox = this.addRenderableWidget(
|
| 117 | new EditBox(this.font, this.marginX() + 28, 74, 200, 15, SEARCH_HINT) {
|
| 118 | @Override
|
| 119 | protected MutableComponent createNarrationMessage() {
|
| 120 | return !SocialInteractionsScreen.this.searchBox.getValue().isEmpty() && SocialInteractionsScreen.this.socialInteractionsPlayerList.isEmpty()
|
| 121 | ? super.createNarrationMessage().append(", ").append(SocialInteractionsScreen.EMPTY_SEARCH)
|
| 122 | : super.createNarrationMessage();
|
| 123 | }
|
| 124 | }
|
| 125 | );
|
| 126 | this.searchBox.setMaxLength(16);
|
| 127 | this.searchBox.setVisible(true);
|
| 128 | this.searchBox.setTextColor(-1);
|
| 129 | this.searchBox.setValue(oldEdit);
|
| 130 | this.searchBox.setHint(SEARCH_HINT);
|
| 131 | this.searchBox.setResponder(this::checkSearchStringUpdate);
|
| 132 | this.blockingHintButton = this.addRenderableWidget(
|
| 133 | Button.builder(BLOCKING_HINT, ConfirmLinkScreen.confirmLink(this, CommonLinks.BLOCKING_HELP))
|
| 134 | .bounds(this.width / 2 - 100, 64 + this.windowHeight(), 200, 20)
|
| 135 | .build()
|
| 136 | );
|
| 137 | this.addWidget(this.socialInteractionsPlayerList);
|
| 138 | this.showPage(this.page);
|
| 139 | this.layout.addToFooter(Button.builder(CommonComponents.GUI_DONE, button -> this.onClose()).width(200).build());
|
| 140 | this.layout.visitWidgets(x$0 -> this.addRenderableWidget(x$0));
|
| 141 | this.repositionElements();
|
| 142 | }
|
| 143 |
|
| 144 | @Override
|
| 145 | public void added() {
|
| 146 | if (this.socialInteractionsPlayerList != null) {
|
| 147 | this.socialInteractionsPlayerList.refreshHasDraftReport();
|
| 148 | }
|
| 149 | }
|
| 150 |
|
| 151 | @Override
|
| 152 | protected void repositionElements() {
|
| 153 | this.layout.arrangeElements();
|
| 154 | this.socialInteractionsPlayerList.updateSizeAndPosition(this.width, this.listEnd() - 88, 88);
|
| 155 | this.searchBox.setPosition(this.marginX() + 28, 74);
|
| 156 | int buttonLeft = this.socialInteractionsPlayerList.getRowLeft();
|
| 157 | int buttonRight = this.socialInteractionsPlayerList.getRowRight();
|
| 158 | int buttonWidth = this.socialInteractionsPlayerList.getRowWidth() / 3;
|
| 159 | this.allButton.setPosition(buttonLeft, 45);
|
| 160 | this.hiddenButton.setPosition((buttonLeft + buttonRight - buttonWidth) / 2 + 1, 45);
|
| 161 | this.blockedButton.setPosition(buttonRight - buttonWidth + 1, 45);
|
| 162 | this.blockingHintButton.setPosition(this.width / 2 - 100, 64 + this.windowHeight());
|
| 163 | }
|
| 164 |
|
| 165 | @Override
|
| 166 | protected void setInitialFocus() {
|
| 167 | this.setInitialFocus(this.searchBox);
|
| 168 | }
|
| 169 |
|
| 170 | @Override
|
| 171 | public void onClose() {
|
| 172 | this.minecraft.gui.setScreen(this.lastScreen);
|
| 173 | }
|
| 174 |
|
| 175 | private void showPage(final SocialInteractionsScreen.Page page) {
|
| 176 | this.page = page;
|
| 177 | this.allButton.setMessage(TAB_ALL);
|
| 178 | this.hiddenButton.setMessage(TAB_HIDDEN);
|
| 179 | this.blockedButton.setMessage(TAB_BLOCKED);
|
| 180 | boolean isEmpty = false;
|
| 181 | switch (page) {
|
| 182 | case ALL:
|
| 183 | this.allButton.setMessage(TAB_ALL_SELECTED);
|
| 184 | Collection<UUID> onlinePlayerIds = this.minecraft.player.connection.getOnlinePlayerIds();
|
| 185 | this.socialInteractionsPlayerList.updatePlayerList(onlinePlayerIds, this.socialInteractionsPlayerList.scrollAmount(), true);
|
| 186 | break;
|
| 187 | case HIDDEN:
|
| 188 | this.hiddenButton.setMessage(TAB_HIDDEN_SELECTED);
|
| 189 | Set<UUID> hiddenPlayers = this.minecraft.getPlayerSocialManager().getHiddenPlayers();
|
| 190 | isEmpty = hiddenPlayers.isEmpty();
|
| 191 | this.socialInteractionsPlayerList.updatePlayerList(hiddenPlayers, this.socialInteractionsPlayerList.scrollAmount(), false);
|
| 192 | break;
|
| 193 | case BLOCKED:
|
| 194 | this.blockedButton.setMessage(TAB_BLOCKED_SELECTED);
|
| 195 | PlayerSocialManager socialManager = this.minecraft.getPlayerSocialManager();
|
| 196 | Set<UUID> blockedPlayers = this.minecraft.player.connection.getOnlinePlayerIds().stream().filter(socialManager::isBlocked).collect(Collectors.toSet());
|
| 197 | isEmpty = blockedPlayers.isEmpty();
|
| 198 | this.socialInteractionsPlayerList.updatePlayerList(blockedPlayers, this.socialInteractionsPlayerList.scrollAmount(), false);
|
| 199 | }
|
| 200 |
|
| 201 | GameNarrator narrator = this.minecraft.getNarrator();
|
| 202 | if (!this.searchBox.getValue().isEmpty() && this.socialInteractionsPlayerList.isEmpty() && !this.searchBox.isFocused()) {
|
| 203 | narrator.saySystemNow(EMPTY_SEARCH);
|
| 204 | } else if (isEmpty) {
|
| 205 | if (page == SocialInteractionsScreen.Page.HIDDEN) {
|
| 206 | narrator.saySystemNow(EMPTY_HIDDEN);
|
| 207 | } else if (page == SocialInteractionsScreen.Page.BLOCKED) {
|
| 208 | narrator.saySystemNow(EMPTY_BLOCKED);
|
| 209 | }
|
| 210 | }
|
| 211 | }
|
| 212 |
|
| 213 | @Override
|
| 214 | public void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 215 | super.extractBackground(graphics, mouseX, mouseY, a);
|
| 216 | int marginX = this.marginX() + 3;
|
| 217 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, BACKGROUND_SPRITE, marginX, 64, 236, this.windowHeight() + 16);
|
| 218 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, SEARCH_SPRITE, marginX + 10, 76, 12, 12);
|
| 219 | }
|
| 220 |
|
| 221 | @Override
|
| 222 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 223 | super.extractRenderState(graphics, mouseX, mouseY, a);
|
| 224 | this.updateServerLabel(this.minecraft);
|
| 225 | if (this.serverLabel != null) {
|
| 226 | graphics.text(this.minecraft.font, this.serverLabel, this.marginX() + 8, 35, -1);
|
| 227 | }
|
| 228 |
|
| 229 | if (!this.socialInteractionsPlayerList.isEmpty()) {
|
| 230 | this.socialInteractionsPlayerList.extractRenderState(graphics, mouseX, mouseY, a);
|
| 231 | } else if (!this.searchBox.getValue().isEmpty()) {
|
| 232 | graphics.centeredText(this.minecraft.font, EMPTY_SEARCH, this.width / 2, (72 + this.listEnd()) / 2, -1);
|
| 233 | } else if (this.page == SocialInteractionsScreen.Page.HIDDEN) {
|
| 234 | graphics.centeredText(this.minecraft.font, EMPTY_HIDDEN, this.width / 2, (72 + this.listEnd()) / 2, -1);
|
| 235 | } else if (this.page == SocialInteractionsScreen.Page.BLOCKED) {
|
| 236 | graphics.centeredText(this.minecraft.font, EMPTY_BLOCKED, this.width / 2, (72 + this.listEnd()) / 2, -1);
|
| 237 | }
|
| 238 |
|
| 239 | this.blockingHintButton.visible = this.page == SocialInteractionsScreen.Page.BLOCKED;
|
| 240 | }
|
| 241 |
|
| 242 | @Override
|
| 243 | public boolean keyPressed(final KeyEvent event) {
|
| 244 | if (!this.isInputCaptured() && this.minecraft.options.keySocialInteractions.matches(event)) {
|
| 245 | this.onClose();
|
| 246 | return true;
|
| 247 | } else {
|
| 248 | return super.keyPressed(event);
|
| 249 | }
|
| 250 | }
|
| 251 |
|
| 252 | @Override
|
| 253 | public boolean isPauseScreen() {
|
| 254 | return false;
|
| 255 | }
|
| 256 |
|
| 257 | private void checkSearchStringUpdate(String searchText) {
|
| 258 | searchText = searchText.toLowerCase(Locale.ROOT);
|
| 259 | if (!searchText.equals(this.lastSearch)) {
|
| 260 | this.socialInteractionsPlayerList.setFilter(searchText);
|
| 261 | this.lastSearch = searchText;
|
| 262 | this.showPage(this.page);
|
| 263 | }
|
| 264 | }
|
| 265 |
|
| 266 | private void updateServerLabel(final Minecraft minecraft) {
|
| 267 | int playerCount = minecraft.getConnection().getOnlinePlayers().size();
|
| 268 | if (this.playerCount != playerCount) {
|
| 269 | String serverName = "";
|
| 270 | ServerData currentServer = minecraft.getCurrentServer();
|
| 271 | if (minecraft.isLocalServer()) {
|
| 272 | serverName = minecraft.getSingleplayerServer().getMotd();
|
| 273 | } else if (currentServer != null) {
|
| 274 | serverName = currentServer.name;
|
| 275 | }
|
| 276 |
|
| 277 | if (playerCount > 1) {
|
| 278 | this.serverLabel = Component.translatable("gui.socialInteractions.server_label.multiple", serverName, playerCount);
|
| 279 | } else {
|
| 280 | this.serverLabel = Component.translatable("gui.socialInteractions.server_label.single", serverName, playerCount);
|
| 281 | }
|
| 282 |
|
| 283 | this.playerCount = playerCount;
|
| 284 | }
|
| 285 | }
|
| 286 |
|
| 287 | public void onAddPlayer(final PlayerInfo info) {
|
| 288 | this.socialInteractionsPlayerList.addPlayer(info, this.page);
|
| 289 | }
|
| 290 |
|
| 291 | public void onRemovePlayer(final UUID id) {
|
| 292 | this.socialInteractionsPlayerList.removePlayer(id);
|
| 293 | }
|
| 294 |
|
| 295 | @Environment(EnvType.CLIENT)
|
| 296 | public enum Page {
|
| 297 | ALL,
|
| 298 | HIDDEN,
|
| 299 | BLOCKED;
|
| 300 | }
|
| 301 | }
|
| 302 |
|