Search in sources :

Example 1 with AnalysePosition

use of com.wynntils.modules.questbook.enums.AnalysePosition in project Wynntils by Wynntils.

the class QuestManager method readQuestBook.

public static void readQuestBook() {
    if (!Reference.onWorld)
        return;
    if (McIf.player().openContainer != null && !(McIf.player().openContainer instanceof ContainerPlayer)) {
        interrupt();
        return;
    }
    boolean empty;
    synchronized (QuestManager.class) {
        if (lastInventory != null && lastInventory.isOpen())
            return;
        empty = queuedPositions.isEmpty();
    }
    if (empty) {
        // Did a "full update" (All positions were up to date, so don't open fake inventory)
        FrameworkManager.getEventBus().post(new QuestBookUpdateEvent.Full());
        return;
    }
    sendMessage(GRAY + "[Analysing quest book...]");
    hasInterrupted = false;
    List<ItemStack> gatheredQuests = new ArrayList<>();
    List<ItemStack> gatheredMiniQuests = new ArrayList<>();
    List<ItemStack> gatheredDiscoveries = new ArrayList<>();
    List<List<String>> gatheredDialogue = new ArrayList<>();
    FakeInventory inv = new FakeInventory(QUEST_BOOK_WINDOW_TITLE_PATTERN, new InventoryOpenByItem(7));
    // 15 seconds
    inv.setLimitTime(15000);
    inv.onReceiveItems((i) -> {
        int mouseButton = 1;
        Pair<Integer, ItemStack> nextClick = null;
        // lores
        updateLores(i);
        boolean fullRead;
        synchronized (QuestManager.class) {
            // get synchronised copy
            fullRead = QuestManager.fullRead;
            // Get next queued position if not currently on a position
            if (currentPosition == null) {
                if (QuestManager.queuedPositions.isEmpty()) {
                    i.close();
                    return;
                }
                currentPosition = queuedPositions.iterator().next();
            }
        }
        // go to the right page
        if (switchToCorrectPage(i, currentPosition))
            return;
        // page scanning
        if (currentPosition == AnalysePosition.QUESTS) {
            nextClick = i.findItem("Dialogue History", FilterType.CONTAINS);
            mouseButton = 0;
            List<String> dialogueLore = ItemUtils.getLore(nextClick.b);
            if (dialogueLore.contains(TextFormatting.GRAY + "There's nothing here yet")) {
                nextClick = null;
                mouseButton = 1;
            } else {
                int remove = 3;
                String page = dialogueLore.get(dialogueLore.size() - 3);
                Matcher matcher = DIALOGUE_PAGE_PATTERN.matcher(page);
                if (!matcher.matches()) {
                    remove = 1;
                    page = dialogueLore.get(dialogueLore.size() - 1);
                    matcher = DIALOGUE_PAGE_PATTERN.matcher(page);
                }
                if (matcher.matches()) {
                    int currentPage = Integer.parseInt(matcher.group(1));
                    int maxPage = Integer.parseInt(matcher.group(2));
                    if (gatheredDialogue.size() < currentPage) {
                        gatheredDialogue.add(new ArrayList<>(dialogueLore.subList(1, dialogueLore.size() - remove - 1)));
                    }
                    if (currentPage >= maxPage || gatheredDialogue.size() >= maxPage) {
                        nextClick = null;
                        mouseButton = 1;
                    }
                } else {
                    nextClick = null;
                    mouseButton = 1;
                }
            }
            if (nextClick == null) {
                // next page item
                nextClick = i.findItem(">>>>>", FilterType.CONTAINS);
                for (ItemStack stack : i.getInventory()) {
                    // also checks for nbt
                    if (!stack.hasDisplayName())
                        continue;
                    List<String> lore = ItemUtils.getUnformattedLore(stack);
                    // not a valid quest
                    if (lore.isEmpty() || (!lore.contains("Right click to track") && !lore.contains("RIGHT-CLICK TO TRACK") && !lore.contains("Right click to stop tracking")))
                        continue;
                    if (fullRead) {
                        gatheredQuests.add(stack);
                        continue;
                    }
                    String displayName = StringUtils.normalizeBadString(getTextWithoutFormattingCodes(stack.getDisplayName()));
                    if (currentQuests.containsKey(displayName) && currentQuests.get(displayName).equals(stack)) {
                        continue;
                    }
                    gatheredQuests.add(stack);
                    nextClick = null;
                    break;
                }
            }
        }
        if (currentPosition == AnalysePosition.MINIQUESTS) {
            // next page item
            nextClick = i.findItem(">>>>>", FilterType.CONTAINS);
            for (ItemStack stack : i.getInventory()) {
                // also checks for nbt
                if (!stack.hasDisplayName())
                    continue;
                List<String> lore = ItemUtils.getUnformattedLore(stack);
                // not a valid mini-quest
                if (lore.isEmpty() || (!lore.contains("Right click to track") && !lore.contains("RIGHT-CLICK TO TRACK")))
                    continue;
                if (fullRead) {
                    gatheredMiniQuests.add(stack);
                    continue;
                }
                String displayName = getTextWithoutFormattingCodes(stack.getDisplayName());
                if (currentMiniQuests.containsKey(displayName) && currentMiniQuests.get(displayName).equals(stack)) {
                    continue;
                }
                gatheredMiniQuests.add(stack);
                nextClick = null;
                break;
            }
        }
        if (currentPosition == AnalysePosition.DISCOVERIES || currentPosition == AnalysePosition.SECRET_DISCOVERIES) {
            // next page item
            nextClick = i.findItem(">>>>>", FilterType.CONTAINS);
            for (ItemStack stack : i.getInventory()) {
                // also checks for nbt
                if (!stack.hasDisplayName())
                    continue;
                List<String> lore = ItemUtils.getLore(stack);
                if (lore.isEmpty() || !getTextWithoutFormattingCodes(lore.get(0)).contains("✔ Combat Lv"))
                    continue;
                if (fullRead) {
                    gatheredDiscoveries.add(stack);
                    continue;
                }
                String displayName = getTextWithoutFormattingCodes(stack.getDisplayName());
                if (currentDiscoveries.containsKey(displayName)) {
                    continue;
                }
                gatheredDiscoveries.add(stack);
                nextClick = null;
                break;
            }
        }
        // effective pagination
        if (nextClick == null) {
            if (!gatheredQuests.isEmpty()) {
                parseQuests(gatheredQuests);
                gatheredQuests.clear();
            }
            if (!gatheredMiniQuests.isEmpty()) {
                parseMiniQuests(gatheredMiniQuests);
                gatheredMiniQuests.clear();
            }
            if (!gatheredDiscoveries.isEmpty()) {
                parseDiscoveries(gatheredDiscoveries);
                gatheredDiscoveries.clear();
            }
            if (!gatheredDialogue.isEmpty()) {
                parseDialogue(gatheredDialogue);
                gatheredDialogue.clear();
            }
            AnalysePosition previousPosition = currentPosition;
            // Remove from queue now that it has been analysed, and get the next position
            synchronized (QuestManager.class) {
                queuedPositions.remove(currentPosition);
                currentPosition = queuedPositions.isEmpty() ? null : queuedPositions.iterator().next();
            }
            FrameworkManager.getEventBus().post(new QuestBookUpdateEvent.Partial(previousPosition));
            if (currentPosition == null) {
                i.close();
                return;
            }
            // Go to next page
            nextClick = i.findItem(currentPosition.getItemName(), FilterType.EQUALS);
            if (nextClick == null) {
                Reference.LOGGER.error("[QuestManager] Failed to switch to next position (" + previousPosition + " to " + currentPosition + ")");
                i.closeUnsuccessfully();
                interrupt();
                return;
            }
            i.clickItem(nextClick.a, mouseButton, ClickType.PICKUP);
            return;
        }
        // 1 because otherwise wynn sends the inventory twice
        i.clickItem(nextClick.a, mouseButton, ClickType.PICKUP);
    });
    inv.onClose((i, result) -> {
        lastInventory = null;
        if (result != InventoryResult.CLOSED_SUCCESSFULLY) {
            interrupt();
            return;
        }
        if (!gatheredQuests.isEmpty())
            parseQuests(gatheredQuests);
        if (!gatheredMiniQuests.isEmpty())
            parseMiniQuests(gatheredMiniQuests);
        if (!gatheredDiscoveries.isEmpty())
            parseDiscoveries(gatheredDiscoveries);
        FrameworkManager.getEventBus().post(new QuestBookUpdateEvent.Full());
        sendMessage(GRAY + "[Quest book analyzed]");
        WynntilsSound.QUESTBOOK_UPDATE.play(0.5f, 1f);
    });
    synchronized (QuestManager.class) {
        if (lastInventory == null || !lastInventory.isOpen()) {
            lastInventory = inv;
            lastInventory.open();
        }
    }
}
Also used : AnalysePosition(com.wynntils.modules.questbook.enums.AnalysePosition) InventoryOpenByItem(com.wynntils.modules.core.instances.inventory.InventoryOpenByItem) QuestBookUpdateEvent(com.wynntils.modules.questbook.events.custom.QuestBookUpdateEvent) Matcher(java.util.regex.Matcher) TextComponentString(net.minecraft.util.text.TextComponentString) ContainerPlayer(net.minecraft.inventory.ContainerPlayer) FakeInventory(com.wynntils.modules.core.instances.inventory.FakeInventory) ItemStack(net.minecraft.item.ItemStack)

Example 2 with AnalysePosition

use of com.wynntils.modules.questbook.enums.AnalysePosition in project Wynntils by Wynntils.

the class ClientEvents method onChat.

@SubscribeEvent(priority = EventPriority.HIGHEST)
public void onChat(GameEvent e) {
    AnalysePosition position;
    boolean fullRead = false;
    boolean readImmediately = false;
    if (e instanceof GameEvent.LevelUp) {
        if (e instanceof GameEvent.LevelUp.Profession)
            position = AnalysePosition.MINIQUESTS;
        else
            position = AnalysePosition.QUESTS;
        fullRead = true;
    } else if (e instanceof GameEvent.QuestCompleted.MiniQuest) {
        QuestManager.completeQuest(((GameEvent.QuestCompleted.MiniQuest) e).getQuestName(), true);
        return;
    } else if (e instanceof GameEvent.QuestCompleted) {
        QuestManager.completeQuest(((GameEvent.QuestCompleted) e).getQuestName(), false);
        return;
    } else if (e instanceof GameEvent.QuestStarted.MiniQuest) {
        position = AnalysePosition.MINIQUESTS;
        readImmediately = ((GameEvent.QuestStarted.MiniQuest) e).getQuest().equalsIgnoreCase(QuestManager.getTrackedQuestName());
    } else if (e instanceof GameEvent.QuestStarted) {
        position = AnalysePosition.QUESTS;
        // Update immediately if started the tracked quest
        readImmediately = ((GameEvent.QuestStarted) e).getQuest().equalsIgnoreCase(QuestManager.getTrackedQuestName());
    } else if (e instanceof GameEvent.QuestUpdated) {
        position = AnalysePosition.QUESTS;
        // Update immediately because the tracked quest may have updated
        readImmediately = QuestManager.hasTrackedQuest();
    } else if (e instanceof GameEvent.DiscoveryFound.Secret)
        position = AnalysePosition.SECRET_DISCOVERIES;
    else if (e instanceof GameEvent.DiscoveryFound)
        position = AnalysePosition.DISCOVERIES;
    else
        return;
    QuestManager.updateAnalysis(position, fullRead, readImmediately && QuestBookConfig.INSTANCE.autoUpdateQuestbook);
}
Also used : AnalysePosition(com.wynntils.modules.questbook.enums.AnalysePosition) GameEvent(com.wynntils.core.events.custom.GameEvent) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Aggregations

AnalysePosition (com.wynntils.modules.questbook.enums.AnalysePosition)2 GameEvent (com.wynntils.core.events.custom.GameEvent)1 FakeInventory (com.wynntils.modules.core.instances.inventory.FakeInventory)1 InventoryOpenByItem (com.wynntils.modules.core.instances.inventory.InventoryOpenByItem)1 QuestBookUpdateEvent (com.wynntils.modules.questbook.events.custom.QuestBookUpdateEvent)1 Matcher (java.util.regex.Matcher)1 ContainerPlayer (net.minecraft.inventory.ContainerPlayer)1 ItemStack (net.minecraft.item.ItemStack)1 TextComponentString (net.minecraft.util.text.TextComponentString)1 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)1