RealmsPendingInvitesScreen.java

com.mojang.realmsclient.gui.screens.RealmsPendingInvitesScreen

信息

  • 全限定名:com.mojang.realmsclient.gui.screens.RealmsPendingInvitesScreen
  • 类型:public class
  • 包:com.mojang.realmsclient.gui.screens
  • 源码路径:src/main/java/com/mojang/realmsclient/gui/screens/RealmsPendingInvitesScreen.java
  • 起始行号:L40
  • 继承:RealmsScreen
  • 职责:

    TODO

字段/常量

  • LOGGER

    • 类型: Logger
    • 修饰符: private static final
    • 源码定位: L41
    • 说明:

      TODO

  • NO_PENDING_INVITES_TEXT

    • 类型: Component
    • 修饰符: private static final
    • 源码定位: L42
    • 说明:

      TODO

  • lastScreen

    • 类型: Screen
    • 修饰符: private final
    • 源码定位: L43
    • 说明:

      TODO

  • pendingInvites

    • 类型: CompletableFuture<List<PendingInvite>>
    • 修饰符: private final
    • 源码定位: L44
    • 说明:

      TODO

  • layout

    • 类型: HeaderAndFooterLayout
    • 修饰符: private final
    • 源码定位: L52
    • 说明:

      TODO

  • pendingInvitationSelectionList

    • 类型: RealmsPendingInvitesScreen.PendingInvitationSelectionList
    • 修饰符: private
    • 源码定位: L53
    • 说明:

      TODO

内部类/嵌套类型

  • com.mojang.realmsclient.gui.screens.RealmsPendingInvitesScreen.Entry

    • 类型: class
    • 修饰符: private
    • 源码定位: L99
    • 说明:

      TODO

  • com.mojang.realmsclient.gui.screens.RealmsPendingInvitesScreen.PendingInvitationSelectionList

    • 类型: class
    • 修饰符: private
    • 源码定位: L221
    • 说明:

      TODO

构造器

public RealmsPendingInvitesScreen(Screen lastScreen, Component title) @ L55

  • 构造器名:RealmsPendingInvitesScreen
  • 源码定位:L55
  • 修饰符:public

参数:

  • lastScreen: Screen
  • title: Component

说明:

TODO

方法

下面的方法块按源码顺序生成。

public void init() @ L60

  • 方法名:init
  • 源码定位:L60
  • 返回类型:void
  • 修饰符:public

参数:

说明:

TODO

protected void repositionElements() @ L77

  • 方法名:repositionElements
  • 源码定位:L77
  • 返回类型:void
  • 修饰符:protected

参数:

说明:

TODO

public void onClose() @ L85

  • 方法名:onClose
  • 源码定位:L85
  • 返回类型:void
  • 修饰符:public

参数:

说明:

TODO

public void extractRenderState(GuiGraphicsExtractor graphics, int xm, int ym, float a) @ L90

  • 方法名:extractRenderState
  • 源码定位:L90
  • 返回类型:void
  • 修饰符:public

参数:

  • graphics: GuiGraphicsExtractor
  • xm: int
  • ym: int
  • a: float

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public class RealmsPendingInvitesScreen extends RealmsScreen {
    private static final Logger LOGGER = LogUtils.getLogger();
    private static final Component NO_PENDING_INVITES_TEXT = Component.translatable("mco.invites.nopending");
    private final Screen lastScreen;
    private final CompletableFuture<List<PendingInvite>> pendingInvites = CompletableFuture.supplyAsync(() -> {
        try {
            return RealmsClient.getOrCreate().pendingInvites().pendingInvites();
        } catch (RealmsServiceException var1) {
            LOGGER.error("Couldn't list invites", (Throwable)var1);
            return List.of();
        }
    }, Util.ioPool());
    private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this);
    private RealmsPendingInvitesScreen.@Nullable PendingInvitationSelectionList pendingInvitationSelectionList;
 
    public RealmsPendingInvitesScreen(Screen lastScreen, Component title) {
        super(title);
        this.lastScreen = lastScreen;
    }
 
    @Override
    public void init() {
        RealmsMainScreen.refreshPendingInvites();
        this.layout.addTitleHeader(this.title, this.font);
        this.pendingInvitationSelectionList = this.layout.addToContents(new RealmsPendingInvitesScreen.PendingInvitationSelectionList(this.minecraft));
        this.pendingInvites.thenAcceptAsync(invites -> {
            List<RealmsPendingInvitesScreen.Entry> entries = invites.stream().map(x$0 -> new RealmsPendingInvitesScreen.Entry(x$0)).toList();
            this.pendingInvitationSelectionList.replaceEntries(entries);
            if (entries.isEmpty()) {
                this.minecraft.getNarrator().saySystemQueued(NO_PENDING_INVITES_TEXT);
            }
        }, this.screenExecutor);
        this.layout.addToFooter(Button.builder(CommonComponents.GUI_DONE, button -> this.onClose()).width(200).build());
        this.layout.visitWidgets(x$0 -> this.addRenderableWidget(x$0));
        this.repositionElements();
    }
 
    @Override
    protected void repositionElements() {
        this.layout.arrangeElements();
        if (this.pendingInvitationSelectionList != null) {
            this.pendingInvitationSelectionList.updateSize(this.width, this.layout);
        }
    }
 
    @Override
    public void onClose() {
        this.minecraft.setScreen(this.lastScreen);
    }
 
    @Override
    public void extractRenderState(GuiGraphicsExtractor graphics, int xm, int ym, float a) {
        super.extractRenderState(graphics, xm, ym, a);
        if (this.pendingInvites.isDone() && this.pendingInvitationSelectionList.hasPendingInvites()) {
            graphics.centeredText(this.font, NO_PENDING_INVITES_TEXT, this.width / 2, this.height / 2 - 20, -1);
        }
    }
 
    @OnlyIn(Dist.CLIENT)
    private class Entry extends ContainerObjectSelectionList.Entry<RealmsPendingInvitesScreen.Entry> {
        private static final Component ACCEPT_INVITE = Component.translatable("mco.invites.button.accept");
        private static final Component REJECT_INVITE = Component.translatable("mco.invites.button.reject");
        private static final WidgetSprites ACCEPT_SPRITE = new WidgetSprites(
            Identifier.withDefaultNamespace("pending_invite/accept"), Identifier.withDefaultNamespace("pending_invite/accept_highlighted")
        );
        private static final WidgetSprites REJECT_SPRITE = new WidgetSprites(
            Identifier.withDefaultNamespace("pending_invite/reject"), Identifier.withDefaultNamespace("pending_invite/reject_highlighted")
        );
        private static final int SPRITE_TEXTURE_SIZE = 18;
        private static final int SPRITE_SIZE = 21;
        private static final int TEXT_LEFT = 38;
        private final PendingInvite pendingInvite;
        private final List<AbstractWidget> children;
        private final SpriteIconButton acceptButton;
        private final SpriteIconButton rejectButton;
        private final StringWidget realmName;
        private final StringWidget realmOwnerName;
        private final StringWidget inviteDate;
 
        Entry(PendingInvite pendingInvite) {
            Objects.requireNonNull(RealmsPendingInvitesScreen.this);
            super();
            this.children = new ArrayList<>();
            this.pendingInvite = pendingInvite;
            int maxTextWidth = RealmsPendingInvitesScreen.this.pendingInvitationSelectionList.getRowWidth() - 32 - 32 - 42;
            this.realmName = new StringWidget(Component.literal(pendingInvite.realmName()), RealmsPendingInvitesScreen.this.font).setMaxWidth(maxTextWidth);
            this.realmOwnerName = new StringWidget(Component.literal(pendingInvite.realmOwnerName()).withColor(-6250336), RealmsPendingInvitesScreen.this.font)
                .setMaxWidth(maxTextWidth);
            this.inviteDate = new StringWidget(
                    ComponentUtils.mergeStyles(RealmsUtil.convertToAgePresentationFromInstant(pendingInvite.date()), Style.EMPTY.withColor(-6250336)),
                    RealmsPendingInvitesScreen.this.font
                )
                .setMaxWidth(maxTextWidth);
            Button.CreateNarration narration = this.getCreateNarration(pendingInvite);
            this.acceptButton = SpriteIconButton.builder(ACCEPT_INVITE, button -> this.handleInvitation(true), false)
                .sprite(ACCEPT_SPRITE, 18, 18)
                .size(21, 21)
                .narration(narration)
                .withTootip()
                .build();
            this.rejectButton = SpriteIconButton.builder(REJECT_INVITE, button -> this.handleInvitation(false), false)
                .sprite(REJECT_SPRITE, 18, 18)
                .size(21, 21)
                .narration(narration)
                .withTootip()
                .build();
            this.children.addAll(List.of(this.acceptButton, this.rejectButton));
        }
 
        private Button.CreateNarration getCreateNarration(PendingInvite pendingInvite) {
            return defaultNarrationSupplier -> {
                MutableComponent narration = CommonComponents.joinForNarration(
                    defaultNarrationSupplier.get(),
                    Component.literal(pendingInvite.realmName()),
                    Component.literal(pendingInvite.realmOwnerName()),
                    RealmsUtil.convertToAgePresentationFromInstant(pendingInvite.date())
                );
                return Component.translatable("narrator.select", narration);
            };
        }
 
        @Override
        public List<? extends GuiEventListener> children() {
            return this.children;
        }
 
        @Override
        public List<? extends NarratableEntry> narratables() {
            return this.children;
        }
 
        @Override
        public void extractContent(GuiGraphicsExtractor graphics, int mouseX, int mouseY, boolean hovered, float a) {
            int x = this.getContentX();
            int y = this.getContentY();
            int textX = x + 38;
            RealmsUtil.extractPlayerFace(graphics, x, y, 32, this.pendingInvite.realmOwnerUuid());
            this.realmName.setPosition(textX, y + 1);
            this.realmName.extractWidgetRenderState(graphics, mouseX, mouseY, x);
            this.realmOwnerName.setPosition(textX, y + 12);
            this.realmOwnerName.extractWidgetRenderState(graphics, mouseX, mouseY, x);
            this.inviteDate.setPosition(textX, y + 24);
            this.inviteDate.extractWidgetRenderState(graphics, mouseX, mouseY, x);
            int buttonY = y + this.getContentHeight() / 2 - 10;
            this.acceptButton.setPosition(x + this.getContentWidth() - 16 - 42, buttonY);
            this.acceptButton.extractRenderState(graphics, mouseX, mouseY, a);
            this.rejectButton.setPosition(x + this.getContentWidth() - 8 - 21, buttonY);
            this.rejectButton.extractRenderState(graphics, mouseX, mouseY, a);
        }
 
        private void handleInvitation(boolean accept) {
            String invitationId = this.pendingInvite.invitationId();
            CompletableFuture.<Boolean>supplyAsync(() -> {
                try {
                    RealmsClient client = RealmsClient.getOrCreate();
                    if (accept) {
                        client.acceptInvitation(invitationId);
                    } else {
                        client.rejectInvitation(invitationId);
                    }
 
                    return true;
                } catch (RealmsServiceException var3) {
                    RealmsPendingInvitesScreen.LOGGER.error("Couldn't handle invite", (Throwable)var3);
                    return false;
                }
            }, Util.ioPool()).thenAcceptAsync(result -> {
                if (result) {
                    RealmsPendingInvitesScreen.this.pendingInvitationSelectionList.removeInvitation(this);
                    RealmsDataFetcher dataFetcher = RealmsPendingInvitesScreen.this.minecraft.realmsDataFetcher();
                    if (accept) {
                        dataFetcher.serverListUpdateTask.reset();
                    }
 
                    dataFetcher.pendingInvitesTask.reset();
                }
            }, RealmsPendingInvitesScreen.this.screenExecutor);
        }
    }
 
    @OnlyIn(Dist.CLIENT)
    private class PendingInvitationSelectionList extends ContainerObjectSelectionList<RealmsPendingInvitesScreen.Entry> {
        public static final int ITEM_HEIGHT = 36;
 
        public PendingInvitationSelectionList(Minecraft minecraft) {
            Objects.requireNonNull(RealmsPendingInvitesScreen.this);
            super(
                minecraft,
                RealmsPendingInvitesScreen.this.width,
                RealmsPendingInvitesScreen.this.layout.getContentHeight(),
                RealmsPendingInvitesScreen.this.layout.getHeaderHeight(),
                36
            );
        }
 
        @Override
        public int getRowWidth() {
            return 280;
        }
 
        public boolean hasPendingInvites() {
            return this.getItemCount() == 0;
        }
 
        public void removeInvitation(RealmsPendingInvitesScreen.Entry entry) {
            this.removeEntry(entry);
        }
    }
}

引用的其他类

  • RealmsMainScreen

    • 引用位置: 方法调用
    • 关联成员: RealmsMainScreen.refreshPendingInvites()
  • RealmsClient

    • 引用位置: 方法调用
    • 关联成员: RealmsClient.getOrCreate()
  • PendingInvite

    • 引用位置: 字段
  • RealmsUtil

    • 引用位置: 方法调用
    • 关联成员: RealmsUtil.convertToAgePresentationFromInstant(), RealmsUtil.extractPlayerFace()
  • GuiGraphicsExtractor

    • 引用位置: 参数
  • Button

    • 引用位置: 方法调用
    • 关联成员: Button.builder()
  • SpriteIconButton

    • 引用位置: 方法调用
    • 关联成员: SpriteIconButton.builder()
  • StringWidget

    • 引用位置: 构造调用
    • 关联成员: StringWidget()
  • WidgetSprites

    • 引用位置: 构造调用
    • 关联成员: WidgetSprites()
  • HeaderAndFooterLayout

    • 引用位置: 字段/构造调用
    • 关联成员: HeaderAndFooterLayout()
  • Screen

    • 引用位置: 参数/字段
  • CommonComponents

    • 引用位置: 方法调用
    • 关联成员: CommonComponents.joinForNarration()
  • Component

    • 引用位置: 参数/字段/方法调用
    • 关联成员: Component.literal(), Component.translatable()
  • ComponentUtils

    • 引用位置: 方法调用
    • 关联成员: ComponentUtils.mergeStyles()
  • RealmsScreen

    • 引用位置: 继承
  • Identifier

    • 引用位置: 方法调用
    • 关联成员: Identifier.withDefaultNamespace()
  • Util

    • 引用位置: 方法调用
    • 关联成员: Util.ioPool()