From 155af1305a5b65d0aac93e214ba6bead3a4d4e72 Mon Sep 17 00:00:00 2001 From: "Erymanthus[#5074] | (u/)RayDeeUx" <51521765+RayDeeUx@users.noreply.github.com> Date: Sat, 28 Oct 2023 13:00:19 -0400 Subject: Change: switch to internal fandom wiki search engine, increase scope of skyhanni's /wiki command to include any item whose item lore includes wiki. (#494) Added option to change hypixel wiki to fandom wiki in more areas than just the /wiki command. #494 --- .../skyhanni/features/commands/WikiCommand.kt | 34 --------- .../skyhanni/features/commands/WikiManager.kt | 82 ++++++++++++++++++++++ 2 files changed, 82 insertions(+), 34 deletions(-) delete mode 100644 src/main/java/at/hannibal2/skyhanni/features/commands/WikiCommand.kt create mode 100644 src/main/java/at/hannibal2/skyhanni/features/commands/WikiManager.kt (limited to 'src/main/java/at/hannibal2/skyhanni/features/commands') diff --git a/src/main/java/at/hannibal2/skyhanni/features/commands/WikiCommand.kt b/src/main/java/at/hannibal2/skyhanni/features/commands/WikiCommand.kt deleted file mode 100644 index 4976c4d9f..000000000 --- a/src/main/java/at/hannibal2/skyhanni/features/commands/WikiCommand.kt +++ /dev/null @@ -1,34 +0,0 @@ -package at.hannibal2.skyhanni.features.commands - -import at.hannibal2.skyhanni.SkyHanniMod -import at.hannibal2.skyhanni.events.PacketEvent -import at.hannibal2.skyhanni.utils.LorenzUtils -import at.hannibal2.skyhanni.utils.OSUtils -import net.minecraft.network.play.client.C01PacketChatMessage -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent - -class WikiCommand { - - @SubscribeEvent - fun onSendPacket(event: PacketEvent.SendEvent) { - val packet = event.packet - - if (!SkyHanniMod.feature.commands.useFandomWiki) return - - if (packet is C01PacketChatMessage) { - val message = packet.message.lowercase() - if (message == "/wiki") { - event.isCanceled = true - OSUtils.openBrowser("https://hypixel-skyblock.fandom.com/wiki/Hypixel_SkyBlock_Wiki") - LorenzUtils.chat("§e[SkyHanni] Opening the Fandom Wiki..") - } else if (message.startsWith("/wiki ")) { - event.isCanceled = true - val search = packet.message.substring(6) - LorenzUtils.chat("§e[SkyHanni] Searching the Fandom Wiki for §c$search") - - val url = "https://www.google.com/search?q=inurl%3Ahypixel-skyblock.fandom.com $search&hl=en" - OSUtils.openBrowser(url.replace(' ', '+')) - } - } - } -} \ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/features/commands/WikiManager.kt b/src/main/java/at/hannibal2/skyhanni/features/commands/WikiManager.kt new file mode 100644 index 000000000..119e01d66 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/commands/WikiManager.kt @@ -0,0 +1,82 @@ +package at.hannibal2.skyhanni.features.commands + +import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator +import at.hannibal2.skyhanni.events.LorenzToolTipEvent +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.LorenzUtils +import at.hannibal2.skyhanni.utils.NEUItems +import at.hannibal2.skyhanni.utils.OSUtils +import at.hannibal2.skyhanni.utils.StringUtils.removeColor +import net.minecraft.network.play.client.C01PacketChatMessage +import net.minecraft.client.Minecraft +import net.minecraft.item.ItemStack +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import org.lwjgl.input.Keyboard + +class WikiManager { + + private val config get() = SkyHanniMod.feature.commands.fandomWiki + private val urlPrefix = "https://hypixel-skyblock.fandom.com/wiki/" + private val urlSearchPrefix = "${urlPrefix}Special:Search?query=" + + @SubscribeEvent + fun onConfigFix(event: ConfigUpdaterMigrator.ConfigFixEvent) { + event.move(6, "commands.useFandomWiki", "commands.fandomWiki.useFandomWiki") + } + + @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 onKeyClickWithTooltipActive(event: LorenzToolTipEvent) { + if (!LorenzUtils.inSkyBlock) return + if (Minecraft.getMinecraft().currentScreen == null) return + if (NEUItems.neuHasFocus()) return //because good heavens if this worked on neuitems... + + if (Keyboard.isKeyDown(config.fandomWikiKeybind)) { + val itemUnderCursor = event.itemStack ?: return + wikiTheItem(itemUnderCursor) + } + } + + private fun wikiTheItem(item: ItemStack) { + var wikiUrlSearch = "" + val itemDisplayName = (item.nameWithEnchantment ?: return).replace("§a✔ ", "").replace("§c✖ ", "") + val internalName = item.getInternalName().asString() ?: return + LorenzUtils.chat("§e[SkyHanni] Searching the Fandom Wiki for §a$itemDisplayName") + if (internalName != "NONE") wikiUrlSearch = "$urlSearchPrefix$internalName&scope=internal" + else wikiUrlSearch = "$urlSearchPrefix${itemDisplayName.removeColor()}&scope=internal" + OSUtils.openBrowser(wikiUrlSearch.replace(' ', '+')) + } + + private fun isEnabled() = config.useFandomWiki +} -- cgit