Skip to content

Commit

Permalink
fix: search for player when removing the sources
Browse files Browse the repository at this point in the history
  • Loading branch information
Apehum committed May 18, 2024
1 parent fa79176 commit 6f925d4
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/main/java/su/plo/voice/spectator/SpectatorAddon.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public final class SpectatorAddon implements AddonInitializer {
private PlasmoVoiceServer voiceServer;
private SpectatorConfig config;

private final Map<SourceLineKey, ServerStaticSource> staticSourceById = Maps.newConcurrentMap();
private final Map<SourceLineKey, ServerEntitySource> entitySourceById = Maps.newConcurrentMap();
private final Map<SourceLineKey, ServerStaticSource> staticSourceByLineKey = Maps.newConcurrentMap();
private final Map<SourceLineKey, ServerEntitySource> entitySourceByLineKey = Maps.newConcurrentMap();
private final Map<UUID, Long> lastPlayerPositionTimestampById = Maps.newConcurrentMap();

@Override
Expand Down Expand Up @@ -120,7 +120,7 @@ private void loadConfig() {
throw new IllegalStateException("Failed to load config", e);
}

staticSourceById.forEach((playerId, source) -> source.setIconVisible(config.showIcon()));
staticSourceByLineKey.forEach((playerId, source) -> source.setIconVisible(config.showIcon()));
}

private Optional<ServerProximitySource<?>> getTargetSource(
Expand All @@ -146,13 +146,21 @@ private void removeSources(@NotNull VoiceServerPlayer player) {

lastPlayerPositionTimestampById.remove(playerUuid);

// todo: fix keys

ServerStaticSource staticSource = staticSourceById.remove(playerUuid);
if (staticSource != null) staticSource.getLine().removeSource(staticSource);

ServerEntitySource entitySource = entitySourceById.remove(playerUuid);
if (entitySource != null) entitySource.getLine().removeSource(entitySource);
staticSourceByLineKey.entrySet()
.stream()
.filter(entry -> entry.getKey().playerId.equals(playerUuid))
.forEach(entry -> {
staticSourceByLineKey.remove(entry.getKey());
entry.getValue().remove();
});

entitySourceByLineKey.entrySet()
.stream()
.filter(entry -> entry.getKey().playerId.equals(playerUuid))
.forEach(entry -> {
entitySourceByLineKey.remove(entry.getKey());
entry.getValue().remove();
});
}

private ServerStaticSource getStaticSource(
Expand All @@ -162,7 +170,7 @@ private ServerStaticSource getStaticSource(
PlayerSourceInfo sourceInfo = playerSource.getSourceInfo();
SourceLineKey sourceLineKey = new SourceLineKey(player.getInstance().getUuid(), playerSource.getLine().getId());

ServerStaticSource staticSource = staticSourceById.computeIfAbsent(
ServerStaticSource staticSource = staticSourceByLineKey.computeIfAbsent(
sourceLineKey,
(sourceId) -> {
ServerStaticSource source = playerSource.getLine().createStaticSource(
Expand Down Expand Up @@ -194,14 +202,14 @@ private ServerEntitySource getEntitySource(

McServerEntity spectatorTarget = player.getInstance().getSpectatorTarget();

ServerEntitySource entitySource = entitySourceById.get(sourceLineKey);
ServerEntitySource entitySource = entitySourceByLineKey.get(sourceLineKey);
if (entitySource == null ||
!entitySource.getEntity().getInstance().equals(spectatorTarget.getInstance())
) {
entitySource = playerSource.getLine().createEntitySource(spectatorTarget, sourceInfo.isStereo());
entitySource.setIconVisible(config.showIcon());
entitySource.addFilter((listener) -> !listener.equals(player));
entitySourceById.put(sourceLineKey, entitySource);
entitySourceByLineKey.put(sourceLineKey, entitySource);
}

entitySource.setStereo(sourceInfo.isStereo());
Expand Down

0 comments on commit 6f925d4

Please sign in to comment.