| 1 | package com.mojang.realmsclient.gui.screens.configuration;
|
| 2 |
|
| 3 | import com.mojang.realmsclient.dto.RealmsServer;
|
| 4 | import java.util.concurrent.CompletableFuture;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 8 | import net.minecraft.client.gui.components.Button;
|
| 9 | import net.minecraft.client.gui.components.EditBox;
|
| 10 | import net.minecraft.client.gui.layouts.CommonLayouts;
|
| 11 | import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
|
| 12 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 13 | import net.minecraft.network.chat.CommonComponents;
|
| 14 | import net.minecraft.network.chat.Component;
|
| 15 | import net.minecraft.realms.RealmsScreen;
|
| 16 | import net.minecraft.util.StringUtil;
|
| 17 | import net.minecraft.util.Util;
|
| 18 | import org.jspecify.annotations.Nullable;
|
| 19 |
|
| 20 | @Environment(EnvType.CLIENT)
|
| 21 | public class RealmsInviteScreen extends RealmsScreen {
|
| 22 | private static final Component TITLE = Component.translatable("mco.configure.world.buttons.invite");
|
| 23 | private static final Component NAME_LABEL = Component.translatable("mco.configure.world.invite.profile.name").withColor(-6250336);
|
| 24 | private static final Component INVITING_PLAYER_TEXT = Component.translatable("mco.configure.world.players.inviting").withColor(-6250336);
|
| 25 | private static final Component NO_SUCH_PLAYER_ERROR_TEXT = Component.translatable("mco.configure.world.players.error").withColor(-65536);
|
| 26 | private static final Component DUPLICATE_PLAYER_TEXT = Component.translatable("mco.configure.world.players.invite.duplicate").withColor(-65536);
|
| 27 | private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this);
|
| 28 | private @Nullable EditBox profileName;
|
| 29 | private @Nullable Button inviteButton;
|
| 30 | private final RealmsServer serverData;
|
| 31 | private final RealmsConfigureWorldScreen configureScreen;
|
| 32 | private @Nullable Component message;
|
| 33 |
|
| 34 | public RealmsInviteScreen(final RealmsConfigureWorldScreen configureScreen, final RealmsServer serverData) {
|
| 35 | super(TITLE);
|
| 36 | this.configureScreen = configureScreen;
|
| 37 | this.serverData = serverData;
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | public void init() {
|
| 42 | this.layout.addTitleHeader(TITLE, this.font);
|
| 43 | LinearLayout content = this.layout.addToContents(LinearLayout.vertical().spacing(8));
|
| 44 | this.profileName = new EditBox(this.minecraft.font, 200, 20, Component.translatable("mco.configure.world.invite.profile.name"));
|
| 45 | content.addChild(CommonLayouts.labeledElement(this.font, this.profileName, NAME_LABEL));
|
| 46 | this.inviteButton = content.addChild(Button.builder(TITLE, button -> this.onInvite()).width(200).build());
|
| 47 | this.layout.addToFooter(Button.builder(CommonComponents.GUI_BACK, button -> this.onClose()).width(200).build());
|
| 48 | this.layout.visitWidgets(x$0 -> this.addRenderableWidget(x$0));
|
| 49 | this.repositionElements();
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | protected void repositionElements() {
|
| 54 | this.layout.arrangeElements();
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | protected void setInitialFocus() {
|
| 59 | if (this.profileName != null) {
|
| 60 | this.setInitialFocus(this.profileName);
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | private void onInvite() {
|
| 65 | if (this.inviteButton != null && this.profileName != null) {
|
| 66 | if (StringUtil.isBlank(this.profileName.getValue())) {
|
| 67 | this.showMessage(NO_SUCH_PLAYER_ERROR_TEXT);
|
| 68 | } else if (this.serverData.players.stream().anyMatch(player -> player.name.equalsIgnoreCase(this.profileName.getValue()))) {
|
| 69 | this.showMessage(DUPLICATE_PLAYER_TEXT);
|
| 70 | } else {
|
| 71 | long serverId = this.serverData.id;
|
| 72 | String name = this.profileName.getValue().trim();
|
| 73 | this.inviteButton.active = false;
|
| 74 | this.profileName.setEditable(false);
|
| 75 | this.showMessage(INVITING_PLAYER_TEXT);
|
| 76 | CompletableFuture.<Boolean>supplyAsync(() -> this.configureScreen.invitePlayer(serverId, name), Util.ioPool()).thenAcceptAsync(success -> {
|
| 77 | if (success) {
|
| 78 | this.minecraft.gui.setScreen(this.configureScreen);
|
| 79 | } else {
|
| 80 | this.showMessage(NO_SUCH_PLAYER_ERROR_TEXT);
|
| 81 | }
|
| 82 |
|
| 83 | this.profileName.setEditable(true);
|
| 84 | this.inviteButton.active = true;
|
| 85 | }, this.screenExecutor);
|
| 86 | }
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | private void showMessage(final Component message) {
|
| 91 | this.message = message;
|
| 92 | this.minecraft.getNarrator().saySystemNow(message);
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | public void onClose() {
|
| 97 | this.minecraft.gui.setScreen(this.configureScreen);
|
| 98 | }
|
| 99 |
|
| 100 | @Override
|
| 101 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int xm, final int ym, final float a) {
|
| 102 | super.extractRenderState(graphics, xm, ym, a);
|
| 103 | if (this.message != null && this.inviteButton != null) {
|
| 104 | graphics.centeredText(this.font, this.message, this.width / 2, this.inviteButton.getY() + this.inviteButton.getHeight() + 8, -1);
|
| 105 | }
|
| 106 | }
|
| 107 | }
|
| 108 |
|