Inventory Tracker
IntermediateDataTracks and displays inventory item counts. Demonstrates how to access inventory data and respond to changes.
15 min read
What It Does
Tracks and displays inventory item counts. Demonstrates how to access inventory data and respond to changes.
Source Code
java
1package com.example;
2
3import net.runelite.api.Client;
4import net.runelite.api.ItemContainer;
5import net.runelite.api.events.ItemContainerChanged;
6import net.runelite.client.eventbus.Subscribe;
7import net.runelite.client.plugins.Plugin;
8import net.runelite.client.plugins.PluginDescriptor;
9import java.util.HashMap;
10import java.util.Map;
11
12@PluginDescriptor(
13 name = "Inventory Tracker",
14 description = "Tracks inventory item counts"
15)
16public class InventoryTrackerPlugin extends Plugin {
17 @Inject
18 private Client client;
19
20 private Map<Integer, Integer> itemCounts = new HashMap<>();
21
22 @Subscribe
23 public void onItemContainerChanged(ItemContainerChanged event) {
24 if (event.getContainerId() == net.runelite.api.InventoryID.INVENTORY.getId()) {
25 ItemContainer container = event.getItemContainer();
26 itemCounts.clear();
27
28 for (net.runelite.api.Item item : container.getItems()) {
29 int itemId = item.getId();
30 int quantity = item.getQuantity();
31 itemCounts.put(itemId, quantity);
32 }
33
34 // Log or display the counts
35 System.out.println("Inventory updated: " + itemCounts.size() + " unique items");
36 }
37 }
38}Code Annotations
Line 20
Map to store item IDs and their quantities
Line 23
Listen for ItemContainerChanged events
Line 24
Check if the changed container is the inventory
Line 29
Iterate through all items in the container
API Classes Used
Key Concepts
- •Map to store item IDs and their quantities
- •Listen for ItemContainerChanged events
- •Check if the changed container is the inventory
- •Iterate through all items in the container
Next Steps
- Learn about ItemContainer API
- Explore event handling
- Add overlay to display counts