package aeronicamc.mods.mxtune.network.messages;

import aeronicamc.mods.mxtune.managers.PlayIdSupplier;
import aeronicamc.mods.mxtune.sound.ClientAudio;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fml.network.NetworkEvent;

import java.util.function.Supplier;

public class PlaySoloMessage extends AbstractMessage<PlaySoloMessage>
{
    private int playId = PlayIdSupplier.INVALID;
    private int entityId;
    private String musicText = "";

    public PlaySoloMessage() { /* NOP */ }

    public PlaySoloMessage(int playId, int entityId, String musicText)
    {
        this.playId = playId;
        this.entityId = entityId;
        this.musicText = musicText;
    }

    @Override
    public void encode(PlaySoloMessage message, PacketBuffer buffer)
    {
        buffer.writeInt(message.playId);
        buffer.writeInt(message.entityId);
        buffer.writeUtf(message.musicText);
    }

    @Override
    public PlaySoloMessage decode(PacketBuffer buffer)
    {
        final int playId = buffer.readInt();
        final int entityId = buffer.readInt();
        final String mml = buffer.readUtf();
        return new PlaySoloMessage(playId, entityId, mml);
    }

    @Override
    public void handle(PlaySoloMessage message, Supplier<NetworkEvent.Context> ctx)
    {
        if (ctx.get().getDirection().getReceptionSide().isClient())
        {
            ctx.get().enqueueWork(() ->
                ClientAudio.play(message.playId, message.entityId, message.musicText));
        }
        ctx.get().setPacketHandled(true);
    }
}

@@ -172,80 +172,252 @@
 aft.
-network.PacketBuffer;%0Aimport net.minecraftforge.fml.network.NetworkEvent
+client.Minecraft;%0Aimport net.minecraft.entity.player.PlayerEntity;%0Aimport net.minecraft.network.PacketBuffer;%0Aimport net.minecraftforge.fml.network.NetworkEvent;%0Aimport org.apache.logging.log4j.LogManager;%0Aimport org.apache.logging.log4j.Logger
 ;%0A%0Ai
@@ -524,16 +524,102 @@
 sage%3E%0A%7B%0A
+    private static final Logger LOGGER = LogManager.getLogger(PlaySoloMessage.class);%0A
     priv
@@ -1629,24 +1629,414 @@
 )%0A        %7B%0A
+            assert Minecraft.getInstance().player != null;%0A            PlayerEntity sender = (PlayerEntity) Minecraft.getInstance().player.level.getEntity(message.entityId);%0A            String senderName = sender != null ? sender.getDisplayName().getString() : %22-error-%22;%0A            LOGGER.debug(%22From: %7B%7D to: %7B%7D%22, senderName, Minecraft.getInstance().player.getDisplayName().getString());%0A
             

Use PlayerEntity since this is a player capability.