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

import com.dulkirfabric.commands.DynamicKeyCommand
import com.dulkirfabric.config.DulkirConfig
import com.dulkirfabric.events.WorldKeyPressEvent
import com.dulkirfabric.util.TextUtils
import meteordevelopment.orbit.EventHandler

object KeyShortCutImpl {
    private var lastCommandHandle: Long = 0
    private var prevCode: Int = 0;

    @EventHandler
    fun onKeyPress(event: WorldKeyPressEvent) {
        DulkirConfig.configOptions.macrosList.forEach {
            if (it.keyBinding.code == event.key) {
                // Spam Prevention
                if (event.key == prevCode && System.currentTimeMillis() - lastCommandHandle < 1000)
                    return

                lastCommandHandle = System.currentTimeMillis()
                prevCode = event.key

                // This conditional allows for these shortcuts to work for commands or normal messages
                // You have to do it this way because the messages are handled differently on the client
                // side in modern versions of Minecraft.
                if (it.command.startsWith("/")) {
                    TextUtils.sendCommand(it.command.trimStart('/'))
                } else {
                    TextUtils.sendMessage(it.command)
                }
            }
        }

        if (DulkirConfig.configOptions.dynamicKey.code == event.key) {
            if (event.key == prevCode && System.currentTimeMillis() - lastCommandHandle < 1000)
                return
            lastCommandHandle = System.currentTimeMillis()
            prevCode = event.key
            TextUtils.sendCommand(DynamicKeyCommand.command.trimStart('/'))
        }
    }
}