Skip to content

Commit

Permalink
feat: track inventory changes (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
pajlada authored Nov 24, 2024
1 parent d9db75f commit 2646214
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,47 @@ You can Inspect the Varplayer in Chisel, e.g. https://chisel.weirdgloop.org/varb
}
}
```

### INVENTORY_CHANGED

Fires when the player's inventory changes

#### User picked up a cabbage into the first slot

```json5
{
"tick": 0,
"ts": "2024-08-25T08:59:42Z",
"type": "INVENTORY_CHANGED",
"data": {
"oldInventory": [
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1
],
"newInventory": [
1965, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1
]
}
}
```

#### User moved the cabbage from the first slot to the last slot

```json5
{
"tick": 0,
"ts": "2024-08-25T08:59:42Z",
"type": "INVENTORY_CHANGED",
"data": {
"newInventory": [
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1
],
"oldInventory": [
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, 1965
]
}
}
```
8 changes: 8 additions & 0 deletions src/main/java/actionlogger/ActionLoggerPlugin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package actionlogger;

import actionlogger.trackers.DialogueTracker;
import actionlogger.trackers.InventoryTracker;
import actionlogger.trackers.VarTracker;
import actionlogger.writers.JsonWriter;
import com.google.gson.Gson;
Expand Down Expand Up @@ -28,6 +29,7 @@ public class ActionLoggerPlugin extends Plugin {

private DialogueTracker dialogueTracker = null;
private VarTracker varTracker = null;
private InventoryTracker inventoryTracker = null;
private JsonWriter writer = null;

@Override
Expand All @@ -41,6 +43,9 @@ protected void startUp() throws IOException {
varTracker = new VarTracker(writer);
eventBus.register(varTracker);

inventoryTracker = new InventoryTracker(writer);
eventBus.register(inventoryTracker);

log.debug("Started up Action Logger");
}

Expand All @@ -53,6 +58,9 @@ protected void shutDown() throws IOException {
eventBus.unregister(varTracker);
varTracker = null;

eventBus.unregister(inventoryTracker);
inventoryTracker = null;

writer.close();
writer = null;

Expand Down
45 changes: 45 additions & 0 deletions src/main/java/actionlogger/trackers/InventoryTracker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package actionlogger.trackers;

import actionlogger.writers.JsonWriter;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.InventoryID;
import net.runelite.api.Item;
import net.runelite.api.events.ItemContainerChanged;
import net.runelite.client.eventbus.Subscribe;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RequiredArgsConstructor
public class InventoryTracker {
private static final String TYPE = "INVENTORY_CHANGED";

private final JsonWriter writer;

private List<Integer> inventoryItemIDs = List.of(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);

@Subscribe
public void onItemContainerChanged(ItemContainerChanged event) {
if (event.getContainerId() != InventoryID.INVENTORY.getId()) {
return;
}

var items = event.getItemContainer().getItems();
var itemIDs = Arrays.stream(items).map(Item::getId).collect(Collectors.toList());

if (!inventoryItemIDs.equals(itemIDs)) {
this.writer.write(TYPE, new InventoryChangedData(this.inventoryItemIDs, itemIDs));
this.inventoryItemIDs = itemIDs;
}
}

@Value
private static class InventoryChangedData {
List<Integer> oldInventory;
List<Integer> newInventory;
}
}

0 comments on commit 2646214

Please sign in to comment.