Files
src
build.gradle
gradle.properties
settings.gradle
Code
9 803
src/client/java/dev/floatinglyrics/client/FloatingLyricsClient.java
package dev.floatinglyrics.client;
import com.mojang.brigadier.arguments.DoubleArgumentType;
import com.mojang.brigadier.arguments.IntegerArgumentType;
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.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.rendering.v1.level.LevelRenderEvents;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style;
import java.net.URI;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class FloatingLyricsClient implements ClientModInitializer {
private static final Logger LOGGER = LoggerFactory.getLogger(FloatingLyricsClient.class);
public static final SpotifyPlayback SPOTIFY = new SpotifyPlayback();
private static final String SPOTIFY_CLIENT_ID = "f918d85e08624c5dbdf8703e2f765829";
private final FloatingLyricsRenderer lyricsRenderer = new FloatingLyricsRenderer();
@Override
public void onInitializeClient() {
LevelRenderEvents.COLLECT_SUBMITS.register(lyricsRenderer::render);
LOGGER.info("Registered floating lyrics world render callback");
ClientPlayConnectionEvents.DISCONNECT.register((listener, client) -> lyricsRenderer.eliminate());
ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> dispatcher.register(
ClientCommands.literal("floatinglyrics")
.then(ClientCommands.literal("show")
.executes(context -> {
lyricsRenderer.show(context.getSource().getLevel(), context.getSource().getPosition(), 0, 1);
context.getSource().sendFeedback(Component.literal("Floating lyrics placed to the south. Use /spotify status to check playback."));
return 1;
})
.then(ClientCommands.literal("east").executes(context -> {
lyricsRenderer.show(context.getSource().getLevel(), context.getSource().getPosition(), 1, 0);
context.getSource().sendFeedback(Component.literal("Floating lyrics placed to the east."));
return 1;
}))
.then(ClientCommands.literal("west").executes(context -> {
lyricsRenderer.show(context.getSource().getLevel(), context.getSource().getPosition(), -1, 0);
context.getSource().sendFeedback(Component.literal("Floating lyrics placed to the west."));
return 1;
}))
.then(ClientCommands.literal("north").executes(context -> {
lyricsRenderer.show(context.getSource().getLevel(), context.getSource().getPosition(), 0, -1);
context.getSource().sendFeedback(Component.literal("Floating lyrics placed to the north."));
return 1;
}))
.then(ClientCommands.literal("south").executes(context -> {
lyricsRenderer.show(context.getSource().getLevel(), context.getSource().getPosition(), 0, 1);
context.getSource().sendFeedback(Component.literal("Floating lyrics placed to the south."));
return 1;
})))
.then(ClientCommands.literal("size").then(ClientCommands.argument("value", DoubleArgumentType.doubleArg(0.01, 0.3)).executes(context -> {
lyricsRenderer.setSize((float) DoubleArgumentType.getDouble(context, "value"));
context.getSource().sendFeedback(Component.literal("Floating lyrics size updated."));
return 1;
})))
.then(ClientCommands.literal("spacing").then(ClientCommands.argument("value", DoubleArgumentType.doubleArg(0.1, 20)).executes(context -> {
lyricsRenderer.setSpacing((float) DoubleArgumentType.getDouble(context, "value"));
context.getSource().sendFeedback(Component.literal("Floating lyrics spacing updated."));
return 1;
})))
.then(ClientCommands.literal("speed").then(ClientCommands.argument("value", DoubleArgumentType.doubleArg(0, 5)).executes(context -> {
lyricsRenderer.setSpeed((float) DoubleArgumentType.getDouble(context, "value"));
context.getSource().sendFeedback(Component.literal("Floating lyrics animation speed updated."));
return 1;
})))
.then(ClientCommands.literal("color1").then(ClientCommands.argument("rgb", IntegerArgumentType.integer(0, 0xffffff)).executes(context -> {
lyricsRenderer.setColorA(IntegerArgumentType.getInteger(context, "rgb"));
context.getSource().sendFeedback(Component.literal("Floating lyrics first color updated."));
return 1;
})))
.then(ClientCommands.literal("color2").then(ClientCommands.argument("rgb", IntegerArgumentType.integer(0, 0xffffff)).executes(context -> {
lyricsRenderer.setColorB(IntegerArgumentType.getInteger(context, "rgb"));
context.getSource().sendFeedback(Component.literal("Floating lyrics second color updated."));
return 1;
})))
.then(ClientCommands.literal("eliminate").executes(context -> {
lyricsRenderer.eliminate();
context.getSource().sendFeedback(Component.literal("Floating lyrics removed."));
return 1;
}))));
ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> dispatcher.register(
ClientCommands.literal("spotify")
.then(ClientCommands.literal("connect")
.executes(context -> {
var source = context.getSource();
SpotifyPlayback.Authorization authorization;
try {
authorization = SPOTIFY.beginAuthorization(SPOTIFY_CLIENT_ID);
} catch (IOException | RuntimeException e) {
source.sendError(Component.literal("Could not start Spotify callback on 127.0.0.1:8888. Check whether the port is in use."));
return 0;
}
authorization.result().whenComplete((ignored, error) -> source.getClient().execute(() -> {
if (error != null) source.sendError(Component.literal("Spotify authorization failed or was cancelled. Check the Minecraft log for token errors."));
else source.sendFeedback(Component.literal("Spotify connected. Use /spotify status to see playback."));
}));
source.sendFeedback(Component.literal("Click here to authorize Spotify")
.withStyle(Style.EMPTY.withClickEvent(new ClickEvent.OpenUrl(URI.create(authorization.url())))));
source.sendFeedback(Component.literal("After authorizing, return to Minecraft. Register http://127.0.0.1:8888/callback as your app's redirect URI."));
return 1;
}))
.then(ClientCommands.literal("status").executes(context -> {
SpotifyPlayback.Playback playback = SPOTIFY.playback();
context.getSource().sendFeedback(Component.literal(playback == null
? "No Spotify playback available. Connect first and start a track."
: (playback.playing() ? "Playing: " : "Paused: ") + playback.title() + " — " + playback.artist()));
return 1;
}))
.then(ClientCommands.literal("disconnect").executes(context -> {
SPOTIFY.disconnect();
context.getSource().sendFeedback(Component.literal("Spotify disconnected."));
return 1;
}))));
}
}
