1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-14 12:10:30 +00:00

Fix pocket speakers incorrectly detaching computers

- Fix UpgradeSpeakerPeripheral not calling super.detach (so old
   computers were never cleaned up)
 - Correctly lock computer accesses inside SpeakerPeripheral

Fixes #1003.

Fingers crossed this is the last bug. Then I can bump the year and push
a new release tomorrow.
This commit is contained in:
Jonathan Coates 2021-12-31 18:24:54 +00:00
parent a9519f68a1
commit 16df86224b
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
3 changed files with 17 additions and 7 deletions

View File

@ -39,14 +39,13 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
private final String type; private final String type;
private final Set<String> additionalTypes; private final Set<String> additionalTypes;
private final Map<String, PeripheralMethod> methodMap; private final Map<String, PeripheralMethod> methodMap;
private boolean attached; private boolean attached = false;
PeripheralWrapper( IPeripheral peripheral, String side ) PeripheralWrapper( IPeripheral peripheral, String side )
{ {
super( environment ); super( environment );
this.side = side; this.side = side;
this.peripheral = peripheral; this.peripheral = peripheral;
attached = false;
type = Objects.requireNonNull( peripheral.getType(), "Peripheral type cannot be null" ); type = Objects.requireNonNull( peripheral.getType(), "Peripheral type cannot be null" );
additionalTypes = peripheral.getAdditionalTypes(); additionalTypes = peripheral.getAdditionalTypes();

View File

@ -53,7 +53,7 @@ public abstract class SpeakerPeripheral implements IPeripheral
public static final int SAMPLE_RATE = 48000; public static final int SAMPLE_RATE = 48000;
private final UUID source = UUID.randomUUID(); private final UUID source = UUID.randomUUID();
private final Set<IComputerAccess> computers = Collections.newSetFromMap( new HashMap<>() ); private final Set<IComputerAccess> computers = new HashSet<>();
private long clock = 0; private long clock = 0;
private long lastPositionTime; private long lastPositionTime;
@ -141,9 +141,12 @@ public abstract class SpeakerPeripheral implements IPeripheral
syncedPosition( pos ); syncedPosition( pos );
// And notify computers that we have space for more audio. // And notify computers that we have space for more audio.
for( IComputerAccess computer : computers ) synchronized( computers )
{ {
computer.queueEvent( "speaker_audio_empty", computer.getAttachmentName() ); for( IComputerAccess computer : computers )
{
computer.queueEvent( "speaker_audio_empty", computer.getAttachmentName() );
}
} }
} }
@ -379,13 +382,19 @@ public abstract class SpeakerPeripheral implements IPeripheral
@Override @Override
public void attach( @Nonnull IComputerAccess computer ) public void attach( @Nonnull IComputerAccess computer )
{ {
computers.add( computer ); synchronized( computers )
{
computers.add( computer );
}
} }
@Override @Override
public void detach( @Nonnull IComputerAccess computer ) public void detach( @Nonnull IComputerAccess computer )
{ {
computers.remove( computer ); synchronized( computers )
{
computers.remove( computer );
}
} }
private static final class PendingSound private static final class PendingSound

View File

@ -21,6 +21,8 @@ public abstract class UpgradeSpeakerPeripheral extends SpeakerPeripheral
@Override @Override
public void detach( @Nonnull IComputerAccess computer ) public void detach( @Nonnull IComputerAccess computer )
{ {
super.detach( computer );
// We could be in the process of shutting down the server, so we can't send packets in this case. // We could be in the process of shutting down the server, so we can't send packets in this case.
MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); MinecraftServer server = ServerLifecycleHooks.getCurrentServer();
if( server == null || server.isStopped() ) return; if( server == null || server.isStopped() ) return;