| 1 | package net.minecraft.client.gui.screens.friends;
|
| 2 |
|
| 3 | import com.mojang.authlib.services.response.PresenceStatus;
|
| 4 | import com.mojang.authlib.services.response.PresenceStatusDto;
|
| 5 | import java.util.Locale;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.client.Minecraft;
|
| 9 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 10 | import net.minecraft.client.gui.components.Button;
|
| 11 | import net.minecraft.client.gui.components.PopupScreen;
|
| 12 | import net.minecraft.client.gui.components.SpriteIconButton;
|
| 13 | import net.minecraft.client.gui.components.StringWidget;
|
| 14 | import net.minecraft.client.gui.components.WidgetSprites;
|
| 15 | import net.minecraft.client.gui.screens.social.PlayerSocialManager;
|
| 16 | import net.minecraft.network.chat.CommonComponents;
|
| 17 | import net.minecraft.network.chat.Component;
|
| 18 | import net.minecraft.resources.Identifier;
|
| 19 | import org.jspecify.annotations.Nullable;
|
| 20 |
|
| 21 | @Environment(EnvType.CLIENT)
|
| 22 | class FriendEntry extends AbstractFriendsEntryContainerWidget {
|
| 23 | private static final WidgetSprites REMOVE_SPRITE = new WidgetSprites(Identifier.withDefaultNamespace("friends/remove"));
|
| 24 | private static final Component UNFRIEND = Component.translatable("gui.friends.unfriend");
|
| 25 | private static final Component CONFIRM_TITLE = Component.translatable("gui.friends.confirm_title");
|
| 26 | private static final Component CONFIRM_UNFRIEND = Component.translatable("gui.friends.confirm_unfriend");
|
| 27 | private static final Component PRESENCE_OFFLINE = Component.translatable("gui.friends.presence.status.offline").withColor(-6250336);
|
| 28 | private final SpriteIconButton removeButton;
|
| 29 | private final StringWidget statusWidget;
|
| 30 | private @Nullable PresenceStatusDto presence;
|
| 31 |
|
| 32 | public FriendEntry(
|
| 33 | final Minecraft minecraft,
|
| 34 | final FriendsOverlayScreen screen,
|
| 35 | final PlayerSocialManager.PlayerData playerData,
|
| 36 | final @Nullable PresenceStatusDto presence,
|
| 37 | final boolean initiallyLoading,
|
| 38 | final Runnable onAction
|
| 39 | ) {
|
| 40 | super(minecraft, screen, 0, 0, screen.getOverlayWidth() - 16, 28, playerData, true);
|
| 41 | this.presence = presence;
|
| 42 | this.statusWidget = new StringWidget(presenceStatusComponent(presence), minecraft.font);
|
| 43 | this.addChild(this.statusWidget);
|
| 44 | Button.CreateNarration narration = getSpriteIconNarration(Component.translatable("gui.friends.narration.button.unfriend", playerData.name()));
|
| 45 | this.removeButton = SpriteIconButton.builder(UNFRIEND, var2 -> this.confirmRemoveFriend(onAction), true)
|
| 46 | .size(20, 20)
|
| 47 | .sprite(REMOVE_SPRITE, 13, 11)
|
| 48 | .tooltip(UNFRIEND)
|
| 49 | .narration(narration)
|
| 50 | .build();
|
| 51 | if (initiallyLoading) {
|
| 52 | this.removeButton.setLoading(true);
|
| 53 | }
|
| 54 |
|
| 55 | this.addChild(this.removeButton);
|
| 56 | }
|
| 57 |
|
| 58 | private static Component presenceStatusComponent(final @Nullable PresenceStatusDto presence) {
|
| 59 | if (presence != null && presence.status() != PresenceStatus.OFFLINE) {
|
| 60 | String key = "gui.friends.presence.status." + presence.status().toString().toLowerCase(Locale.ROOT);
|
| 61 | return Component.translatable(key).withColor(-16711936);
|
| 62 | } else {
|
| 63 | return PRESENCE_OFFLINE;
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | void applyPresence(final @Nullable PresenceStatusDto newPresence) {
|
| 68 | this.presence = newPresence;
|
| 69 | this.statusWidget.setMessage(presenceStatusComponent(newPresence));
|
| 70 | }
|
| 71 |
|
| 72 | public int presenceStatusSortOrder() {
|
| 73 | PresenceStatus status = this.presence == null ? PresenceStatus.OFFLINE : this.presence.status();
|
| 74 |
|
| 75 | return switch (status) {
|
| 76 | case PLAYING_HOSTED_SERVER -> 0;
|
| 77 | case PLAYING_SERVER -> 1;
|
| 78 | case PLAYING_REALMS -> 2;
|
| 79 | case PLAYING_OFFLINE -> 3;
|
| 80 | case ONLINE -> 4;
|
| 81 | case OFFLINE -> 5;
|
| 82 | };
|
| 83 | }
|
| 84 |
|
| 85 | @Override
|
| 86 | void disable() {
|
| 87 | this.removeButton.active = false;
|
| 88 | }
|
| 89 |
|
| 90 | @Override
|
| 91 | protected Component getEntryNarration() {
|
| 92 | return Component.translatable("gui.friends.narration.entry.friend", this.playerName);
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | protected void extractWidgetRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 97 | super.extractWidgetRenderState(graphics, mouseX, mouseY, a);
|
| 98 | int verticalCenter = this.getY() + (this.getHeight() - 20) / 2;
|
| 99 | int removeX = this.getX() + this.getWidth() - 20;
|
| 100 | this.removeButton.setPosition(removeX, verticalCenter);
|
| 101 | this.removeButton.extractRenderState(graphics, mouseX, mouseY, a);
|
| 102 | int statusWidgetX = this.playerFaceWidget.getRight() + 4;
|
| 103 | int statusWidth = removeX - statusWidgetX - 2;
|
| 104 | this.statusWidget.setMaxWidth(statusWidth, StringWidget.TextOverflow.SCROLLING);
|
| 105 | this.statusWidget.setPosition(statusWidgetX, this.nameWidget.getBottom() + 2);
|
| 106 | this.statusWidget.extractRenderState(graphics, mouseX, mouseY, a);
|
| 107 | }
|
| 108 |
|
| 109 | private void confirmRemoveFriend(final Runnable action) {
|
| 110 | this.minecraft
|
| 111 | .gui
|
| 112 | .setScreen(new PopupScreen.Builder(this.screen, CONFIRM_TITLE).addMessage(CONFIRM_UNFRIEND).addButton(CommonComponents.GUI_REMOVE, var2 -> {
|
| 113 | this.removeButton.setLoading(true);
|
| 114 | this.screen.startFriendAction();
|
| 115 | action.run();
|
| 116 | this.minecraft.gui.setScreen(this.screen);
|
| 117 | }).addButton(CommonComponents.GUI_CANCEL, var1 -> this.minecraft.gui.setScreen(this.screen)).build());
|
| 118 | }
|
| 119 | }
|
| 120 |
|