fix: keep active sounds from restarting during movement
This commit is contained in:
@@ -12,13 +12,13 @@ import java.util.stream.Collectors;
|
||||
public final class PlaybackService {
|
||||
private final JavaPlugin plugin; private final ZoneIndex index = new ZoneIndex();
|
||||
private final Map<UUID, Map<String, ActiveSound>> active = new HashMap<>();
|
||||
private boolean blending; private boolean followPlayer; private long leaveGraceTicks, loopRestartTicks; private int taskId = -1;
|
||||
private boolean blending; private boolean followPlayer, fadeVolumeUpdates; private long leaveGraceTicks, loopRestartTicks; private int taskId = -1;
|
||||
|
||||
public PlaybackService(JavaPlugin plugin) { this.plugin = plugin; }
|
||||
public void configure(Collection<AudioZone> zones, int intervalTicks, long leaveGraceTicks, boolean blending,
|
||||
boolean followPlayer, long loopRestartTicks) {
|
||||
boolean followPlayer, boolean fadeVolumeUpdates, long loopRestartTicks) {
|
||||
stopAll(); index.rebuild(zones); this.blending = blending; this.leaveGraceTicks = leaveGraceTicks;
|
||||
this.followPlayer = followPlayer; this.loopRestartTicks = loopRestartTicks;
|
||||
this.followPlayer = followPlayer; this.fadeVolumeUpdates = fadeVolumeUpdates; this.loopRestartTicks = loopRestartTicks;
|
||||
if (taskId != -1) Bukkit.getScheduler().cancelTask(taskId);
|
||||
taskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, this::tick, intervalTicks, intervalTicks);
|
||||
}
|
||||
@@ -39,7 +39,7 @@ public final class PlaybackService {
|
||||
} else if (zone.loop() && loopRestartTicks > 0 && now - sound.startedAtTick() >= loopRestartTicks) {
|
||||
play(player, zone, volume);
|
||||
current.put(zone.normalizedName(), new ActiveSound(zone, Long.MAX_VALUE, now, now, volume));
|
||||
} else if (followPlayer && now - sound.lastPacketTick() >= 40 && Math.abs(sound.volume() - volume) >= 0.10f) {
|
||||
} else if (followPlayer && fadeVolumeUpdates && now - sound.lastPacketTick() >= 40 && Math.abs(sound.volume() - volume) >= 0.10f) {
|
||||
// Bukkit has no volume-update packet. Rate-limit fade packets and stop the previous
|
||||
// packet first; otherwise repeated entity sound packets overlap on the client.
|
||||
play(player, zone, volume);
|
||||
@@ -70,7 +70,7 @@ public final class PlaybackService {
|
||||
else player.playSound(zone.originLocation(player.getWorld()), zone.sound(), zone.category(), zone.volume(), zone.pitch());
|
||||
}
|
||||
private float volumeFor(AudioZone zone, Player player) {
|
||||
if (!followPlayer) return zone.volume();
|
||||
if (!followPlayer || !fadeVolumeUpdates) return zone.volume();
|
||||
double fade = zone.fadeDistance();
|
||||
double distance = Math.sqrt(zone.distanceToAudibleAreaSquared(player.getLocation()));
|
||||
double multiplier = fade == 0 ? (distance == 0 ? 1 : 0) : Math.max(0, 1 - distance / fade);
|
||||
|
||||
Reference in New Issue
Block a user