Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
21 579
src/client/java/com/lyricsmod/LyricsMod.java
package com.lyricsmod;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicLong;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.rendering.v1.hud.HudElementRegistry;
import net.minecraft.client.MinecraftClient;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
/** Client entrypoint and asynchronous song loading. */
public final class LyricsMod implements ClientModInitializer {
public static final LyricsPlayer PLAYER = new LyricsPlayer();
private static final AtomicLong REQUEST = new AtomicLong();
@Override public void onInitializeClient() {
ConfigManager.load();
LyricsRenderer renderer = new LyricsRenderer();
HudElementRegistry.addLast(Identifier.of("lyricsmod", "lyrics"), (context, tickCounter) -> renderer.render(context));
CommandRegistrar.register();
Keybinds.register();
}
public static void message(String text) {
MinecraftClient client = MinecraftClient.getInstance();
client.execute(() -> { if (client.player != null) client.player.sendMessage(Text.literal("[Lyrics] " + text), false); });
}
public static void action(String text) {
MinecraftClient client = MinecraftClient.getInstance();
if (client.player != null) client.player.sendMessage(Text.literal("[Lyrics] " + text), true);
}
public static void stop() { REQUEST.incrementAndGet(); PLAYER.stop(); }
private static void load(CompletableFuture<LyricSong> future) {
long request = REQUEST.incrementAndGet();
future.whenComplete((song, failure) -> MinecraftClient.getInstance().execute(() -> {
if (request != REQUEST.get()) return;
if (failure != null) {
Throwable cause = failure.getCause() == null ? failure : failure.getCause();
message("Could not load lyrics: " + cause.getMessage());
} else if (song.lines().isEmpty()) message("No lyrics found in this file.");
else { PLAYER.play(song); message("Loaded " + song.title() + (song.synced() ? "" : " (unsynced)")); }
}));
}
public static void fetch(String artist, String title) { message("Searching LRCLIB..."); load(LrclibClient.fetch(artist, title)); }
public static void play(String name) {
load(CompletableFuture.supplyAsync(() -> {
try {
Path root = ConfigManager.ROOT.resolve("songs").toAbsolutePath().normalize();
Path file = root.resolve(name.endsWith(".lrc") ? name : name + ".lrc").normalize();
if (!file.startsWith(root)) throw new IllegalArgumentException("Invalid file name");
if (Files.isRegularFile(file)) return LrcParser.parse(Files.readString(file), name);
int split = name.indexOf(" - ");
if (split < 1 || split + 3 >= name.length()) throw new IllegalArgumentException("File not found; use a filename or Artist - Title");
return LrclibClient.fetch(name.substring(0, split), name.substring(split + 3)).join();
} catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); }
}));
}
}