RealmsPendingInvitesScreen .java

com.mojang.realmsclient.gui.screens.RealmsPendingInvitesScreen (class): members: PendingInvitationSelectionList, pendingInvitationSelectionList, Entry.

com.mojang.realmsclient.gui.screensclass244 linescommonextends RealmsScreen10.0 KB

API Surface (10)

void init()
void repositionElements()
void onClose()
void extractRenderState(final GuiGraphicsExtractor graphics, final int xm, final int ym, final float a)
List<? extends GuiEventListener> children()
List<? extends NarratableEntry> narratables()
void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a)
int getRowWidth()
boolean hasPendingInvites()
void removeInvitation(final RealmsPendingInvitesScreen.Entry entry)

Fields & Constants (6)

LOGGER
NO_PENDING_INVITES_TEXT
lastScreen
pendingInvites
layout
pendingInvitationSelectionList

String literals (10)

"mco.invites.nopending"
"Couldn't list invites"
"mco.invites.button.accept"
"mco.invites.button.reject"
"pending_invite/accept"
"pending_invite/accept_highlighted"
"pending_invite/reject"
"pending_invite/reject_highlighted"
"narrator.select"
"Couldn't handle invite"

Full Source

RealmsPendingInvitesScreen.java244 lines · Minecraft 26.3 · verbatim
1
package com.mojang.realmsclient.gui.screens;
2
3
import com.mojang.logging.LogUtils;
4
import com.mojang.realmsclient.RealmsMainScreen;
5
import com.mojang.realmsclient.client.RealmsClient;
6
import com.mojang.realmsclient.dto.PendingInvite;
7
import com.mojang.realmsclient.exception.RealmsServiceException;
8
import com.mojang.realmsclient.gui.RealmsDataFetcher;
9
import com.mojang.realmsclient.util.RealmsUtil;
10
import java.util.ArrayList;
11
import java.util.List;
12
import java.util.concurrent.CompletableFuture;
13
import net.fabricmc.api.EnvType;
14
import net.fabricmc.api.Environment;
15
import net.minecraft.client.Minecraft;
16
import net.minecraft.client.gui.GuiGraphicsExtractor;
17
import net.minecraft.client.gui.components.AbstractWidget;
18
import net.minecraft.client.gui.components.Button;
19
import net.minecraft.client.gui.components.ContainerObjectSelectionList;
20
import net.minecraft.client.gui.components.SpriteIconButton;
21
import net.minecraft.client.gui.components.StringWidget;
22
import net.minecraft.client.gui.components.WidgetSprites;
23
import net.minecraft.client.gui.components.events.GuiEventListener;
24
import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
25
import net.minecraft.client.gui.narration.NarratableEntry;
26
import net.minecraft.client.gui.screens.Screen;
27
import net.minecraft.network.chat.CommonComponents;
28
import net.minecraft.network.chat.Component;
29
import net.minecraft.network.chat.ComponentUtils;
30
import net.minecraft.network.chat.MutableComponent;
31
import net.minecraft.network.chat.Style;
32
import net.minecraft.realms.RealmsScreen;
33
import net.minecraft.resources.Identifier;
34
import net.minecraft.util.Util;
35
import org.jspecify.annotations.Nullable;
36
import org.slf4j.Logger;
37
38
@Environment(EnvType.CLIENT)
39
public class RealmsPendingInvitesScreen extends RealmsScreen {
40
	private static final Logger LOGGER = LogUtils.getLogger();
41
	private static final Component NO_PENDING_INVITES_TEXT = Component.translatable("mco.invites.nopending");
42
	private final Screen lastScreen;
43
	private final CompletableFuture<List<PendingInvite>> pendingInvites = CompletableFuture.supplyAsync(() -> {
44
		try {
45
			return RealmsClient.getOrCreate().pendingInvites().pendingInvites();
46
		} catch (RealmsServiceException e) {
47
			LOGGER.error("Couldn't list invites", e);
48
			return List.of();
49
		}
50
	}, Util.nonCriticalIoPool());
51
	private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this);
52
	private RealmsPendingInvitesScreen.@Nullable PendingInvitationSelectionList pendingInvitationSelectionList;
53
54
	public RealmsPendingInvitesScreen(final Screen lastScreen, final Component title) {
55
		super(title);
56
		this.lastScreen = lastScreen;
57
	}
58
59
	@Override
60
	public void init() {
61
		RealmsMainScreen.refreshPendingInvites();
62
		this.layout.addTitleHeader(this.title, this.font);
63
		this.pendingInvitationSelectionList = this.layout.addToContents(new RealmsPendingInvitesScreen.PendingInvitationSelectionList(this.minecraft));
64
		this.pendingInvites.thenAcceptAsync(invites -> {
65
			List<RealmsPendingInvitesScreen.Entry> entries = invites.stream().map(x$0 -> new RealmsPendingInvitesScreen.Entry(x$0)).toList();
66
			this.pendingInvitationSelectionList.replaceEntries(entries);
67
			if (entries.isEmpty()) {
68
				this.minecraft.getNarrator().saySystemQueued(NO_PENDING_INVITES_TEXT);
69
			}
70
		}, this.screenExecutor);
71
		this.layout.addToFooter(Button.builder(CommonComponents.GUI_DONE, button -> this.onClose()).width(200).build());
72
		this.layout.visitWidgets(x$0 -> this.addRenderableWidget(x$0));
73
		this.repositionElements();
74
	}
75
76
	@Override
77
	protected void repositionElements() {
78
		this.layout.arrangeElements();
79
		if (this.pendingInvitationSelectionList != null) {
80
			this.pendingInvitationSelectionList.updateSize(this.width, this.layout);
81
		}
82
	}
83
84
	@Override
85
	public void onClose() {
86
		this.minecraft.gui.setScreen(this.lastScreen);
87
	}
88
89
	@Override
90
	public void extractRenderState(final GuiGraphicsExtractor graphics, final int xm, final int ym, final float a) {
91
		super.extractRenderState(graphics, xm, ym, a);
92
		if (this.pendingInvites.isDone() && this.pendingInvitationSelectionList.hasPendingInvites()) {
93
			graphics.centeredText(this.font, NO_PENDING_INVITES_TEXT, this.width / 2, this.height / 2 - 20, -1);
94
		}
95
	}
96
97
	@Environment(EnvType.CLIENT)
98
	private class Entry extends ContainerObjectSelectionList.Entry<RealmsPendingInvitesScreen.Entry> {
99
		private static final Component ACCEPT_INVITE = Component.translatable("mco.invites.button.accept");
100
		private static final Component REJECT_INVITE = Component.translatable("mco.invites.button.reject");
101
		private static final WidgetSprites ACCEPT_SPRITE = new WidgetSprites(
102
			Identifier.withDefaultNamespace("pending_invite/accept"), Identifier.withDefaultNamespace("pending_invite/accept_highlighted")
103
		);
104
		private static final WidgetSprites REJECT_SPRITE = new WidgetSprites(
105
			Identifier.withDefaultNamespace("pending_invite/reject"), Identifier.withDefaultNamespace("pending_invite/reject_highlighted")
106
		);
107
		private static final int SPRITE_TEXTURE_SIZE = 18;
108
		private static final int SPRITE_SIZE = 21;
109
		private static final int TEXT_LEFT = 38;
110
		private final PendingInvite pendingInvite;
111
		private final List<AbstractWidget> children = new ArrayList<>();
112
		private final SpriteIconButton acceptButton;
113
		private final SpriteIconButton rejectButton;
114
		private final StringWidget realmName;
115
		private final StringWidget realmOwnerName;
116
		private final StringWidget inviteDate;
117
118
		public Entry(final PendingInvite pendingInvite) {
119
			this.pendingInvite = pendingInvite;
120
			int maxTextWidth = RealmsPendingInvitesScreen.this.pendingInvitationSelectionList.getRowWidth() - 32 - 32 - 42;
121
			this.realmName = new StringWidget(Component.literal(pendingInvite.realmName()), RealmsPendingInvitesScreen.this.font).setMaxWidth(maxTextWidth);
122
			this.realmOwnerName = new StringWidget(Component.literal(pendingInvite.realmOwnerName()).withColor(-6250336), RealmsPendingInvitesScreen.this.font)
123
				.setMaxWidth(maxTextWidth);
124
			this.inviteDate = new StringWidget(
125
					ComponentUtils.mergeStyles(RealmsUtil.convertToAgePresentationFromInstant(pendingInvite.date()), Style.EMPTY.withColor(-6250336)),
126
					RealmsPendingInvitesScreen.this.font
127
				)
128
				.setMaxWidth(maxTextWidth);
129
			Button.CreateNarration narration = this.getCreateNarration(pendingInvite);
130
			this.acceptButton = SpriteIconButton.builder(ACCEPT_INVITE, button -> this.handleInvitation(true), false)
131
				.sprite(ACCEPT_SPRITE, 18, 18)
132
				.size(21, 21)
133
				.narration(narration)
134
				.withTootip()
135
				.build();
136
			this.rejectButton = SpriteIconButton.builder(REJECT_INVITE, button -> this.handleInvitation(false), false)
137
				.sprite(REJECT_SPRITE, 18, 18)
138
				.size(21, 21)
139
				.narration(narration)
140
				.withTootip()
141
				.build();
142
			this.children.addAll(List.of(this.acceptButton, this.rejectButton));
143
		}
144
145
		private Button.CreateNarration getCreateNarration(final PendingInvite pendingInvite) {
146
			return defaultNarrationSupplier -> {
147
				MutableComponent narration = CommonComponents.joinForNarration(
148
					defaultNarrationSupplier.get(),
149
					Component.literal(pendingInvite.realmName()),
150
					Component.literal(pendingInvite.realmOwnerName()),
151
					RealmsUtil.convertToAgePresentationFromInstant(pendingInvite.date())
152
				);
153
				return Component.translatable("narrator.select", narration);
154
			};
155
		}
156
157
		@Override
158
		public List<? extends GuiEventListener> children() {
159
			return this.children;
160
		}
161
162
		@Override
163
		public List<? extends NarratableEntry> narratables() {
164
			return this.children;
165
		}
166
167
		@Override
168
		public void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a) {
169
			int x = this.getContentX();
170
			int y = this.getContentY();
171
			int textX = x + 38;
172
			RealmsUtil.extractPlayerFace(graphics, x, y, 32, this.pendingInvite.realmOwnerUuid());
173
			this.realmName.setPosition(textX, y + 1);
174
			this.realmName.extractWidgetRenderState(graphics, mouseX, mouseY, x);
175
			this.realmOwnerName.setPosition(textX, y + 12);
176
			this.realmOwnerName.extractWidgetRenderState(graphics, mouseX, mouseY, x);
177
			this.inviteDate.setPosition(textX, y + 24);
178
			this.inviteDate.extractWidgetRenderState(graphics, mouseX, mouseY, x);
179
			int buttonY = y + this.getContentHeight() / 2 - 10;
180
			this.acceptButton.setPosition(x + this.getContentWidth() - 16 - 42, buttonY);
181
			this.acceptButton.extractRenderState(graphics, mouseX, mouseY, a);
182
			this.rejectButton.setPosition(x + this.getContentWidth() - 8 - 21, buttonY);
183
			this.rejectButton.extractRenderState(graphics, mouseX, mouseY, a);
184
		}
185
186
		private void handleInvitation(final boolean accept) {
187
			String invitationId = this.pendingInvite.invitationId();
188
			CompletableFuture.<Boolean>supplyAsync(() -> {
189
				try {
190
					RealmsClient client = RealmsClient.getOrCreate();
191
					if (accept) {
192
						client.acceptInvitation(invitationId);
193
					} else {
194
						client.rejectInvitation(invitationId);
195
					}
196
197
					return true;
198
				} catch (RealmsServiceException e) {
199
					RealmsPendingInvitesScreen.LOGGER.error("Couldn't handle invite", e);
200
					return false;
201
				}
202
			}, Util.ioPool()).thenAcceptAsync(result -> {
203
				if (result) {
204
					RealmsPendingInvitesScreen.this.pendingInvitationSelectionList.removeInvitation(this);
205
					RealmsDataFetcher dataFetcher = RealmsPendingInvitesScreen.this.minecraft.realmsDataFetcher();
206
					if (accept) {
207
						dataFetcher.serverListUpdateTask.reset();
208
					}
209
210
					dataFetcher.pendingInvitesTask.reset();
211
				}
212
			}, RealmsPendingInvitesScreen.this.screenExecutor);
213
		}
214
	}
215
216
	@Environment(EnvType.CLIENT)
217
	private class PendingInvitationSelectionList extends ContainerObjectSelectionList<RealmsPendingInvitesScreen.Entry> {
218
		public static final int ITEM_HEIGHT = 36;
219
220
		public PendingInvitationSelectionList(final Minecraft minecraft) {
221
			super(
222
				minecraft,
223
				RealmsPendingInvitesScreen.this.width,
224
				RealmsPendingInvitesScreen.this.layout.getContentHeight(),
225
				RealmsPendingInvitesScreen.this.layout.getHeaderHeight(),
226
				36
227
			);
228
		}
229
230
		@Override
231
		public int getRowWidth() {
232
			return 280;
233
		}
234
235
		public boolean hasPendingInvites() {
236
			return this.getItemCount() == 0;
237
		}
238
239
		public void removeInvitation(final RealmsPendingInvitesScreen.Entry entry) {
240
			this.removeEntry(entry);
241
		}
242
	}
243
}
244

Decompiled Minecraft 26.3 source of RealmsPendingInvitesScreen (com/mojang/realmsclient/gui/screens/RealmsPendingInvitesScreen.java) with Mojang official mappings, stored verbatim on the site.