| 1 | package net.minecraft.client.gui.screens.worldselection;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.io.IOException;
|
| 5 | import java.util.function.Consumer;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.SharedConstants;
|
| 9 | import net.minecraft.client.gui.components.Button;
|
| 10 | import net.minecraft.client.gui.components.EditBox;
|
| 11 | import net.minecraft.client.gui.components.StringWidget;
|
| 12 | import net.minecraft.client.gui.components.Tooltip;
|
| 13 | import net.minecraft.client.gui.layouts.GridLayout;
|
| 14 | import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
|
| 15 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 16 | import net.minecraft.client.gui.screens.Screen;
|
| 17 | import net.minecraft.network.chat.CommonComponents;
|
| 18 | import net.minecraft.network.chat.Component;
|
| 19 | import net.minecraft.util.FileUtil;
|
| 20 | import net.minecraft.world.level.GameType;
|
| 21 | import net.minecraft.world.level.LevelSettings;
|
| 22 | import net.minecraft.world.level.WorldDataConfiguration;
|
| 23 | import net.minecraft.world.level.LevelSettings.DifficultySettings;
|
| 24 | import net.minecraft.world.level.levelgen.WorldOptions;
|
| 25 | import net.minecraft.world.level.levelgen.presets.WorldPresets;
|
| 26 | import net.minecraft.world.level.storage.LevelSummary;
|
| 27 | import org.jspecify.annotations.Nullable;
|
| 28 | import org.slf4j.Logger;
|
| 29 |
|
| 30 | @Environment(EnvType.CLIENT)
|
| 31 | public class SelectWorldScreen extends Screen {
|
| 32 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 33 | public static final WorldOptions TEST_OPTIONS = new WorldOptions("test1".hashCode(), true, false);
|
| 34 | protected final Screen lastScreen;
|
| 35 | private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this, 8 + 9 + 8 + 20 + 4, 60);
|
| 36 | private @Nullable Button deleteButton;
|
| 37 | private @Nullable Button playWorldButton;
|
| 38 | private @Nullable Button editButton;
|
| 39 | private @Nullable Button recreateButton;
|
| 40 | protected @Nullable EditBox searchBox;
|
| 41 | private @Nullable WorldSelectionList list;
|
| 42 |
|
| 43 | public SelectWorldScreen(final Screen lastScreen) {
|
| 44 | super(Component.translatable("selectWorld.title"));
|
| 45 | this.lastScreen = lastScreen;
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | protected void init() {
|
| 50 | LinearLayout header = this.layout.addToHeader(LinearLayout.vertical().spacing(4));
|
| 51 | header.defaultCellSetting().alignHorizontallyCenter();
|
| 52 | header.addChild(new StringWidget(this.title, this.font));
|
| 53 | LinearLayout subHeader = header.addChild(LinearLayout.horizontal().spacing(4));
|
| 54 | if (SharedConstants.DEBUG_WORLD_RECREATE) {
|
| 55 | subHeader.addChild(this.createDebugWorldRecreateButton());
|
| 56 | }
|
| 57 |
|
| 58 | this.searchBox = subHeader.addChild(new EditBox(this.font, this.width / 2 - 100, 22, 200, 20, this.searchBox, Component.translatable("selectWorld.search")));
|
| 59 | this.searchBox.setResponder(value -> {
|
| 60 | if (this.list != null) {
|
| 61 | this.list.updateFilter(value);
|
| 62 | }
|
| 63 | });
|
| 64 | this.searchBox.setHint(Component.translatable("gui.selectWorld.search").setStyle(EditBox.SEARCH_HINT_STYLE));
|
| 65 | Consumer<WorldSelectionList.WorldListEntry> joinWorld = WorldSelectionList.WorldListEntry::joinWorld;
|
| 66 | this.list = this.layout
|
| 67 | .addToContents(
|
| 68 | new WorldSelectionList.Builder(this.minecraft, this)
|
| 69 | .width(this.width)
|
| 70 | .height(this.layout.getContentHeight())
|
| 71 | .filter(this.searchBox.getValue())
|
| 72 | .oldList(this.list)
|
| 73 | .onEntrySelect(this::updateButtonStatus)
|
| 74 | .onEntryInteract(joinWorld)
|
| 75 | .build()
|
| 76 | );
|
| 77 | this.createFooterButtons(joinWorld, this.list);
|
| 78 | this.layout.visitWidgets(x$0 -> this.addRenderableWidget(x$0));
|
| 79 | this.repositionElements();
|
| 80 | this.updateButtonStatus(null);
|
| 81 | }
|
| 82 |
|
| 83 | private void createFooterButtons(final Consumer<WorldSelectionList.WorldListEntry> joinWorld, final WorldSelectionList list) {
|
| 84 | GridLayout footer = this.layout.addToFooter(new GridLayout().columnSpacing(8).rowSpacing(4));
|
| 85 | footer.defaultCellSetting().alignHorizontallyCenter();
|
| 86 | GridLayout.RowHelper rowHelper = footer.createRowHelper(4);
|
| 87 | this.playWorldButton = rowHelper.addChild(Button.builder(LevelSummary.PLAY_WORLD, button -> list.getSelectedOpt().ifPresent(joinWorld)).build(), 2);
|
| 88 | rowHelper.addChild(
|
| 89 | Button.builder(Component.translatable("selectWorld.create"), button -> CreateWorldScreen.openFresh(this.minecraft, list::returnToScreen)).build(), 2
|
| 90 | );
|
| 91 | this.editButton = rowHelper.addChild(
|
| 92 | Button.builder(Component.translatable("selectWorld.edit"), button -> list.getSelectedOpt().ifPresent(WorldSelectionList.WorldListEntry::editWorld))
|
| 93 | .width(71)
|
| 94 | .build()
|
| 95 | );
|
| 96 | this.deleteButton = rowHelper.addChild(
|
| 97 | Button.builder(Component.translatable("selectWorld.delete"), button -> list.getSelectedOpt().ifPresent(WorldSelectionList.WorldListEntry::deleteWorld))
|
| 98 | .width(71)
|
| 99 | .build()
|
| 100 | );
|
| 101 | this.recreateButton = rowHelper.addChild(
|
| 102 | Button.builder(Component.translatable("selectWorld.recreate"), button -> list.getSelectedOpt().ifPresent(WorldSelectionList.WorldListEntry::recreateWorld))
|
| 103 | .width(71)
|
| 104 | .build()
|
| 105 | );
|
| 106 | rowHelper.addChild(Button.builder(CommonComponents.GUI_BACK, button -> this.minecraft.gui.setScreen(this.lastScreen)).width(71).build());
|
| 107 | }
|
| 108 |
|
| 109 | private Button createDebugWorldRecreateButton() {
|
| 110 | return Button.builder(Component.literal("DEBUG recreate"), button -> {
|
| 111 | try {
|
| 112 | String levelName = "DEBUG world";
|
| 113 | if (this.list != null && !this.list.children().isEmpty()) {
|
| 114 | WorldSelectionList.Entry entry = this.list.children().getFirst();
|
| 115 | if (entry instanceof WorldSelectionList.WorldListEntry worldEntry && worldEntry.getLevelName().equals("DEBUG world")) {
|
| 116 | worldEntry.doDeleteWorld();
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 | LevelSettings levelSettings = new LevelSettings("DEBUG world", GameType.SPECTATOR, DifficultySettings.DEFAULT, true, WorldDataConfiguration.DEFAULT);
|
| 121 | String resultFolder = FileUtil.findAvailableName(this.minecraft.getLevelSource().getBaseDir(), "DEBUG world", "");
|
| 122 | this.minecraft.createWorldOpenFlows().createFreshLevel(resultFolder, levelSettings, TEST_OPTIONS, WorldPresets::createNormalWorldDimensions, this);
|
| 123 | } catch (IOException e) {
|
| 124 | LOGGER.error("Failed to recreate the debug world", e);
|
| 125 | }
|
| 126 | }).width(72).build();
|
| 127 | }
|
| 128 |
|
| 129 | @Override
|
| 130 | protected void repositionElements() {
|
| 131 | if (this.list != null) {
|
| 132 | this.list.updateSize(this.width, this.layout);
|
| 133 | }
|
| 134 |
|
| 135 | this.layout.arrangeElements();
|
| 136 | }
|
| 137 |
|
| 138 | @Override
|
| 139 | protected void setInitialFocus() {
|
| 140 | if (this.searchBox != null) {
|
| 141 | this.setInitialFocus(this.searchBox);
|
| 142 | }
|
| 143 | }
|
| 144 |
|
| 145 | @Override
|
| 146 | public void onClose() {
|
| 147 | this.minecraft.gui.setScreen(this.lastScreen);
|
| 148 | }
|
| 149 |
|
| 150 | public void updateButtonStatus(final @Nullable LevelSummary summary) {
|
| 151 | if (this.playWorldButton != null && this.editButton != null && this.recreateButton != null && this.deleteButton != null) {
|
| 152 | if (summary == null) {
|
| 153 | this.playWorldButton.setMessage(LevelSummary.PLAY_WORLD);
|
| 154 | this.playWorldButton.active = false;
|
| 155 | this.editButton.active = false;
|
| 156 | this.recreateButton.active = false;
|
| 157 | this.deleteButton.active = false;
|
| 158 | } else {
|
| 159 | this.playWorldButton.setMessage(summary.primaryActionMessage());
|
| 160 | this.playWorldButton.active = summary.primaryActionActive();
|
| 161 | this.editButton.active = summary.canEdit();
|
| 162 | this.recreateButton.active = summary.canRecreate();
|
| 163 | this.deleteButton.active = summary.canDelete();
|
| 164 | if (summary.requiresFileFixing()) {
|
| 165 | this.editButton.setTooltip(Tooltip.create(Component.translatable("selectWorld.requiresFileFixingTooltip.edit")));
|
| 166 | this.playWorldButton.setTooltip(Tooltip.create(Component.translatable("selectWorld.requiresFileFixingTooltip.play")));
|
| 167 | this.recreateButton.setTooltip(Tooltip.create(Component.translatable("selectWorld.requiresFileFixingTooltip.recreate")));
|
| 168 | } else {
|
| 169 | this.editButton.setTooltip(null);
|
| 170 | this.playWorldButton.setTooltip(null);
|
| 171 | this.recreateButton.setTooltip(null);
|
| 172 | }
|
| 173 | }
|
| 174 | }
|
| 175 | }
|
| 176 |
|
| 177 | @Override
|
| 178 | public void removed() {
|
| 179 | if (this.list != null) {
|
| 180 | this.list.children().forEach(WorldSelectionList.Entry::close);
|
| 181 | }
|
| 182 | }
|
| 183 | }
|
| 184 |
|