aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/features/debug/SoundVisualizer.kt
blob: cc14122353b4477ee439c6730fcf3c2009785caf (plain)
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
package moe.nea.firmament.features.debug

import net.minecraft.text.Text
import moe.nea.firmament.annotations.Subscribe
import moe.nea.firmament.commands.thenExecute
import moe.nea.firmament.commands.thenLiteral
import moe.nea.firmament.events.CommandEvent
import moe.nea.firmament.events.SoundReceiveEvent
import moe.nea.firmament.events.WorldReadyEvent
import moe.nea.firmament.events.WorldRenderLastEvent
import moe.nea.firmament.util.red
import moe.nea.firmament.util.render.RenderInWorldContext

object SoundVisualizer {

	var showSounds = false

	var sounds = mutableListOf<SoundReceiveEvent>()


	@Subscribe
	fun onSubCommand(event: CommandEvent.SubCommand) {
		event.subcommand("dev") {
			thenLiteral("sounds") {
				thenExecute {
					showSounds = !showSounds
					if (!showSounds) {
						sounds.clear()
					}
				}
			}
		}
	}

	@Subscribe
	fun onWorldSwap(event: WorldReadyEvent) {
		sounds.clear()
	}

	@Subscribe
	fun onRender(event: WorldRenderLastEvent) {
		RenderInWorldContext.renderInWorld(event) {
			sounds.forEach { event ->
				withFacingThePlayer(event.position) {
					text(
						Text.literal(event.sound.value().id.toString()).also {
							if (event.cancelled)
								it.red()
						},
						verticalAlign = RenderInWorldContext.VerticalAlign.CENTER,
					)
				}
			}
		}
	}

	@Subscribe
	fun onSoundReceive(event: SoundReceiveEvent) {
		if (!showSounds) return
		if (sounds.size > 1000) {
			sounds.subList(0, 200).clear()
		}
		sounds.add(event)
	}
}