1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
package at.hannibal2.skyhanni.utils
import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.test.command.ErrorManager
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import net.minecraft.client.Minecraft
import net.minecraft.client.audio.ISound
import net.minecraft.client.audio.PositionedSound
import net.minecraft.client.audio.SoundCategory
import net.minecraft.util.ResourceLocation
object SoundUtils {
private val beepSound by lazy { createSound("random.orb", 1f) }
private val clickSound by lazy { createSound("gui.button.press", 1f) }
private val errorSound by lazy { createSound("mob.endermen.portal", 0f) }
val plingSound by lazy { createSound("note.pling", 1f) }
val centuryActiveTimerAlert by lazy { createSound("skyhanni:centurytimer.active", 1f) }
fun ISound.playSound() {
Minecraft.getMinecraft().addScheduledTask {
val gameSettings = Minecraft.getMinecraft().gameSettings
val oldLevel = gameSettings.getSoundLevel(SoundCategory.PLAYERS)
gameSettings.setSoundLevel(SoundCategory.PLAYERS, 1f)
try {
Minecraft.getMinecraft().soundHandler.playSound(this)
} catch (e: Exception) {
if (e is IllegalArgumentException) {
e.message?.let {
if (it.startsWith("value already present:")) {
println("SkyHanni Sound error: $it")
return@addScheduledTask
}
}
}
ErrorManager.logErrorWithData(
e, "Failed to play a sound",
"soundLocation" to this.soundLocation
)
} finally {
gameSettings.setSoundLevel(SoundCategory.PLAYERS, oldLevel)
}
}
}
fun createSound(name: String, pitch: Float, volume: Float = 50f): ISound {
val sound: ISound = object : PositionedSound(ResourceLocation(name)) {
init {
this.volume = volume
repeat = false
repeatDelay = 0
attenuationType = ISound.AttenuationType.NONE
this.pitch = pitch
}
}
return sound
}
fun playBeepSound() {
beepSound.playSound()
}
fun playClickSound() {
clickSound.playSound()
}
fun playPlingSound() {
plingSound.playSound()
}
fun command(args: Array<String>) {
if (args.isEmpty()) {
ChatUtils.userError("Specify a sound effect to test")
return
}
val soundName = args[0]
val pitch = args.getOrNull(1)?.toFloat() ?: 1.0f
val volume = args.getOrNull(2)?.toFloat() ?: 50.0f
createSound(soundName, pitch, volume).playSound()
}
fun playErrorSound() {
errorSound.playSound()
}
// TODO use duration for delay
fun repeatSound(delay: Long, repeat: Int, sound: ISound) {
SkyHanniMod.coroutineScope.launch {
repeat(repeat) {
sound.playSound()
delay(delay)
}
}
}
}
|