Skip to content

Commit

Permalink
FIx sounds getting stuck on when gui is clicked quickly
Browse files Browse the repository at this point in the history
  • Loading branch information
cam72cam committed Nov 29, 2023
1 parent 6b41b30 commit 81f555b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import cam72cam.immersiverailroading.multiblock.*;
import cam72cam.immersiverailroading.net.*;
import cam72cam.immersiverailroading.registry.DefinitionManager;
import cam72cam.immersiverailroading.registry.EntityRollingStockDefinition;
import cam72cam.immersiverailroading.render.SmokeParticle;
import cam72cam.immersiverailroading.render.block.RailBaseModel;
import cam72cam.immersiverailroading.render.item.*;
Expand Down Expand Up @@ -222,6 +223,7 @@ public void postRender(EntityMoveableRollingStock entity, RenderState state, flo
});

ClientEvents.TICK.subscribe(GuiBuilder::onClientTick);
ClientEvents.TICK.subscribe(EntityRollingStockDefinition.ControlSoundsDefinition::cleanupStoppedSounds);

Particles.SMOKE = Particle.register(SmokeParticle::new, SmokeParticle::renderAll);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ public static class ControlSoundsDefinition {
private final Map<UUID, List<ISound>> sounds = new HashMap<>();
private final Map<UUID, Float> lastMoveSoundValue = new HashMap<>();
private final Map<UUID, Boolean> wasSoundPressed = new HashMap<>();
private static final List<ISound> toStop = new ArrayList<>();

public ControlSoundsDefinition(Identifier engage, Identifier move, Float movePercent, Identifier disengage) {
this.engage = engage;
Expand All @@ -228,6 +229,16 @@ public ControlSoundsDefinition(DataBlock data) {
disengage = data.getValue("disengage").asIdentifier();
}

public static void cleanupStoppedSounds() {
if (toStop.isEmpty()) {
return;
}
for (ISound sound : toStop) {
sound.stop();
}
toStop.clear();
}

private void createSound(EntityRollingStock stock, Identifier sound, Vec3d pos, boolean repeats) {
if (sound == null) {
return;
Expand Down Expand Up @@ -265,9 +276,8 @@ public void effects(EntityRollingStock stock, boolean isPressed, float value, Ve
} else if (wasPressed && !isPressed) {
// Release
if (this.sounds.containsKey(stock.getUUID())) {
for (ISound snd : this.sounds.remove(stock.getUUID())) {
snd.stop();
}
// Start and Stop may have happend between ticks, we want to wait till the next tick to stop the sound
toStop.addAll(this.sounds.remove(stock.getUUID()));
}
createSound(stock, disengage, pos, false);
} else if (move != null && movePercent != null){
Expand Down

0 comments on commit 81f555b

Please sign in to comment.