| 1 | package com.mojang.realmsclient.gui.screens.configuration;
|
| 2 |
|
| 3 | import com.mojang.realmsclient.dto.RealmTierConfigurationDto;
|
| 4 | import com.mojang.realmsclient.dto.RealmsRegion;
|
| 5 | import com.mojang.realmsclient.dto.RealmsServer;
|
| 6 | import com.mojang.realmsclient.dto.RegionSelectionPreference;
|
| 7 | import com.mojang.realmsclient.dto.RegionSelectionPreferenceDto;
|
| 8 | import com.mojang.realmsclient.dto.ServiceQuality;
|
| 9 | import com.mojang.realmsclient.gui.screens.RealmsPopups;
|
| 10 | import java.util.Map;
|
| 11 | import java.util.Objects;
|
| 12 | import java.util.Optional;
|
| 13 | import net.fabricmc.api.EnvType;
|
| 14 | import net.fabricmc.api.Environment;
|
| 15 | import net.minecraft.ChatFormatting;
|
| 16 | import net.minecraft.client.Minecraft;
|
| 17 | import net.minecraft.client.OptionInstance;
|
| 18 | import net.minecraft.client.gui.Font;
|
| 19 | import net.minecraft.client.gui.components.Button;
|
| 20 | import net.minecraft.client.gui.components.EditBox;
|
| 21 | import net.minecraft.client.gui.components.ImageWidget;
|
| 22 | import net.minecraft.client.gui.components.ScrollableLayout;
|
| 23 | import net.minecraft.client.gui.components.StringWidget;
|
| 24 | import net.minecraft.client.gui.components.Tooltip;
|
| 25 | import net.minecraft.client.gui.components.tabs.GridLayoutTab;
|
| 26 | import net.minecraft.client.gui.layouts.CommonLayouts;
|
| 27 | import net.minecraft.client.gui.layouts.EqualSpacingLayout;
|
| 28 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 29 | import net.minecraft.client.gui.navigation.ScreenRectangle;
|
| 30 | import net.minecraft.network.chat.Component;
|
| 31 | import net.minecraft.network.chat.MutableComponent;
|
| 32 | import net.minecraft.resources.Identifier;
|
| 33 | import org.jspecify.annotations.Nullable;
|
| 34 |
|
| 35 | @Environment(EnvType.CLIENT)
|
| 36 | public class RealmsSettingsTab extends GridLayoutTab implements RealmsConfigurationTab {
|
| 37 | private static final int COMPONENT_WIDTH = 212;
|
| 38 | public static final Component TITLE = Component.translatable("mco.configure.world.settings.title");
|
| 39 | private static final Component NAME_LABEL = Component.translatable("mco.configure.world.name");
|
| 40 | private static final Component DESCRIPTION_LABEL = Component.translatable("mco.configure.world.description");
|
| 41 | private static final Component RENDER_DISTANCE_LABEL = Component.translatable("mco.configure.world.renderDistance");
|
| 42 | private static final Component RENDER_DISTANCE_TOOLTIP = Component.translatable("mco.configure.world.renderDistance.tooltip");
|
| 43 | private static final Component SIMULATION_DISTANCE_LABEL = Component.translatable("mco.configure.world.simulationDistance");
|
| 44 | private static final Component SIMULATION_DISTANCE_TOOLTIP = Component.translatable("mco.configure.world.simulationDistance.tooltip");
|
| 45 | private static final Component REGION_PREFERENCE_LABEL = Component.translatable("mco.configure.world.region_preference");
|
| 46 | private static final Tooltip REALM_NAME_VALIDATION_ERROR_TOOLTIP = Tooltip.create(Component.translatable("mco.configure.world.name.validation.whitespace"));
|
| 47 | private final RealmsConfigureWorldScreen configurationScreen;
|
| 48 | private final Minecraft minecraft;
|
| 49 | private RealmsServer serverData;
|
| 50 | private final Map<RealmsRegion, ServiceQuality> regionServiceQuality;
|
| 51 | private final ScrollableLayout scrollableLayout;
|
| 52 | private final Button closeOpenButton;
|
| 53 | private final EditBox descEdit;
|
| 54 | private final EditBox nameEdit;
|
| 55 | private final @Nullable OptionInstance<Integer> renderDistanceOption;
|
| 56 | private final @Nullable OptionInstance<Integer> simulationDistanceOption;
|
| 57 | private int initialRenderDistance;
|
| 58 | private int initialSimulationDistance;
|
| 59 | private final StringWidget selectedRegionStringWidget;
|
| 60 | private final ImageWidget selectedRegionImageWidget;
|
| 61 | private RealmsSettingsTab.RegionSelection preferredRegionSelection;
|
| 62 |
|
| 63 | public RealmsSettingsTab(
|
| 64 | final RealmsConfigureWorldScreen configurationScreen,
|
| 65 | final Minecraft minecraft,
|
| 66 | final RealmsServer serverData,
|
| 67 | final @Nullable RealmTierConfigurationDto tierConfiguration,
|
| 68 | final Map<RealmsRegion, ServiceQuality> regionServiceQuality
|
| 69 | ) {
|
| 70 | super(TITLE);
|
| 71 | this.configurationScreen = configurationScreen;
|
| 72 | this.minecraft = minecraft;
|
| 73 | this.serverData = serverData;
|
| 74 | this.regionServiceQuality = regionServiceQuality;
|
| 75 | if (tierConfiguration != null) {
|
| 76 | RealmTierConfigurationDto.RealmTierRangeDto renderDistanceRange = tierConfiguration.renderDistance();
|
| 77 | this.initialRenderDistance = currentOrDefault(renderDistanceRange);
|
| 78 | this.renderDistanceOption = createDistanceOption(
|
| 79 | "mco.configure.world.renderDistance", RENDER_DISTANCE_TOOLTIP, renderDistanceRange, this.initialRenderDistance
|
| 80 | );
|
| 81 | RealmTierConfigurationDto.RealmTierRangeDto simulationDistanceRange = tierConfiguration.simDistance();
|
| 82 | this.initialSimulationDistance = currentOrDefault(simulationDistanceRange);
|
| 83 | this.simulationDistanceOption = createDistanceOption(
|
| 84 | "mco.configure.world.simulationDistance", SIMULATION_DISTANCE_TOOLTIP, simulationDistanceRange, this.initialSimulationDistance
|
| 85 | );
|
| 86 | } else {
|
| 87 | this.renderDistanceOption = null;
|
| 88 | this.simulationDistanceOption = null;
|
| 89 | }
|
| 90 |
|
| 91 | LinearLayout content = LinearLayout.vertical();
|
| 92 | content.defaultCellSetting().padding(8);
|
| 93 | this.scrollableLayout = this.layout.addChild(new ScrollableLayout(minecraft, content, configurationScreen.getContentHeight()), 0, 0);
|
| 94 | Font font = configurationScreen.getFont();
|
| 95 | this.nameEdit = new EditBox(minecraft.font, 0, 0, 212, 20, Component.translatable("mco.configure.world.name"));
|
| 96 | this.nameEdit.setMaxLength(32);
|
| 97 | this.nameEdit.setResponder(value -> {
|
| 98 | if (!this.isRealmNameValid()) {
|
| 99 | this.nameEdit.setTextColor(-2142128);
|
| 100 | this.nameEdit.setTooltip(REALM_NAME_VALIDATION_ERROR_TOOLTIP);
|
| 101 | } else {
|
| 102 | this.nameEdit.setTooltip(null);
|
| 103 | this.nameEdit.setTextColor(-2039584);
|
| 104 | }
|
| 105 | });
|
| 106 | content.addChild(CommonLayouts.labeledElement(font, this.nameEdit, NAME_LABEL));
|
| 107 | this.descEdit = new EditBox(minecraft.font, 0, 0, 212, 20, Component.translatable("mco.configure.world.description"));
|
| 108 | this.descEdit.setMaxLength(32);
|
| 109 | content.addChild(CommonLayouts.labeledElement(font, this.descEdit, DESCRIPTION_LABEL));
|
| 110 | if (this.renderDistanceOption != null && this.simulationDistanceOption != null) {
|
| 111 | content.addChild(CommonLayouts.labeledElement(font, this.renderDistanceOption.createButton(minecraft.options, 0, 0, 212), RENDER_DISTANCE_LABEL));
|
| 112 | content.addChild(CommonLayouts.labeledElement(font, this.simulationDistanceOption.createButton(minecraft.options, 0, 0, 212), SIMULATION_DISTANCE_LABEL));
|
| 113 | }
|
| 114 |
|
| 115 | LinearLayout selectedRegionAndButton = LinearLayout.vertical();
|
| 116 | EqualSpacingLayout selectedRegion = selectedRegionAndButton.addChild(new EqualSpacingLayout(0, 0, 212, 9, EqualSpacingLayout.Orientation.HORIZONTAL));
|
| 117 | this.selectedRegionStringWidget = selectedRegion.addChild(new StringWidget(192, 9, Component.empty(), font));
|
| 118 | this.selectedRegionImageWidget = selectedRegion.addChild(ImageWidget.sprite(10, 8, ServiceQuality.UNKNOWN.getIcon()));
|
| 119 | selectedRegionAndButton.addChild(
|
| 120 | Button.builder(Component.translatable("mco.configure.world.buttons.region_preference"), button -> this.openPreferenceSelector())
|
| 121 | .bounds(0, 0, 212, 20)
|
| 122 | .build()
|
| 123 | );
|
| 124 | content.addChild(CommonLayouts.labeledElement(font, selectedRegionAndButton, REGION_PREFERENCE_LABEL));
|
| 125 | this.closeOpenButton = content.addChild(
|
| 126 | Button.builder(
|
| 127 | Component.empty(),
|
| 128 | button -> {
|
| 129 | if (serverData.state == RealmsServer.State.OPEN) {
|
| 130 | minecraft.gui
|
| 131 | .setScreen(
|
| 132 | RealmsPopups.customPopupScreen(
|
| 133 | configurationScreen,
|
| 134 | Component.translatable("mco.configure.world.close.question.title"),
|
| 135 | Component.translatable("mco.configure.world.close.question.line1"),
|
| 136 | popup -> {
|
| 137 | if (this.save()) {
|
| 138 | configurationScreen.closeTheWorld();
|
| 139 | }
|
| 140 | }
|
| 141 | )
|
| 142 | );
|
| 143 | } else if (this.save()) {
|
| 144 | configurationScreen.openTheWorld(false);
|
| 145 | }
|
| 146 | }
|
| 147 | )
|
| 148 | .bounds(0, 0, 212, 20)
|
| 149 | .build(),
|
| 150 | content.newCellSettings().paddingBottom(8)
|
| 151 | );
|
| 152 | this.closeOpenButton.active = false;
|
| 153 | this.updateData(serverData);
|
| 154 | }
|
| 155 |
|
| 156 | private static int currentOrDefault(final RealmTierConfigurationDto.RealmTierRangeDto range) {
|
| 157 | return Objects.requireNonNullElse(range.current(), range.defaultValue());
|
| 158 | }
|
| 159 |
|
| 160 | private static OptionInstance<Integer> createDistanceOption(
|
| 161 | final String captionId, final Component tooltip, final RealmTierConfigurationDto.RealmTierRangeDto range, final int initialValue
|
| 162 | ) {
|
| 163 | return new OptionInstance<>(
|
| 164 | captionId,
|
| 165 | OptionInstance.cachedConstantTooltip(tooltip),
|
| 166 | (var0, value) -> Component.translatable("options.chunks", value),
|
| 167 | new OptionInstance.IntRange(range.min(), range.max()),
|
| 168 | initialValue,
|
| 169 | var0 -> {}
|
| 170 | );
|
| 171 | }
|
| 172 |
|
| 173 | private static MutableComponent getTranslatableFromPreference(final RealmsSettingsTab.RegionSelection regionSelection) {
|
| 174 | return (regionSelection.preference().equals(RegionSelectionPreference.MANUAL) && regionSelection.region() != null
|
| 175 | ? Component.translatable(regionSelection.region().translationKey)
|
| 176 | : Component.translatable(regionSelection.preference().translationKey))
|
| 177 | .withStyle(ChatFormatting.GRAY);
|
| 178 | }
|
| 179 |
|
| 180 | private static Identifier getServiceQualityIcon(
|
| 181 | final RealmsSettingsTab.RegionSelection regionSelection, final Map<RealmsRegion, ServiceQuality> regionServiceQuality
|
| 182 | ) {
|
| 183 | if (regionSelection.region() != null && regionServiceQuality.containsKey(regionSelection.region())) {
|
| 184 | ServiceQuality serviceQuality = regionServiceQuality.getOrDefault(regionSelection.region(), ServiceQuality.UNKNOWN);
|
| 185 | return serviceQuality.getIcon();
|
| 186 | } else {
|
| 187 | return ServiceQuality.UNKNOWN.getIcon();
|
| 188 | }
|
| 189 | }
|
| 190 |
|
| 191 | private boolean isRealmNameValid() {
|
| 192 | String name = this.nameEdit.getValue();
|
| 193 | String trimmedName = name.trim();
|
| 194 | return !trimmedName.isEmpty() && name.length() == trimmedName.length();
|
| 195 | }
|
| 196 |
|
| 197 | private void openPreferenceSelector() {
|
| 198 | this.minecraft
|
| 199 | .gui
|
| 200 | .setScreen(
|
| 201 | new RealmsPreferredRegionSelectionScreen(
|
| 202 | this.configurationScreen, this::applyRegionPreferenceSelection, this.regionServiceQuality, this.preferredRegionSelection
|
| 203 | )
|
| 204 | );
|
| 205 | }
|
| 206 |
|
| 207 | private void applyRegionPreferenceSelection(final RegionSelectionPreference preference, final @Nullable RealmsRegion region) {
|
| 208 | this.preferredRegionSelection = new RealmsSettingsTab.RegionSelection(preference, region);
|
| 209 | this.updateRegionPreferenceValues();
|
| 210 | }
|
| 211 |
|
| 212 | private void updateRegionPreferenceValues() {
|
| 213 | this.selectedRegionStringWidget.setMessage(getTranslatableFromPreference(this.preferredRegionSelection));
|
| 214 | this.selectedRegionImageWidget.updateResource(getServiceQualityIcon(this.preferredRegionSelection, this.regionServiceQuality));
|
| 215 | this.selectedRegionImageWidget.visible = this.preferredRegionSelection.preference == RegionSelectionPreference.MANUAL;
|
| 216 | }
|
| 217 |
|
| 218 | @Override
|
| 219 | public void doLayout(final ScreenRectangle screenRectangle) {
|
| 220 | this.scrollableLayout.arrangeElements();
|
| 221 | this.scrollableLayout.setMaxHeight(screenRectangle.height() - 2);
|
| 222 | super.doLayout(screenRectangle);
|
| 223 | }
|
| 224 |
|
| 225 | @Override
|
| 226 | public void onSelected(final RealmsServer serverData) {
|
| 227 | this.updateData(serverData);
|
| 228 | }
|
| 229 |
|
| 230 | @Override
|
| 231 | public void updateData(final RealmsServer serverData) {
|
| 232 | this.serverData = serverData;
|
| 233 | if (serverData.regionSelectionPreference == null) {
|
| 234 | serverData.regionSelectionPreference = RegionSelectionPreferenceDto.DEFAULT;
|
| 235 | }
|
| 236 |
|
| 237 | if (serverData.regionSelectionPreference.regionSelectionPreference == RegionSelectionPreference.MANUAL
|
| 238 | && serverData.regionSelectionPreference.preferredRegion == null) {
|
| 239 | Optional<RealmsRegion> first = this.regionServiceQuality.keySet().stream().findFirst();
|
| 240 | first.ifPresent(region -> serverData.regionSelectionPreference.preferredRegion = region);
|
| 241 | }
|
| 242 |
|
| 243 | String key = serverData.state == RealmsServer.State.OPEN ? "mco.configure.world.buttons.close" : "mco.configure.world.buttons.open";
|
| 244 | this.closeOpenButton.setMessage(Component.translatable(key));
|
| 245 | this.closeOpenButton.active = true;
|
| 246 | this.preferredRegionSelection = new RealmsSettingsTab.RegionSelection(
|
| 247 | serverData.regionSelectionPreference.regionSelectionPreference, serverData.regionSelectionPreference.preferredRegion
|
| 248 | );
|
| 249 | this.nameEdit.setValue(Objects.requireNonNullElse(serverData.getName(), ""));
|
| 250 | this.descEdit.setValue(serverData.getDescription());
|
| 251 | this.updateRegionPreferenceValues();
|
| 252 | }
|
| 253 |
|
| 254 | @Override
|
| 255 | public void onDeselected(final RealmsServer serverData) {
|
| 256 | this.save();
|
| 257 | }
|
| 258 |
|
| 259 | public boolean save() {
|
| 260 | String realmName = this.nameEdit.getValue().trim();
|
| 261 | int renderDistance = this.renderDistanceOption == null ? this.initialRenderDistance : this.renderDistanceOption.get();
|
| 262 | int simulationDistance = this.simulationDistanceOption == null ? this.initialSimulationDistance : this.simulationDistanceOption.get();
|
| 263 | int previousRenderDistance = this.initialRenderDistance;
|
| 264 | int previousSimulationDistance = this.initialSimulationDistance;
|
| 265 | Integer updatedRenderDistance = renderDistance == this.initialRenderDistance ? null : renderDistance;
|
| 266 | Integer updatedSimulationDistance = simulationDistance == this.initialSimulationDistance ? null : simulationDistance;
|
| 267 | if (this.serverData.regionSelectionPreference != null
|
| 268 | && Objects.equals(realmName, this.serverData.name)
|
| 269 | && Objects.equals(this.descEdit.getValue(), this.serverData.motd)
|
| 270 | && this.preferredRegionSelection.preference() == this.serverData.regionSelectionPreference.regionSelectionPreference
|
| 271 | && this.preferredRegionSelection.region() == this.serverData.regionSelectionPreference.preferredRegion
|
| 272 | && updatedRenderDistance == null
|
| 273 | && updatedSimulationDistance == null) {
|
| 274 | return true;
|
| 275 | }
|
| 276 |
|
| 277 | this.initialRenderDistance = renderDistance;
|
| 278 | this.initialSimulationDistance = simulationDistance;
|
| 279 | boolean saved = this.configurationScreen
|
| 280 | .saveSettings(
|
| 281 | realmName,
|
| 282 | this.descEdit.getValue(),
|
| 283 | this.preferredRegionSelection.preference(),
|
| 284 | this.preferredRegionSelection.region(),
|
| 285 | updatedRenderDistance,
|
| 286 | updatedSimulationDistance
|
| 287 | );
|
| 288 | if (!saved) {
|
| 289 | this.initialRenderDistance = previousRenderDistance;
|
| 290 | this.initialSimulationDistance = previousSimulationDistance;
|
| 291 | }
|
| 292 |
|
| 293 | return saved;
|
| 294 | }
|
| 295 |
|
| 296 | @Environment(EnvType.CLIENT)
|
| 297 | public record RegionSelection(RegionSelectionPreference preference, @Nullable RealmsRegion region) {
|
| 298 | }
|
| 299 | }
|
| 300 |
|