aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/misc/FandomWikiFromMenus.kt
blob: dbc3725bf5b8210fd60fa0272a89a7872e1e9ae7 (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 at.hannibal2.skyhanni.features.misc

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator
import at.hannibal2.skyhanni.features.commands.WikiManager
import at.hannibal2.skyhanni.utils.InventoryUtils
import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.ItemUtils.nameWithEnchantment
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.OSUtils
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import io.github.moulberry.notenoughupdates.events.SlotClickEvent
import net.minecraftforge.fml.common.eventhandler.EventPriority
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

class FandomWikiFromMenus {

    private val config get() = SkyHanniMod.feature.commands.fandomWiki

    @SubscribeEvent
    fun onConfigFix(event: ConfigUpdaterMigrator.ConfigFixEvent) {
        event.move(6, "fandomWiki", "commands.fandomWiki")
    }

    @SubscribeEvent(priority = EventPriority.HIGH)
    fun onSlotClick(event: SlotClickEvent) {
        if (!LorenzUtils.inSkyBlock) return
        if (!isEnabled()) return
        val chestName = InventoryUtils.openInventoryName()

        if (chestName.isEmpty()) return

        val itemClickedStack = event.slot.stack ?: return
        val itemClickedName = itemClickedStack.displayName
        val itemInHand = InventoryUtils.getItemInHand() ?: return
        val itemInHandName = itemInHand.nameWithEnchantment ?: return

        val wikiDisplayName: String
        val wikiInternalName: String

        val inWikiInventory = // TODO better name for this inventory
            event.slotId == 11 && itemClickedName.contains("Wiki Command") && chestName.contains("Wiki")
        if ((itemInHandName == "") || inWikiInventory) {
            LorenzUtils.clickableChat("Click here to visit the Hypixel Skyblock Fandom Wiki!", "wiki")
            return
        }

        val inOtherWikiInventory = // TODO better name for this inventory
            event.slotId == 15 && itemClickedName.contains("Wikithis Command") && chestName.contains("Wiki")
        if (inOtherWikiInventory) {
            wikiDisplayName = itemInHandName
            val internalName = itemInHand.getInternalName().asString()
            wikiInternalName = internalName
        } else {
            //.lowercase() to match "Wiki!" and ".*wiki.*" lore lines in one fell swoop
            val inThirdWikiInventory = // TODO better name for this inventory
                (itemClickedStack.getLore()
                    .let { it.any { line -> line == "§7§eClick to view on the SkyBlock" } && it.last() == "§eWiki!" })
            if (inThirdWikiInventory) {
                wikiDisplayName = itemClickedName.removeColor().replace("✔ ", "").replace("✖ ", "")
                wikiInternalName = wikiDisplayName
            } else return
        }

        if (!config.skipWikiChat) {
            LorenzUtils.clickableChat(
                "Click here to search for $wikiDisplayName §eon the Hypixel Skyblock Fandom Wiki!",
                "wiki $wikiInternalName"
            )
        } else {
            LorenzUtils.chat("Searching the Fandom Wiki for §a$wikiDisplayName")
            val wikiUrlCustom = "${WikiManager.urlSearchPrefix}$wikiInternalName&scope=internal"
            OSUtils.openBrowser(wikiUrlCustom.replace(' ', '+'))
        }
        event.isCanceled = true
    }

    private fun isEnabled() = config.enabled
}