| 1 | package com.mojang.realmsclient.gui;
|
| 2 |
|
| 3 | import com.mojang.realmsclient.gui.screens.RealmsPendingInvitesScreen;
|
| 4 | import com.mojang.realmsclient.util.RealmsPersistence;
|
| 5 | import java.net.URI;
|
| 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.ImageWidget;
|
| 12 | import net.minecraft.client.gui.components.SpriteIconButton;
|
| 13 | import net.minecraft.client.gui.components.Tooltip;
|
| 14 | import net.minecraft.client.gui.components.WidgetSprites;
|
| 15 | import net.minecraft.client.gui.layouts.FrameLayout;
|
| 16 | import net.minecraft.client.gui.layouts.Layout;
|
| 17 | import net.minecraft.client.gui.layouts.LayoutSettings;
|
| 18 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 19 | import net.minecraft.client.gui.screens.ConfirmLinkScreen;
|
| 20 | import net.minecraft.client.gui.screens.Screen;
|
| 21 | import net.minecraft.client.renderer.RenderPipelines;
|
| 22 | import net.minecraft.network.chat.Component;
|
| 23 | import net.minecraft.resources.Identifier;
|
| 24 | import org.jspecify.annotations.Nullable;
|
| 25 |
|
| 26 | @Environment(EnvType.CLIENT)
|
| 27 | public final class RealmsHeader {
|
| 28 | private static final Identifier INVITE_SPRITE = Identifier.withDefaultNamespace("icon/invite");
|
| 29 | private static final Identifier NEWS_SPRITE = Identifier.withDefaultNamespace("icon/news");
|
| 30 | private static final int CONTENT_WIDTH = 308;
|
| 31 | private static final Tooltip NO_PENDING_INVITES_TOOLTIP = Tooltip.create(Component.translatable("mco.invites.nopending"));
|
| 32 | private static final Tooltip PENDING_INVITES_TOOLTIP = Tooltip.create(Component.translatable("mco.invites.pending"));
|
| 33 | private static final Component INVITES_TITLE = Component.translatable("mco.invites.title");
|
| 34 | private static final Component NEWS_TITLE = Component.translatable("mco.news");
|
| 35 | private static final Component JOIN_REALM_TEXT = Component.translatable("mco.selectServer.joinRealm");
|
| 36 | private final Minecraft minecraft = Minecraft.getInstance();
|
| 37 | private final Screen screen;
|
| 38 | private final Button joinRealmButton;
|
| 39 | private final RealmsHeader.NotificationButton pendingInvitesButton;
|
| 40 | private final RealmsHeader.NotificationButton newsButton;
|
| 41 | private volatile @Nullable URI newsLink;
|
| 42 |
|
| 43 | public RealmsHeader(final Screen screen, final Runnable openJoinRealmScreen) {
|
| 44 | this.screen = screen;
|
| 45 | this.pendingInvitesButton = new RealmsHeader.NotificationButton(
|
| 46 | INVITES_TITLE, INVITE_SPRITE, var2 -> this.minecraft.gui.setScreen(new RealmsPendingInvitesScreen(screen, INVITES_TITLE)), null
|
| 47 | );
|
| 48 | this.newsButton = new RealmsHeader.NotificationButton(NEWS_TITLE, NEWS_SPRITE, var1 -> this.openNews(), NEWS_TITLE);
|
| 49 | this.joinRealmButton = Button.builder(JOIN_REALM_TEXT, button -> openJoinRealmScreen.run()).build();
|
| 50 | }
|
| 51 |
|
| 52 | public Layout createLayout(final ImageWidget logo, final int headerHeight, final int buttonSpacing) {
|
| 53 | int sideCellWidth = (308 - logo.getWidth()) / 2;
|
| 54 | this.joinRealmButton.setWidth(Math.min(this.minecraft.font.width(JOIN_REALM_TEXT) + 16, sideCellWidth));
|
| 55 | LinearLayout buttons = LinearLayout.horizontal().spacing(buttonSpacing);
|
| 56 | buttons.defaultCellSetting().alignVerticallyMiddle();
|
| 57 | buttons.addChild(this.pendingInvitesButton);
|
| 58 | buttons.addChild(this.newsButton);
|
| 59 | LinearLayout centeredContent = LinearLayout.horizontal();
|
| 60 | centeredContent.defaultCellSetting().alignVerticallyMiddle();
|
| 61 | centeredContent.addChild(new FrameLayout(sideCellWidth, headerHeight)).addChild(this.joinRealmButton, LayoutSettings::alignHorizontallyLeft);
|
| 62 | centeredContent.addChild(logo, LayoutSettings::alignHorizontallyCenter);
|
| 63 | centeredContent.addChild(new FrameLayout(sideCellWidth, headerHeight)).addChild(buttons, LayoutSettings::alignHorizontallyRight);
|
| 64 | return centeredContent;
|
| 65 | }
|
| 66 |
|
| 67 | public void setPendingInvites(final int numberOfPendingInvites) {
|
| 68 | this.pendingInvitesButton.setNotificationCount(numberOfPendingInvites);
|
| 69 | this.pendingInvitesButton.setTooltip(numberOfPendingInvites == 0 ? NO_PENDING_INVITES_TOOLTIP : PENDING_INVITES_TOOLTIP);
|
| 70 | }
|
| 71 |
|
| 72 | public void setNews(final @Nullable URI newsLink, final boolean hasUnreadNews) {
|
| 73 | this.newsLink = newsLink;
|
| 74 | this.newsButton.setNotificationCount(hasUnreadNews ? Integer.MAX_VALUE : 0);
|
| 75 | }
|
| 76 |
|
| 77 | public void setJoinRealmButtonActive(final boolean active) {
|
| 78 | this.joinRealmButton.active = active;
|
| 79 | }
|
| 80 |
|
| 81 | private void openNews() {
|
| 82 | URI newsLink = this.newsLink;
|
| 83 | if (newsLink != null) {
|
| 84 | ConfirmLinkScreen.confirmLinkNow(this.screen, newsLink);
|
| 85 | if (this.newsButton.notificationCount() != 0) {
|
| 86 | RealmsPersistence.RealmsPersistenceData data = RealmsPersistence.readFile();
|
| 87 | data.hasUnreadNews = false;
|
| 88 | RealmsPersistence.writeFile(data);
|
| 89 | this.newsButton.setNotificationCount(0);
|
| 90 | }
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | @Environment(EnvType.CLIENT)
|
| 95 | private static class NotificationButton extends SpriteIconButton.CenteredIcon {
|
| 96 | private static final Identifier[] NOTIFICATION_ICONS = new Identifier[]{
|
| 97 | Identifier.withDefaultNamespace("notification/1"),
|
| 98 | Identifier.withDefaultNamespace("notification/2"),
|
| 99 | Identifier.withDefaultNamespace("notification/3"),
|
| 100 | Identifier.withDefaultNamespace("notification/4"),
|
| 101 | Identifier.withDefaultNamespace("notification/5"),
|
| 102 | Identifier.withDefaultNamespace("notification/more")
|
| 103 | };
|
| 104 | private static final int UNKNOWN_COUNT = Integer.MAX_VALUE;
|
| 105 | private static final int SIZE = 20;
|
| 106 | private static final int SPRITE_SIZE = 14;
|
| 107 | private static final int NOTIFICATION_SIZE = 8;
|
| 108 | private static final int NOTIFICATION_X_OFFSET = -5;
|
| 109 | private static final int NOTIFICATION_Y_OFFSET = -3;
|
| 110 | private int notificationCount;
|
| 111 |
|
| 112 | public NotificationButton(final Component title, final Identifier texture, final Button.OnPress onPress, final @Nullable Component tooltip) {
|
| 113 | super(20, 20, title, 14, 14, 0, 0, new WidgetSprites(texture), onPress, tooltip, null, false);
|
| 114 | }
|
| 115 |
|
| 116 | private int notificationCount() {
|
| 117 | return this.notificationCount;
|
| 118 | }
|
| 119 |
|
| 120 | public void setNotificationCount(final int notificationCount) {
|
| 121 | this.notificationCount = notificationCount;
|
| 122 | }
|
| 123 |
|
| 124 | @Override
|
| 125 | public void extractContents(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 126 | super.extractContents(graphics, mouseX, mouseY, a);
|
| 127 | if (this.isActive() && this.notificationCount != 0) {
|
| 128 | this.extractNotificationCounter(graphics);
|
| 129 | }
|
| 130 | }
|
| 131 |
|
| 132 | private void extractNotificationCounter(final GuiGraphicsExtractor graphics) {
|
| 133 | graphics.blitSprite(
|
| 134 | RenderPipelines.GUI_TEXTURED,
|
| 135 | NOTIFICATION_ICONS[Math.min(this.notificationCount, NOTIFICATION_ICONS.length) - 1],
|
| 136 | this.getX() + this.getWidth() + -5,
|
| 137 | this.getY() + -3,
|
| 138 | 8,
|
| 139 | 8
|
| 140 | );
|
| 141 | }
|
| 142 | }
|
| 143 | }
|
| 144 |
|