mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-11-11 19:03:03 +00:00
Fix speaker.playSound overwriting current sound
playSound should return false if we've already played a sound this tick, rather than overwriting it.
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
// SPDX-FileCopyrightText: 2024 The CC: Tweaked Developers
|
||||
//
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
package dan200.computercraft.gametest
|
||||
|
||||
import dan200.computercraft.gametest.api.sequence
|
||||
import dan200.computercraft.gametest.api.thenOnComputer
|
||||
import dan200.computercraft.gametest.api.tryMultipleTimes
|
||||
import dan200.computercraft.shared.peripheral.speaker.SpeakerPeripheral
|
||||
import dan200.computercraft.test.core.assertArrayEquals
|
||||
import net.minecraft.gametest.framework.GameTest
|
||||
import net.minecraft.gametest.framework.GameTestHelper
|
||||
import net.minecraft.sounds.SoundEvents
|
||||
|
||||
class Speaker_Test {
|
||||
/**
|
||||
* [SpeakerPeripheral.playSound] fails if there is already a sound queued.
|
||||
*/
|
||||
@GameTest
|
||||
fun Fails_to_play_multiple_sounds(helper: GameTestHelper) = helper.sequence {
|
||||
thenOnComputer {
|
||||
callPeripheral("right", "playSound", SoundEvents.NOTE_BLOCK_HARP.key().location().toString())
|
||||
.assertArrayEquals(true)
|
||||
|
||||
tryMultipleTimes(2) { // We could technically call this a tick later, so try twice
|
||||
callPeripheral("right", "playSound", SoundEvents.NOTE_BLOCK_HARP.key().location().toString())
|
||||
.assertArrayEquals(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -321,3 +321,16 @@ fun GameTestHelper.placeItemAt(stack: ItemStack, pos: BlockPos, direction: Direc
|
||||
val hit = BlockHitResult(Vec3.atCenterOf(absolutePos), direction, absolutePos, false)
|
||||
stack.useOn(UseOnContext(player, InteractionHand.MAIN_HAND, hit))
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a function multiple times until it succeeds.
|
||||
*/
|
||||
inline fun tryMultipleTimes(count: Int, action: () -> Unit) {
|
||||
for (remaining in count - 1 downTo 0) {
|
||||
try {
|
||||
action()
|
||||
} catch (e: AssertionError) {
|
||||
if (remaining == 0) throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,6 +86,7 @@ object TestHooks {
|
||||
Printer_Test::class.java,
|
||||
Printout_Test::class.java,
|
||||
Recipe_Test::class.java,
|
||||
Speaker_Test::class.java,
|
||||
Turtle_Test::class.java,
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user