krisimisi71

Develop a music plugin that allows players to play songs directly from YouTube links within the server. Include features such as a command for adding/removing songs, adjustable volume settings, and permissions for who can use the music commands. Ensure the plugin supports audio streaming without causing server lag and provides a simple INVENTORY GUI for song selection.

Kodari AI
KodariApr 27 at 5:21 pm 30,419 (v2)
YouTubeMusicPlayer
A Fabric 1.21.4 mod that lets players stream audio from YouTube links in-game. Features a chest-style inventory GUI for browsing and selecting songs, commands for adding/removing tracks, per-player volume control, and permission-based access. Audio streams via VLC/VLCJ on the client side to avoid server lag. Admins manage the shared playlist; players browse and play from a clean GUI interface.

Let me read the required docs before writing any code.

YouTubeMusicPlayer
A Fabric 1.21.4 mod enabling in-game YouTube music streaming. Features a chest-style inventory GUI for browsing and playing songs, server commands for adding/removing tracks (op-level 2), per-player volume control, and permission-based access. Audio streams entirely client-side via VLCJ and yt-dlp to eliminate server lag. Server maintains a shared playlist synced to all clients on join, while playback and volume are handled independently per player.

Here’s my plan:

  • Server side: PlaylistManager persists songs to JSON, commands for add/remove/list/open, sync playlist via PlaylistSyncPayload S2C on join and mutations

  • Network: Three payloads — PlaylistSyncPayload (S2C), OpenGuiPayload (S2C), RemoveSongPayload (C2S)

  • Client side: ClientPlaylistManager holds the local copy, AudioPlayerManager uses VLCJ + yt-dlp subprocess to stream audio, MusicPlayerScreen renders a scrollable chest-style GUI with Play/Remove per song and volume controls

  • VLCJ + JNA bundled via Fabric JiJ; VLC must be installed on client machine; yt-dlp must be on PATH

The mod is fully structured with 18 files. Here’s a summary of how everything fits together:

Server side: PlaylistManager loads/saves songs to config/youtubemusic/playlist.json (Gson-based, no external dep). Commands /music add|remove|list|open are registered via CommandRegistrationCallback. On player join and every playlist mutation, PlaylistSyncPayload (S2C) pushes the full list to all clients. The RemoveSongPayload (C2S) receiver validates op level 2 before removing.

Network: Three clean CustomPayload records using PacketCodec — PlaylistSyncPayload uses a manual PacketCodec to encode a variable-length list of SongEntry tuples. OpenGuiPayload is a unit (zero-byte) packet. RemoveSongPayload carries a single song ID string.

Client side: ClientPlaylistManager holds the live synced song list. AudioPlayerManager initializes VLCJ via reflection to handle the missing-VLC case gracefully, maps volume 0–100 to VLC’s 0–200 range, and resolves YouTube URLs by spawning a yt-dlp --get-url subprocess via CompletableFuture. Direct audio URLs (mp3/m4a/ogg) skip yt-dlp. MusicPlayerScreen is a scrollable panel with per-row ▶ Play and ✕ Remove buttons, a ■ Stop button, volume ◄►controls, a scrollbar, and live now-playing status.

Requirements for players: VLC media player and yt-dlp must both be installed and on PATH for YouTube streaming to work — the GUI shows clear error banners if either is missing.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
17 1,063
src/client/java/com/youtubemusic/YouTubeMusicModClient.java
package com.youtubemusic;

import com.youtubemusic.client.ClientPlaylistManager;
import com.youtubemusic.client.audio.AudioPlayerManager;
import com.youtubemusic.client.gui.MusicPlayerScreen;
import com.youtubemusic.network.OpenGuiPayload;
import com.youtubemusic.network.PlaylistSyncPayload;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.minecraft.client.MinecraftClient;

public class YouTubeMusicModClient implements ClientModInitializer {

    @Override
    public void onInitializeClient() {
        // Receive full playlist sync from server
        ClientPlayNetworking.registerGlobalReceiver(PlaylistSyncPayload.ID, (payload, context) -> {
            ClientPlaylistManager.getInstance().updatePlaylist(payload.songs());
            // If the music GUI is currently open, refresh it
            MinecraftClient client = context.client();
            if (client.currentScreen instanceof MusicPlayerScreen screen) {
                screen.onPlaylistUpdated();
            }
        });

        // Server tells client to open the music GUI
        ClientPlayNetworking.registerGlobalReceiver(OpenGuiPayload.ID, (payload, context) -> {
            context.client().execute(() ->
                context.client().setScreen(new MusicPlayerScreen())
            );
        });

        YouTubeMusicMod.LOGGER.info("YouTube Music Player client initialized.");

        // Ensure VLCJ is eagerly initialized so first-play has minimal delay
        AudioPlayerManager.getInstance();
    }
}