-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKeyBindHandler.kt
44 lines (35 loc) · 1.42 KB
/
KeyBindHandler.kt
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
package com.pleahmacaka.examplemod.keybind
import com.pleahmacaka.examplemod.keybind.KeyBinds.KB_EXAMPLE_KEYBIND_ONE
import com.pleahmacaka.examplemod.keybind.KeyBinds.KB_EXAMPLE_KEYBIND_TWO
import com.pleahmacaka.examplemod.keybind.KeyBinds.KEYBINDINGS
import net.minecraft.client.KeyMapping
import net.minecraft.client.Minecraft
import net.minecraft.client.player.LocalPlayer
import net.minecraft.network.chat.Component
import net.minecraftforge.client.event.InputEvent
import net.minecraftforge.client.event.RegisterKeyMappingsEvent
import org.lwjgl.glfw.GLFW
import thedarkcolour.kotlinforforge.forge.FORGE_BUS
object KeyBindHandler {
private fun onKeyInput(event: InputEvent.Key) {
val key = KEYBINDINGS.find { keyMapping ->
keyMapping.key.value == event.key
} ?: return
when (event.action) {
GLFW.GLFW_PRESS -> {
pressed(key)
}
}
}
private fun pressed(kb: KeyMapping) {
val player: LocalPlayer = Minecraft.getInstance().player ?: return
when (kb) {
KB_EXAMPLE_KEYBIND_ONE -> player.sendSystemMessage(Component.nullToEmpty("one!"))
KB_EXAMPLE_KEYBIND_TWO -> player.sendSystemMessage(Component.nullToEmpty("two!"))
}
}
fun registerKeybindings(event: RegisterKeyMappingsEvent) {
FORGE_BUS.addListener(KeyBindHandler::onKeyInput)
KEYBINDINGS.forEach { event.register(it) }
}
}