Skip to content

Commit

Permalink
Merge pull request #43 from Thource/4/fix-npe
Browse files Browse the repository at this point in the history
[#4] Fix NPE caused by fetching item compositions too soon
  • Loading branch information
Thource authored Nov 6, 2022
2 parents fb313a9 + 658a30f commit 319a5f9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import net.runelite.api.GameState;
import net.runelite.api.ItemComposition;
import net.runelite.client.callback.ClientThread;
import net.runelite.client.game.ItemManager;
Expand Down Expand Up @@ -57,7 +58,7 @@ public ItemStack(int id, DudeWheresMyStuffPlugin plugin) {
this.name = "Loading";
this.quantity = 0L;

plugin.getClientThread().invoke(() -> populateFromComposition(plugin.getItemManager()));
plugin.getClientThread().invoke(() -> populateFromComposition(plugin));
}

/**
Expand Down Expand Up @@ -105,15 +106,22 @@ public ItemStack(
/**
* Populates an item's data from the item id.
*
* @param itemManager itemManager
* @param plugin plugin
*/
private void populateFromComposition(ItemManager itemManager) {
private boolean populateFromComposition(DudeWheresMyStuffPlugin plugin) {
if (plugin.getClient().getGameState().getState() < GameState.LOGIN_SCREEN.getState()) {
return false;
}

ItemManager itemManager = plugin.getItemManager();
ItemComposition composition = itemManager.getItemComposition(id);
name = composition.getName();
gePrice = itemManager.getItemPrice(id);
haPrice = composition.getHaPrice();
stackable = composition.isStackable();
itemIdentification = ItemIdentification.get(itemManager.canonicalize(id));

return true;
}

long getTotalGePrice() {
Expand All @@ -128,9 +136,9 @@ public void setId(int id) {
this.id = id;
}

public void setId(int id, ItemManager itemManager) {
public void setId(int id, DudeWheresMyStuffPlugin plugin) {
this.id = id;
populateFromComposition(itemManager);
populateFromComposition(plugin);
}

public void stripPrices() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private boolean updateSecateurs() {
}

if (secateurs.getId() != secateursId) {
secateurs.setId(secateursId, plugin.getItemManager());
secateurs.setId(secateursId, plugin);
}

secateurs.setQuantity(quantity);
Expand Down Expand Up @@ -300,7 +300,7 @@ private boolean updateWateringCan() {
return false;
}

wateringCan.setId(wateringCanId, plugin.getItemManager());
wateringCan.setId(wateringCanId, plugin);
wateringCan.setQuantity(1);
return true;
}
Expand All @@ -326,21 +326,21 @@ public void load(ConfigManager configManager, String managerConfigKey, String pr
.forEach(
loadedItem -> {
if (Arrays.stream(WATERING_CAN_IDS).anyMatch(i -> i == loadedItem.getId())) {
wateringCan.setId(loadedItem.getId(), plugin.getItemManager());
wateringCan.setId(loadedItem.getId(), plugin);
wateringCan.setQuantity(loadedItem.getQuantity());
return;
}

if (loadedItem.getId() == ItemID.MAGIC_SECATEURS
|| loadedItem.getId() == ItemID.SECATEURS) {
secateurs.setId(loadedItem.getId(), plugin.getItemManager());
secateurs.setId(loadedItem.getId(), plugin);
secateurs.setQuantity(loadedItem.getQuantity());
return;
}

if (loadedItem.getId() == ItemID.BOTTOMLESS_COMPOST_BUCKET
|| loadedItem.getId() == ItemID.BOTTOMLESS_COMPOST_BUCKET_22997) {
bottomlessBucket.setId(loadedItem.getId(), plugin.getItemManager());
bottomlessBucket.setId(loadedItem.getId(), plugin);
bottomlessBucket.setQuantity(loadedItem.getQuantity());
return;
}
Expand Down

0 comments on commit 319a5f9

Please sign in to comment.