Files
src
build.gradle
gradle.properties
manifest.kod
README.md
settings.gradle
Code
15 1,318
src/client/java/de/superlyrics/client/SuperLyricsClient.java
package de.superlyrics.client;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.fabric.api.client.rendering.v1.hud.HudElementRegistry;
import net.fabricmc.fabric.api.client.rendering.v1.hud.VanillaHudElements;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import org.lwjgl.glfw.GLFW;
import java.nio.file.Path;
public final class SuperLyricsClient implements ClientModInitializer {
private static KeyBinding toggleKey;
private static KeyBinding nextKey;
private static KeyBinding stopKey;
private static KeyBinding songsKey;
private static TrackManager tracks;
@Override
public void onInitializeClient() {
Path musicDir = MinecraftClient.getInstance().runDirectory.toPath().resolve("superlyrics");
tracks = new TrackManager(musicDir);
KeyBinding.Category category = KeyBinding.Category.create(Identifier.of("superlyrics", "main"));
toggleKey = KeyBindingHelper.registerKeyBinding(new KeyBinding(
"key.superlyrics.toggle", GLFW.GLFW_KEY_F8, category));
nextKey = KeyBindingHelper.registerKeyBinding(new KeyBinding(
"key.superlyrics.next", GLFW.GLFW_KEY_F9, category));
stopKey = KeyBindingHelper.registerKeyBinding(new KeyBinding(
"key.superlyrics.stop", GLFW.GLFW_KEY_F10, category));
songsKey = KeyBindingHelper.registerKeyBinding(new KeyBinding(
"key.superlyrics.songs", GLFW.GLFW_KEY_F7, category));
ClientTickEvents.END_CLIENT_TICK.register(client -> {
while (toggleKey.wasPressed()) tracks.toggle();
while (nextKey.wasPressed()) tracks.next();
while (stopKey.wasPressed()) tracks.stop();
while (songsKey.wasPressed()) client.setScreen(new SongListScreen(tracks));
});
HudElementRegistry.attachElementBefore(
VanillaHudElements.CHAT,
Identifier.of("superlyrics", "music_hud"),
SuperLyricsClient::renderHud
);
tracks.ensureDirectory();
}
private static void renderHud(net.minecraft.client.gui.DrawContext context,
net.minecraft.client.render.RenderTickCounter tickCounter) {
MinecraftClient client = MinecraftClient.getInstance();
if (client.player == null || client.options.hudHidden || tracks == null) return;
if (!tracks.isHudVisible()) return;
int width = context.getScaledWindowWidth();
String title = tracks.getCurrentTitle();
String lyric = tracks.getCurrentLyric();
boolean hasTitle = title != null && !title.isBlank();
boolean hasLyric = lyric != null && !lyric.isBlank();
if (!hasTitle && !hasLyric) return;
int maxPanelWidth = Math.max(1, Math.min(360, width - 24));
int maxTextWidth = Math.max(1, maxPanelWidth - 24);
String shownTitle = hasTitle ? fitText(title, maxTextWidth, client) : null;
String shownLyric = hasLyric ? fitText(lyric, maxTextWidth, client) : null;
int textWidth = Math.max(
shownTitle == null ? 0 : client.textRenderer.getWidth(shownTitle),
shownLyric == null ? 0 : client.textRenderer.getWidth(shownLyric)
);
int panelWidth = Math.min(maxPanelWidth, Math.max(120, textWidth + 24));
int panelHeight = hasTitle && hasLyric ? 44 : 28;
int x = (width - panelWidth) / 2;
int y = 12;
context.fill(x, y, x + panelWidth, y + panelHeight, 0xDD11121E);
context.fill(x, y, x + panelWidth / 2, y + 2, 0xFFFF75C8);
context.fill(x + panelWidth / 2, y, x + panelWidth, y + 2, 0xFF69CFFF);
if (hasTitle) {
context.drawTextWithShadow(client.textRenderer, Text.literal(shownTitle),
x + 12, y + (hasLyric ? 8 : 10), 0xFFFFA3D9);
}
if (hasLyric) {
if (hasTitle) context.fill(x + 12, y + 23, x + panelWidth - 12, y + 24, 0x553F84A5);
context.drawTextWithShadow(client.textRenderer, Text.literal(shownLyric),
x + 12, y + (hasTitle ? 28 : 10), 0xFFB6E8FF);
}
}
private static String fitText(String text, int maxWidth, MinecraftClient client) {
String shown = text;
while (client.textRenderer.getWidth(shown + "…") > maxWidth && shown.length() > 1) {
shown = shown.substring(0, shown.length() - 1);
}
return shown.equals(text) ? shown : shown + "…";
}
}