RealmsPlayersTab.java

com.mojang.realmsclient.gui.screens.configuration.RealmsPlayersTab

信息

  • 全限定名:com.mojang.realmsclient.gui.screens.configuration.RealmsPlayersTab
  • 类型:package-private class
  • 包:com.mojang.realmsclient.gui.screens.configuration
  • 源码路径:src/main/java/com/mojang/realmsclient/gui/screens/configuration/RealmsPlayersTab.java
  • 起始行号:L35
  • 继承:GridLayoutTab
  • 实现:RealmsConfigurationTab
  • 职责:

    TODO

字段/常量

  • LOGGER

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

      TODO

  • TITLE

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

      TODO

  • QUESTION_TITLE

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

      TODO

  • PADDING

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

      TODO

  • configurationScreen

    • 类型: RealmsConfigureWorldScreen
    • 修饰符: private final
    • 源码定位: L40
    • 说明:

      TODO

  • minecraft

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

      TODO

  • font

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

      TODO

  • serverData

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

      TODO

  • invitedList

    • 类型: RealmsPlayersTab.InvitedObjectSelectionList
    • 修饰符: private final
    • 源码定位: L44
    • 说明:

      TODO

内部类/嵌套类型

  • com.mojang.realmsclient.gui.screens.configuration.RealmsPlayersTab.Entry

    • 类型: class
    • 修饰符: private abstract static
    • 源码定位: L85
    • 说明:

      TODO

  • com.mojang.realmsclient.gui.screens.configuration.RealmsPlayersTab.HeaderEntry

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

      TODO

  • com.mojang.realmsclient.gui.screens.configuration.RealmsPlayersTab.InvitedObjectSelectionList

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

      TODO

  • com.mojang.realmsclient.gui.screens.configuration.RealmsPlayersTab.PlayerEntry

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

      TODO

构造器

RealmsPlayersTab(RealmsConfigureWorldScreen configurationScreen, Minecraft minecraft, RealmsServer serverData) @ L46

  • 构造器名:RealmsPlayersTab
  • 源码定位:L46
  • 修饰符:package-private

参数:

  • configurationScreen: RealmsConfigureWorldScreen
  • minecraft: Minecraft
  • serverData: RealmsServer

说明:

TODO

方法

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

public int calculateListHeight() @ L68

  • 方法名:calculateListHeight
  • 源码定位:L68
  • 返回类型:int
  • 修饰符:public

参数:

说明:

TODO

public void doLayout(ScreenRectangle screenRectangle) @ L72

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

参数:

  • screenRectangle: ScreenRectangle

说明:

TODO

public void updateData(RealmsServer serverData) @ L78

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

参数:

  • serverData: RealmsServer

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
class RealmsPlayersTab extends GridLayoutTab implements RealmsConfigurationTab {
    private static final Logger LOGGER = LogUtils.getLogger();
    static final Component TITLE = Component.translatable("mco.configure.world.players.title");
    private static final Component QUESTION_TITLE = Component.translatable("mco.question");
    private static final int PADDING = 8;
    private final RealmsConfigureWorldScreen configurationScreen;
    private final Minecraft minecraft;
    private final Font font;
    private RealmsServer serverData;
    private final RealmsPlayersTab.InvitedObjectSelectionList invitedList;
 
    RealmsPlayersTab(RealmsConfigureWorldScreen configurationScreen, Minecraft minecraft, RealmsServer serverData) {
        super(TITLE);
        this.configurationScreen = configurationScreen;
        this.minecraft = minecraft;
        this.font = configurationScreen.getFont();
        this.serverData = serverData;
        GridLayout.RowHelper helper = this.layout.spacing(8).createRowHelper(1);
        this.invitedList = helper.addChild(
            new RealmsPlayersTab.InvitedObjectSelectionList(configurationScreen.width, this.calculateListHeight()),
            LayoutSettings.defaults().alignVerticallyTop().alignHorizontallyCenter()
        );
        helper.addChild(
            Button.builder(
                    Component.translatable("mco.configure.world.buttons.invite"),
                    button -> minecraft.setScreen(new RealmsInviteScreen(configurationScreen, serverData))
                )
                .build(),
            LayoutSettings.defaults().alignVerticallyBottom().alignHorizontallyCenter()
        );
        this.updateData(serverData);
    }
 
    public int calculateListHeight() {
        return this.configurationScreen.getContentHeight() - 20 - 16;
    }
 
    @Override
    public void doLayout(ScreenRectangle screenRectangle) {
        this.invitedList.updateSizeAndPosition(this.configurationScreen.width, this.calculateListHeight(), this.configurationScreen.layout.getHeaderHeight());
        super.doLayout(screenRectangle);
    }
 
    @Override
    public void updateData(RealmsServer serverData) {
        this.serverData = serverData;
        this.invitedList.updateList(serverData);
    }
 
    @OnlyIn(Dist.CLIENT)
    private abstract static class Entry extends ContainerObjectSelectionList.Entry<RealmsPlayersTab.Entry> {
    }
 
    @OnlyIn(Dist.CLIENT)
    private class HeaderEntry extends RealmsPlayersTab.Entry {
        private String cachedNumberOfInvites;
        private final FocusableTextWidget invitedWidget;
 
        public HeaderEntry() {
            Objects.requireNonNull(RealmsPlayersTab.this);
            super();
            this.cachedNumberOfInvites = "";
            Component invitedText = Component.translatable("mco.configure.world.invited.number", "").withStyle(ChatFormatting.UNDERLINE);
            this.invitedWidget = FocusableTextWidget.builder(invitedText, RealmsPlayersTab.this.font)
                .alwaysShowBorder(false)
                .backgroundFill(FocusableTextWidget.BackgroundFill.ON_FOCUS)
                .build();
        }
 
        @Override
        public void extractContent(GuiGraphicsExtractor graphics, int mouseX, int mouseY, boolean hovered, float a) {
            String numberOfInvites = RealmsPlayersTab.this.serverData.players != null ? Integer.toString(RealmsPlayersTab.this.serverData.players.size()) : "0";
            if (!numberOfInvites.equals(this.cachedNumberOfInvites)) {
                this.cachedNumberOfInvites = numberOfInvites;
                Component invitedComponent = Component.translatable("mco.configure.world.invited.number", numberOfInvites).withStyle(ChatFormatting.UNDERLINE);
                this.invitedWidget.setMessage(invitedComponent);
            }
 
            this.invitedWidget
                .setPosition(
                    RealmsPlayersTab.this.invitedList.getRowLeft() + RealmsPlayersTab.this.invitedList.getRowWidth() / 2 - this.invitedWidget.getWidth() / 2,
                    this.getY() + this.getHeight() / 2 - this.invitedWidget.getHeight() / 2
                );
            this.invitedWidget.extractRenderState(graphics, mouseX, mouseY, a);
        }
 
        private int height(int lineHeight) {
            return lineHeight + this.invitedWidget.getPadding() * 2;
        }
 
        @Override
        public List<? extends NarratableEntry> narratables() {
            return List.of(this.invitedWidget);
        }
 
        @Override
        public List<? extends GuiEventListener> children() {
            return List.of(this.invitedWidget);
        }
    }
 
    @OnlyIn(Dist.CLIENT)
    private class InvitedObjectSelectionList extends ContainerObjectSelectionList<RealmsPlayersTab.Entry> {
        private static final int PLAYER_ENTRY_HEIGHT = 36;
 
        public InvitedObjectSelectionList(int width, int height) {
            Objects.requireNonNull(RealmsPlayersTab.this);
            super(Minecraft.getInstance(), width, height, RealmsPlayersTab.this.configurationScreen.getHeaderHeight(), 36);
        }
 
        private void updateList(RealmsServer serverData) {
            this.clearEntries();
            this.populateList(serverData);
        }
 
        private void populateList(RealmsServer serverData) {
            RealmsPlayersTab.HeaderEntry entry = RealmsPlayersTab.this.new HeaderEntry();
            this.addEntry(entry, entry.height(9));
 
            for (RealmsPlayersTab.PlayerEntry newChild : serverData.players.stream().map(x$0 -> RealmsPlayersTab.this.new PlayerEntry(x$0)).toList()) {
                this.addEntry(newChild);
            }
        }
 
        @Override
        protected void extractListBackground(GuiGraphicsExtractor graphics) {
        }
 
        @Override
        protected void extractListSeparators(GuiGraphicsExtractor graphics) {
        }
 
        @Override
        public int getRowWidth() {
            return 300;
        }
    }
 
    @OnlyIn(Dist.CLIENT)
    private class PlayerEntry extends RealmsPlayersTab.Entry {
        protected static final int SKIN_FACE_SIZE = 32;
        private static final Component NORMAL_USER_TEXT = Component.translatable("mco.configure.world.invites.normal.tooltip");
        private static final Component OP_TEXT = Component.translatable("mco.configure.world.invites.ops.tooltip");
        private static final Component REMOVE_TEXT = Component.translatable("mco.configure.world.invites.remove.tooltip");
        private static final Identifier MAKE_OP_SPRITE = Identifier.withDefaultNamespace("player_list/make_operator");
        private static final Identifier REMOVE_OP_SPRITE = Identifier.withDefaultNamespace("player_list/remove_operator");
        private static final Identifier REMOVE_PLAYER_SPRITE = Identifier.withDefaultNamespace("player_list/remove_player");
        private static final int ICON_WIDTH = 8;
        private static final int ICON_HEIGHT = 7;
        private final PlayerInfo playerInfo;
        private final Button removeButton;
        private final Button makeOpButton;
        private final Button removeOpButton;
 
        public PlayerEntry(PlayerInfo playerInfo) {
            Objects.requireNonNull(RealmsPlayersTab.this);
            super();
            this.playerInfo = playerInfo;
            int index = RealmsPlayersTab.this.serverData.players.indexOf(this.playerInfo);
            this.makeOpButton = SpriteIconButton.builder(NORMAL_USER_TEXT, button -> this.op(index), false)
                .sprite(MAKE_OP_SPRITE, 8, 7)
                .width(16 + RealmsPlayersTab.this.configurationScreen.getFont().width(NORMAL_USER_TEXT))
                .narration(
                    defaultNarrationSupplier -> CommonComponents.joinForNarration(
                        Component.translatable("mco.invited.player.narration", playerInfo.name),
                        defaultNarrationSupplier.get(),
                        Component.translatable("narration.cycle_button.usage.focused", OP_TEXT)
                    )
                )
                .build();
            this.removeOpButton = SpriteIconButton.builder(OP_TEXT, button -> this.deop(index), false)
                .sprite(REMOVE_OP_SPRITE, 8, 7)
                .width(16 + RealmsPlayersTab.this.configurationScreen.getFont().width(OP_TEXT))
                .narration(
                    defaultNarrationSupplier -> CommonComponents.joinForNarration(
                        Component.translatable("mco.invited.player.narration", playerInfo.name),
                        defaultNarrationSupplier.get(),
                        Component.translatable("narration.cycle_button.usage.focused", NORMAL_USER_TEXT)
                    )
                )
                .build();
            this.removeButton = SpriteIconButton.builder(REMOVE_TEXT, button -> this.uninvite(index), false)
                .sprite(REMOVE_PLAYER_SPRITE, 8, 7)
                .width(16 + RealmsPlayersTab.this.configurationScreen.getFont().width(REMOVE_TEXT))
                .narration(
                    defaultNarrationSupplier -> CommonComponents.joinForNarration(
                        Component.translatable("mco.invited.player.narration", playerInfo.name), defaultNarrationSupplier.get()
                    )
                )
                .build();
            this.updateOpButtons();
        }
 
        private void op(int index) {
            UUID selectedInvite = RealmsPlayersTab.this.serverData.players.get(index).uuid;
            RealmsUtil.<Ops>supplyAsync(
                    client -> client.op(RealmsPlayersTab.this.serverData.id, selectedInvite),
                    e -> RealmsPlayersTab.LOGGER.error("Couldn't op the user", (Throwable)e)
                )
                .thenAcceptAsync(ops -> {
                    this.updateOps(ops);
                    this.updateOpButtons();
                    this.setFocused(this.removeOpButton);
                }, RealmsPlayersTab.this.minecraft);
        }
 
        private void deop(int index) {
            UUID selectedInvite = RealmsPlayersTab.this.serverData.players.get(index).uuid;
            RealmsUtil.<Ops>supplyAsync(
                    client -> client.deop(RealmsPlayersTab.this.serverData.id, selectedInvite),
                    e -> RealmsPlayersTab.LOGGER.error("Couldn't deop the user", (Throwable)e)
                )
                .thenAcceptAsync(ops -> {
                    this.updateOps(ops);
                    this.updateOpButtons();
                    this.setFocused(this.makeOpButton);
                }, RealmsPlayersTab.this.minecraft);
        }
 
        private void uninvite(int index) {
            if (index >= 0 && index < RealmsPlayersTab.this.serverData.players.size()) {
                PlayerInfo playerInfo = RealmsPlayersTab.this.serverData.players.get(index);
                RealmsConfirmScreen confirmScreen = new RealmsConfirmScreen(
                    result -> {
                        if (result) {
                            RealmsUtil.runAsync(
                                client -> client.uninvite(RealmsPlayersTab.this.serverData.id, playerInfo.uuid),
                                e -> RealmsPlayersTab.LOGGER.error("Couldn't uninvite user", (Throwable)e)
                            );
                            RealmsPlayersTab.this.serverData.players.remove(index);
                            RealmsPlayersTab.this.updateData(RealmsPlayersTab.this.serverData);
                        }
 
                        RealmsPlayersTab.this.minecraft.setScreen(RealmsPlayersTab.this.configurationScreen);
                    },
                    RealmsPlayersTab.QUESTION_TITLE,
                    Component.translatable("mco.configure.world.uninvite.player", playerInfo.name)
                );
                RealmsPlayersTab.this.minecraft.setScreen(confirmScreen);
            }
        }
 
        private void updateOps(Ops ops) {
            for (PlayerInfo playerInfo : RealmsPlayersTab.this.serverData.players) {
                playerInfo.operator = ops.ops().contains(playerInfo.name);
            }
        }
 
        private void updateOpButtons() {
            this.makeOpButton.visible = !this.playerInfo.operator;
            this.removeOpButton.visible = !this.makeOpButton.visible;
        }
 
        private Button activeOpButton() {
            return this.makeOpButton.visible ? this.makeOpButton : this.removeOpButton;
        }
 
        @Override
        public List<? extends GuiEventListener> children() {
            return ImmutableList.of(this.activeOpButton(), this.removeButton);
        }
 
        @Override
        public List<? extends NarratableEntry> narratables() {
            return ImmutableList.of(this.activeOpButton(), this.removeButton);
        }
 
        @Override
        public void extractContent(GuiGraphicsExtractor graphics, int mouseX, int mouseY, boolean hovered, float a) {
            int inviteColor;
            if (!this.playerInfo.accepted) {
                inviteColor = -6250336;
            } else if (this.playerInfo.online) {
                inviteColor = -16711936;
            } else {
                inviteColor = -1;
            }
 
            int skinYPos = this.getContentYMiddle() - 16;
            RealmsUtil.extractPlayerFace(graphics, this.getContentX(), skinYPos, 32, this.playerInfo.uuid);
            int textYPos = this.getContentYMiddle() - 9 / 2;
            graphics.text(RealmsPlayersTab.this.font, this.playerInfo.name, this.getContentX() + 8 + 32, textYPos, inviteColor);
            int iconYPos = this.getContentYMiddle() - 10;
            int removeButtonXPos = this.getContentRight() - this.removeButton.getWidth();
            this.removeButton.setPosition(removeButtonXPos, iconYPos);
            this.removeButton.extractRenderState(graphics, mouseX, mouseY, a);
            int opButtonXPos = removeButtonXPos - this.activeOpButton().getWidth() - 8;
            this.makeOpButton.setPosition(opButtonXPos, iconYPos);
            this.makeOpButton.extractRenderState(graphics, mouseX, mouseY, a);
            this.removeOpButton.setPosition(opButtonXPos, iconYPos);
            this.removeOpButton.extractRenderState(graphics, mouseX, mouseY, a);
        }
    }
}

引用的其他类