| 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 java.net.URI;
|
| 6 | import java.util.List;
|
| 7 | import java.util.function.Consumer;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.ChatFormatting;
|
| 11 | import net.minecraft.client.Minecraft;
|
| 12 | import net.minecraft.client.gui.components.AbstractWidget;
|
| 13 | import net.minecraft.client.gui.components.ImageWidget;
|
| 14 | import net.minecraft.client.gui.components.LoadingDotsWidget;
|
| 15 | import net.minecraft.client.gui.components.MultiLineTextWidget;
|
| 16 | import net.minecraft.client.gui.components.ScrollableLayout;
|
| 17 | import net.minecraft.client.gui.layouts.FrameLayout;
|
| 18 | import net.minecraft.client.gui.layouts.Layout;
|
| 19 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 20 | import net.minecraft.client.gui.navigation.ScreenRectangle;
|
| 21 | import net.minecraft.client.gui.screens.PrivacyConfirmLinkScreen;
|
| 22 | import net.minecraft.network.chat.Component;
|
| 23 | import net.minecraft.network.chat.ClickEvent.OpenUrl;
|
| 24 | import net.minecraft.resources.Identifier;
|
| 25 | import net.minecraft.util.CommonLinks;
|
| 26 | import org.jspecify.annotations.Nullable;
|
| 27 |
|
| 28 | @Environment(EnvType.CLIENT)
|
| 29 | class FriendsTab extends AbstractFriendsTab {
|
| 30 | private static final Component TAB_TITLE = Component.translatable("gui.friends.tab_friends");
|
| 31 | private static final Component MICROSOFT_ACCOUNT_LINK = Component.translatable("gui.friends.empty_state.link")
|
| 32 | .withStyle(style -> style.withUnderlined(true).withColor(ChatFormatting.GRAY).withClickEvent(new OpenUrl(CommonLinks.PRIVACY_AND_ONLINE_SETTINGS)));
|
| 33 | private static final Component EMPTY_STATE = Component.translatable("gui.friends.empty_state", MICROSOFT_ACCOUNT_LINK).withStyle(ChatFormatting.GRAY);
|
| 34 | private static final Component MANAGE_ACCOUNT_FOOTER = Component.translatable("gui.friends.manage_account_footer", MICROSOFT_ACCOUNT_LINK)
|
| 35 | .withStyle(ChatFormatting.GRAY);
|
| 36 | private static final Identifier ILLUSTRATION = Identifier.withDefaultNamespace("friends/illustrations_00");
|
| 37 | private final FriendsOverlayScreen screen;
|
| 38 | private final LinearLayout layout;
|
| 39 | private final LinearLayout friendScrollableContent;
|
| 40 | private final LoadingDotsWidget loadingDotsWidget;
|
| 41 | private final AddFriendWidget addFriendWidget;
|
| 42 | private final ScrollableLayout scrollableLayout;
|
| 43 | private @Nullable FrameLayout contentFrame;
|
| 44 |
|
| 45 | FriendsTab(final Minecraft minecraft, final LoadingDotsWidget loadingDotsWidget, final FriendsOverlayScreen screen, final int width, final int height) {
|
| 46 | super(width, height);
|
| 47 | this.screen = screen;
|
| 48 | this.layout = LinearLayout.vertical();
|
| 49 | this.layout.defaultCellSetting().alignHorizontallyCenter();
|
| 50 | this.loadingDotsWidget = loadingDotsWidget;
|
| 51 | this.addFriendWidget = new AddFriendWidget(width, this::onSendFriendRequestFinished);
|
| 52 | this.layout.addChild(this.addFriendWidget);
|
| 53 | this.friendScrollableContent = LinearLayout.vertical();
|
| 54 | this.friendScrollableContent.defaultCellSetting();
|
| 55 | this.scrollableLayout = new ScrollableLayout(
|
| 56 | minecraft, this.friendScrollableContent, height - this.addFriendWidget.contentHeight(), ScrollableLayout.ReserveStrategy.BOTH
|
| 57 | );
|
| 58 | this.scrollableLayout.setScrollbarSpacing(2);
|
| 59 | this.layout.addChild(this.scrollableLayout);
|
| 60 | this.rearrangeElements();
|
| 61 | }
|
| 62 |
|
| 63 | public void showLoading() {
|
| 64 | this.friendScrollableContent.removeChildren();
|
| 65 | this.contentFrame = this.createCenteredFrame(this.loadingDotsWidget, this.getListContentWidth(), this.height - this.addFriendWidget.contentHeight());
|
| 66 | this.friendScrollableContent.addChild(this.contentFrame);
|
| 67 | this.addFriendWidget.applyState(AddFriendWidget.State.SENDING);
|
| 68 | }
|
| 69 |
|
| 70 | public void showError(final Component message) {
|
| 71 | this.friendScrollableContent.removeChildren();
|
| 72 | int maxWidth = this.getListContentWidth();
|
| 73 | MultiLineTextWidget text = this.createCenteredText(message.copy().withStyle(ChatFormatting.GRAY), this.screen.getFont(), maxWidth);
|
| 74 | this.contentFrame = this.createCenteredFrame(text, maxWidth, this.height - this.addFriendWidget.contentHeight());
|
| 75 | this.friendScrollableContent.addChild(this.contentFrame);
|
| 76 | this.addFriendWidget.applyState(AddFriendWidget.State.DISABLED);
|
| 77 | }
|
| 78 |
|
| 79 | public void showEmpty() {
|
| 80 | this.friendScrollableContent.removeChildren();
|
| 81 | LinearLayout content = new LinearLayout(0, 0, LinearLayout.Orientation.VERTICAL).spacing(8);
|
| 82 | content.defaultCellSetting().alignHorizontallyCenter().alignVerticallyMiddle();
|
| 83 | content.addChild(ImageWidget.sprite(128, 48, ILLUSTRATION));
|
| 84 | int maxWidth = this.getListContentWidth();
|
| 85 | MultiLineTextWidget textWidget = this.createCenteredText(EMPTY_STATE, this.screen.getFont(), maxWidth);
|
| 86 | textWidget.setComponentClickHandler(style -> {
|
| 87 | if (style.getClickEvent() instanceof OpenUrl(URI uri)) {
|
| 88 | PrivacyConfirmLinkScreen.confirmLinkNow(this.screen, uri);
|
| 89 | }
|
| 90 | });
|
| 91 | content.addChild(textWidget);
|
| 92 | int frameHeight = this.scrollableLayout.getHeight();
|
| 93 | this.contentFrame = this.createCenteredFrame(content, maxWidth, frameHeight);
|
| 94 | this.friendScrollableContent.addChild(this.contentFrame);
|
| 95 | this.addFriendWidget.applyState(this.addFriendWidget.getValue().isEmpty() ? AddFriendWidget.State.EMPTY_INPUT : AddFriendWidget.State.READY);
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | void rearrangeElements() {
|
| 100 | this.scrollableLayout.setMinHeight(this.height - this.addFriendWidget.contentHeight());
|
| 101 | this.scrollableLayout.setMaxHeight(this.height - this.addFriendWidget.contentHeight());
|
| 102 | if (this.contentFrame != null) {
|
| 103 | this.contentFrame.setMinHeight(this.height - this.addFriendWidget.contentHeight());
|
| 104 | }
|
| 105 | }
|
| 106 |
|
| 107 | private void onSendFriendRequestFinished() {
|
| 108 | this.screen.refreshLists();
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public Component getTabTitle() {
|
| 113 | return TAB_TITLE;
|
| 114 | }
|
| 115 |
|
| 116 | @Override
|
| 117 | public Component getTabExtraNarration() {
|
| 118 | return Component.empty();
|
| 119 | }
|
| 120 |
|
| 121 | @Override
|
| 122 | public void visitChildren(final Consumer<AbstractWidget> childrenConsumer) {
|
| 123 | this.layout.visitWidgets(childrenConsumer);
|
| 124 | }
|
| 125 |
|
| 126 | @Override
|
| 127 | public void doLayout(final ScreenRectangle screenRectangle) {
|
| 128 | this.layout.arrangeElements();
|
| 129 | FrameLayout.alignInRectangle(this.layout, screenRectangle, 0.5F, 0.16666667F);
|
| 130 | }
|
| 131 |
|
| 132 | @Override
|
| 133 | public Layout getLayout() {
|
| 134 | return this.layout;
|
| 135 | }
|
| 136 |
|
| 137 | void updateEntries(final List<FriendEntry> friendEntries) {
|
| 138 | this.friendScrollableContent.removeChildren();
|
| 139 | this.contentFrame = null;
|
| 140 | friendEntries.forEach(this.friendScrollableContent::addChild);
|
| 141 | this.friendScrollableContent.addChild(this.createManageAccountFooter());
|
| 142 | this.addFriendWidget.applyState(this.addFriendWidget.getValue().isEmpty() ? AddFriendWidget.State.EMPTY_INPUT : AddFriendWidget.State.READY);
|
| 143 | }
|
| 144 |
|
| 145 | void applyPresenceUpdate(final PresenceResponse latestPresence) {
|
| 146 | this.friendScrollableContent.visitWidgets(widget -> {
|
| 147 | if (widget instanceof FriendEntry entry) {
|
| 148 | PresenceStatusDto newPresenceStatus = null;
|
| 149 |
|
| 150 | for (PresenceStatusDto presenceStatus : latestPresence.presence()) {
|
| 151 | if (presenceStatus.profileId().equals(entry.playerId())) {
|
| 152 | newPresenceStatus = presenceStatus;
|
| 153 | break;
|
| 154 | }
|
| 155 | }
|
| 156 |
|
| 157 | entry.applyPresence(newPresenceStatus);
|
| 158 | }
|
| 159 | });
|
| 160 | }
|
| 161 |
|
| 162 | private FrameLayout createManageAccountFooter() {
|
| 163 | int maxWidth = this.getListContentWidth();
|
| 164 | MultiLineTextWidget textWidget = this.createCenteredText(MANAGE_ACCOUNT_FOOTER, this.screen.getFont(), maxWidth);
|
| 165 | textWidget.setComponentClickHandler(style -> {
|
| 166 | if (style.getClickEvent() instanceof OpenUrl(URI uri)) {
|
| 167 | AbstractWidget.playButtonClickSound(Minecraft.getInstance().getSoundManager());
|
| 168 | PrivacyConfirmLinkScreen.confirmLinkNow(this.screen, uri);
|
| 169 | }
|
| 170 | });
|
| 171 | FrameLayout frame = new FrameLayout(maxWidth, textWidget.getHeight());
|
| 172 | frame.defaultChildLayoutSetting().alignHorizontallyCenter().alignVerticallyMiddle();
|
| 173 | frame.addChild(textWidget);
|
| 174 | return frame;
|
| 175 | }
|
| 176 |
|
| 177 | @Override
|
| 178 | protected Layout entriesContainer() {
|
| 179 | return this.friendScrollableContent;
|
| 180 | }
|
| 181 | }
|
| 182 |
|