From 2d44f7f44f1951d85dc41fc7674eedf09a35381b Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sun, 24 Nov 2024 11:35:26 +0100 Subject: [PATCH] feat: track inventory changes --- README.md | 32 +++++++++++++ .../java/actionlogger/ActionLoggerPlugin.java | 8 ++++ .../trackers/InventoryTracker.java | 45 +++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 src/main/java/actionlogger/trackers/InventoryTracker.java diff --git a/README.md b/README.md index a823275..a8eed34 100644 --- a/README.md +++ b/README.md @@ -121,3 +121,35 @@ 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] + } +} +``` diff --git a/src/main/java/actionlogger/ActionLoggerPlugin.java b/src/main/java/actionlogger/ActionLoggerPlugin.java index 32a6bdc..61f5822 100644 --- a/src/main/java/actionlogger/ActionLoggerPlugin.java +++ b/src/main/java/actionlogger/ActionLoggerPlugin.java @@ -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; @@ -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 @@ -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"); } @@ -53,6 +58,9 @@ protected void shutDown() throws IOException { eventBus.unregister(varTracker); varTracker = null; + eventBus.unregister(inventoryTracker); + inventoryTracker = null; + writer.close(); writer = null; diff --git a/src/main/java/actionlogger/trackers/InventoryTracker.java b/src/main/java/actionlogger/trackers/InventoryTracker.java new file mode 100644 index 0000000..0913301 --- /dev/null +++ b/src/main/java/actionlogger/trackers/InventoryTracker.java @@ -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 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 oldInventory; + List newInventory; + } +}