| 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.dto.InviteCodeList;
|
| 6 | import com.mojang.realmsclient.dto.RealmsServer;
|
| 7 | import com.mojang.realmsclient.exception.RealmsServiceException;
|
| 8 | import com.mojang.realmsclient.gui.screens.AbstractRealmsCodeScreen;
|
| 9 | import com.mojang.realmsclient.gui.screens.RealmsGenericErrorScreen;
|
| 10 | import com.mojang.realmsclient.gui.screens.configuration.RealmsInviteCodesScreen.InviteCodeSelectionList.Entry;
|
| 11 | import com.mojang.realmsclient.util.RealmsUtil;
|
| 12 | import java.util.List;
|
| 13 | import net.fabricmc.api.EnvType;
|
| 14 | import net.fabricmc.api.Environment;
|
| 15 | import net.minecraft.client.Minecraft;
|
| 16 | import net.minecraft.client.gui.components.Button;
|
| 17 | import net.minecraft.client.gui.components.LoadingDotsWidget;
|
| 18 | import net.minecraft.client.gui.components.MultiLineTextWidget;
|
| 19 | import net.minecraft.client.gui.components.ObjectSelectionList;
|
| 20 | import net.minecraft.client.gui.components.StringWidget;
|
| 21 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 22 | import net.minecraft.client.gui.screens.Screen;
|
| 23 | import net.minecraft.network.chat.CommonComponents;
|
| 24 | import net.minecraft.network.chat.Component;
|
| 25 | import org.jspecify.annotations.Nullable;
|
| 26 | import org.slf4j.Logger;
|
| 27 |
|
| 28 | @Environment(EnvType.CLIENT)
|
| 29 | public class RealmsInviteCodesScreen extends AbstractRealmsCodeScreen {
|
| 30 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 31 | static final Component TITLE = Component.translatable("mco.configure.world.buttons.invite_codes");
|
| 32 | private static final Component LOADING_TEXT = Component.translatable("mco.configure.world.invite_codes.loading");
|
| 33 | private static final Component COPY_LABEL = Component.translatable("mco.configure.world.invite_codes.copy");
|
| 34 | private static final Component DELETE_LABEL = Component.translatable("mco.configure.world.invite_codes.delete");
|
| 35 | private static final Component EDIT_LABEL = Component.translatable("mco.configure.world.invite_codes.edit");
|
| 36 | private static final Component CREATE_LABEL = Component.translatable("mco.configure.world.invite_codes.create");
|
| 37 | private static final int MAX_INVITE_CODES = 5;
|
| 38 | private static final int SPACING = 8;
|
| 39 | private static final int CONTENT_WIDTH = 308;
|
| 40 | private static final int BOTTOM_BUTTON_WIDTH = 150;
|
| 41 | private static final int TOP_BUTTON_WIDTH = 97;
|
| 42 | private static final int FOOTER_HEIGHT = 64;
|
| 43 | private static final int ENTRY_HEIGHT = 28;
|
| 44 | private final RealmsConfigureWorldScreen configureScreen;
|
| 45 | private final long realmId;
|
| 46 | private @Nullable Button copyButton;
|
| 47 | private @Nullable Button editButton;
|
| 48 | private @Nullable Button deleteButton;
|
| 49 | private @Nullable Button createButton;
|
| 50 | private @Nullable Button backButton;
|
| 51 | private RealmsInviteCodesScreen.@Nullable InviteCodeSelectionList inviteCodeList;
|
| 52 | private List<InviteCode> inviteCodes = List.of();
|
| 53 | private boolean fetchedInviteCodes = false;
|
| 54 |
|
| 55 | public RealmsInviteCodesScreen(final RealmsConfigureWorldScreen configureScreen, final RealmsServer serverData) {
|
| 56 | super(TITLE);
|
| 57 | this.configureScreen = configureScreen;
|
| 58 | this.realmId = serverData.id;
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | public void init() {
|
| 63 | this.layout.removeChildren();
|
| 64 | LinearLayout header = this.layout.addToHeader(LinearLayout.vertical().spacing(8));
|
| 65 | header.defaultCellSetting().alignHorizontallyCenter();
|
| 66 | Component headerTitle = this.fetchedInviteCodes
|
| 67 | ? Component.translatable("mco.configure.world.invite_codes.title", this.inviteCodes.size(), 5)
|
| 68 | : this.getTitle();
|
| 69 | header.addChild(new StringWidget(headerTitle, this.font));
|
| 70 | int headerHeight = 33;
|
| 71 | if (this.fetchedInviteCodes && this.inviteCodes.isEmpty()) {
|
| 72 | MultiLineTextWidget subtitle = header.addChild(
|
| 73 | new MultiLineTextWidget(Component.translatable("mco.configure.world.invite_codes.subtitle", 5).withColor(-6250336), this.font)
|
| 74 | .setCentered(true)
|
| 75 | .setMaxWidth(308)
|
| 76 | );
|
| 77 | headerHeight += subtitle.getHeight();
|
| 78 | }
|
| 79 |
|
| 80 | this.layout.setHeaderHeight(headerHeight);
|
| 81 | LinearLayout footer = this.layout.addToFooter(LinearLayout.vertical().spacing(8));
|
| 82 | LinearLayout actionRow = footer.addChild(LinearLayout.horizontal().spacing(8));
|
| 83 | this.copyButton = actionRow.addChild(Button.builder(COPY_LABEL, var1x -> this.onCopy()).width(97).build());
|
| 84 | this.deleteButton = actionRow.addChild(Button.builder(DELETE_LABEL, var1x -> this.onDelete()).width(97).build());
|
| 85 | this.editButton = actionRow.addChild(Button.builder(EDIT_LABEL, var1x -> this.onEdit()).width(97).build());
|
| 86 | LinearLayout manageRow = footer.addChild(LinearLayout.horizontal().spacing(8));
|
| 87 | this.createButton = manageRow.addChild(Button.builder(CREATE_LABEL, var1x -> this.onCreate()).width(150).build());
|
| 88 | this.backButton = manageRow.addChild(Button.builder(CommonComponents.GUI_BACK, var1x -> this.onClose()).width(150).build());
|
| 89 | this.layout.setFooterHeight(64);
|
| 90 | if (this.fetchedInviteCodes) {
|
| 91 | this.inviteCodeList = this.layout.addToContents(new RealmsInviteCodesScreen.InviteCodeSelectionList(this.inviteCodes));
|
| 92 | } else {
|
| 93 | this.inviteCodeList = null;
|
| 94 | this.layout.addToContents(new LoadingDotsWidget(this.font, LOADING_TEXT));
|
| 95 | }
|
| 96 |
|
| 97 | this.updateButtonStates();
|
| 98 | this.layout.visitWidgets(x$0 -> this.addRenderableWidget(x$0));
|
| 99 | this.repositionElements();
|
| 100 | if (!this.fetchedInviteCodes) {
|
| 101 | this.fetchInviteCodes();
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | private void fetchInviteCodes() {
|
| 106 | this.setControlsActive(false);
|
| 107 | Screen errorReturnScreen = this.fetchedInviteCodes ? this : this.configureScreen;
|
| 108 | RealmsUtil.<InviteCodeList>supplyAsync(
|
| 109 | client -> client.inviteCodes(this.realmId), exception -> this.handleFailure(exception, "Couldn't get invite codes", errorReturnScreen)
|
| 110 | )
|
| 111 | .thenAcceptAsync(result -> {
|
| 112 | this.inviteCodes = result.inviteCodes();
|
| 113 | this.fetchedInviteCodes = true;
|
| 114 | this.rebuildWidgets();
|
| 115 | this.scheduleNarration();
|
| 116 | }, this.screenExecutor);
|
| 117 | }
|
| 118 |
|
| 119 | private void updateButtonStates() {
|
| 120 | boolean hasSelection = this.inviteCodeList != null && this.inviteCodeList.getSelectedCode() != null;
|
| 121 | if (this.copyButton != null) {
|
| 122 | this.copyButton.active = hasSelection;
|
| 123 | }
|
| 124 |
|
| 125 | if (this.editButton != null) {
|
| 126 | this.editButton.active = hasSelection;
|
| 127 | }
|
| 128 |
|
| 129 | if (this.deleteButton != null) {
|
| 130 | this.deleteButton.active = hasSelection;
|
| 131 | }
|
| 132 |
|
| 133 | if (this.createButton != null) {
|
| 134 | this.createButton.active = this.fetchedInviteCodes && this.inviteCodes.size() < 5;
|
| 135 | }
|
| 136 | }
|
| 137 |
|
| 138 | private void setControlsActive(final boolean controlsActive) {
|
| 139 | if (this.inviteCodeList != null) {
|
| 140 | this.inviteCodeList.active = controlsActive;
|
| 141 | }
|
| 142 |
|
| 143 | if (controlsActive) {
|
| 144 | this.updateButtonStates();
|
| 145 | } else {
|
| 146 | if (this.copyButton != null) {
|
| 147 | this.copyButton.active = false;
|
| 148 | }
|
| 149 |
|
| 150 | if (this.editButton != null) {
|
| 151 | this.editButton.active = false;
|
| 152 | }
|
| 153 |
|
| 154 | if (this.deleteButton != null) {
|
| 155 | this.deleteButton.active = false;
|
| 156 | }
|
| 157 |
|
| 158 | if (this.createButton != null) {
|
| 159 | this.createButton.active = false;
|
| 160 | }
|
| 161 | }
|
| 162 | }
|
| 163 |
|
| 164 | private void handleFailure(final RealmsServiceException exception, final String message, final Screen errorReturnScreen) {
|
| 165 | LOGGER.error("{}", message, exception);
|
| 166 | this.screenExecutor.execute(() -> {
|
| 167 | this.setControlsActive(true);
|
| 168 | if (this.backButton != null) {
|
| 169 | this.backButton.active = true;
|
| 170 | }
|
| 171 |
|
| 172 | this.minecraft.gui.setScreen(new RealmsGenericErrorScreen(exception, errorReturnScreen));
|
| 173 | });
|
| 174 | }
|
| 175 |
|
| 176 | @Override
|
| 177 | protected void repositionElements() {
|
| 178 | super.repositionElements();
|
| 179 | if (this.inviteCodeList != null) {
|
| 180 | this.inviteCodeList.updateSize(this.width, this.layout);
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | @Override
|
| 185 | protected boolean shouldRenderListBackgroundAndSeparators() {
|
| 186 | return this.inviteCodeList == null;
|
| 187 | }
|
| 188 |
|
| 189 | @Override
|
| 190 | public Component getNarrationMessage() {
|
| 191 | return this.fetchedInviteCodes ? super.getNarrationMessage() : CommonComponents.joinForNarration(super.getNarrationMessage(), LOADING_TEXT);
|
| 192 | }
|
| 193 |
|
| 194 | @Override
|
| 195 | public void onClose() {
|
| 196 | if (this.backButton == null || this.backButton.active) {
|
| 197 | this.minecraft.gui.setScreen(this.configureScreen);
|
| 198 | }
|
| 199 | }
|
| 200 |
|
| 201 | private void onCopy() {
|
| 202 | if (this.inviteCodeList != null) {
|
| 203 | InviteCode selected = this.inviteCodeList.getSelectedCode();
|
| 204 | if (selected != null) {
|
| 205 | this.minecraft.keyboardHandler.setClipboard(selected.code());
|
| 206 | }
|
| 207 | }
|
| 208 | }
|
| 209 |
|
| 210 | private void onDelete() {
|
| 211 | if (this.inviteCodeList != null) {
|
| 212 | InviteCode selected = this.inviteCodeList.getSelectedCode();
|
| 213 | if (selected != null) {
|
| 214 | this.setControlsActive(false);
|
| 215 | if (this.backButton != null) {
|
| 216 | this.backButton.active = false;
|
| 217 | }
|
| 218 |
|
| 219 | RealmsUtil.runAsync(client -> client.deleteInviteCode(selected.code()), exception -> this.handleFailure(exception, "Couldn't delete invite code", this))
|
| 220 | .thenRunAsync(() -> {
|
| 221 | if (this.backButton != null) {
|
| 222 | this.backButton.active = true;
|
| 223 | }
|
| 224 |
|
| 225 | this.refreshInviteCodes();
|
| 226 | }, this.screenExecutor);
|
| 227 | }
|
| 228 | }
|
| 229 | }
|
| 230 |
|
| 231 | private void onCreate() {
|
| 232 | if (this.fetchedInviteCodes && this.inviteCodes.size() < 5) {
|
| 233 | this.setControlsActive(false);
|
| 234 | if (this.backButton != null) {
|
| 235 | this.backButton.active = false;
|
| 236 | }
|
| 237 |
|
| 238 | RealmsUtil.runAsync(
|
| 239 | client -> client.createInviteCode(this.realmId, true, null), exception -> this.handleFailure(exception, "Couldn't create invite code", this)
|
| 240 | )
|
| 241 | .thenRunAsync(() -> {
|
| 242 | if (this.backButton != null) {
|
| 243 | this.backButton.active = true;
|
| 244 | }
|
| 245 |
|
| 246 | this.refreshInviteCodes();
|
| 247 | }, this.screenExecutor);
|
| 248 | }
|
| 249 | }
|
| 250 |
|
| 251 | private void onEdit() {
|
| 252 | if (this.inviteCodeList != null) {
|
| 253 | InviteCode selected = this.inviteCodeList.getSelectedCode();
|
| 254 | if (selected != null) {
|
| 255 | this.minecraft.gui.setScreen(new RealmsEditInviteCodeScreen(this, this.realmId, selected));
|
| 256 | }
|
| 257 | }
|
| 258 | }
|
| 259 |
|
| 260 | void refreshInviteCodes() {
|
| 261 | if (this.inviteCodeList != null) {
|
| 262 | this.inviteCodeList.setSelected((Entry)null);
|
| 263 | }
|
| 264 |
|
| 265 | this.fetchInviteCodes();
|
| 266 | }
|
| 267 |
|
| 268 | @Environment(EnvType.CLIENT)
|
| 269 | private class InviteCodeSelectionList
|
| 270 | extends ObjectSelectionList<com.mojang.realmsclient.gui.screens.configuration.RealmsInviteCodesScreen.InviteCodeSelectionList.Entry> {
|
| 271 | public InviteCodeSelectionList(final List<InviteCode> inviteCodes) {
|
| 272 | super(
|
| 273 | Minecraft.getInstance(),
|
| 274 | RealmsInviteCodesScreen.this.width,
|
| 275 | RealmsInviteCodesScreen.this.layout.getContentHeight(),
|
| 276 | RealmsInviteCodesScreen.this.layout.getHeaderHeight(),
|
| 277 | 28
|
| 278 | );
|
| 279 | inviteCodes.forEach(
|
| 280 | inviteCode -> this.addEntry(new com.mojang.realmsclient.gui.screens.configuration.RealmsInviteCodesScreen.InviteCodeSelectionList.Entry(this, inviteCode))
|
| 281 | );
|
| 282 | }
|
| 283 |
|
| 284 | public @Nullable InviteCode getSelectedCode() {
|
| 285 | com.mojang.realmsclient.gui.screens.configuration.RealmsInviteCodesScreen.InviteCodeSelectionList.Entry selected = this.getSelected();
|
| 286 | return selected == null ? null : selected.inviteCode;
|
| 287 | }
|
| 288 |
|
| 289 | public void setSelected(final Entry/* @Nullable */ entry) {
|
| 290 | super.setSelected(entry);
|
| 291 | RealmsInviteCodesScreen.this.updateButtonStates();
|
| 292 | }
|
| 293 |
|
| 294 | @Override
|
| 295 | public int getRowWidth() {
|
| 296 | return 308;
|
| 297 | }
|
| 298 | }
|
| 299 | }
|
| 300 |
|