Fix computer running sound not stopping when stopped before initial delay expired.

This commit is contained in:
Florian Nücke
2022-01-28 02:40:24 +01:00
parent a1217ba819
commit b4dc58edbd
2 changed files with 14 additions and 3 deletions

View File

@@ -18,6 +18,7 @@ public final class LoopingBlockEntitySound extends AbstractTickableSoundInstance
///////////////////////////////////////////////////////////////////
private final BlockEntity blockEntity;
private boolean isCanceled;
///////////////////////////////////////////////////////////////////
@@ -36,6 +37,10 @@ public final class LoopingBlockEntitySound extends AbstractTickableSoundInstance
///////////////////////////////////////////////////////////////////
public void cancel() {
isCanceled = true;
}
@Override
public void tick() {
volume = Mth.clamp(volume + FADE_IN_PER_TICK, 0, 1);
@@ -44,4 +49,9 @@ public final class LoopingBlockEntitySound extends AbstractTickableSoundInstance
stop();
}
}
@Override
public boolean canPlaySound() {
return !isCanceled;
}
}

View File

@@ -8,7 +8,7 @@ import net.minecraft.world.level.block.entity.BlockEntity;
import java.util.WeakHashMap;
public final class LoopingSoundManager {
private static final WeakHashMap<BlockEntity, TickableSoundInstance> BLOCK_ENTITY_SOUNDS = new WeakHashMap<>();
private static final WeakHashMap<BlockEntity, LoopingBlockEntitySound> BLOCK_ENTITY_SOUNDS = new WeakHashMap<>();
///////////////////////////////////////////////////////////////////
@@ -21,14 +21,15 @@ public final class LoopingSoundManager {
}
public static void stop(final BlockEntity blockEntity) {
final TickableSoundInstance instance = BLOCK_ENTITY_SOUNDS.remove(blockEntity);
final LoopingBlockEntitySound instance = BLOCK_ENTITY_SOUNDS.remove(blockEntity);
if (instance != null) {
instance.cancel();
Minecraft.getInstance().getSoundManager().stop(instance);
}
}
public static boolean isPlaying(final BlockEntity blockEntity) {
final TickableSoundInstance instance = BLOCK_ENTITY_SOUNDS.get(blockEntity);
final LoopingBlockEntitySound instance = BLOCK_ENTITY_SOUNDS.get(blockEntity);
return instance != null && !instance.isStopped();
}
}