aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/features/inventory/WardrobeKeybinds.kt
blob: 6e2b4a9591108a129207d7df3a3eee960b77399e (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package moe.nea.firmament.features.inventory

import org.lwjgl.glfw.GLFW
import net.minecraft.item.Items
import moe.nea.firmament.annotations.Subscribe
import moe.nea.firmament.events.HandledScreenKeyPressedEvent
import moe.nea.firmament.features.FirmamentFeature
import moe.nea.firmament.gui.config.ManagedConfig
import moe.nea.firmament.util.MC
import moe.nea.firmament.util.mc.SlotUtils.clickLeftMouseButton

object WardrobeKeybinds : FirmamentFeature {
	override val identifier: String
		get() = "wardrobe-keybinds"

	object TConfig : ManagedConfig(identifier, Category.INVENTORY) {
		val wardrobeKeybinds by toggle("wardrobe-keybinds") { false }
		val changePageKeybind by keyBinding("change-page") { GLFW.GLFW_KEY_ENTER }
		val nextPage by keyBinding("next-page") { GLFW.GLFW_KEY_D }
		val previousPage by keyBinding("previous-page") { GLFW.GLFW_KEY_A }
		val slotKeybinds = (1..9).map {
			keyBinding("slot-$it") { GLFW.GLFW_KEY_0 + it }
		}
	}

	override val config: ManagedConfig?
		get() = TConfig

	val slotKeybindsWithSlot = TConfig.slotKeybinds.withIndex().map { (index, keybinding) ->
		index + 36 to keybinding
	}

	@Subscribe
	fun switchSlot(event: HandledScreenKeyPressedEvent) {
		if (MC.player == null || MC.world == null || MC.interactionManager == null) return

		val regex = Regex("Wardrobe \\([12]/2\\)")
		if (!regex.matches(event.screen.title.string)) return
		if (!TConfig.wardrobeKeybinds) return

		if (
			event.matches(TConfig.changePageKeybind) ||
			event.matches(TConfig.previousPage) ||
			event.matches(TConfig.nextPage)
		) {
			event.cancel()

			val handler = event.screen.screenHandler
			val previousSlot = handler.getSlot(45)
			val nextSlot = handler.getSlot(53)

			val backPressed = event.matches(TConfig.changePageKeybind) || event.matches(TConfig.previousPage)
			val nextPressed = event.matches(TConfig.changePageKeybind) || event.matches(TConfig.nextPage)

			if (backPressed && previousSlot.stack.item == Items.ARROW) {
				previousSlot.clickLeftMouseButton(handler)
			} else if (nextPressed && nextSlot.stack.item == Items.ARROW) {
				nextSlot.clickLeftMouseButton(handler)
			}
		}



		val slot =
			slotKeybindsWithSlot
				.find { event.matches(it.second.get()) }
				?.first ?: return

		event.cancel()

		val handler = event.screen.screenHandler
		val invSlot = handler.getSlot(slot)

		val itemStack = invSlot.stack
		if (itemStack.item != Items.PINK_DYE && itemStack.item != Items.LIME_DYE) return

		invSlot.clickLeftMouseButton(handler)
	}

}