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
This commit is contained in:
Jonathan Coates
2022-04-28 19:59:31 +01:00
parent 118b89ea41
commit 9cb7a5bec7
13 changed files with 229 additions and 111 deletions
@@ -6,11 +6,11 @@
package dan200.computercraft.client.sound;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.shared.peripheral.speaker.SpeakerPosition;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.client.audio.SoundHandler;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.vector.Vector3d;
/**
* An instance of a speaker, which is either playing a {@link DfpwmStream} stream or a normal sound.
@@ -44,7 +44,7 @@ public class SpeakerInstance
}
}
public void playAudio( Vector3d position, float volume )
public void playAudio( SpeakerPosition position, float volume )
{
SoundHandler soundManager = Minecraft.getInstance().getSoundManager();
@@ -63,7 +63,7 @@ public class SpeakerInstance
}
}
public void playSound( Vector3d position, ResourceLocation location, float volume, float pitch )
public void playSound( SpeakerPosition position, ResourceLocation location, float volume, float pitch )
{
SoundHandler soundManager = Minecraft.getInstance().getSoundManager();
currentStream = null;
@@ -78,7 +78,7 @@ public class SpeakerInstance
soundManager.play( sound );
}
void setPosition( Vector3d position )
void setPosition( SpeakerPosition position )
{
if( sound != null ) sound.setPosition( position );
}
@@ -5,7 +5,7 @@
*/
package dan200.computercraft.client.sound;
import net.minecraft.util.math.vector.Vector3d;
import dan200.computercraft.shared.peripheral.speaker.SpeakerPosition;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.sound.PlayStreamingSourceEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
@@ -48,7 +48,7 @@ public class SpeakerManager
if( sound != null ) sound.stop();
}
public static void moveSound( UUID source, Vector3d position )
public static void moveSound( UUID source, SpeakerPosition position )
{
SpeakerInstance sound = sounds.get( source );
if( sound != null ) sound.setPosition( position );
@@ -5,13 +5,14 @@
*/
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 net.minecraft.util.math.vector.Vector3d;
import javax.annotation.Nullable;
import java.util.concurrent.Executor;
@@ -22,7 +23,11 @@ public class SpeakerSound extends LocatableSound implements ITickableSound
Executor executor;
DfpwmStream stream;
SpeakerSound( ResourceLocation sound, DfpwmStream stream, Vector3d position, float volume, float pitch )
private Entity entity;
private boolean stopped = false;
SpeakerSound( ResourceLocation sound, DfpwmStream stream, SpeakerPosition position, float volume, float pitch )
{
super( sound, SoundCategory.RECORDS );
setPosition( position );
@@ -32,22 +37,35 @@ public class SpeakerSound extends LocatableSound implements ITickableSound
attenuation = AttenuationType.LINEAR;
}
void setPosition( Vector3d position )
void setPosition( SpeakerPosition position )
{
x = (float) position.x();
y = (float) position.y();
z = (float) position.z();
x = position.position().x;
y = position.position().y;
z = position.position().z;
entity = position.entity();
}
@Override
public boolean isStopped()
{
return false;
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