Skip to content

Commit

Permalink
fix: Added null handling in item containers for ItemReq
Browse files Browse the repository at this point in the history
Co-Authored-By: pajlada <962989+pajlada@users.noreply.github.com>
  • Loading branch information
Zoinkwiz and pajlada committed Nov 21, 2024
1 parent 3d5aadd commit 481bfe9
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 23 deletions.
25 changes: 6 additions & 19 deletions src/main/java/com/questhelper/QuestHelperPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.inject.Binder;
import com.google.inject.Injector;
import com.google.inject.Provides;
import com.questhelper.bank.banktab.BankTabItem;
import com.questhelper.bank.banktab.BankTabItems;
import com.questhelper.managers.*;
import com.questhelper.panel.QuestHelperPanel;
Expand All @@ -43,28 +44,14 @@
import com.questhelper.util.worldmap.WorldMapAreaManager;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import javax.inject.Inject;
import javax.inject.Named;
import javax.swing.SwingUtilities;
import com.questhelper.tools.Icon;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.ChatMessageType;
import net.runelite.api.Client;
import net.runelite.api.GameState;
import net.runelite.api.InventoryID;
import net.runelite.api.Item;
import net.runelite.api.ItemContainer;
import net.runelite.api.Menu;
import net.runelite.api.MenuEntry;
import net.runelite.api.VarPlayer;
import net.runelite.api.WorldType;
import net.runelite.api.*;
import net.runelite.api.events.ChatMessage;
import net.runelite.api.events.CommandExecuted;
import net.runelite.api.events.GameStateChanged;
Expand Down Expand Up @@ -256,20 +243,20 @@ public void onGameTick(GameTick event)
public void onItemContainerChanged(ItemContainerChanged event)
{
Item[] items = event.getItemContainer().getItems();
if (event.getItemContainer() == client.getItemContainer(InventoryID.BANK))
if (event.getContainerId() == InventoryID.BANK.getId())
{
ItemAndLastUpdated bankData = QuestContainerManager.getBankData();
bankData.update(client.getTickCount(), items);
questBankManager.updateLocalBank(event.getItemContainer());
}

if (event.getItemContainer() == client.getItemContainer(InventoryID.INVENTORY))
if (event.getContainerId() == InventoryID.INVENTORY.getId())
{
ItemAndLastUpdated inventoryData = QuestContainerManager.getInventoryData();
inventoryData.update(client.getTickCount(), items);
}

if (event.getItemContainer() == client.getItemContainer(InventoryID.EQUIPMENT))
if (event.getContainerId() == InventoryID.EQUIPMENT.getId())
{
ItemAndLastUpdated equippedData = QuestContainerManager.getEquippedData();
equippedData.update(client.getTickCount(), items);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import lombok.Getter;
import lombok.Setter;
import net.runelite.api.Item;
import javax.annotation.Nullable;

import java.util.concurrent.Callable;

Expand All @@ -54,7 +55,7 @@ public void update(int updateTick, Item[] items)
this.items = items;
}

public Item[] getItems()
public @Nullable Item[] getItems()
{
if (methodToObtainItems != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.*;
import java.util.stream.Collectors;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import net.runelite.api.Client;
import net.runelite.api.InventoryID;
Expand Down Expand Up @@ -520,6 +521,10 @@ public int checkTotalMatchesInContainers(Client client, ItemAndLastUpdated... co
// Consideration: If any have changed, AND ItemRequirement requires unique item, then we do need to aggregate all the results
for (ItemAndLastUpdated container : containers)
{
if (container.getItems() == null)
{
continue;
}
ContainerState stateForItemInContainer = knownContainerStates.get(container.getContainerType());
// Generic container, always check
if (container.getContainerType() == TrackedContainers.UNDEFINED)
Expand Down Expand Up @@ -566,6 +571,10 @@ private int getTotalMatchesInContainersIfExclusive(Client client, ItemAndLastUpd
List<Item> allItems = new ArrayList<>();
for (ItemAndLastUpdated container : containers)
{
if (container.getItems() == null)
{
continue;
}
allItems.addAll(List.of(container.getItems()));
}

Expand Down Expand Up @@ -646,7 +655,7 @@ private ItemAndLastUpdated[] containersToCheckDefault()
return containers.toArray(new ItemAndLastUpdated[0]);
}

private int getMaxMatchingItems(Client client, Item[] items)
private int getMaxMatchingItems(Client client, @NonNull Item[] items)
{
// TODO: Is this right to do? Misleading on number for some scenarios
// Perhaps additionalOptions should have some text change instead assosciated
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/questhelper/MockedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public abstract class MockedTest extends MockedTestBase
protected PlayerStateManager playerStateManager = Mockito.mock(PlayerStateManager.class);

@Bind
protected QuestHelperPlugin questHelperPlugin = Mockito.mock(QuestHelperPlugin.class);
protected QuestHelperPlugin questHelperPlugin = Mockito.spy(QuestHelperPlugin.class);

@Bind
protected ClientToolbar clientToolbar = Mockito.mock(ClientToolbar.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import net.runelite.api.Scene;
import net.runelite.api.Tile;
import net.runelite.api.coords.WorldPoint;
import net.runelite.api.events.ItemContainerChanged;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -106,6 +107,8 @@ private ConditionalStep init(WorldPoint playerLocation, Item[] mockedItems)
});
when(client.getScene()).thenReturn(mockedScene);

this.questHelperPlugin.onItemContainerChanged(new ItemContainerChanged(InventoryID.INVENTORY.getId(), mockedItemContainer));

this.injector.injectMembers(helper);
helper.setInjector(injector);
helper.setQuest(QuestHelperQuest.THE_CURSE_OF_ARRAV);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ public void testSolvable(List<Item> items, int puzzle1SolutionValue, int puzzle2
public void testNotSolvable(List<Item> items, int puzzle1SolutionValue, int puzzle2SolutionValue)
{
solution.load(client, items, puzzle1SolutionValue, puzzle2SolutionValue, discs, valueToRequirement, valueToDoubleDiscRequirement, discToValue, valuePossibleSingleDiscExchangesRequirements);

assertFalse(solution.isGood());
}

Expand Down

0 comments on commit 481bfe9

Please sign in to comment.