From d8ff97e4a5e6358d24dee16971dce50843ef094a Mon Sep 17 00:00:00 2001 From: Collin Smith Date: Fri, 17 Jan 2020 03:16:35 -0800 Subject: [PATCH] Added LocationListener to subscribe to item location changes (specifically for belt and possibly cursor as well) --- core/src/com/riiablo/save/CharData.java | 9 ++++---- core/src/com/riiablo/save/ItemData.java | 28 ++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/core/src/com/riiablo/save/CharData.java b/core/src/com/riiablo/save/CharData.java index c668fe9e..80665ab6 100644 --- a/core/src/com/riiablo/save/CharData.java +++ b/core/src/com/riiablo/save/CharData.java @@ -364,7 +364,7 @@ public class CharData implements ItemData.UpdateListener, Pool.Poolable { itemData.cursor = itemData.unequip(bodyLoc); item = itemData.getItem(itemData.cursor); } - item.location = Location.CURSOR; + itemData.setLocation(item, Location.CURSOR); } public void cursorToBody(BodyLoc bodyLoc, boolean merc) { @@ -396,11 +396,12 @@ public class CharData implements ItemData.UpdateListener, Pool.Poolable { public void cursorToBelt(int x, int y) { assert itemData.cursor != ItemData.INVALID_ITEM; - Item item = itemData.getItem(itemData.cursor); - item.location = Location.BELT; + int i = itemData.cursor; + itemData.cursor = ItemData.INVALID_ITEM; + Item item = itemData.getItem(i); item.gridX = (byte) x; item.gridY = (byte) y; - itemData.cursor = ItemData.INVALID_ITEM; + itemData.setLocation(item, Location.BELT); } public void swapBeltItem(int i) { diff --git a/core/src/com/riiablo/save/ItemData.java b/core/src/com/riiablo/save/ItemData.java index 96a85575..488b61b7 100644 --- a/core/src/com/riiablo/save/ItemData.java +++ b/core/src/com/riiablo/save/ItemData.java @@ -28,6 +28,7 @@ public class ItemData { final Array alternateListeners = new Array<>(false, 16); final Array storeListeners = new Array<>(false, 16); + final Array locationListeners = new Array<>(false, 16); final EnumIntMap equipped = new EnumIntMap<>(BodyLoc.class, INVALID_ITEM); final Array equipListeners = new Array<>(false, 16); @@ -172,7 +173,7 @@ public class ItemData { Item item = itemData.get(i); if (item.location == Location.STORED) notifyStoreRemoved(item); cursor = i; - item.location = Location.CURSOR; + setLocation(item, Location.CURSOR); } void storeCursor(StoreLoc storeLoc, int x, int y) { @@ -183,7 +184,7 @@ public class ItemData { void store(StoreLoc storeLoc, int i, int x, int y) { Item item = itemData.get(i); - item.location = Location.STORED; + setLocation(item, Location.STORED); item.storeLoc = storeLoc; item.gridX = (byte) x; item.gridY = (byte) y; @@ -197,7 +198,7 @@ public class ItemData { void equip(BodyLoc bodyLoc, int i) { Item item = itemData.get(i); - item.location = Location.EQUIPPED; + setLocation(item, Location.EQUIPPED); item.bodyLoc = bodyLoc; int j = equipped.put(bodyLoc, i); assert j == INVALID_ITEM : "Item " + j + " should have been unequipped by this point."; @@ -325,4 +326,25 @@ public class ItemData { void onAdded(ItemData items, StoreLoc storeLoc, Item item); void onRemoved(ItemData items, StoreLoc storeLoc, Item item); } + + void setLocation(Item item, Location location) { + if (item.location != location) { + Location oldLocation = item.location; + item.location = location; + notifyLocationChanged(item, oldLocation); + } + } + + public boolean addLocationListener(LocationListener l) { + locationListeners.add(l); + return true; + } + + private void notifyLocationChanged(Item item, Location oldLocation) { + for (LocationListener l : locationListeners) l.onChanged(this, oldLocation, item.location, item); + } + + public interface LocationListener { + void onChanged(ItemData items, Location oldLocation, Location location, Item item); + } }