RealmsPlayersTab .java

com.mojang.realmsclient.gui.screens.configuration.RealmsPlayersTab (class): members: InvitedObjectSelectionList, calculateListHeight, QUESTION_TITLE, font.

com.mojang.realmsclient.gui.screens.configurationclass322 linescommonextends GridLayoutTab13.4 KB

Implements

API Surface (12)

int calculateListHeight()
void doLayout(final ScreenRectangle screenRectangle)
void updateData(final RealmsServer serverData)
void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a)
List<? extends NarratableEntry> narratables()
List<? extends GuiEventListener> children()
void extractListBackground(final GuiGraphicsExtractor graphics)
void extractListSeparators(final GuiGraphicsExtractor graphics)
int getRowWidth()
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)

Fields & Constants (9)

LOGGER
TITLE
QUESTION_TITLE
PADDING
configurationScreen
minecraft
font
serverData
invitedList

String literals (16)

"mco.configure.world.players.title"
"mco.question"
"mco.configure.world.buttons.invite"
"mco.configure.world.invited.number"
"mco.configure.world.invites.normal.tooltip"
"mco.configure.world.invites.ops.tooltip"
"mco.configure.world.invites.remove.tooltip"
"player_list/make_operator"
"player_list/remove_operator"
"player_list/remove_player"
"mco.invited.player.narration"
"narration.cycle_button.usage.focused"
"Couldn't op the user"
"Couldn't deop the user"
"Couldn't uninvite user"
"mco.configure.world.uninvite.player"

Full Source

RealmsPlayersTab.java322 lines · Minecraft 26.3 · verbatim
1
package com.mojang.realmsclient.gui.screens.configuration;
2
3
import com.google.common.collect.ImmutableList;
4
import com.mojang.logging.LogUtils;
5
import com.mojang.realmsclient.dto.Ops;
6
import com.mojang.realmsclient.dto.PlayerInfo;
7
import com.mojang.realmsclient.dto.RealmsServer;
8
import com.mojang.realmsclient.gui.screens.RealmsConfirmScreen;
9
import com.mojang.realmsclient.util.RealmsUtil;
10
import java.util.List;
11
import java.util.UUID;
12
import net.fabricmc.api.EnvType;
13
import net.fabricmc.api.Environment;
14
import net.minecraft.ChatFormatting;
15
import net.minecraft.client.Minecraft;
16
import net.minecraft.client.gui.Font;
17
import net.minecraft.client.gui.GuiGraphicsExtractor;
18
import net.minecraft.client.gui.components.Button;
19
import net.minecraft.client.gui.components.ContainerObjectSelectionList;
20
import net.minecraft.client.gui.components.FocusableTextWidget;
21
import net.minecraft.client.gui.components.SpriteIconButton;
22
import net.minecraft.client.gui.components.events.GuiEventListener;
23
import net.minecraft.client.gui.components.tabs.GridLayoutTab;
24
import net.minecraft.client.gui.layouts.GridLayout;
25
import net.minecraft.client.gui.layouts.LayoutSettings;
26
import net.minecraft.client.gui.layouts.LinearLayout;
27
import net.minecraft.client.gui.narration.NarratableEntry;
28
import net.minecraft.client.gui.navigation.ScreenRectangle;
29
import net.minecraft.network.chat.CommonComponents;
30
import net.minecraft.network.chat.Component;
31
import net.minecraft.resources.Identifier;
32
import org.slf4j.Logger;
33
34
@Environment(EnvType.CLIENT)
35
public class RealmsPlayersTab extends GridLayoutTab implements RealmsConfigurationTab {
36
	private static final Logger LOGGER = LogUtils.getLogger();
37
	public static final Component TITLE = Component.translatable("mco.configure.world.players.title");
38
	private static final Component QUESTION_TITLE = Component.translatable("mco.question");
39
	private static final int PADDING = 8;
40
	private final RealmsConfigureWorldScreen configurationScreen;
41
	private final Minecraft minecraft;
42
	private final Font font;
43
	private RealmsServer serverData;
44
	private final RealmsPlayersTab.InvitedObjectSelectionList invitedList;
45
46
	public RealmsPlayersTab(final RealmsConfigureWorldScreen configurationScreen, final Minecraft minecraft, final RealmsServer serverData) {
47
		super(TITLE);
48
		this.configurationScreen = configurationScreen;
49
		this.minecraft = minecraft;
50
		this.font = configurationScreen.getFont();
51
		this.serverData = serverData;
52
		GridLayout.RowHelper helper = this.layout.spacing(8).createRowHelper(1);
53
		this.invitedList = helper.addChild(
54
			new RealmsPlayersTab.InvitedObjectSelectionList(configurationScreen.width, this.calculateListHeight()),
55
			LayoutSettings.defaults().alignVerticallyTop().alignHorizontallyCenter()
56
		);
57
		LinearLayout actions = helper.addChild(LinearLayout.horizontal().spacing(8), LayoutSettings.defaults().alignVerticallyBottom().alignHorizontallyCenter());
58
		actions.addChild(
59
			Button.builder(
60
					Component.translatable("mco.configure.world.buttons.invite"), var3 -> minecraft.gui.setScreen(new RealmsInviteScreen(configurationScreen, serverData))
61
				)
62
				.build()
63
		);
64
		actions.addChild(
65
			Button.builder(RealmsInviteCodesScreen.TITLE, var3 -> minecraft.gui.setScreen(new RealmsInviteCodesScreen(configurationScreen, serverData))).build()
66
		);
67
		this.updateData(serverData);
68
	}
69
70
	public int calculateListHeight() {
71
		return this.configurationScreen.getContentHeight() - 20 - 16;
72
	}
73
74
	@Override
75
	public void doLayout(final ScreenRectangle screenRectangle) {
76
		this.invitedList.updateSizeAndPosition(this.configurationScreen.width, this.calculateListHeight(), this.invitedList.getX(), this.invitedList.getY());
77
		super.doLayout(screenRectangle);
78
	}
79
80
	@Override
81
	public void updateData(final RealmsServer serverData) {
82
		this.serverData = serverData;
83
		this.invitedList.updateList(serverData);
84
	}
85
86
	@Environment(EnvType.CLIENT)
87
	private abstract static class Entry extends ContainerObjectSelectionList.Entry<RealmsPlayersTab.Entry> {
88
	}
89
90
	@Environment(EnvType.CLIENT)
91
	private class HeaderEntry extends RealmsPlayersTab.Entry {
92
		private String cachedNumberOfInvites = "";
93
		private final FocusableTextWidget invitedWidget;
94
95
		public HeaderEntry() {
96
			Component invitedText = Component.translatable("mco.configure.world.invited.number", "").withStyle(ChatFormatting.UNDERLINE);
97
			this.invitedWidget = FocusableTextWidget.builder(invitedText, RealmsPlayersTab.this.font)
98
				.alwaysShowBorder(false)
99
				.backgroundFill(FocusableTextWidget.BackgroundFill.ON_FOCUS)
100
				.build();
101
		}
102
103
		@Override
104
		public void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a) {
105
			String numberOfInvites = RealmsPlayersTab.this.serverData.players != null ? Integer.toString(RealmsPlayersTab.this.serverData.players.size()) : "0";
106
			if (!numberOfInvites.equals(this.cachedNumberOfInvites)) {
107
				this.cachedNumberOfInvites = numberOfInvites;
108
				Component invitedComponent = Component.translatable("mco.configure.world.invited.number", numberOfInvites).withStyle(ChatFormatting.UNDERLINE);
109
				this.invitedWidget.setMessage(invitedComponent);
110
			}
111
112
			this.invitedWidget
113
				.setPosition(
114
					RealmsPlayersTab.this.invitedList.getRowLeft() + RealmsPlayersTab.this.invitedList.getRowWidth() / 2 - this.invitedWidget.getWidth() / 2,
115
					this.getY() + this.getHeight() / 2 - this.invitedWidget.getHeight() / 2
116
				);
117
			this.invitedWidget.extractRenderState(graphics, mouseX, mouseY, a);
118
		}
119
120
		private int height(final int lineHeight) {
121
			return lineHeight + this.invitedWidget.getPadding() * 2;
122
		}
123
124
		@Override
125
		public List<? extends NarratableEntry> narratables() {
126
			return List.of(this.invitedWidget);
127
		}
128
129
		@Override
130
		public List<? extends GuiEventListener> children() {
131
			return List.of(this.invitedWidget);
132
		}
133
	}
134
135
	@Environment(EnvType.CLIENT)
136
	private class InvitedObjectSelectionList extends ContainerObjectSelectionList<RealmsPlayersTab.Entry> {
137
		private static final int PLAYER_ENTRY_HEIGHT = 36;
138
139
		public InvitedObjectSelectionList(final int width, final int height) {
140
			super(Minecraft.getInstance(), width, height, RealmsPlayersTab.this.configurationScreen.getHeaderHeight(), 36);
141
		}
142
143
		private void updateList(final RealmsServer serverData) {
144
			this.clearEntries();
145
			this.populateList(serverData);
146
		}
147
148
		private void populateList(final RealmsServer serverData) {
149
			RealmsPlayersTab.HeaderEntry entry = RealmsPlayersTab.this.new HeaderEntry();
150
			this.addEntry(entry, entry.height(9));
151
152
			for (RealmsPlayersTab.PlayerEntry newChild : serverData.players.stream().map(x$0 -> RealmsPlayersTab.this.new PlayerEntry(x$0)).toList()) {
153
				this.addEntry(newChild);
154
			}
155
		}
156
157
		@Override
158
		protected void extractListBackground(final GuiGraphicsExtractor graphics) {
159
		}
160
161
		@Override
162
		protected void extractListSeparators(final GuiGraphicsExtractor graphics) {
163
		}
164
165
		@Override
166
		public int getRowWidth() {
167
			return 300;
168
		}
169
	}
170
171
	@Environment(EnvType.CLIENT)
172
	private class PlayerEntry extends RealmsPlayersTab.Entry {
173
		protected static final int SKIN_FACE_SIZE = 32;
174
		private static final Component NORMAL_USER_TEXT = Component.translatable("mco.configure.world.invites.normal.tooltip");
175
		private static final Component OP_TEXT = Component.translatable("mco.configure.world.invites.ops.tooltip");
176
		private static final Component REMOVE_TEXT = Component.translatable("mco.configure.world.invites.remove.tooltip");
177
		private static final Identifier MAKE_OP_SPRITE = Identifier.withDefaultNamespace("player_list/make_operator");
178
		private static final Identifier REMOVE_OP_SPRITE = Identifier.withDefaultNamespace("player_list/remove_operator");
179
		private static final Identifier REMOVE_PLAYER_SPRITE = Identifier.withDefaultNamespace("player_list/remove_player");
180
		private static final int ICON_WIDTH = 8;
181
		private static final int ICON_HEIGHT = 7;
182
		private final PlayerInfo playerInfo;
183
		private final Button removeButton;
184
		private final Button makeOpButton;
185
		private final Button removeOpButton;
186
187
		public PlayerEntry(final PlayerInfo playerInfo) {
188
			this.playerInfo = playerInfo;
189
			int index = RealmsPlayersTab.this.serverData.players.indexOf(this.playerInfo);
190
			this.makeOpButton = SpriteIconButton.builder(NORMAL_USER_TEXT, button -> this.op(index), false)
191
				.sprite(MAKE_OP_SPRITE, 8, 7)
192
				.width(16 + RealmsPlayersTab.this.configurationScreen.getFont().width(NORMAL_USER_TEXT))
193
				.narration(
194
					defaultNarrationSupplier -> CommonComponents.joinForNarration(
195
						Component.translatable("mco.invited.player.narration", playerInfo.name),
196
						defaultNarrationSupplier.get(),
197
						Component.translatable("narration.cycle_button.usage.focused", OP_TEXT)
198
					)
199
				)
200
				.build();
201
			this.removeOpButton = SpriteIconButton.builder(OP_TEXT, button -> this.deop(index), false)
202
				.sprite(REMOVE_OP_SPRITE, 8, 7)
203
				.width(16 + RealmsPlayersTab.this.configurationScreen.getFont().width(OP_TEXT))
204
				.narration(
205
					defaultNarrationSupplier -> CommonComponents.joinForNarration(
206
						Component.translatable("mco.invited.player.narration", playerInfo.name),
207
						defaultNarrationSupplier.get(),
208
						Component.translatable("narration.cycle_button.usage.focused", NORMAL_USER_TEXT)
209
					)
210
				)
211
				.build();
212
			this.removeButton = SpriteIconButton.builder(REMOVE_TEXT, button -> this.uninvite(index), false)
213
				.sprite(REMOVE_PLAYER_SPRITE, 8, 7)
214
				.width(16 + RealmsPlayersTab.this.configurationScreen.getFont().width(REMOVE_TEXT))
215
				.narration(
216
					defaultNarrationSupplier -> CommonComponents.joinForNarration(
217
						Component.translatable("mco.invited.player.narration", playerInfo.name), defaultNarrationSupplier.get()
218
					)
219
				)
220
				.build();
221
			this.updateOpButtons();
222
		}
223
224
		private void op(final int index) {
225
			UUID selectedInvite = RealmsPlayersTab.this.serverData.players.get(index).uuid;
226
			RealmsUtil.<Ops>supplyAsync(
227
					client -> client.op(RealmsPlayersTab.this.serverData.id, selectedInvite), e -> RealmsPlayersTab.LOGGER.error("Couldn't op the user", e)
228
				)
229
				.thenAcceptAsync(ops -> {
230
					this.updateOps(ops);
231
					this.updateOpButtons();
232
					this.setFocused(this.removeOpButton);
233
				}, RealmsPlayersTab.this.minecraft);
234
		}
235
236
		private void deop(final int index) {
237
			UUID selectedInvite = RealmsPlayersTab.this.serverData.players.get(index).uuid;
238
			RealmsUtil.<Ops>supplyAsync(
239
					client -> client.deop(RealmsPlayersTab.this.serverData.id, selectedInvite), e -> RealmsPlayersTab.LOGGER.error("Couldn't deop the user", e)
240
				)
241
				.thenAcceptAsync(ops -> {
242
					this.updateOps(ops);
243
					this.updateOpButtons();
244
					this.setFocused(this.makeOpButton);
245
				}, RealmsPlayersTab.this.minecraft);
246
		}
247
248
		private void uninvite(final int index) {
249
			if (index >= 0 && index < RealmsPlayersTab.this.serverData.players.size()) {
250
				PlayerInfo playerInfo = RealmsPlayersTab.this.serverData.players.get(index);
251
				RealmsConfirmScreen confirmScreen = new RealmsConfirmScreen(
252
					result -> {
253
						if (result) {
254
							RealmsUtil.runAsync(
255
								client -> client.uninvite(RealmsPlayersTab.this.serverData.id, playerInfo.uuid), e -> RealmsPlayersTab.LOGGER.error("Couldn't uninvite user", e)
256
							);
257
							RealmsPlayersTab.this.serverData.players.remove(index);
258
							RealmsPlayersTab.this.updateData(RealmsPlayersTab.this.serverData);
259
						}
260
261
						RealmsPlayersTab.this.minecraft.gui.setScreen(RealmsPlayersTab.this.configurationScreen);
262
					},
263
					RealmsPlayersTab.QUESTION_TITLE,
264
					Component.translatable("mco.configure.world.uninvite.player", playerInfo.name)
265
				);
266
				RealmsPlayersTab.this.minecraft.gui.setScreen(confirmScreen);
267
			}
268
		}
269
270
		private void updateOps(final Ops ops) {
271
			for (PlayerInfo playerInfo : RealmsPlayersTab.this.serverData.players) {
272
				playerInfo.operator = ops.ops().contains(playerInfo.name);
273
			}
274
		}
275
276
		private void updateOpButtons() {
277
			this.makeOpButton.visible = !this.playerInfo.operator;
278
			this.removeOpButton.visible = !this.makeOpButton.visible;
279
		}
280
281
		private Button activeOpButton() {
282
			return this.makeOpButton.visible ? this.makeOpButton : this.removeOpButton;
283
		}
284
285
		@Override
286
		public List<? extends GuiEventListener> children() {
287
			return ImmutableList.of(this.activeOpButton(), this.removeButton);
288
		}
289
290
		@Override
291
		public List<? extends NarratableEntry> narratables() {
292
			return ImmutableList.of(this.activeOpButton(), this.removeButton);
293
		}
294
295
		@Override
296
		public void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a) {
297
			int inviteColor;
298
			if (!this.playerInfo.accepted) {
299
				inviteColor = -6250336;
300
			} else if (this.playerInfo.online) {
301
				inviteColor = -16711936;
302
			} else {
303
				inviteColor = -1;
304
			}
305
306
			int skinYPos = this.getContentYMiddle() - 16;
307
			RealmsUtil.extractPlayerFace(graphics, this.getContentX(), skinYPos, 32, this.playerInfo.uuid);
308
			int textYPos = this.getContentYMiddle() - 9 / 2;
309
			graphics.text(RealmsPlayersTab.this.font, this.playerInfo.name, this.getContentX() + 8 + 32, textYPos, inviteColor);
310
			int iconYPos = this.getContentYMiddle() - 10;
311
			int removeButtonXPos = this.getContentRight() - this.removeButton.getWidth();
312
			this.removeButton.setPosition(removeButtonXPos, iconYPos);
313
			this.removeButton.extractRenderState(graphics, mouseX, mouseY, a);
314
			int opButtonXPos = removeButtonXPos - this.activeOpButton().getWidth() - 8;
315
			this.makeOpButton.setPosition(opButtonXPos, iconYPos);
316
			this.makeOpButton.extractRenderState(graphics, mouseX, mouseY, a);
317
			this.removeOpButton.setPosition(opButtonXPos, iconYPos);
318
			this.removeOpButton.extractRenderState(graphics, mouseX, mouseY, a);
319
		}
320
	}
321
}
322

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