PlayerTabOverlay .java

net.minecraft.client.gui.components.PlayerTabOverlay (class): members: HEART_ABSORBING_FULL_BLINKING_SPRITE, HEART_ABSORBING_HALF_BLINKING_SPRITE, hud.

net.minecraft.client.gui.componentsclass384 linesclient15.4 KB

API Surface (10)

Component getNameForDisplay(final PlayerInfo info)
void setVisible(final boolean visible)
void extractRenderState(final GuiGraphicsExtractor graphics, final int screenWidth, final Scoreboard scoreboard, final @Nullable Objective displayObjective)
void extractPingIcon(final GuiGraphicsExtractor graphics, final int slotWidth, final int xo, final int yo, final PlayerInfo info)
void setFooter(final @Nullable Component footer)
void setHeader(final @Nullable Component header)
void reset()
void update(final int value, final long tick)
int displayedValue()
boolean isBlinking(final long tick)

Fields & Constants (29)

PING_UNKNOWN_SPRITE
PING_1_SPRITE
PING_2_SPRITE
PING_3_SPRITE
PING_4_SPRITE
PING_5_SPRITE
HEART_CONTAINER_BLINKING_SPRITE
HEART_CONTAINER_SPRITE
HEART_FULL_BLINKING_SPRITE
HEART_HALF_BLINKING_SPRITE
HEART_ABSORBING_FULL_BLINKING_SPRITE
HEART_FULL_SPRITE
HEART_ABSORBING_HALF_BLINKING_SPRITE
HEART_HALF_SPRITE
PLAYER_COMPARATOR
MAX_ROWS_PER_COL
minecraft
hud
footer
header
visible
healthStates
scoreboard
displayObjective
yo
entry
left
right
profileId

String literals (16)

"icon/ping_unknown"
"icon/ping_1"
"icon/ping_2"
"icon/ping_3"
"icon/ping_4"
"icon/ping_5"
"hud/heart/container_blinking"
"hud/heart/container"
"hud/heart/full_blinking"
"hud/heart/half_blinking"
"hud/heart/absorbing_full_blinking"
"hud/heart/full"
"hud/heart/absorbing_half_blinking"
"hud/heart/half"
"multiplayer.player.list.narration"
"multiplayer.player.list.hp"

Full Source

PlayerTabOverlay.java384 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui.components;
2
3
import com.mojang.authlib.GameProfile;
4
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
5
import java.util.ArrayList;
6
import java.util.Comparator;
7
import java.util.List;
8
import java.util.Map;
9
import java.util.Set;
10
import java.util.UUID;
11
import java.util.stream.Collectors;
12
import net.fabricmc.api.EnvType;
13
import net.fabricmc.api.Environment;
14
import net.minecraft.ChatFormatting;
15
import net.minecraft.Optionull;
16
import net.minecraft.client.Minecraft;
17
import net.minecraft.client.gui.GuiGraphicsExtractor;
18
import net.minecraft.client.gui.Hud;
19
import net.minecraft.client.multiplayer.PlayerInfo;
20
import net.minecraft.client.renderer.RenderPipelines;
21
import net.minecraft.client.renderer.entity.player.AvatarRenderer;
22
import net.minecraft.network.chat.Component;
23
import net.minecraft.network.chat.ComponentUtils;
24
import net.minecraft.network.chat.MutableComponent;
25
import net.minecraft.network.chat.numbers.NumberFormat;
26
import net.minecraft.network.chat.numbers.StyledFormat;
27
import net.minecraft.resources.Identifier;
28
import net.minecraft.util.ARGB;
29
import net.minecraft.util.FormattedCharSequence;
30
import net.minecraft.util.Mth;
31
import net.minecraft.world.entity.player.Player;
32
import net.minecraft.world.level.GameType;
33
import net.minecraft.world.scores.Objective;
34
import net.minecraft.world.scores.PlayerTeam;
35
import net.minecraft.world.scores.ReadOnlyScoreInfo;
36
import net.minecraft.world.scores.ScoreHolder;
37
import net.minecraft.world.scores.Scoreboard;
38
import net.minecraft.world.scores.criteria.ObjectiveCriteria.RenderType;
39
import org.jspecify.annotations.Nullable;
40
41
@Environment(EnvType.CLIENT)
42
public class PlayerTabOverlay {
43
	private static final Identifier PING_UNKNOWN_SPRITE = Identifier.withDefaultNamespace("icon/ping_unknown");
44
	private static final Identifier PING_1_SPRITE = Identifier.withDefaultNamespace("icon/ping_1");
45
	private static final Identifier PING_2_SPRITE = Identifier.withDefaultNamespace("icon/ping_2");
46
	private static final Identifier PING_3_SPRITE = Identifier.withDefaultNamespace("icon/ping_3");
47
	private static final Identifier PING_4_SPRITE = Identifier.withDefaultNamespace("icon/ping_4");
48
	private static final Identifier PING_5_SPRITE = Identifier.withDefaultNamespace("icon/ping_5");
49
	private static final Identifier HEART_CONTAINER_BLINKING_SPRITE = Identifier.withDefaultNamespace("hud/heart/container_blinking");
50
	private static final Identifier HEART_CONTAINER_SPRITE = Identifier.withDefaultNamespace("hud/heart/container");
51
	private static final Identifier HEART_FULL_BLINKING_SPRITE = Identifier.withDefaultNamespace("hud/heart/full_blinking");
52
	private static final Identifier HEART_HALF_BLINKING_SPRITE = Identifier.withDefaultNamespace("hud/heart/half_blinking");
53
	private static final Identifier HEART_ABSORBING_FULL_BLINKING_SPRITE = Identifier.withDefaultNamespace("hud/heart/absorbing_full_blinking");
54
	private static final Identifier HEART_FULL_SPRITE = Identifier.withDefaultNamespace("hud/heart/full");
55
	private static final Identifier HEART_ABSORBING_HALF_BLINKING_SPRITE = Identifier.withDefaultNamespace("hud/heart/absorbing_half_blinking");
56
	private static final Identifier HEART_HALF_SPRITE = Identifier.withDefaultNamespace("hud/heart/half");
57
	private static final Comparator<PlayerInfo> PLAYER_COMPARATOR = Comparator.<PlayerInfo>comparingInt(p -> -p.getTabListOrder())
58
		.thenComparingInt(p -> p.getGameMode() == GameType.SPECTATOR ? 1 : 0)
59
		.thenComparing(p -> Optionull.mapOrDefault(p.getTeam(), PlayerTeam::getName, ""))
60
		.thenComparing(p -> p.getProfile().name(), String::compareToIgnoreCase);
61
	public static final int MAX_ROWS_PER_COL = 20;
62
	private final Minecraft minecraft;
63
	private final Hud hud;
64
	private @Nullable Component footer;
65
	private @Nullable Component header;
66
	private boolean visible;
67
	private final Map<UUID, PlayerTabOverlay.HealthState> healthStates = new Object2ObjectOpenHashMap<>();
68
69
	public PlayerTabOverlay(final Minecraft minecraft, final Hud hud) {
70
		this.minecraft = minecraft;
71
		this.hud = hud;
72
	}
73
74
	public Component getNameForDisplay(final PlayerInfo info) {
75
		return info.getTabListDisplayName() != null
76
			? this.decorateName(info, info.getTabListDisplayName().copy())
77
			: this.decorateName(info, PlayerTeam.formatNameForTeam(info.getTeam(), Component.literal(info.getProfile().name())));
78
	}
79
80
	private Component decorateName(final PlayerInfo info, final MutableComponent name) {
81
		return info.getGameMode() == GameType.SPECTATOR ? name.withStyle(ChatFormatting.ITALIC) : name;
82
	}
83
84
	public void setVisible(final boolean visible) {
85
		if (this.visible != visible) {
86
			this.healthStates.clear();
87
			this.visible = visible;
88
			if (visible) {
89
				Component players = ComponentUtils.formatList(this.getPlayerInfos(), Component.literal(", "), this::getNameForDisplay);
90
				this.minecraft.getNarrator().saySystemNow(Component.translatable("multiplayer.player.list.narration", players));
91
			}
92
		}
93
	}
94
95
	private List<PlayerInfo> getPlayerInfos() {
96
		return this.minecraft.player.connection.getListedOnlinePlayers().stream().sorted(PLAYER_COMPARATOR).limit(80L).toList();
97
	}
98
99
	public void extractRenderState(
100
		final GuiGraphicsExtractor graphics, final int screenWidth, final Scoreboard scoreboard, final @Nullable Objective displayObjective
101
	) {
102
		List<PlayerInfo> playerInfos = this.getPlayerInfos();
103
		List<PlayerTabOverlay.ScoreDisplayEntry> entriesToDisplay = new ArrayList<>(playerInfos.size());
104
		int spacerWidth = this.minecraft.font.width(" ");
105
		int maxNameWidth = 0;
106
		int maxScoreWidth = 0;
107
108
		for (PlayerInfo info : playerInfos) {
109
			Component playerName = this.getNameForDisplay(info);
110
			maxNameWidth = Math.max(maxNameWidth, this.minecraft.font.width(playerName));
111
			int playerScore = 0;
112
			Component formattedPlayerScore = null;
113
			int playerScoreWidth = 0;
114
			if (displayObjective != null) {
115
				ScoreHolder scoreHolder = ScoreHolder.fromGameProfile(info.getProfile());
116
				ReadOnlyScoreInfo scoreInfo = scoreboard.getPlayerScoreInfo(scoreHolder, displayObjective);
117
				if (scoreInfo != null) {
118
					playerScore = scoreInfo.value();
119
				}
120
121
				if (displayObjective.getRenderType() != RenderType.HEARTS) {
122
					if (scoreInfo != null) {
123
						NumberFormat objectiveDefaultFormat = displayObjective.numberFormatOrDefault(StyledFormat.PLAYER_LIST_DEFAULT);
124
						formattedPlayerScore = scoreInfo.formatValue(objectiveDefaultFormat);
125
						playerScoreWidth = this.minecraft.font.width(formattedPlayerScore);
126
					}
127
128
					maxScoreWidth = Math.max(maxScoreWidth, playerScoreWidth > 0 ? spacerWidth + playerScoreWidth : 0);
129
				}
130
			}
131
132
			entriesToDisplay.add(new PlayerTabOverlay.ScoreDisplayEntry(playerName, playerScore, formattedPlayerScore, playerScoreWidth));
133
		}
134
135
		if (!this.healthStates.isEmpty()) {
136
			Set<UUID> playerIds = playerInfos.stream().map(player -> player.getProfile().id()).collect(Collectors.toSet());
137
			this.healthStates.keySet().removeIf(id -> !playerIds.contains(id));
138
		}
139
140
		int slots = playerInfos.size();
141
		int rows = slots;
142
143
		int cols;
144
		for (cols = 1; rows > 20; rows = (slots + cols - 1) / cols) {
145
			cols++;
146
		}
147
148
		boolean showHead = this.minecraft.getConnection().onlineMode();
149
		int widthForScore;
150
		if (displayObjective != null) {
151
			if (displayObjective.getRenderType() == RenderType.HEARTS) {
152
				widthForScore = 90;
153
			} else {
154
				widthForScore = maxScoreWidth;
155
			}
156
		} else {
157
			widthForScore = 0;
158
		}
159
160
		int slotWidth = Math.min(cols * ((showHead ? 9 : 0) + maxNameWidth + widthForScore + 13), screenWidth - 50) / cols;
161
		int xxo = screenWidth / 2 - (slotWidth * cols + (cols - 1) * 5) / 2;
162
		int yyo = 10;
163
		int maxLineWidth = slotWidth * cols + (cols - 1) * 5;
164
		List<FormattedCharSequence> headerLines = null;
165
		if (this.header != null) {
166
			headerLines = this.minecraft.font.split(this.header, screenWidth - 50);
167
168
			for (FormattedCharSequence line : headerLines) {
169
				maxLineWidth = Math.max(maxLineWidth, this.minecraft.font.width(line));
170
			}
171
		}
172
173
		List<FormattedCharSequence> footerLines = null;
174
		if (this.footer != null) {
175
			footerLines = this.minecraft.font.split(this.footer, screenWidth - 50);
176
177
			for (FormattedCharSequence line : footerLines) {
178
				maxLineWidth = Math.max(maxLineWidth, this.minecraft.font.width(line));
179
			}
180
		}
181
182
		if (headerLines != null) {
183
			graphics.fill(screenWidth / 2 - maxLineWidth / 2 - 1, yyo - 1, screenWidth / 2 + maxLineWidth / 2 + 1, yyo + headerLines.size() * 9, Integer.MIN_VALUE);
184
185
			for (FormattedCharSequence line : headerLines) {
186
				int lineWidth = this.minecraft.font.width(line);
187
				graphics.text(this.minecraft.font, line, screenWidth / 2 - lineWidth / 2, yyo, -1);
188
				yyo += 9;
189
			}
190
191
			yyo++;
192
		}
193
194
		graphics.fill(screenWidth / 2 - maxLineWidth / 2 - 1, yyo - 1, screenWidth / 2 + maxLineWidth / 2 + 1, yyo + rows * 9, Integer.MIN_VALUE);
195
		int background = this.minecraft.options.getBackgroundColor(553648127);
196
197
		for (int i = 0; i < slots; i++) {
198
			int col = i / rows;
199
			int row = i % rows;
200
			int xo = xxo + col * slotWidth + col * 5;
201
			int yo = yyo + row * 9;
202
			graphics.fill(xo, yo, xo + slotWidth, yo + 8, background);
203
			if (i < playerInfos.size()) {
204
				PlayerInfo info = playerInfos.get(i);
205
				PlayerTabOverlay.ScoreDisplayEntry displayInfo = entriesToDisplay.get(i);
206
				GameProfile profile = info.getProfile();
207
				if (showHead) {
208
					Player playerByUUID = this.minecraft.level.getPlayerByUUID(profile.id());
209
					boolean flip = playerByUUID != null && AvatarRenderer.isPlayerUpsideDown(playerByUUID);
210
					PlayerFaceExtractor.extractRenderState(graphics, info.getSkin().body().texturePath(), xo, yo, 8, info.showHat(), flip, -1);
211
					xo += 9;
212
				}
213
214
				graphics.text(this.minecraft.font, displayInfo.name, xo, yo, info.getGameMode() == GameType.SPECTATOR ? -1862270977 : -1);
215
				if (displayObjective != null && info.getGameMode() != GameType.SPECTATOR) {
216
					int left = xo + maxNameWidth + 1;
217
					int right = left + widthForScore;
218
					if (right - left > 5) {
219
						this.extractTablistScore(displayObjective, yo, displayInfo, left, right, profile.id(), graphics);
220
					}
221
				}
222
223
				this.extractPingIcon(graphics, slotWidth, xo - (showHead ? 9 : 0), yo, info);
224
			}
225
		}
226
227
		if (footerLines != null) {
228
			yyo += rows * 9 + 1;
229
			graphics.fill(screenWidth / 2 - maxLineWidth / 2 - 1, yyo - 1, screenWidth / 2 + maxLineWidth / 2 + 1, yyo + footerLines.size() * 9, Integer.MIN_VALUE);
230
231
			for (FormattedCharSequence line : footerLines) {
232
				int lineWidth = this.minecraft.font.width(line);
233
				graphics.text(this.minecraft.font, line, screenWidth / 2 - lineWidth / 2, yyo, -1);
234
				yyo += 9;
235
			}
236
		}
237
	}
238
239
	protected void extractPingIcon(final GuiGraphicsExtractor graphics, final int slotWidth, final int xo, final int yo, final PlayerInfo info) {
240
		Identifier sprite;
241
		if (info.getLatency() < 0) {
242
			sprite = PING_UNKNOWN_SPRITE;
243
		} else if (info.getLatency() < 150) {
244
			sprite = PING_5_SPRITE;
245
		} else if (info.getLatency() < 300) {
246
			sprite = PING_4_SPRITE;
247
		} else if (info.getLatency() < 600) {
248
			sprite = PING_3_SPRITE;
249
		} else if (info.getLatency() < 1000) {
250
			sprite = PING_2_SPRITE;
251
		} else {
252
			sprite = PING_1_SPRITE;
253
		}
254
255
		graphics.blitSprite(RenderPipelines.GUI_TEXTURED, sprite, xo + slotWidth - 11, yo, 10, 8);
256
	}
257
258
	private void extractTablistScore(
259
		final Objective displayObjective,
260
		final int yo,
261
		final PlayerTabOverlay.ScoreDisplayEntry entry,
262
		final int left,
263
		final int right,
264
		final UUID profileId,
265
		final GuiGraphicsExtractor graphics
266
	) {
267
		if (displayObjective.getRenderType() == RenderType.HEARTS) {
268
			this.extractTablistHearts(yo, left, right, profileId, graphics, entry.score);
269
		} else if (entry.formattedScore != null) {
270
			graphics.text(this.minecraft.font, entry.formattedScore, right - entry.scoreWidth, yo, -1);
271
		}
272
	}
273
274
	private void extractTablistHearts(final int yo, final int left, final int right, final UUID profileId, final GuiGraphicsExtractor graphics, final int score) {
275
		PlayerTabOverlay.HealthState health = this.healthStates.computeIfAbsent(profileId, id -> new PlayerTabOverlay.HealthState(score));
276
		health.update(score, this.hud.getGuiTicks());
277
		int fullHearts = Mth.positiveCeilDiv(Math.max(score, health.displayedValue()), 2);
278
		int heartsToRender = Math.max(score, Math.max(health.displayedValue(), 20)) / 2;
279
		boolean blink = health.isBlinking(this.hud.getGuiTicks());
280
		if (fullHearts > 0) {
281
			int widthPerHeart = Mth.floor(Math.min((float)(right - left - 4) / heartsToRender, 9.0F));
282
			if (widthPerHeart <= 3) {
283
				float pct = Mth.clamp(score / 20.0F, 0.0F, 1.0F);
284
				int color = (int)((1.0F - pct) * 255.0F) << 16 | (int)(pct * 255.0F) << 8;
285
				float hearts = score / 2.0F;
286
				Component hpText = Component.translatable("multiplayer.player.list.hp", hearts);
287
				Component text;
288
				if (right - this.minecraft.font.width(hpText) >= left) {
289
					text = hpText;
290
				} else {
291
					text = Component.literal(Float.toString(hearts));
292
				}
293
294
				graphics.text(this.minecraft.font, text, (right + left - this.minecraft.font.width(text)) / 2, yo, ARGB.opaque(color));
295
			} else {
296
				Identifier sprite = blink ? HEART_CONTAINER_BLINKING_SPRITE : HEART_CONTAINER_SPRITE;
297
298
				for (int heart = fullHearts; heart < heartsToRender; heart++) {
299
					graphics.blitSprite(RenderPipelines.GUI_TEXTURED, sprite, left + heart * widthPerHeart, yo, 9, 9);
300
				}
301
302
				for (int heart = 0; heart < fullHearts; heart++) {
303
					graphics.blitSprite(RenderPipelines.GUI_TEXTURED, sprite, left + heart * widthPerHeart, yo, 9, 9);
304
					if (blink) {
305
						if (heart * 2 + 1 < health.displayedValue()) {
306
							graphics.blitSprite(RenderPipelines.GUI_TEXTURED, HEART_FULL_BLINKING_SPRITE, left + heart * widthPerHeart, yo, 9, 9);
307
						}
308
309
						if (heart * 2 + 1 == health.displayedValue()) {
310
							graphics.blitSprite(RenderPipelines.GUI_TEXTURED, HEART_HALF_BLINKING_SPRITE, left + heart * widthPerHeart, yo, 9, 9);
311
						}
312
					}
313
314
					if (heart * 2 + 1 < score) {
315
						graphics.blitSprite(
316
							RenderPipelines.GUI_TEXTURED, heart >= 10 ? HEART_ABSORBING_FULL_BLINKING_SPRITE : HEART_FULL_SPRITE, left + heart * widthPerHeart, yo, 9, 9
317
						);
318
					}
319
320
					if (heart * 2 + 1 == score) {
321
						graphics.blitSprite(
322
							RenderPipelines.GUI_TEXTURED, heart >= 10 ? HEART_ABSORBING_HALF_BLINKING_SPRITE : HEART_HALF_SPRITE, left + heart * widthPerHeart, yo, 9, 9
323
						);
324
					}
325
				}
326
			}
327
		}
328
	}
329
330
	public void setFooter(final @Nullable Component footer) {
331
		this.footer = footer;
332
	}
333
334
	public void setHeader(final @Nullable Component header) {
335
		this.header = header;
336
	}
337
338
	public void reset() {
339
		this.header = null;
340
		this.footer = null;
341
	}
342
343
	@Environment(EnvType.CLIENT)
344
	private static class HealthState {
345
		private static final long DISPLAY_UPDATE_DELAY = 20L;
346
		private static final long DECREASE_BLINK_DURATION = 20L;
347
		private static final long INCREASE_BLINK_DURATION = 10L;
348
		private int lastValue;
349
		private int displayedValue;
350
		private long lastUpdateTick;
351
		private long blinkUntilTick;
352
353
		public HealthState(final int value) {
354
			this.displayedValue = value;
355
			this.lastValue = value;
356
		}
357
358
		public void update(final int value, final long tick) {
359
			if (value != this.lastValue) {
360
				long blinkDuration = value < this.lastValue ? 20L : 10L;
361
				this.blinkUntilTick = tick + blinkDuration;
362
				this.lastValue = value;
363
				this.lastUpdateTick = tick;
364
			}
365
366
			if (tick - this.lastUpdateTick > 20L) {
367
				this.displayedValue = value;
368
			}
369
		}
370
371
		public int displayedValue() {
372
			return this.displayedValue;
373
		}
374
375
		public boolean isBlinking(final long tick) {
376
			return this.blinkUntilTick > tick && (this.blinkUntilTick - tick) % 6L >= 3L;
377
		}
378
	}
379
380
	@Environment(EnvType.CLIENT)
381
	private record ScoreDisplayEntry(Component name, int score, @Nullable Component formattedScore, int scoreWidth) {
382
	}
383
}
384

Decompiled Minecraft 26.3 source of PlayerTabOverlay (net/minecraft/client/gui/components/PlayerTabOverlay.java) with Mojang official mappings, stored verbatim on the site.