aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hysky/skyblocker/skyblock/item/WikiLookup.java
blob: c954953e5879d5cd3a104fdae0c7b5589d166301 (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
package de.hysky.skyblocker.skyblock.item;

import de.hysky.skyblocker.config.SkyblockerConfigManager;
import de.hysky.skyblocker.skyblock.itemlist.ItemRepository;
import de.hysky.skyblocker.utils.ItemUtils;
import de.hysky.skyblocker.utils.Utils;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.screen.slot.Slot;
import net.minecraft.text.Text;
import net.minecraft.util.Util;
import org.lwjgl.glfw.GLFW;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CompletableFuture;

public class WikiLookup {
    private static final Logger LOGGER = LoggerFactory.getLogger(WikiLookup.class);
    public static KeyBinding wikiLookup;
    private static String id;

    public static void init() {
        wikiLookup = KeyBindingHelper.registerKeyBinding(new KeyBinding(
                "key.wikiLookup",
                InputUtil.Type.KEYSYM,
                GLFW.GLFW_KEY_F4,
                "key.categories.skyblocker"
        ));
    }

    public static void getSkyblockId(Slot slot) {
        //Grabbing the skyblock NBT data
    	//Setting to null prevents a bug where using wiki lookup on an empty slot would bring up the last looked up page
        id = ItemUtils.getItemIdOptional(slot.getStack()).orElse(null);
    }

    public static void openWiki(Slot slot, PlayerEntity player) {
        if (Utils.isOnSkyblock() && SkyblockerConfigManager.get().general.wikiLookup.enableWikiLookup) {
            getSkyblockId(slot);

            //Fixes a crash where using wiki lookup for the first time on an item that doesn't have an id (because id is initialized to null)
            //and now also because we set it to null above for said reason
            if (id != null) {
                try {
                    String wikiLink = ItemRepository.getWikiLink(id, player);
                    if (wikiLink != null) CompletableFuture.runAsync(() -> Util.getOperatingSystem().open(wikiLink));
                } catch (IndexOutOfBoundsException | IllegalStateException e) {
                    LOGGER.error("[Skyblocker] Error while retrieving wiki article...", e);
                    if (player != null) {
                        player.sendMessage(Text.of("[Skyblocker] Error while retrieving wiki article, see logs..."), false);
                    }
                }
            }
        }
    }

}