RealmsSlotOptionsScreen.java

com.mojang.realmsclient.gui.screens.configuration.RealmsSlotOptionsScreen

信息

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

    TODO

字段/常量

  • DEFAULT_DIFFICULTY

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

      TODO

  • DIFFICULTIES

    • 类型: List<Difficulty>
    • 修饰符: public static final
    • 源码定位: L31
    • 说明:

      TODO

  • DEFAULT_GAME_MODE

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

      TODO

  • GAME_MODES

    • 类型: List<GameType>
    • 修饰符: public static final
    • 源码定位: L33
    • 说明:

      TODO

  • TITLE

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

      TODO

  • WORLD_NAME_EDIT_LABEL

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

      TODO

  • SPAWN_PROTECTION_TEXT

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

      TODO

  • GAME_MODE_BUTTON

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

      TODO

  • DIFFICULTY_BUTTON

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

      TODO

  • FORCE_GAME_MODE_BUTTON

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

      TODO

  • SPACING

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

      TODO

  • layout

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

      TODO

  • parentScreen

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

      TODO

  • slot

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

      TODO

  • worldType

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

      TODO

  • defaultSlotName

    • 类型: String
    • 修饰符: private final
    • 源码定位: L45
    • 说明:

      TODO

  • spawnProtection

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

      TODO

  • forceGameMode

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

      TODO

  • difficulty

    • 类型: Difficulty
    • 修饰符: private
    • 源码定位: L48
    • 说明:

      TODO

  • gameMode

    • 类型: GameType
    • 修饰符: private
    • 源码定位: L49
    • 说明:

      TODO

  • worldName

    • 类型: String
    • 修饰符: private
    • 源码定位: L50
    • 说明:

      TODO

  • warningHeader

    • 类型: StringWidget
    • 修饰符: private
    • 源码定位: L51
    • 说明:

      TODO

  • spawnProtectionButton

    • 类型: RealmsSlotOptionsScreen.SettingsSlider
    • 修饰符: private
    • 源码定位: L52
    • 说明:

      TODO

内部类/嵌套类型

  • com.mojang.realmsclient.gui.screens.configuration.RealmsSlotOptionsScreen.SettingsSlider
    • 类型: class
    • 修饰符: private
    • 源码定位: L205
    • 说明:

      TODO

构造器

public RealmsSlotOptionsScreen(RealmsConfigureWorldScreen configureWorldScreen, RealmsSlot slot, RealmsServer.WorldType worldType, int activeSlot) @ L54

  • 构造器名:RealmsSlotOptionsScreen
  • 源码定位:L54
  • 修饰符:public

参数:

  • configureWorldScreen: RealmsConfigureWorldScreen
  • slot: RealmsSlot
  • worldType: RealmsServer.WorldType
  • activeSlot: int

说明:

TODO

方法

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

public void init() @ L72

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

参数:

说明:

TODO

protected void repositionElements() @ L128

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

参数:

说明:

TODO

public void onClose() @ L133

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

参数:

说明:

TODO

private static <T> T findByIndex(List<T> values, int index, int defaultIndex) @ L138

  • 方法名:findByIndex
  • 源码定位:L138
  • 返回类型: T
  • 修饰符:private static

参数:

  • values: List
  • index: int
  • defaultIndex: int

说明:

TODO

private static <T> int findIndex(List<T> values, T value, int defaultIndex) @ L142

  • 方法名:findIndex
  • 源码定位:L142
  • 返回类型: int
  • 修饰符:private static

参数:

  • values: List
  • value: T
  • defaultIndex: int

说明:

TODO

public Component getNarrationMessage() @ L147

  • 方法名:getNarrationMessage
  • 源码定位:L147
  • 返回类型:Component
  • 修饰符:public

参数:

说明:

TODO

private void setWorldName(String value) @ L154

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

参数:

  • value: String

说明:

TODO

private void saveSettings() @ L162

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

参数:

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public class RealmsSlotOptionsScreen extends RealmsScreen {
    private static final int DEFAULT_DIFFICULTY = 2;
    public static final List<Difficulty> DIFFICULTIES = ImmutableList.of(Difficulty.PEACEFUL, Difficulty.EASY, Difficulty.NORMAL, Difficulty.HARD);
    private static final int DEFAULT_GAME_MODE = 0;
    public static final List<GameType> GAME_MODES = ImmutableList.of(GameType.SURVIVAL, GameType.CREATIVE, GameType.ADVENTURE);
    private static final Component TITLE = Component.translatable("mco.configure.world.buttons.options");
    private static final Component WORLD_NAME_EDIT_LABEL = Component.translatable("mco.configure.world.edit.slot.name");
    private static final Component SPAWN_PROTECTION_TEXT = Component.translatable("mco.configure.world.spawnProtection");
    private static final Component GAME_MODE_BUTTON = Component.translatable("selectWorld.gameMode");
    private static final Component DIFFICULTY_BUTTON = Component.translatable("options.difficulty");
    private static final Component FORCE_GAME_MODE_BUTTON = Component.translatable("mco.configure.world.forceGameMode");
    private static final int SPACING = 8;
    private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this);
    private final RealmsConfigureWorldScreen parentScreen;
    private final RealmsSlot slot;
    private final RealmsServer.WorldType worldType;
    private final String defaultSlotName;
    private int spawnProtection;
    private boolean forceGameMode;
    private Difficulty difficulty;
    private GameType gameMode;
    private String worldName;
    private @Nullable StringWidget warningHeader;
    private RealmsSlotOptionsScreen.@Nullable SettingsSlider spawnProtectionButton;
 
    public RealmsSlotOptionsScreen(RealmsConfigureWorldScreen configureWorldScreen, RealmsSlot slot, RealmsServer.WorldType worldType, int activeSlot) {
        super(TITLE);
        this.parentScreen = configureWorldScreen;
        this.slot = slot;
        this.worldType = worldType;
        this.difficulty = findByIndex(DIFFICULTIES, slot.options.difficulty, 2);
        this.gameMode = findByIndex(GAME_MODES, slot.options.gameMode, 0);
        this.defaultSlotName = slot.options.getDefaultSlotName(activeSlot);
        this.setWorldName(slot.options.getSlotName(activeSlot));
        if (worldType == RealmsServer.WorldType.NORMAL) {
            this.spawnProtection = slot.options.spawnProtection;
            this.forceGameMode = slot.options.forceGameMode;
        } else {
            this.spawnProtection = 0;
            this.forceGameMode = false;
        }
    }
 
    @Override
    public void init() {
        LinearLayout header = this.layout.addToHeader(LinearLayout.vertical().spacing(8));
        header.defaultCellSetting().alignHorizontallyCenter();
        header.addChild(new StringWidget(TITLE, this.minecraft.font));
 
        Component warning = switch (this.worldType) {
            case ADVENTUREMAP -> Component.translatable("mco.configure.world.edit.subscreen.adventuremap").withColor(-65536);
            case INSPIRATION -> Component.translatable("mco.configure.world.edit.subscreen.inspiration").withColor(-65536);
            case EXPERIENCE -> Component.translatable("mco.configure.world.edit.subscreen.experience").withColor(-65536);
            default -> null;
        };
        if (warning != null) {
            this.layout.setHeaderHeight(41 + 9 + 8);
            this.warningHeader = header.addChild(new StringWidget(warning, this.font));
        }
 
        GridLayout contentGrid = this.layout.addToContents(new GridLayout().spacing(8));
        contentGrid.defaultCellSetting().alignHorizontallyCenter();
        GridLayout.RowHelper rowHelper = contentGrid.createRowHelper(2);
        EditBox worldNameEdit = new EditBox(this.minecraft.font, 0, 0, 150, 20, null, WORLD_NAME_EDIT_LABEL);
        worldNameEdit.setValue(this.worldName);
        worldNameEdit.setResponder(this::setWorldName);
        rowHelper.addChild(CommonLayouts.labeledElement(this.font, worldNameEdit, WORLD_NAME_EDIT_LABEL), 2);
        CycleButton<Difficulty> difficultyCycleButton = rowHelper.addChild(
            CycleButton.builder(Difficulty::getDisplayName, this.difficulty)
                .withValues(DIFFICULTIES)
                .create(0, 0, 150, 20, DIFFICULTY_BUTTON, (var1x, value) -> this.difficulty = value)
        );
        CycleButton<GameType> gameTypeCycleButton = rowHelper.addChild(
            CycleButton.builder(GameType::getShortDisplayName, this.gameMode)
                .withValues(GAME_MODES)
                .create(0, 0, 150, 20, GAME_MODE_BUTTON, (var1x, value) -> this.gameMode = value)
        );
        CycleButton<Boolean> forceGameModeButton = rowHelper.addChild(
            CycleButton.onOffBuilder(this.forceGameMode).create(0, 0, 150, 20, FORCE_GAME_MODE_BUTTON, (var1x, value) -> this.forceGameMode = value)
        );
        this.spawnProtectionButton = rowHelper.addChild(new RealmsSlotOptionsScreen.SettingsSlider(0, 0, 150, this.spawnProtection, 0.0F, 16.0F));
        if (this.worldType != RealmsServer.WorldType.NORMAL) {
            this.spawnProtectionButton.active = false;
            forceGameModeButton.active = false;
        }
 
        if (this.slot.isHardcore()) {
            difficultyCycleButton.active = false;
            gameTypeCycleButton.active = false;
            forceGameModeButton.active = false;
        }
 
        LinearLayout footer = this.layout.addToFooter(LinearLayout.horizontal().spacing(8));
        footer.addChild(Button.builder(CommonComponents.GUI_CONTINUE, var1x -> this.saveSettings()).build());
        footer.addChild(Button.builder(CommonComponents.GUI_CANCEL, var1x -> this.onClose()).build());
        this.layout.visitWidgets(x$0 -> this.addRenderableWidget(x$0));
        this.repositionElements();
    }
 
    @Override
    protected void repositionElements() {
        this.layout.arrangeElements();
    }
 
    @Override
    public void onClose() {
        this.minecraft.setScreen(this.parentScreen);
    }
 
    private static <T> T findByIndex(List<T> values, int index, int defaultIndex) {
        return index >= 0 && index < values.size() ? values.get(index) : values.get(defaultIndex);
    }
 
    private static <T> int findIndex(List<T> values, T value, int defaultIndex) {
        int result = values.indexOf(value);
        return result == -1 ? defaultIndex : result;
    }
 
    @Override
    public Component getNarrationMessage() {
        return (Component)(this.warningHeader == null
            ? super.getNarrationMessage()
            : CommonComponents.joinForNarration(this.getTitle(), this.warningHeader.getMessage()));
    }
 
    private void setWorldName(String value) {
        if (value.equals(this.defaultSlotName)) {
            this.worldName = "";
        } else {
            this.worldName = value;
        }
    }
 
    private void saveSettings() {
        int difficultyId = findIndex(DIFFICULTIES, this.difficulty, 2);
        int gameModeId = findIndex(GAME_MODES, this.gameMode, 0);
        if (this.worldType != RealmsServer.WorldType.ADVENTUREMAP
            && this.worldType != RealmsServer.WorldType.EXPERIENCE
            && this.worldType != RealmsServer.WorldType.INSPIRATION) {
            this.parentScreen
                .saveSlotSettings(
                    new RealmsSlot(
                        this.slot.slotId,
                        new RealmsWorldOptions(
                            this.spawnProtection,
                            difficultyId,
                            gameModeId,
                            this.forceGameMode,
                            this.worldName,
                            this.slot.options.version,
                            this.slot.options.compatibility
                        ),
                        this.slot.settings
                    )
                );
        } else {
            this.parentScreen
                .saveSlotSettings(
                    new RealmsSlot(
                        this.slot.slotId,
                        new RealmsWorldOptions(
                            this.slot.options.spawnProtection,
                            difficultyId,
                            gameModeId,
                            this.slot.options.forceGameMode,
                            this.worldName,
                            this.slot.options.version,
                            this.slot.options.compatibility
                        ),
                        this.slot.settings
                    )
                );
        }
    }
 
    @OnlyIn(Dist.CLIENT)
    private class SettingsSlider extends AbstractSliderButton {
        private final double minValue;
        private final double maxValue;
 
        public SettingsSlider(int x, int y, int width, int currentValue, float minValue, float maxValue) {
            Objects.requireNonNull(RealmsSlotOptionsScreen.this);
            super(x, y, width, 20, CommonComponents.EMPTY, 0.0);
            this.minValue = minValue;
            this.maxValue = maxValue;
            this.value = (Mth.clamp((float)currentValue, minValue, maxValue) - minValue) / (maxValue - minValue);
            this.updateMessage();
        }
 
        @Override
        public void applyValue() {
            if (RealmsSlotOptionsScreen.this.spawnProtectionButton.active) {
                RealmsSlotOptionsScreen.this.spawnProtection = (int)Mth.lerp(Mth.clamp(this.value, 0.0, 1.0), this.minValue, this.maxValue);
            }
        }
 
        @Override
        protected void updateMessage() {
            this.setMessage(
                CommonComponents.optionNameValue(
                    RealmsSlotOptionsScreen.SPAWN_PROTECTION_TEXT,
                    (Component)(RealmsSlotOptionsScreen.this.spawnProtection == 0
                        ? CommonComponents.OPTION_OFF
                        : Component.literal(String.valueOf(RealmsSlotOptionsScreen.this.spawnProtection)))
                )
            );
        }
    }
}

引用的其他类

  • RealmsServer

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

    • 引用位置: 参数/字段/构造调用
    • 关联成员: RealmsSlot()
  • RealmsWorldOptions

    • 引用位置: 构造调用
    • 关联成员: RealmsWorldOptions()
  • RealmsConfigureWorldScreen

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

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

    • 引用位置: 方法调用
    • 关联成员: CycleButton.builder(), CycleButton.onOffBuilder()
  • EditBox

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

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

    • 引用位置: 方法调用
    • 关联成员: CommonLayouts.labeledElement()
  • GridLayout

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

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

    • 引用位置: 方法调用
    • 关联成员: LinearLayout.horizontal(), LinearLayout.vertical()
  • CommonComponents

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

    • 引用位置: 字段/方法调用/返回值
    • 关联成员: Component.literal(), Component.translatable()
  • RealmsScreen

    • 引用位置: 继承
  • Mth

    • 引用位置: 方法调用
    • 关联成员: Mth.clamp(), Mth.lerp()
  • Difficulty

    • 引用位置: 字段
  • GameType

    • 引用位置: 字段