| 1 | package com.mojang.realmsclient.gui.screens.configuration;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.realmsclient.dto.InviteCode;
|
| 5 | import com.mojang.realmsclient.exception.RealmsServiceException;
|
| 6 | import com.mojang.realmsclient.gui.screens.AbstractRealmsCodeScreen;
|
| 7 | import com.mojang.realmsclient.gui.screens.RealmsGenericErrorScreen;
|
| 8 | import com.mojang.realmsclient.util.RealmsUtil;
|
| 9 | import java.time.Instant;
|
| 10 | import java.time.ZoneId;
|
| 11 | import java.time.ZonedDateTime;
|
| 12 | import java.time.format.DateTimeFormatter;
|
| 13 | import java.time.format.FormatStyle;
|
| 14 | import net.fabricmc.api.EnvType;
|
| 15 | import net.fabricmc.api.Environment;
|
| 16 | import net.minecraft.client.gui.components.AbstractSliderButton;
|
| 17 | import net.minecraft.client.gui.components.Button;
|
| 18 | import net.minecraft.client.gui.components.CycleButton;
|
| 19 | import net.minecraft.client.gui.components.StringWidget;
|
| 20 | import net.minecraft.client.gui.layouts.GridLayout;
|
| 21 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 22 | import net.minecraft.client.gui.layouts.SpacerElement;
|
| 23 | import net.minecraft.client.input.KeyEvent;
|
| 24 | import net.minecraft.network.chat.CommonComponents;
|
| 25 | import net.minecraft.network.chat.Component;
|
| 26 | import net.minecraft.util.Mth;
|
| 27 | import net.minecraft.util.Util;
|
| 28 | import org.jspecify.annotations.Nullable;
|
| 29 | import org.slf4j.Logger;
|
| 30 |
|
| 31 | @Environment(EnvType.CLIENT)
|
| 32 | public class RealmsEditInviteCodeScreen extends AbstractRealmsCodeScreen {
|
| 33 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 34 | private static final Component TITLE = Component.translatable("mco.configure.world.invite_codes.edit.title");
|
| 35 | private static final Component ACTIVE_LABEL = Component.translatable("mco.configure.world.invite_codes.edit.active");
|
| 36 | private static final Component EXPIRATION_DATE_LABEL = Component.translatable("mco.configure.world.invite_codes.edit.expiration_date");
|
| 37 | private static final Component COPY_LABEL = Component.translatable("mco.configure.world.invite_codes.copy");
|
| 38 | private static final Component DELETE_LABEL = Component.translatable("mco.configure.world.invite_codes.delete");
|
| 39 | private static final Component NEVER_EXPIRES_LABEL = Component.translatable(
|
| 40 | "mco.configure.world.invite_codes.edit.expires", InviteCodeExpiration.NEVER.getLabel()
|
| 41 | )
|
| 42 | .withColor(-6250336);
|
| 43 | private static final DateTimeFormatter DATE_FORMAT = Util.localizedDateFormatter(FormatStyle.SHORT);
|
| 44 | private static final int SPACING = 8;
|
| 45 | private static final int ACTIVE_BUTTON_WIDTH = 100;
|
| 46 | private static final int CONTENT_WIDTH = 308;
|
| 47 | private static final int CODE_COLUMN_WIDTH = 200;
|
| 48 | private static final int FOOTER_HEIGHT = 64;
|
| 49 | private final RealmsInviteCodesScreen parentScreen;
|
| 50 | private final long realmId;
|
| 51 | private final InviteCode inviteCode;
|
| 52 | private boolean active;
|
| 53 | private InviteCodeExpiration expiration;
|
| 54 | private boolean expirationChanged = false;
|
| 55 | private @Nullable CycleButton<Boolean> activeButton;
|
| 56 | private RealmsEditInviteCodeScreen.@Nullable ExpirationSlider expirationSlider;
|
| 57 | private @Nullable Button deleteButton;
|
| 58 | private @Nullable Button saveButton;
|
| 59 | private @Nullable Button backButton;
|
| 60 |
|
| 61 | public RealmsEditInviteCodeScreen(final RealmsInviteCodesScreen parentScreen, final long realmId, final InviteCode inviteCode) {
|
| 62 | super(TITLE);
|
| 63 | this.parentScreen = parentScreen;
|
| 64 | this.realmId = realmId;
|
| 65 | this.inviteCode = inviteCode;
|
| 66 | this.active = inviteCode.enabled();
|
| 67 | this.expiration = inviteCode.expirationDate() != null ? InviteCodeExpiration.closestTo(inviteCode.expirationDate()) : InviteCodeExpiration.NEVER;
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | public void init() {
|
| 72 | this.layout.addTitleHeader(TITLE, this.font);
|
| 73 | LinearLayout content = this.layout.addToContents(LinearLayout.vertical().spacing(8));
|
| 74 | content.defaultCellSetting().alignHorizontallyLeft();
|
| 75 | GridLayout topRow = content.addChild(new GridLayout().spacing(8));
|
| 76 | LinearLayout codeColumn = topRow.addChild(LinearLayout.vertical().spacing(4), 0, 0, topRow.newCellSettings().alignVerticallyMiddle());
|
| 77 | codeColumn.defaultCellSetting().alignHorizontallyLeft();
|
| 78 | codeColumn.addChild(new StringWidget(Component.literal(this.inviteCode.code()), this.font).setMaxWidth(200));
|
| 79 | codeColumn.addChild(new StringWidget(this.getExpiresLabel(), this.font).setMaxWidth(200));
|
| 80 | topRow.addChild(SpacerElement.width(200), 1, 0);
|
| 81 | this.activeButton = CycleButton.onOffBuilder(this.active).create(0, 0, 100, 20, ACTIVE_LABEL, (var1x, value) -> this.setActive(value));
|
| 82 | topRow.addChild(this.activeButton, 0, 1, topRow.newCellSettings().alignVerticallyMiddle());
|
| 83 | this.expirationSlider = content.addChild(new RealmsEditInviteCodeScreen.ExpirationSlider(308));
|
| 84 | GridLayout footer = this.layout.addToFooter(new GridLayout().spacing(8));
|
| 85 | footer.defaultCellSetting().alignHorizontallyCenter();
|
| 86 | GridLayout.RowHelper rowHelper = footer.createRowHelper(2);
|
| 87 | rowHelper.addChild(Button.builder(COPY_LABEL, var1x -> this.onCopy()).width(150).build());
|
| 88 | this.deleteButton = rowHelper.addChild(Button.builder(DELETE_LABEL, var1x -> this.onDelete()).width(150).build());
|
| 89 | this.saveButton = rowHelper.addChild(Button.builder(CommonComponents.GUI_DONE, var1x -> this.onSave()).width(150).build());
|
| 90 | this.backButton = rowHelper.addChild(Button.builder(CommonComponents.GUI_BACK, var1x -> this.onClose()).width(150).build());
|
| 91 | this.layout.setFooterHeight(64);
|
| 92 | this.setControlsActive(true);
|
| 93 | this.layout.visitWidgets(x$0 -> this.addRenderableWidget(x$0));
|
| 94 | this.repositionElements();
|
| 95 | }
|
| 96 |
|
| 97 | private void setActive(final boolean value) {
|
| 98 | this.active = value;
|
| 99 | if (this.expirationSlider != null) {
|
| 100 | this.expirationSlider.active = value;
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | private void setControlsActive(final boolean controlsActive) {
|
| 105 | if (this.activeButton != null) {
|
| 106 | this.activeButton.active = controlsActive;
|
| 107 | }
|
| 108 |
|
| 109 | if (this.expirationSlider != null) {
|
| 110 | this.expirationSlider.active = controlsActive && this.active;
|
| 111 | }
|
| 112 |
|
| 113 | if (this.deleteButton != null) {
|
| 114 | this.deleteButton.active = controlsActive;
|
| 115 | }
|
| 116 |
|
| 117 | if (this.saveButton != null) {
|
| 118 | this.saveButton.active = controlsActive;
|
| 119 | }
|
| 120 |
|
| 121 | if (this.backButton != null) {
|
| 122 | this.backButton.active = controlsActive;
|
| 123 | }
|
| 124 | }
|
| 125 |
|
| 126 | private Component getExpiresLabel() {
|
| 127 | Instant expirationDate = this.inviteCode.expirationDate();
|
| 128 | if (expirationDate == null) {
|
| 129 | return NEVER_EXPIRES_LABEL;
|
| 130 | }
|
| 131 |
|
| 132 | String formattedDate = ZonedDateTime.ofInstant(expirationDate, ZoneId.systemDefault()).format(DATE_FORMAT);
|
| 133 | return Component.translatable("mco.configure.world.invite_codes.edit.expires", formattedDate).withColor(-6250336);
|
| 134 | }
|
| 135 |
|
| 136 | private void onCopy() {
|
| 137 | this.minecraft.keyboardHandler.setClipboard(this.inviteCode.code());
|
| 138 | }
|
| 139 |
|
| 140 | private void onDelete() {
|
| 141 | this.setControlsActive(false);
|
| 142 | RealmsUtil.runAsync(client -> client.deleteInviteCode(this.inviteCode.code()), exception -> this.handleFailure(exception, "Couldn't delete invite code"))
|
| 143 | .thenRunAsync(() -> {
|
| 144 | this.minecraft.gui.setScreen(this.parentScreen);
|
| 145 | this.parentScreen.refreshInviteCodes();
|
| 146 | }, this.screenExecutor);
|
| 147 | }
|
| 148 |
|
| 149 | private void onSave() {
|
| 150 | if (this.active == this.inviteCode.enabled() && !this.expirationChanged) {
|
| 151 | this.minecraft.gui.setScreen(this.parentScreen);
|
| 152 | } else {
|
| 153 | this.setControlsActive(false);
|
| 154 | Instant expirationDate = InviteCodeExpiration.resolveExpirationDate(this.inviteCode.expirationDate(), this.expiration, this.expirationChanged, Instant.now());
|
| 155 | RealmsUtil.runAsync(
|
| 156 | client -> client.updateInviteCode(this.realmId, this.inviteCode.code(), this.active, expirationDate),
|
| 157 | exception -> this.handleFailure(exception, "Couldn't update invite code")
|
| 158 | )
|
| 159 | .thenRunAsync(() -> {
|
| 160 | this.minecraft.gui.setScreen(this.parentScreen);
|
| 161 | this.parentScreen.refreshInviteCodes();
|
| 162 | }, this.screenExecutor);
|
| 163 | }
|
| 164 | }
|
| 165 |
|
| 166 | private void handleFailure(final RealmsServiceException exception, final String message) {
|
| 167 | LOGGER.error("{}", message, exception);
|
| 168 | this.screenExecutor.execute(() -> {
|
| 169 | this.setControlsActive(true);
|
| 170 | this.minecraft.gui.setScreen(new RealmsGenericErrorScreen(exception, this));
|
| 171 | });
|
| 172 | }
|
| 173 |
|
| 174 | @Override
|
| 175 | public void onClose() {
|
| 176 | if (this.backButton == null || this.backButton.active) {
|
| 177 | this.minecraft.gui.setScreen(this.parentScreen);
|
| 178 | }
|
| 179 | }
|
| 180 |
|
| 181 | @Environment(EnvType.CLIENT)
|
| 182 | private class ExpirationSlider extends AbstractSliderButton {
|
| 183 | private final InviteCodeExpiration[] values = InviteCodeExpiration.values();
|
| 184 |
|
| 185 | private ExpirationSlider(final int width) {
|
| 186 | super(0, 0, width, 20, CommonComponents.EMPTY, (double)RealmsEditInviteCodeScreen.this.expiration.ordinal() / (InviteCodeExpiration.values().length - 1));
|
| 187 | this.updateMessage();
|
| 188 | }
|
| 189 |
|
| 190 | @Override
|
| 191 | protected void updateMessage() {
|
| 192 | this.setMessage(CommonComponents.optionNameValue(RealmsEditInviteCodeScreen.EXPIRATION_DATE_LABEL, RealmsEditInviteCodeScreen.this.expiration.getLabel()));
|
| 193 | }
|
| 194 |
|
| 195 | @Override
|
| 196 | protected void applyValue() {
|
| 197 | int index = Mth.clamp((int)Math.round(this.value * (this.values.length - 1)), 0, this.values.length - 1);
|
| 198 | InviteCodeExpiration selectedExpiration = this.values[index];
|
| 199 | this.value = this.sliderValue(index);
|
| 200 | if (selectedExpiration != RealmsEditInviteCodeScreen.this.expiration) {
|
| 201 | RealmsEditInviteCodeScreen.this.expiration = selectedExpiration;
|
| 202 | RealmsEditInviteCodeScreen.this.expirationChanged = true;
|
| 203 | }
|
| 204 | }
|
| 205 |
|
| 206 | @Override
|
| 207 | public boolean keyPressed(final KeyEvent event) {
|
| 208 | if (this.canChangeValue && (event.isLeft() || event.isRight())) {
|
| 209 | int direction = event.isLeft() ? -1 : 1;
|
| 210 | int index = Mth.clamp(RealmsEditInviteCodeScreen.this.expiration.ordinal() + direction, 0, this.values.length - 1);
|
| 211 | this.setValue(this.sliderValue(index));
|
| 212 | return true;
|
| 213 | } else {
|
| 214 | return super.keyPressed(event);
|
| 215 | }
|
| 216 | }
|
| 217 |
|
| 218 | private double sliderValue(final int index) {
|
| 219 | return (double)index / (this.values.length - 1);
|
| 220 | }
|
| 221 | }
|
| 222 | }
|
| 223 |
|