aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/commands/WikiManager.kt
blob: c515cb7f080c2cd6c822b207a31cbbdc337beb5c (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
package at.hannibal2.skyhanni.features.commands

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator
import at.hannibal2.skyhanni.events.PacketEvent
import at.hannibal2.skyhanni.utils.InventoryUtils
import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName
import at.hannibal2.skyhanni.utils.ItemUtils.nameWithEnchantment
import at.hannibal2.skyhanni.utils.KeyboardManager.isKeyHeld
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.NEUItems
import at.hannibal2.skyhanni.utils.OSUtils
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import net.minecraft.client.gui.inventory.GuiContainer
import net.minecraft.item.ItemStack
import net.minecraft.network.play.client.C01PacketChatMessage
import net.minecraftforge.client.event.GuiScreenEvent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

class WikiManager {

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

    companion object {
        private val urlPrefix = "https://hypixel-skyblock.fandom.com/wiki/"
        val urlSearchPrefix = "${urlPrefix}Special:Search?query="
    }

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

    @SubscribeEvent
    fun onSendPacket(event: PacketEvent.SendEvent) {
        if (!LorenzUtils.inSkyBlock) return
        if (!isEnabled()) return

        val packet = event.packet

        if (packet is C01PacketChatMessage) {
            val message = packet.message.lowercase()
            if (!(message.startsWith("/wiki"))) return
            event.isCanceled = true
            if (message == "/wiki") {
                LorenzUtils.chat("§e[SkyHanni] Opening the Fandom Wiki..")
                OSUtils.openBrowser("${urlPrefix}Hypixel_SkyBlock_Wiki")
            } else if (message.startsWith("/wiki ") || message == ("/wikithis")) { //conditional to see if we need Special:Search page
                if (message == ("/wikithis")) {
                    val itemInHand = InventoryUtils.getItemInHand() ?: return
                    wikiTheItem(itemInHand)
                } else {
                    val search = packet.message.split("/wiki ").last()
                    LorenzUtils.chat("§e[SkyHanni] Searching the Fandom Wiki for §a$search")
                    val wikiUrlCustom = "$urlSearchPrefix$search&scope=internal"
                    OSUtils.openBrowser(wikiUrlCustom.replace(' ', '+'))
                }
            }
        }
    }

    @SubscribeEvent
    fun onKeybind(event: GuiScreenEvent.KeyboardInputEvent.Post) {
        if (!LorenzUtils.inSkyBlock) return
        val gui = event.gui as? GuiContainer ?: return
        if (NEUItems.neuHasFocus()) return //because good heavens if this worked on neuitems...
        val stack = gui.slotUnderMouse?.stack ?: return

        if (!config.fandomWikiKeybind.isKeyHeld()) return
        wikiTheItem(stack)
    }

    private fun wikiTheItem(item: ItemStack) {
        val itemDisplayName = (item.nameWithEnchantment ?: return).replace("§a✔ ", "").replace("§c✖ ", "")
        val internalName = item.getInternalName().asString()
        LorenzUtils.chat("§e[SkyHanni] Searching the Fandom Wiki for §a$itemDisplayName")
        val wikiUrlSearch = if (internalName != "NONE") "$urlSearchPrefix$internalName&scope=internal"
        else "$urlSearchPrefix${itemDisplayName.removeColor()}&scope=internal"
        OSUtils.openBrowser(wikiUrlSearch.replace(' ', '+'))
    }

    private fun isEnabled() = config.enabled
}