1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-07-13 23:42:53 +00:00
Jonathan Coates 9cb7a5bec7
Track owning entity when sending sounds
This allows us to sync the position to the entity immediately, rather
than the sound jumping about.

Someone has set up rick-rolling pocket computers (<3 to whoever did
this), and the lag on them irritates me enough to fix this.

Fixes #1074
2022-04-28 19:59:31 +01:00

77 lines
1.9 KiB
Java

/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2022. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.client.sound;
import dan200.computercraft.shared.peripheral.speaker.SpeakerPosition;
import net.minecraft.client.audio.IAudioStream;
import net.minecraft.client.audio.ITickableSound;
import net.minecraft.client.audio.LocatableSound;
import net.minecraft.client.audio.SoundSource;
import net.minecraft.entity.Entity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundCategory;
import javax.annotation.Nullable;
import java.util.concurrent.Executor;
public class SpeakerSound extends LocatableSound implements ITickableSound
{
SoundSource source;
Executor executor;
DfpwmStream stream;
private Entity entity;
private boolean stopped = false;
SpeakerSound( ResourceLocation sound, DfpwmStream stream, SpeakerPosition position, float volume, float pitch )
{
super( sound, SoundCategory.RECORDS );
setPosition( position );
this.stream = stream;
this.volume = volume;
this.pitch = pitch;
attenuation = AttenuationType.LINEAR;
}
void setPosition( SpeakerPosition position )
{
x = position.position().x;
y = position.position().y;
z = position.position().z;
entity = position.entity();
}
@Override
public boolean isStopped()
{
return stopped;
}
@Override
public void tick()
{
if( entity == null ) return;
if( !entity.isAlive() )
{
stopped = true;
looping = false;
}
else
{
x = entity.getX();
y = entity.getY();
z = entity.getZ();
}
}
@Nullable
public IAudioStream getStream()
{
return stream;
}
}