NowPlayingToast .java

net.minecraft.client.gui.components.toasts.NowPlayingToast (class): members: notificationDisplayTimeMultiplier, MUSIC_COLOR_CHANGE_FREQUENCY_MS, showToast.

net.minecraft.client.gui.components.toastsclass125 linesclient4.2 KB

Implements

API Surface (12)

void extractToast(final GuiGraphicsExtractor graphics, final Font font)
void tickMusicNotes()
void showToast(final Options options)
void update(final ToastManager manager, final long fullyVisibleForMs)
void extractRenderState(final GuiGraphicsExtractor graphics, final Font font, final long fullyVisibleForMs)
void onFinishedRendering()
int width()
int height()
float xPos(final int screenWidth, final float visiblePortion)
float yPos(final int firstSlotIndex)
Toast.Visibility getWantedVisibility()
void setWantedVisibility(final Toast.Visibility visibility)

Fields & Constants (16)

NOW_PLAYING_BACKGROUND_SPRITE
MUSIC_NOTES_SPRITE
PADDING
MUSIC_NOTES_SIZE
HEIGHT
MUSIC_NOTES_SPACE
VISIBILITY_DURATION
TEXT_COLOR
MUSIC_COLOR_CHANGE_FREQUENCY_MS
musicNoteColorTick
lastMusicNoteColorChange
musicNoteColor
updateToast
notificationDisplayTimeMultiplier
minecraft
wantedVisibility

String literals (2)

"toast/now_playing"
"icon/music_notes"

Full Source

NowPlayingToast.java125 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui.components.toasts;
2
3
import net.fabricmc.api.EnvType;
4
import net.fabricmc.api.Environment;
5
import net.minecraft.client.Minecraft;
6
import net.minecraft.client.Options;
7
import net.minecraft.client.color.ColorLerper;
8
import net.minecraft.client.gui.Font;
9
import net.minecraft.client.gui.GuiGraphicsExtractor;
10
import net.minecraft.client.renderer.RenderPipelines;
11
import net.minecraft.network.chat.Component;
12
import net.minecraft.resources.Identifier;
13
import net.minecraft.world.item.DyeColor;
14
import org.jspecify.annotations.Nullable;
15
16
@Environment(EnvType.CLIENT)
17
public class NowPlayingToast implements Toast {
18
	private static final Identifier NOW_PLAYING_BACKGROUND_SPRITE = Identifier.withDefaultNamespace("toast/now_playing");
19
	private static final Identifier MUSIC_NOTES_SPRITE = Identifier.parse("icon/music_notes");
20
	private static final int PADDING = 7;
21
	private static final int MUSIC_NOTES_SIZE = 16;
22
	private static final int HEIGHT = 30;
23
	private static final int MUSIC_NOTES_SPACE = 30;
24
	private static final int VISIBILITY_DURATION = 5000;
25
	private static final int TEXT_COLOR = DyeColor.LIGHT_GRAY.getTextColor();
26
	private static final long MUSIC_COLOR_CHANGE_FREQUENCY_MS = 25L;
27
	private static int musicNoteColorTick;
28
	private static long lastMusicNoteColorChange;
29
	private static int musicNoteColor = -1;
30
	private boolean updateToast;
31
	private double notificationDisplayTimeMultiplier;
32
	private final Minecraft minecraft;
33
	private Toast.Visibility wantedVisibility = Toast.Visibility.HIDE;
34
35
	public NowPlayingToast() {
36
		this.minecraft = Minecraft.getInstance();
37
	}
38
39
	public static void extractToast(final GuiGraphicsExtractor graphics, final Font font) {
40
		String currentSong = getCurrentSongName();
41
		if (currentSong != null) {
42
			graphics.blitSprite(RenderPipelines.GUI_TEXTURED, NOW_PLAYING_BACKGROUND_SPRITE, 0, 0, getWidth(currentSong, font), 30);
43
			int notesOffset = 7;
44
			graphics.blitSprite(RenderPipelines.GUI_TEXTURED, MUSIC_NOTES_SPRITE, 7, 7, 16, 16, musicNoteColor);
45
			graphics.text(font, getNowPlayingString(currentSong), 30, 15 - 9 / 2, TEXT_COLOR);
46
		}
47
	}
48
49
	private static @Nullable String getCurrentSongName() {
50
		return Minecraft.getInstance().getMusicManager().getCurrentMusicTranslationKey();
51
	}
52
53
	public static void tickMusicNotes() {
54
		if (getCurrentSongName() != null) {
55
			long now = System.currentTimeMillis();
56
			if (now > lastMusicNoteColorChange + 25L) {
57
				musicNoteColorTick++;
58
				lastMusicNoteColorChange = now;
59
				musicNoteColor = ColorLerper.getLerpedColor(ColorLerper.Type.MUSIC_NOTE, musicNoteColorTick);
60
			}
61
		}
62
	}
63
64
	private static Component getNowPlayingString(final @Nullable String currentSongKey) {
65
		return currentSongKey == null ? Component.empty() : Component.translatable(currentSongKey.replace("/", "."));
66
	}
67
68
	public void showToast(final Options options) {
69
		this.updateToast = true;
70
		this.notificationDisplayTimeMultiplier = options.notificationDisplayTime().get();
71
		this.setWantedVisibility(Toast.Visibility.SHOW);
72
	}
73
74
	@Override
75
	public void update(final ToastManager manager, final long fullyVisibleForMs) {
76
		if (this.updateToast) {
77
			this.wantedVisibility = fullyVisibleForMs < 5000.0 * this.notificationDisplayTimeMultiplier ? Toast.Visibility.SHOW : Toast.Visibility.HIDE;
78
			tickMusicNotes();
79
		}
80
	}
81
82
	@Override
83
	public void extractRenderState(final GuiGraphicsExtractor graphics, final Font font, final long fullyVisibleForMs) {
84
		extractToast(graphics, font);
85
	}
86
87
	@Override
88
	public void onFinishedRendering() {
89
		this.updateToast = false;
90
	}
91
92
	@Override
93
	public int width() {
94
		return getWidth(getCurrentSongName(), this.minecraft.font);
95
	}
96
97
	private static int getWidth(final @Nullable String currentSong, final Font font) {
98
		return 30 + font.width(getNowPlayingString(currentSong)) + 7;
99
	}
100
101
	@Override
102
	public int height() {
103
		return 30;
104
	}
105
106
	@Override
107
	public float xPos(final int screenWidth, final float visiblePortion) {
108
		return this.width() * visiblePortion - this.width();
109
	}
110
111
	@Override
112
	public float yPos(final int firstSlotIndex) {
113
		return 0.0F;
114
	}
115
116
	@Override
117
	public Toast.Visibility getWantedVisibility() {
118
		return this.wantedVisibility;
119
	}
120
121
	public void setWantedVisibility(final Toast.Visibility visibility) {
122
		this.wantedVisibility = visibility;
123
	}
124
}
125

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