| 1 | package com.mojang.realmsclient.gui.screens.configuration;
|
| 2 |
|
| 3 | import com.mojang.realmsclient.dto.RealmsRegion;
|
| 4 | import com.mojang.realmsclient.dto.RegionSelectionPreference;
|
| 5 | import com.mojang.realmsclient.dto.ServiceQuality;
|
| 6 | import java.util.Map;
|
| 7 | import java.util.Objects;
|
| 8 | import java.util.function.BiConsumer;
|
| 9 | import net.fabricmc.api.EnvType;
|
| 10 | import net.fabricmc.api.Environment;
|
| 11 | import net.minecraft.client.gui.components.Button;
|
| 12 | import net.minecraft.client.gui.components.ObjectSelectionList;
|
| 13 | import net.minecraft.client.gui.components.StringWidget;
|
| 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 org.jspecify.annotations.Nullable;
|
| 20 |
|
| 21 | @Environment(EnvType.CLIENT)
|
| 22 | public class RealmsPreferredRegionSelectionScreen extends Screen {
|
| 23 | private static final Component REGION_SELECTION_LABEL = Component.translatable("mco.configure.world.region_preference.title");
|
| 24 | private static final int SPACING = 8;
|
| 25 | private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this);
|
| 26 | private final Screen parent;
|
| 27 | private final BiConsumer<RegionSelectionPreference, @Nullable RealmsRegion> applySettings;
|
| 28 | private final Map<RealmsRegion, ServiceQuality> regionServiceQuality;
|
| 29 | private RealmsPreferredRegionSelectionScreen.@Nullable RegionSelectionList list;
|
| 30 | private RealmsSettingsTab.RegionSelection selection;
|
| 31 | private @Nullable Button doneButton;
|
| 32 |
|
| 33 | public RealmsPreferredRegionSelectionScreen(
|
| 34 | final Screen parent,
|
| 35 | final BiConsumer<RegionSelectionPreference, @Nullable RealmsRegion> applySettings,
|
| 36 | final Map<RealmsRegion, ServiceQuality> regionServiceQuality,
|
| 37 | final RealmsSettingsTab.RegionSelection currentSelection
|
| 38 | ) {
|
| 39 | super(REGION_SELECTION_LABEL);
|
| 40 | this.parent = parent;
|
| 41 | this.applySettings = applySettings;
|
| 42 | this.regionServiceQuality = regionServiceQuality;
|
| 43 | this.selection = currentSelection;
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | public void onClose() {
|
| 48 | this.minecraft.gui.setScreen(this.parent);
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | protected void init() {
|
| 53 | LinearLayout header = this.layout.addToHeader(LinearLayout.vertical().spacing(8));
|
| 54 | header.defaultCellSetting().alignHorizontallyCenter();
|
| 55 | header.addChild(new StringWidget(this.getTitle(), this.font));
|
| 56 | this.list = this.layout.addToContents(new RealmsPreferredRegionSelectionScreen.RegionSelectionList());
|
| 57 | LinearLayout footer = this.layout.addToFooter(LinearLayout.horizontal().spacing(8));
|
| 58 | this.doneButton = footer.addChild(Button.builder(CommonComponents.GUI_DONE, button -> this.onDone()).build());
|
| 59 | footer.addChild(Button.builder(CommonComponents.GUI_CANCEL, button -> this.onClose()).build());
|
| 60 | this.list.setSelected(this.list.children().stream().filter(e -> Objects.equals(e.regionSelection, this.selection)).findFirst().orElse(null));
|
| 61 | this.layout.visitWidgets(x$0 -> this.addRenderableWidget(x$0));
|
| 62 | this.repositionElements();
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | protected void repositionElements() {
|
| 67 | this.layout.arrangeElements();
|
| 68 | if (this.list != null) {
|
| 69 | this.list.updateSize(this.width, this.layout);
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | private void onDone() {
|
| 74 | this.applySettings.accept(this.selection.preference(), this.selection.region());
|
| 75 | this.onClose();
|
| 76 | }
|
| 77 |
|
| 78 | private void updateButtonValidity() {
|
| 79 | if (this.doneButton != null && this.list != null) {
|
| 80 | this.doneButton.active = this.list.getSelected() != null;
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | @Environment(EnvType.CLIENT)
|
| 85 | private class RegionSelectionList
|
| 86 | extends ObjectSelectionList<com.mojang.realmsclient.gui.screens.configuration.RealmsPreferredRegionSelectionScreen.RegionSelectionList.Entry> {
|
| 87 | private RegionSelectionList() {
|
| 88 | super(
|
| 89 | RealmsPreferredRegionSelectionScreen.this.minecraft,
|
| 90 | RealmsPreferredRegionSelectionScreen.this.width,
|
| 91 | RealmsPreferredRegionSelectionScreen.this.height - 77,
|
| 92 | 40,
|
| 93 | 16
|
| 94 | );
|
| 95 | this.addEntry(
|
| 96 | new com.mojang.realmsclient.gui.screens.configuration.RealmsPreferredRegionSelectionScreen.RegionSelectionList.Entry(
|
| 97 | this, RegionSelectionPreference.AUTOMATIC_PLAYER, null
|
| 98 | )
|
| 99 | );
|
| 100 | this.addEntry(
|
| 101 | new com.mojang.realmsclient.gui.screens.configuration.RealmsPreferredRegionSelectionScreen.RegionSelectionList.Entry(
|
| 102 | this, RegionSelectionPreference.AUTOMATIC_OWNER, null
|
| 103 | )
|
| 104 | );
|
| 105 | RealmsPreferredRegionSelectionScreen.this.regionServiceQuality
|
| 106 | .keySet()
|
| 107 | .stream()
|
| 108 | .map(
|
| 109 | region -> new com.mojang.realmsclient.gui.screens.configuration.RealmsPreferredRegionSelectionScreen.RegionSelectionList.Entry(
|
| 110 | this, RegionSelectionPreference.MANUAL, region
|
| 111 | )
|
| 112 | )
|
| 113 | .forEach(x$0 -> this.addEntry(x$0));
|
| 114 | }
|
| 115 |
|
| 116 | public void setSelected(final Entry/* @Nullable */ selected) {
|
| 117 | super.setSelected(selected);
|
| 118 | if (selected != null) {
|
| 119 | RealmsPreferredRegionSelectionScreen.this.selection = selected.regionSelection;
|
| 120 | }
|
| 121 |
|
| 122 | RealmsPreferredRegionSelectionScreen.this.updateButtonValidity();
|
| 123 | }
|
| 124 | }
|
| 125 | }
|
| 126 |
|