Files
lyrics_examples
src
.gitignore
build.gradle
config_spotify_sky_example.json
gradle.properties
LICENSE
README_PL.md
settings.gradle
Code
14 770
src/main/java/pl/spotify/sky/SpotifySkyClient.java
package pl.spotify.sky;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.command.v2.ClientCommands;
import net.fabricmc.fabric.api.client.rendering.v1.hud.HudElementRegistry;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphicsExtractor;
import net.minecraft.resources.Identifier;
import net.minecraft.network.chat.Component;
/** A client-only overlay placed in the sky region of the view. No server permissions needed. */
public final class SpotifySkyClient implements ClientModInitializer {
private SkyConfig config;
private SpotifyConnect spotify;
@Override
public void onInitializeClient() {
config = SkyConfig.load();
spotify = new SpotifyConnect(config);
ClientLifecycleEvents.CLIENT_STOPPING.register(client -> spotify.close());
HudElementRegistry.addLast(Identifier.fromNamespaceAndPath("spotify_sky", "floating_text"),
(graphics, delta) -> drawSky(graphics));
ClientCommandRegistrationCallback.EVENT.register((dispatcher, buildContext) -> {
dispatcher.register(ClientCommands.literal("spotifysky")
.executes(ctx -> {
ctx.getSource().sendFeedback(Component.literal("/spotifysky connect | status | show | hide | disconnect | reload | lyricsreload"));
return 1;
})
.then(ClientCommands.literal("connect").executes(ctx -> {
spotify.connect();
ctx.getSource().sendFeedback(Component.literal("[Spotify Sky] Rozpoczynam logowanie..."));
return 1;
}))
.then(ClientCommands.literal("status").executes(ctx -> {
SpotifyConnect.Playing track = spotify.current();
ctx.getSource().sendFeedback(Component.literal("[Spotify Sky] " + spotify.status()
+ (track.id().isEmpty() ? "" : " | ID utworu: " + track.id())
+ (spotify.hasLyrics() ? " | LRC wczytane" : " | brak LRC")));
return 1;
}))
.then(ClientCommands.literal("show").executes(ctx -> {
spotify.show(true);
ctx.getSource().sendFeedback(Component.literal("[Spotify Sky] Pokazuję napisy"));
return 1;
}))
.then(ClientCommands.literal("hide").executes(ctx -> {
spotify.show(false);
ctx.getSource().sendFeedback(Component.literal("[Spotify Sky] Ukrywam napisy"));
return 1;
}))
.then(ClientCommands.literal("disconnect").executes(ctx -> {
spotify.disconnect();
ctx.getSource().sendFeedback(Component.literal("[Spotify Sky] Rozłączam Spotify"));
return 1;
}))
.then(ClientCommands.literal("reload").executes(ctx -> {
config = SkyConfig.load();
spotify.setConfig(config);
ctx.getSource().sendFeedback(Component.literal("[Spotify Sky] Wczytano config/spotify_sky.json"));
return 1;
}))
.then(ClientCommands.literal("lyricsreload").executes(ctx -> {
spotify.reloadLyrics();
ctx.getSource().sendFeedback(Component.literal("[Spotify Sky] Odświeżam lokalny plik LRC"));
return 1;
}))
);
});
}
private void drawSky(GuiGraphicsExtractor graphics) {
Minecraft client = Minecraft.getInstance();
if (client.player == null || client.level == null || !config.enabled || !spotify.isShown()) return;
if (config.onlyWhenLookingUp && client.player.getXRot() > -8.0f) return;
SpotifyConnect.Playing track = spotify.current();
if (!track.playing() || track.title().isBlank()) return;
int centerX = client.getWindow().getGuiScaledWidth() / 2;
int y = Math.round(client.getWindow().getGuiScaledHeight() * config.heightFraction);
var page = config.showLyrics ? spotify.lyricPage(config.lyricLinesPerPage)
: java.util.List.<TimedLyrics.DisplayLine>of();
if (!page.isEmpty()) {
// Entire 3-line verse is displayed; active line is larger and brighter.
int step = 31;
for (int i = 0; i < page.size(); i++) {
var line = page.get(i);
int color = line.active() ? config.lyricColor : config.upcomingLyricColor;
float size = line.active() ? 1.60f : 1.33f;
skyLine(graphics, client, line.text(), centerX, y + i * step, color, size);
}
if (config.showTitle) {
skyLine(graphics, client, track.title() + " - " + track.artist(), centerX,
y + page.size() * step + 10, config.titleColor, 0.85f);
}
} else if (config.showTitle) {
// Spotify does not provide the text of the song. No LRC => title only.
skyLine(graphics, client, track.title(), centerX, y, config.titleColor, 1.75f);
if (!track.artist().isBlank()) skyLine(graphics, client, track.artist(), centerX, y + 30,
config.lyricColor, 1.12f);
}
}
private void skyLine(GuiGraphicsExtractor graphics, Minecraft client, String input,
int centerX, int y, int color, float requestedSize) {
// Limit length and shrink long lines to avoid overflowing the game window.
String text = input.length() > 160 ? input.substring(0, 157) + "..." : input;
int width = client.font.width(text);
float allowed = client.getWindow().getGuiScaledWidth() * 0.91f;
float size = Math.min(requestedSize, allowed / Math.max(1, width));
graphics.pose().pushMatrix();
graphics.pose().scale(size, size);
graphics.text(client.font, text,
Math.round(centerX / size - width / 2f), Math.round(y / size), color, true);
graphics.pose().popMatrix();
}
}