GameModeSwitcherScreen.java

net.minecraft.client.gui.screens.debug.GameModeSwitcherScreen

信息

  • 全限定名:net.minecraft.client.gui.screens.debug.GameModeSwitcherScreen
  • 类型:public class
  • 包:net.minecraft.client.gui.screens.debug
  • 源码路径:src/main/java/net/minecraft/client/gui/screens/debug/GameModeSwitcherScreen.java
  • 起始行号:L29
  • 继承:Screen
  • 职责:

    TODO

字段/常量

  • SLOT_SPRITE

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

      TODO

  • SELECTION_SPRITE

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

      TODO

  • GAMEMODE_SWITCHER_LOCATION

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

      TODO

  • SPRITE_SHEET_WIDTH

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

      TODO

  • SPRITE_SHEET_HEIGHT

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

      TODO

  • SLOT_AREA

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

      TODO

  • SLOT_PADDING

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

      TODO

  • SLOT_AREA_PADDED

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

      TODO

  • HELP_TIPS_OFFSET_Y

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

      TODO

  • ALL_SLOTS_WIDTH

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

      TODO

  • previousHovered

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

      TODO

  • currentlyHovered

    • 类型: GameModeSwitcherScreen.GameModeIcon
    • 修饰符: private
    • 源码定位: L41
    • 说明:

      TODO

  • firstMouseX

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

      TODO

  • firstMouseY

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

      TODO

  • setFirstMousePos

    • 类型: boolean
    • 修饰符: private
    • 源码定位: L44
    • 说明:

      TODO

  • slots

    • 类型: List<GameModeSwitcherScreen.GameModeSlot>
    • 修饰符: private final
    • 源码定位: L45
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.client.gui.screens.debug.GameModeSwitcherScreen.GameModeIcon

    • 类型: enum
    • 修饰符: private static
    • 源码定位: L158
    • 说明:

      TODO

  • net.minecraft.client.gui.screens.debug.GameModeSwitcherScreen.GameModeSlot

    • 类型: class
    • 修饰符: public static
    • 源码定位: L201
    • 说明:

      TODO

构造器

public GameModeSwitcherScreen() @ L47

  • 构造器名:GameModeSwitcherScreen
  • 源码定位:L47
  • 修饰符:public

参数:

说明:

TODO

方法

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

private GameType getDefaultSelected() @ L53

  • 方法名:getDefaultSelected
  • 源码定位:L53
  • 返回类型:GameType
  • 修饰符:private

参数:

说明:

TODO

protected void init() @ L63

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

参数:

说明:

TODO

public void extractRenderState(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a) @ L75

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

参数:

  • graphics: GuiGraphicsExtractor
  • mouseX: int
  • mouseY: int
  • a: float

说明:

TODO

public void extractBackground(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a) @ L99

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

参数:

  • graphics: GuiGraphicsExtractor
  • mouseX: int
  • mouseY: int
  • a: float

说明:

TODO

private void switchToHoveredGameMode() @ L106

  • 方法名:switchToHoveredGameMode
  • 源码定位:L106
  • 返回类型:void
  • 修饰符:private

参数:

说明:

TODO

private static void switchToHoveredGameMode(Minecraft minecraft, GameModeSwitcherScreen.GameModeIcon toGameMode) @ L110

  • 方法名:switchToHoveredGameMode
  • 源码定位:L110
  • 返回类型:void
  • 修饰符:private static

参数:

  • minecraft: Minecraft
  • toGameMode: GameModeSwitcherScreen.GameModeIcon

说明:

TODO

public boolean keyPressed(KeyEvent event) @ L119

  • 方法名:keyPressed
  • 源码定位:L119
  • 返回类型:boolean
  • 修饰符:public

参数:

  • event: KeyEvent

说明:

TODO

public boolean keyReleased(KeyEvent event) @ L130

  • 方法名:keyReleased
  • 源码定位:L130
  • 返回类型:boolean
  • 修饰符:public

参数:

  • event: KeyEvent

说明:

TODO

public boolean mouseReleased(MouseButtonEvent event) @ L141

  • 方法名:mouseReleased
  • 源码定位:L141
  • 返回类型:boolean
  • 修饰符:public

参数:

  • event: MouseButtonEvent

说明:

TODO

public boolean isPauseScreen() @ L152

  • 方法名:isPauseScreen
  • 源码定位:L152
  • 返回类型:boolean
  • 修饰符:public

参数:

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public class GameModeSwitcherScreen extends Screen {
    private static final Identifier SLOT_SPRITE = Identifier.withDefaultNamespace("gamemode_switcher/slot");
    private static final Identifier SELECTION_SPRITE = Identifier.withDefaultNamespace("gamemode_switcher/selection");
    private static final Identifier GAMEMODE_SWITCHER_LOCATION = Identifier.withDefaultNamespace("textures/gui/container/gamemode_switcher.png");
    private static final int SPRITE_SHEET_WIDTH = 128;
    private static final int SPRITE_SHEET_HEIGHT = 128;
    private static final int SLOT_AREA = 26;
    private static final int SLOT_PADDING = 5;
    private static final int SLOT_AREA_PADDED = 31;
    private static final int HELP_TIPS_OFFSET_Y = 5;
    private static final int ALL_SLOTS_WIDTH = GameModeSwitcherScreen.GameModeIcon.values().length * 31 - 5;
    private final GameModeSwitcherScreen.GameModeIcon previousHovered;
    private GameModeSwitcherScreen.GameModeIcon currentlyHovered;
    private int firstMouseX;
    private int firstMouseY;
    private boolean setFirstMousePos;
    private final List<GameModeSwitcherScreen.GameModeSlot> slots = Lists.newArrayList();
 
    public GameModeSwitcherScreen() {
        super(GameNarrator.NO_TITLE);
        this.previousHovered = GameModeSwitcherScreen.GameModeIcon.getFromGameType(this.getDefaultSelected());
        this.currentlyHovered = this.previousHovered;
    }
 
    private GameType getDefaultSelected() {
        MultiPlayerGameMode gameMode = Minecraft.getInstance().gameMode;
        GameType previous = gameMode.getPreviousPlayerMode();
        if (previous != null) {
            return previous;
        } else {
            return gameMode.getPlayerMode() == GameType.CREATIVE ? GameType.SURVIVAL : GameType.CREATIVE;
        }
    }
 
    @Override
    protected void init() {
        super.init();
        this.slots.clear();
        this.currentlyHovered = this.previousHovered;
 
        for (int i = 0; i < GameModeSwitcherScreen.GameModeIcon.VALUES.length; i++) {
            GameModeSwitcherScreen.GameModeIcon icon = GameModeSwitcherScreen.GameModeIcon.VALUES[i];
            this.slots.add(new GameModeSwitcherScreen.GameModeSlot(icon, this.width / 2 - ALL_SLOTS_WIDTH / 2 + i * 31, this.height / 2 - 31));
        }
    }
 
    @Override
    public void extractRenderState(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a) {
        graphics.centeredText(this.font, this.currentlyHovered.name, this.width / 2, this.height / 2 - 31 - 20, -1);
        MutableComponent selectKey = Component.translatable(
            "debug.gamemodes.select_next", this.minecraft.options.keyDebugSwitchGameMode.getTranslatedKeyMessage().copy().withStyle(ChatFormatting.AQUA)
        );
        graphics.centeredText(this.font, selectKey, this.width / 2, this.height / 2 + 5, -1);
        if (!this.setFirstMousePos) {
            this.firstMouseX = mouseX;
            this.firstMouseY = mouseY;
            this.setFirstMousePos = true;
        }
 
        boolean sameAsFirstMousePos = this.firstMouseX == mouseX && this.firstMouseY == mouseY;
 
        for (GameModeSwitcherScreen.GameModeSlot slot : this.slots) {
            slot.extractRenderState(graphics, mouseX, mouseY, a);
            slot.setSelected(this.currentlyHovered == slot.icon);
            if (!sameAsFirstMousePos && slot.isHoveredOrFocused()) {
                this.currentlyHovered = slot.icon;
            }
        }
    }
 
    @Override
    public void extractBackground(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a) {
        int xo = this.width / 2 - 62;
        int yo = this.height / 2 - 31 - 27;
        graphics.blit(RenderPipelines.GUI_TEXTURED, GAMEMODE_SWITCHER_LOCATION, xo, yo, 0.0F, 0.0F, 125, 75, 128, 128);
    }
 
    private void switchToHoveredGameMode() {
        switchToHoveredGameMode(this.minecraft, this.currentlyHovered);
    }
 
    private static void switchToHoveredGameMode(Minecraft minecraft, GameModeSwitcherScreen.GameModeIcon toGameMode) {
        if (minecraft.canSwitchGameMode()) {
            GameModeSwitcherScreen.GameModeIcon currentGameMode = GameModeSwitcherScreen.GameModeIcon.getFromGameType(minecraft.gameMode.getPlayerMode());
            if (toGameMode != currentGameMode && GameModeCommand.PERMISSION_CHECK.check(minecraft.player.permissions())) {
                minecraft.player.connection.send(new ServerboundChangeGameModePacket(toGameMode.mode));
            }
        }
    }
 
    @Override
    public boolean keyPressed(KeyEvent event) {
        if (this.minecraft.options.keyDebugSwitchGameMode.matches(event)) {
            this.setFirstMousePos = false;
            this.currentlyHovered = this.currentlyHovered.getNext();
            return true;
        } else {
            return super.keyPressed(event);
        }
    }
 
    @Override
    public boolean keyReleased(KeyEvent event) {
        if (this.minecraft.options.keyDebugModifier.matches(event)) {
            this.switchToHoveredGameMode();
            this.minecraft.setScreen(null);
            return true;
        } else {
            return super.keyReleased(event);
        }
    }
 
    @Override
    public boolean mouseReleased(MouseButtonEvent event) {
        if (this.minecraft.options.keyDebugModifier.matchesMouse(event)) {
            this.switchToHoveredGameMode();
            this.minecraft.setScreen(null);
            return true;
        } else {
            return super.mouseReleased(event);
        }
    }
 
    @Override
    public boolean isPauseScreen() {
        return false;
    }
 
    @OnlyIn(Dist.CLIENT)
    private static enum GameModeIcon {
        CREATIVE(Component.translatable("gameMode.creative"), GameType.CREATIVE, new ItemStack(Blocks.GRASS_BLOCK)),
        SURVIVAL(Component.translatable("gameMode.survival"), GameType.SURVIVAL, new ItemStack(Items.IRON_SWORD)),
        ADVENTURE(Component.translatable("gameMode.adventure"), GameType.ADVENTURE, new ItemStack(Items.MAP)),
        SPECTATOR(Component.translatable("gameMode.spectator"), GameType.SPECTATOR, new ItemStack(Items.ENDER_EYE));
 
        private static final GameModeSwitcherScreen.GameModeIcon[] VALUES = values();
        private static final int ICON_AREA = 16;
        private static final int ICON_TOP_LEFT = 5;
        private final Component name;
        private final GameType mode;
        private final ItemStack renderStack;
 
        private GameModeIcon(Component name, GameType mode, ItemStack renderStack) {
            this.name = name;
            this.mode = mode;
            this.renderStack = renderStack;
        }
 
        private void extractIcon(GuiGraphicsExtractor graphics, int x, int y) {
            graphics.item(this.renderStack, x, y);
        }
 
        private GameModeSwitcherScreen.GameModeIcon getNext() {
            return switch (this) {
                case CREATIVE -> SURVIVAL;
                case SURVIVAL -> ADVENTURE;
                case ADVENTURE -> SPECTATOR;
                case SPECTATOR -> CREATIVE;
            };
        }
 
        private static GameModeSwitcherScreen.GameModeIcon getFromGameType(GameType gameType) {
            return switch (gameType) {
                case SPECTATOR -> SPECTATOR;
                case SURVIVAL -> SURVIVAL;
                case CREATIVE -> CREATIVE;
                case ADVENTURE -> ADVENTURE;
            };
        }
    }
 
    @OnlyIn(Dist.CLIENT)
    public static class GameModeSlot extends AbstractWidget {
        private final GameModeSwitcherScreen.GameModeIcon icon;
        private boolean isSelected;
 
        public GameModeSlot(GameModeSwitcherScreen.GameModeIcon icon, int x, int y) {
            super(x, y, 26, 26, icon.name);
            this.icon = icon;
        }
 
        @Override
        public void extractWidgetRenderState(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a) {
            this.extractSlot(graphics);
            if (this.isSelected) {
                this.extractSelection(graphics);
            }
 
            this.icon.extractIcon(graphics, this.getX() + 5, this.getY() + 5);
        }
 
        @Override
        public void updateWidgetNarration(NarrationElementOutput output) {
            this.defaultButtonNarrationText(output);
        }
 
        @Override
        public boolean isHoveredOrFocused() {
            return super.isHoveredOrFocused() || this.isSelected;
        }
 
        public void setSelected(boolean isSelected) {
            this.isSelected = isSelected;
        }
 
        private void extractSlot(GuiGraphicsExtractor graphics) {
            graphics.blitSprite(RenderPipelines.GUI_TEXTURED, GameModeSwitcherScreen.SLOT_SPRITE, this.getX(), this.getY(), 26, 26);
        }
 
        private void extractSelection(GuiGraphicsExtractor graphics) {
            graphics.blitSprite(RenderPipelines.GUI_TEXTURED, GameModeSwitcherScreen.SELECTION_SPRITE, this.getX(), this.getY(), 26, 26);
        }
    }
}

引用的其他类