Skip to content

Commit

Permalink
chore: simplify QuestManager's public startUpQuest API
Browse files Browse the repository at this point in the history
We now only expose one function with both parameters (QuestHelper + whether the sidepanel should open)

This function handles the thread dispatching, if needed. This means the single function can be called from both the AWT
thread & the client thread without having to wait an extra tick in some scenarios.
  • Loading branch information
pajlada committed Nov 24, 2024
1 parent cb021c0 commit d51e558
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 39 deletions.
48 changes: 16 additions & 32 deletions src/main/java/com/questhelper/managers/QuestManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import com.questhelper.requirements.item.ItemRequirement;
import com.questhelper.steps.QuestStep;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client;
import net.runelite.api.GameState;
Expand Down Expand Up @@ -86,9 +85,6 @@ public class QuestManager
@Named("developerMode")
private boolean developerMode;

@Setter
private QuestHelper sidebarSelectedQuest;

@Getter
private QuestHelper selectedQuest;
private boolean loadQuestList = false;
Expand Down Expand Up @@ -128,24 +124,10 @@ public void setupOnLogin()
*/
public void updateQuestState()
{
handleSidebarQuest();
handleSelectedQuest();
handleQuestListUpdate();
}

/**
* Handles the quest selected in the sidebar.
* Starts up the sidebar quest and resets it to null.
*/
private void handleSidebarQuest()
{
if (sidebarSelectedQuest != null)
{
startUpQuest(sidebarSelectedQuest);
sidebarSelectedQuest = null;
}
}

/**
* Handles the currently selected quest.
* Updates steps, highlights, and item requirements.
Expand Down Expand Up @@ -251,33 +233,35 @@ public void updateQuestList()
}
}

/**
* Starts up a quest.
* Shuts down any active quest and initializes the new quest.
*
* @param questHelper The quest to be started.
*/
public void startUpQuest(QuestHelper questHelper)
private void doStartUpQuest(QuestHelper questHelper, boolean shouldOpenSidebarIfConfig)
{
startUpQuest(questHelper, true);
if (!(client.getGameState() == GameState.LOGGED_IN))
{
return;
}

shutDownPreviousQuest();
initializeNewQuest(questHelper, shouldOpenSidebarIfConfig);
}

/**
* Starts up a quest.
* Shuts down any active quest and initializes the new quest.
* <p>
* This can be called from any thread
*
* @param questHelper The quest to be started.
* @param shouldOpenSidebarIfConfig Flag to open the sidebar if configured.
*/
public void startUpQuest(QuestHelper questHelper, boolean shouldOpenSidebarIfConfig)
{
if (!(client.getGameState() == GameState.LOGGED_IN))
{
return;
if (client.isClientThread()) {
this.doStartUpQuest(questHelper, shouldOpenSidebarIfConfig);
} else {
clientThread.invokeLater(() -> {
this.doStartUpQuest(questHelper, shouldOpenSidebarIfConfig);
});
}

shutDownPreviousQuest();
initializeNewQuest(questHelper, shouldOpenSidebarIfConfig);
}

private void initializeNewQuest(QuestHelper questHelper, boolean shouldOpenSidebarIfConfig)
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/questhelper/managers/QuestMenuHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ private void handleShieldOfArrav()
QuestHelperQuest.SHIELD_OF_ARRAV_PHOENIX_GANG :
QuestHelperQuest.SHIELD_OF_ARRAV_BLACK_ARM_GANG;

questManager.startUpQuest(QuestHelperQuest.getByName(questToStart.getName()));
questManager.startUpQuest(QuestHelperQuest.getByName(questToStart.getName()), true);
}

/**
* Handles the special case for starting up the "Recipe for Disaster" quest.
*/
private void handleRecipeForDisaster()
{
questManager.startUpQuest(QuestHelperQuest.getByName(QuestHelperQuest.RECIPE_FOR_DISASTER_START.getName()));
questManager.startUpQuest(QuestHelperQuest.getByName(QuestHelperQuest.RECIPE_FOR_DISASTER_START.getName()), true);
}

/**
Expand All @@ -160,7 +160,7 @@ private void handleGenericQuest(String questName)
QuestHelper questHelper = QuestHelperQuest.getByName(questName);
if (questHelper != null)
{
questManager.startUpQuest(questHelper);
questManager.startUpQuest(questHelper, true);
}
}

Expand Down Expand Up @@ -356,7 +356,7 @@ private void handleMenuEntryClick(String newEntry, String target)
if (newEntry.startsWith("Start"))
{
String quest = Text.removeTags(target);
questManager.startUpQuest(QuestHelperQuest.getByName(quest));
questManager.startUpQuest(QuestHelperQuest.getByName(quest), true);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/questhelper/panel/QuestHelperPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ public void setSelectedQuest(QuestHelper questHelper)
if ("true".equals(configManager.getConfiguration(QuestHelperConfig.QUEST_BACKGROUND_GROUP, "selected-assist-level")))
{
searchQuestsPanel.setVisible(true);
questManager.setSidebarSelectedQuest(questHelper);
questManager.startUpQuest(questHelper, true);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public void actionPerformed(ActionEvent e)
@Override
public void actionPerformed(ActionEvent e)
{
questManager.setSidebarSelectedQuest(finalQuest1);
questManager.startUpQuest(finalQuest1, true);
}
});

Expand All @@ -198,7 +198,7 @@ public void mouseClicked(MouseEvent e)
{
if (SwingUtilities.isLeftMouseButton(e))
{
questManager.setSidebarSelectedQuest(finalQuest);
questManager.startUpQuest(finalQuest, true);
}
}
}
Expand Down

0 comments on commit d51e558

Please sign in to comment.