summaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/inventory/QuickCraftFeatures.kt
blob: 70069a84ea2a09bed11cf96118c632f236da9719 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package at.hannibal2.skyhanni.features.inventory

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.events.GuiContainerEvent
import at.hannibal2.skyhanni.events.LorenzToolTipEvent
import at.hannibal2.skyhanni.events.RepositoryReloadEvent
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.InventoryUtils
import at.hannibal2.skyhanni.utils.InventoryUtils.getAllItems
import at.hannibal2.skyhanni.utils.ItemUtils.name
import at.hannibal2.skyhanni.utils.KeyboardManager
import at.hannibal2.skyhanni.utils.LorenzColor
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.RenderUtils.highlight
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import net.minecraft.client.gui.inventory.GuiChest
import net.minecraft.inventory.ContainerChest
import net.minecraft.item.ItemStack
import net.minecraftforge.fml.common.eventhandler.EventPriority
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

@SkyHanniModule
object QuickCraftFeatures {

    private val config get() = SkyHanniMod.feature.inventory
    private val quickCraftSlots = listOf(16, 25, 34)
    private var quickCraftableItems = emptyList<String>()

    enum class InventoryType(val inventoryName: String) {
        CRAFT_ITEM("Craft Item"),
        MORE_QUICK_CRAFT_OPTIONS("Quick Crafting"),
        ;
    }

    private fun InventoryType.ignoreSlot(slotNumber: Int?): Boolean = when (this) {
        InventoryType.CRAFT_ITEM -> slotNumber !in quickCraftSlots
        InventoryType.MORE_QUICK_CRAFT_OPTIONS -> slotNumber !in 10..44
    }

    @SubscribeEvent
    fun onRepoReload(event: RepositoryReloadEvent) {
        quickCraftableItems = event.getConstant<List<String>>("QuickCraftableItems")
    }

    @SubscribeEvent
    fun onToolTip(event: LorenzToolTipEvent) {
        val inventoryType = getInventoryType() ?: return
        if (inventoryType.ignoreSlot(event.slot.slotNumber)) return

        if (needsQuickCraftConfirmation(event.itemStack)) {
            event.toolTip.replaceAll {
                it.replace(
                    "Click to craft!",
                    "§c${KeyboardManager.getModifierKeyName()} + Click to craft!"
                )
            }
        }
    }

    @SubscribeEvent
    fun onForegroundDrawn(event: GuiContainerEvent.ForegroundDrawnEvent) {
        val inventoryType = getInventoryType() ?: return
        if (KeyboardManager.isModifierKeyDown()) return
        if (event.gui !is GuiChest) return
        val chest = event.gui.inventorySlots as ContainerChest

        for ((slot, stack) in chest.getAllItems()) {
            if (inventoryType.ignoreSlot(slot.slotNumber)) continue
            if (stack.name == "§cQuick Crafting Slot") continue
            if (needsQuickCraftConfirmation(stack)) {
                slot highlight LorenzColor.DARK_GRAY.addOpacity(180)
            }
        }
    }

    @SubscribeEvent(priority = EventPriority.HIGH)
    fun onSlotClick(event: GuiContainerEvent.SlotClickEvent) {
        val inventoryType = getInventoryType() ?: return
        if (inventoryType.ignoreSlot(event.slot?.slotNumber)) return

        val clickedItem = event.slot?.stack ?: return
        if (!KeyboardManager.isModifierKeyDown() && needsQuickCraftConfirmation(clickedItem)) {
            event.cancel()
        }
    }

    private fun needsQuickCraftConfirmation(item: ItemStack): Boolean {
        return !quickCraftableItems.contains(item.displayName.removeColor())
    }

    private fun getInventoryType(): InventoryType? {
        if (!LorenzUtils.inSkyBlock || !config.quickCraftingConfirmation) return null

        val inventoryName = InventoryUtils.openInventoryName()
        return InventoryType.entries.firstOrNull { it.inventoryName == inventoryName }
    }
}