package aeronicamc.mods.mxtune.sound;

import aeronicamc.mods.mxtune.config.MXTuneConfig;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.vector.Vector3d;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class MovingMusic extends MxSound
{
    private static final Logger LOGGER = LogManager.getLogger(MovingMusic.class);
    private final Minecraft mc = Minecraft.getInstance();
    private final Entity entity;
    private int counter;
    private float lastDistance;

    /**
     * Implements ISound<br></br>
     * For musical machines carried or used in the world
     * @param audioData audio data associated with the entity
     * @param entity the entity that is the source of the music
     */
    public MovingMusic(AudioData audioData, Entity entity)
    {
        super(audioData);
        this.entity = entity;
        this.stopped = false;
        this.x = (float) entity.getX();
        this.y = (float) entity.getY();
        this.z = (float) entity.getZ();
        LOGGER.debug("MovingMusic entity {}", entity.getName().getContents());
    }

    @Override
    public void onUpdate()
    {
        if (!this.entity.isAlive() || isStopped())
        {
            setDonePlaying();
            ClientAudio.queueAudioDataRemoval(playID);
            LOGGER.debug("MovingMusic playID {} done", playID);
        } else if (audioData != null && mc.player != null)
        {
            this.x = entity.getX();
            this.y = entity.getY();
            this.z = entity.getZ();

            Vector3d vec3d = new Vector3d(mc.player.getX(), mc.player.getY(), mc.player.getZ());
            float distance = (float) vec3d.distanceTo(new Vector3d(entity.getX(), entity.getY(), entity.getZ()));
            this.volumeBase = (1.0F - MathHelper.clamp(distance / MXTuneConfig.getListenerRange(), 0.0F, 1.0F));
            if ((counter++ % 20 == 0) && (distance != lastDistance))
            {
                this.lastDistance = distance;
            }
        }
    }
}

@@ -1153,16 +1153,27 @@
  entity 
+%7B%7D, playId 
 %7B%7D%22, ent
@@ -1199,16 +1199,39 @@
 ntents()
+, audioData.getPlayId()
 );%0A    %7D
@@ -1318,11 +1318,12 @@
 e() 
-%7C%7C 
+&& !
 isSt
@@ -1478,16 +1478,25 @@
  %7B%7D done
+, this %7B%7D
 %22, playI
@@ -1492,24 +1492,30 @@
  %7B%7D%22, playID
+, this
 );%0A        %7D

Fix race condition causing multiple stop logging messages.