aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/com/dulkirfabric/features/IPhoneAlarm.kt
blob: 51d47fb59c0e4bd1f23b29136f31de7b29d1a0be (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
package com.dulkirfabric.features

import com.dulkirfabric.DulkirModFabric.mc
import com.dulkirfabric.config.DulkirConfig
import com.dulkirfabric.events.ClientTickEvent
import com.dulkirfabric.util.SoundUtil
import com.dulkirfabric.util.TimeMark
import meteordevelopment.orbit.EventHandler
import net.minecraft.client.sound.SoundInstance
import net.minecraft.util.math.BlockPos
import java.time.Duration

object IPhoneAlarm {
    private var lastPosition: BlockPos? = null
    private var lastMoved = TimeMark.farPast()
    private val soundInstances = mutableListOf<SoundInstance>()

    @EventHandler
    fun onTick(event: ClientTickEvent) {
        if (DulkirConfig.configOptions.alarmTimeout <= 0) return
        val p = mc.player ?: return
        val lastPosition = this.lastPosition
        this.lastPosition = p.blockPos
        if (lastPosition != this.lastPosition) {
            lastMoved = TimeMark.now()
            SoundUtil.stop(soundInstances)
            soundInstances.clear()
        } else {
            if (lastMoved.timePassed() > Duration.ofSeconds(DulkirConfig.configOptions.alarmTimeout.toLong())) {
                soundInstances.add(SoundUtil.playIPhoneAlarm())
                lastMoved = TimeMark.ago(Duration.ofSeconds(4L))
            }
        }
    }

}