From 16df86224be34d61d2b1fe38b1d7f1675a35b5f7 Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Fri, 31 Dec 2021 18:24:54 +0000 Subject: [PATCH] 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. --- .../core/apis/PeripheralAPI.java | 3 +-- .../peripheral/speaker/SpeakerPeripheral.java | 19 ++++++++++++++----- .../speaker/UpgradeSpeakerPeripheral.java | 2 ++ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/main/java/dan200/computercraft/core/apis/PeripheralAPI.java b/src/main/java/dan200/computercraft/core/apis/PeripheralAPI.java index 35e20ca03..1f3d8765a 100644 --- a/src/main/java/dan200/computercraft/core/apis/PeripheralAPI.java +++ b/src/main/java/dan200/computercraft/core/apis/PeripheralAPI.java @@ -39,14 +39,13 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange private final String type; private final Set additionalTypes; private final Map methodMap; - private boolean attached; + private boolean attached = false; PeripheralWrapper( IPeripheral peripheral, String side ) { super( environment ); this.side = side; this.peripheral = peripheral; - attached = false; type = Objects.requireNonNull( peripheral.getType(), "Peripheral type cannot be null" ); additionalTypes = peripheral.getAdditionalTypes(); diff --git a/src/main/java/dan200/computercraft/shared/peripheral/speaker/SpeakerPeripheral.java b/src/main/java/dan200/computercraft/shared/peripheral/speaker/SpeakerPeripheral.java index dc896cb43..c947cae30 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/speaker/SpeakerPeripheral.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/speaker/SpeakerPeripheral.java @@ -53,7 +53,7 @@ public abstract class SpeakerPeripheral implements IPeripheral public static final int SAMPLE_RATE = 48000; private final UUID source = UUID.randomUUID(); - private final Set computers = Collections.newSetFromMap( new HashMap<>() ); + private final Set computers = new HashSet<>(); private long clock = 0; private long lastPositionTime; @@ -141,9 +141,12 @@ public abstract class SpeakerPeripheral implements IPeripheral syncedPosition( pos ); // 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 public void attach( @Nonnull IComputerAccess computer ) { - computers.add( computer ); + synchronized( computers ) + { + computers.add( computer ); + } } @Override public void detach( @Nonnull IComputerAccess computer ) { - computers.remove( computer ); + synchronized( computers ) + { + computers.remove( computer ); + } } private static final class PendingSound diff --git a/src/main/java/dan200/computercraft/shared/peripheral/speaker/UpgradeSpeakerPeripheral.java b/src/main/java/dan200/computercraft/shared/peripheral/speaker/UpgradeSpeakerPeripheral.java index 90e6e5616..c9ef48fea 100644 --- a/src/main/java/dan200/computercraft/shared/peripheral/speaker/UpgradeSpeakerPeripheral.java +++ b/src/main/java/dan200/computercraft/shared/peripheral/speaker/UpgradeSpeakerPeripheral.java @@ -21,6 +21,8 @@ public abstract class UpgradeSpeakerPeripheral extends SpeakerPeripheral @Override 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. MinecraftServer server = ServerLifecycleHooks.getCurrentServer(); if( server == null || server.isStopped() ) return;