2025-11-24 22:52:51 +03:00

194 lines
10 KiB
Java

/*
* Decompiled with CFR 0.152.
*
* Could not load the following classes:
* com.google.common.collect.ImmutableList
*/
package com.mojang.realmsclient.gui.screens.configuration;
import com.google.common.collect.ImmutableList;
import com.mojang.realmsclient.dto.RealmsServer;
import com.mojang.realmsclient.dto.RealmsSlot;
import com.mojang.realmsclient.dto.RealmsWorldOptions;
import com.mojang.realmsclient.gui.screens.RealmsPopups;
import com.mojang.realmsclient.gui.screens.configuration.RealmsConfigureWorldScreen;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.AbstractSliderButton;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.CycleButton;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.realms.RealmsLabel;
import net.minecraft.realms.RealmsScreen;
import net.minecraft.util.Mth;
import net.minecraft.world.Difficulty;
import net.minecraft.world.level.GameType;
public class RealmsSlotOptionsScreen
extends RealmsScreen {
private static final int DEFAULT_DIFFICULTY = 2;
public static final List<Difficulty> DIFFICULTIES = ImmutableList.of((Object)Difficulty.PEACEFUL, (Object)Difficulty.EASY, (Object)Difficulty.NORMAL, (Object)Difficulty.HARD);
private static final int DEFAULT_GAME_MODE = 0;
public static final List<GameType> GAME_MODES = ImmutableList.of((Object)GameType.SURVIVAL, (Object)GameType.CREATIVE, (Object)GameType.ADVENTURE);
private static final Component NAME_LABEL = Component.translatable("mco.configure.world.edit.slot.name");
private static final Component SPAWN_PROTECTION_TEXT = Component.translatable("mco.configure.world.spawnProtection");
private EditBox nameEdit;
protected final RealmsConfigureWorldScreen parentScreen;
private int column1X;
private int columnWidth;
private final RealmsSlot slot;
private final RealmsServer.WorldType worldType;
private Difficulty difficulty;
private GameType gameMode;
private final String defaultSlotName;
private String worldName;
private int spawnProtection;
private boolean forceGameMode;
private SettingsSlider spawnProtectionButton;
public RealmsSlotOptionsScreen(RealmsConfigureWorldScreen configureWorldScreen, RealmsSlot slot, RealmsServer.WorldType worldType, int activeSlot) {
super(Component.translatable("mco.configure.world.buttons.options"));
this.parentScreen = configureWorldScreen;
this.slot = slot;
this.worldType = worldType;
this.difficulty = RealmsSlotOptionsScreen.findByIndex(DIFFICULTIES, slot.options.difficulty, 2);
this.gameMode = RealmsSlotOptionsScreen.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 onClose() {
this.minecraft.setScreen(this.parentScreen);
}
private static <T> T findByIndex(List<T> values, int index, int defaultIndex) {
try {
return values.get(index);
}
catch (IndexOutOfBoundsException e) {
return 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 void init() {
this.columnWidth = 170;
this.column1X = this.width / 2 - this.columnWidth;
int column2X = this.width / 2 + 10;
if (this.worldType != RealmsServer.WorldType.NORMAL) {
MutableComponent warning = this.worldType == RealmsServer.WorldType.ADVENTUREMAP ? Component.translatable("mco.configure.world.edit.subscreen.adventuremap") : (this.worldType == RealmsServer.WorldType.INSPIRATION ? Component.translatable("mco.configure.world.edit.subscreen.inspiration") : Component.translatable("mco.configure.world.edit.subscreen.experience"));
this.addLabel(new RealmsLabel(warning, this.width / 2, 26, -65536));
}
this.nameEdit = this.addWidget(new EditBox(this.minecraft.font, this.column1X, RealmsSlotOptionsScreen.row(1), this.columnWidth, 20, null, Component.translatable("mco.configure.world.edit.slot.name")));
this.nameEdit.setValue(this.worldName);
this.nameEdit.setResponder(this::setWorldName);
CycleButton<Difficulty> difficultyCycleButton = this.addRenderableWidget(CycleButton.builder(Difficulty::getDisplayName, this.difficulty).withValues((Collection<Difficulty>)DIFFICULTIES).create(column2X, RealmsSlotOptionsScreen.row(1), this.columnWidth, 20, Component.translatable("options.difficulty"), (button, value) -> {
this.difficulty = value;
}));
CycleButton<GameType> gameTypeCycleButton = this.addRenderableWidget(CycleButton.builder(GameType::getShortDisplayName, this.gameMode).withValues((Collection<GameType>)GAME_MODES).create(this.column1X, RealmsSlotOptionsScreen.row(3), this.columnWidth, 20, Component.translatable("selectWorld.gameMode"), (button, value) -> {
this.gameMode = value;
}));
CycleButton<Boolean> forceGameModeButton = this.addRenderableWidget(CycleButton.onOffBuilder(this.forceGameMode).create(column2X, RealmsSlotOptionsScreen.row(3), this.columnWidth, 20, Component.translatable("mco.configure.world.forceGameMode"), (button, value) -> {
this.forceGameMode = value;
}));
this.spawnProtectionButton = this.addRenderableWidget(new SettingsSlider(this.column1X, RealmsSlotOptionsScreen.row(5), this.columnWidth, 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;
}
this.addRenderableWidget(Button.builder(Component.translatable("mco.configure.world.buttons.done"), button -> this.saveSettings()).bounds(this.column1X, RealmsSlotOptionsScreen.row(13), this.columnWidth, 20).build());
this.addRenderableWidget(Button.builder(CommonComponents.GUI_CANCEL, button -> this.onClose()).bounds(column2X, RealmsSlotOptionsScreen.row(13), this.columnWidth, 20).build());
}
private CycleButton.OnValueChange<Boolean> confirmDangerousOption(Component message, Consumer<Boolean> setter) {
return (button, value) -> {
if (value.booleanValue()) {
setter.accept(true);
} else {
this.minecraft.setScreen(RealmsPopups.warningPopupScreen(this, message, popupScreen -> {
setter.accept(false);
popupScreen.onClose();
}));
}
};
}
@Override
public Component getNarrationMessage() {
return CommonComponents.joinForNarration(this.getTitle(), this.createLabelNarration());
}
@Override
public void render(GuiGraphics graphics, int xm, int ym, float a) {
super.render(graphics, xm, ym, a);
graphics.drawCenteredString(this.font, this.title, this.width / 2, 17, -1);
graphics.drawString(this.font, NAME_LABEL, this.column1X + this.columnWidth / 2 - this.font.width(NAME_LABEL) / 2, RealmsSlotOptionsScreen.row(0) - 5, -1);
this.nameEdit.render(graphics, xm, ym, a);
}
private void setWorldName(String value) {
this.worldName = value.equals(this.defaultSlotName) ? "" : value;
}
private void saveSettings() {
int difficultyId = RealmsSlotOptionsScreen.findIndex(DIFFICULTIES, this.difficulty, 2);
int gameModeId = RealmsSlotOptionsScreen.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.slot.options.spawnProtection, difficultyId, gameModeId, this.slot.options.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.spawnProtection, difficultyId, gameModeId, this.forceGameMode, this.worldName, this.slot.options.version, this.slot.options.compatibility), this.slot.settings));
}
}
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) {
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) {
return;
}
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(SPAWN_PROTECTION_TEXT, RealmsSlotOptionsScreen.this.spawnProtection == 0 ? CommonComponents.OPTION_OFF : Component.literal(String.valueOf(RealmsSlotOptionsScreen.this.spawnProtection))));
}
}
}