| 1 | package com.mojang.realmsclient.gui.screens;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.realmsclient.exception.RealmsServiceException;
|
| 5 | import com.mojang.realmsclient.util.RealmsUtil;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 9 | import net.minecraft.client.gui.components.Button;
|
| 10 | import net.minecraft.client.gui.components.EditBox;
|
| 11 | import net.minecraft.client.gui.layouts.CommonLayouts;
|
| 12 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 13 | import net.minecraft.client.gui.screens.Screen;
|
| 14 | import net.minecraft.network.chat.CommonComponents;
|
| 15 | import net.minecraft.network.chat.Component;
|
| 16 | import net.minecraft.util.StringUtil;
|
| 17 | import org.jspecify.annotations.Nullable;
|
| 18 | import org.slf4j.Logger;
|
| 19 |
|
| 20 | @Environment(EnvType.CLIENT)
|
| 21 | public class RealmsJoinRealmWithCodeScreen extends AbstractRealmsCodeScreen {
|
| 22 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 23 | private static final Component TITLE = Component.translatable("mco.selectServer.joinCode.title");
|
| 24 | private static final Component JOIN_CODE_LABEL = Component.translatable("mco.selectServer.joinCode").withColor(-6250336);
|
| 25 | private static final Component JOIN_CODE_HINT = Component.translatable("mco.selectServer.joinCode.hint");
|
| 26 | private static final Component JOIN_REALM_TEXT = Component.translatable("mco.selectServer.joinCode.select");
|
| 27 | private static final Component VALIDATING_CODE_TEXT = Component.translatable("mco.selectServer.joinCode.validating").withColor(-1);
|
| 28 | private static final Component INVALID_CODE_TEXT = Component.translatable("mco.selectServer.joinCode.invalid").withColor(-65536);
|
| 29 | private final Screen lastScreen;
|
| 30 | private final Runnable onJoin;
|
| 31 | private @Nullable EditBox joinCode;
|
| 32 | private @Nullable Button joinButton;
|
| 33 | private @Nullable Component message;
|
| 34 |
|
| 35 | public RealmsJoinRealmWithCodeScreen(final Screen lastScreen, final Runnable onJoin) {
|
| 36 | super(TITLE);
|
| 37 | this.lastScreen = lastScreen;
|
| 38 | this.onJoin = onJoin;
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | protected void init() {
|
| 43 | this.layout.addTitleHeader(TITLE, this.font);
|
| 44 | LinearLayout content = this.layout.addToContents(LinearLayout.vertical().spacing(8));
|
| 45 | this.joinCode = new EditBox(this.font, 200, 20, JOIN_CODE_LABEL);
|
| 46 | this.joinCode.setHint(JOIN_CODE_HINT);
|
| 47 | content.addChild(CommonLayouts.labeledElement(this.font, this.joinCode, JOIN_CODE_LABEL));
|
| 48 | this.joinButton = content.addChild(Button.builder(JOIN_REALM_TEXT, var1x -> this.joinRealm()).width(200).build());
|
| 49 | this.joinButton.active = false;
|
| 50 | this.joinCode.setResponder(value -> {
|
| 51 | this.joinButton.active = !StringUtil.isBlank(value);
|
| 52 | this.message = null;
|
| 53 | });
|
| 54 | this.layout.addToFooter(Button.builder(CommonComponents.GUI_BACK, var1x -> this.onClose()).width(200).build());
|
| 55 | this.layout.visitWidgets(x$0 -> this.addRenderableWidget(x$0));
|
| 56 | this.repositionElements();
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | protected void setInitialFocus() {
|
| 61 | if (this.joinCode != null) {
|
| 62 | this.setInitialFocus(this.joinCode);
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | private void joinRealm() {
|
| 67 | if (this.joinCode != null && this.joinButton != null) {
|
| 68 | String code = this.joinCode.getValue().trim();
|
| 69 | if (!StringUtil.isBlank(code)) {
|
| 70 | this.joinCode.setEditable(false);
|
| 71 | this.joinButton.active = false;
|
| 72 | this.showMessage(VALIDATING_CODE_TEXT);
|
| 73 | RealmsUtil.runAsync(client -> client.redeemInviteCode(code), this::onJoinFailure).thenRunAsync(() -> {
|
| 74 | if (this.minecraft.gui.screen() == this) {
|
| 75 | this.onJoin.run();
|
| 76 | }
|
| 77 | }, this.screenExecutor);
|
| 78 | }
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | private void onJoinFailure(final RealmsServiceException exception) {
|
| 83 | LOGGER.error("Couldn't redeem invite code", exception);
|
| 84 | this.screenExecutor.execute(() -> {
|
| 85 | if (this.minecraft.gui.screen() == this && this.joinCode != null && this.joinButton != null) {
|
| 86 | this.joinCode.setEditable(true);
|
| 87 | this.joinButton.active = !StringUtil.isBlank(this.joinCode.getValue());
|
| 88 | this.showMessage(INVALID_CODE_TEXT);
|
| 89 | }
|
| 90 | });
|
| 91 | }
|
| 92 |
|
| 93 | private void showMessage(final Component message) {
|
| 94 | this.message = message;
|
| 95 | this.minecraft.getNarrator().saySystemNow(message);
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 100 | super.extractRenderState(graphics, mouseX, mouseY, a);
|
| 101 | if (this.message != null && this.joinButton != null) {
|
| 102 | graphics.centeredText(this.font, this.message, this.width / 2, this.joinButton.getY() + this.joinButton.getHeight() + 8, -1);
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | @Override
|
| 107 | public void onClose() {
|
| 108 | this.minecraft.gui.setScreen(this.lastScreen);
|
| 109 | }
|
| 110 | }
|
| 111 |
|