Skip to content

Commit 755635b

Browse files
committed
fix
1 parent bd61c32 commit 755635b

2 files changed

Lines changed: 23 additions & 13 deletions

File tree

src/main/java/cn/ussshenzhou/channel/audio/client/receive/AudioReceiveHandler.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,20 @@
3333
public class AudioReceiveHandler {
3434
public static final int PLAY_RATE10 = 2;
3535
@SuppressWarnings("MapOrSetKeyShouldOverrideHashCodeEquals")
36-
public static final Set<SpeakerBlockEntity> CHANNELED_BLOCK_CACHE_C = Collections.newSetFromMap(new MapMaker().weakKeys().makeMap());
36+
private static final Set<SpeakerBlockEntity> CHANNELED_BLOCK_CACHE_C = Collections.newSetFromMap(new MapMaker().weakKeys().makeMap());
3737

3838
@SubscribeEvent
3939
public static void removeRemovedBlock(ClientTickEvent.Pre event) {
40-
CHANNELED_BLOCK_CACHE_C.removeIf(BlockEntity::isRemoved);
40+
synchronized (CHANNELED_BLOCK_CACHE_C) {
41+
CHANNELED_BLOCK_CACHE_C.removeIf(BlockEntity::isRemoved);
42+
}
43+
}
44+
45+
public static void add(SpeakerBlockEntity blockEntity){
46+
synchronized (CHANNELED_BLOCK_CACHE_C) {
47+
//noinspection MapOrSetKeyShouldOverrideHashCodeEquals
48+
AudioReceiveHandler.CHANNELED_BLOCK_CACHE_C.add(blockEntity);
49+
}
4150
}
4251

4352
public static void handle(AudioPacket2C packet) {
@@ -92,16 +101,18 @@ private static void handleInternal(AudioPacket2C packet, Level level) throws Exc
92101
return;
93102
}
94103
var channels = IntArraySet.of(packet.channels);
95-
for (var speaker : CHANNELED_BLOCK_CACHE_C) {
96-
if (!channels.contains(speaker.getChannel())) {
97-
continue;
98-
}
99-
var pos = speaker.getBlockPos();
100-
if (earPos.distanceToSqr(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5) > 64 * 64) {
101-
return;
104+
synchronized (CHANNELED_BLOCK_CACHE_C) {
105+
for (var speaker : CHANNELED_BLOCK_CACHE_C) {
106+
if (!channels.contains(speaker.getChannel())) {
107+
continue;
108+
}
109+
var pos = speaker.getBlockPos();
110+
if (earPos.distanceToSqr(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5) > 64 * 64) {
111+
continue;
112+
}
113+
var audio = getSpeakerAudioAndCheckSampleRate(speaker, 48000);
114+
audio.push(packet.from, decoded);
102115
}
103-
var audio = getSpeakerAudioAndCheckSampleRate(speaker, 48000);
104-
audio.push(packet.from, decoded);
105116
}
106117
}
107118

src/main/java/cn/ussshenzhou/channel/blockentity/ChanneledBlockEntity.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ public void setLevel(Level level) {
3737
if (!level.isClientSide()) {
3838
RelayHandler.addBlockEntity(this);
3939
} else if (this instanceof SpeakerBlockEntity speaker){
40-
//noinspection MapOrSetKeyShouldOverrideHashCodeEquals
41-
AudioReceiveHandler.CHANNELED_BLOCK_CACHE_C.add(speaker);
40+
AudioReceiveHandler.add(speaker);
4241
}
4342
}
4443

0 commit comments

Comments
 (0)