diff --git a/StardewModdingAPI/Events/EventArgs.cs b/StardewModdingAPI/Events/EventArgs.cs index 1ebafa54..7f2e28c7 100644 --- a/StardewModdingAPI/Events/EventArgs.cs +++ b/StardewModdingAPI/Events/EventArgs.cs @@ -1,5 +1,6 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Input; +using StardewModdingAPI.Inheritance; using StardewValley; using StardewValley.Menus; using System; @@ -94,11 +95,17 @@ namespace StardewModdingAPI.Events public class EventArgsInventoryChanged : EventArgs { - public EventArgsInventoryChanged(List inventory) + public EventArgsInventoryChanged(List inventory, List changedItems) { Inventory = inventory; + Added = changedItems.Where(n => n.ChangeType == ChangeType.Added).ToList(); + Removed = changedItems.Where(n => n.ChangeType == ChangeType.Removed).ToList(); + QuantityChanged = changedItems.Where(n => n.ChangeType == ChangeType.StackChange).ToList(); } public List Inventory { get; private set; } + public List Added { get; private set; } + public List Removed { get; private set; } + public List QuantityChanged { get; private set; } } public class EventArgsIntChanged : EventArgs diff --git a/StardewModdingAPI/Events/Player.cs b/StardewModdingAPI/Events/Player.cs index 7904a6e2..47d21f63 100644 --- a/StardewModdingAPI/Events/Player.cs +++ b/StardewModdingAPI/Events/Player.cs @@ -1,4 +1,5 @@ -using StardewValley; +using StardewModdingAPI.Inheritance; +using StardewValley; using System; using System.Collections.Generic; using System.Linq; @@ -17,9 +18,9 @@ namespace StardewModdingAPI.Events FarmerChanged.Invoke(null, new EventArgsFarmerChanged(priorFarmer, newFarmer)); } - public static void InvokeInventoryChanged(List inventory) + public static void InvokeInventoryChanged(List inventory, List changedItems) { - InventoryChanged.Invoke(null, new EventArgsInventoryChanged(inventory)); + InventoryChanged.Invoke(null, new EventArgsInventoryChanged(inventory, changedItems)); } } } diff --git a/StardewModdingAPI/Inheritance/ItemStackChange.cs b/StardewModdingAPI/Inheritance/ItemStackChange.cs new file mode 100644 index 00000000..88003579 --- /dev/null +++ b/StardewModdingAPI/Inheritance/ItemStackChange.cs @@ -0,0 +1,23 @@ +using StardewValley; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StardewModdingAPI.Inheritance +{ + public enum ChangeType + { + Removed, + Added, + StackChange + } + + public class ItemStackChange + { + public Item Item { get; set; } + public int StackChange { get; set; } + public ChangeType ChangeType { get; set; } + } +} diff --git a/StardewModdingAPI/Inheritance/SGame.cs b/StardewModdingAPI/Inheritance/SGame.cs index c19dadf2..366f3045 100644 --- a/StardewModdingAPI/Inheritance/SGame.cs +++ b/StardewModdingAPI/Inheritance/SGame.cs @@ -45,7 +45,8 @@ namespace StardewModdingAPI.Inheritance public int PreviousGameLocations { get; private set; } public int PreviousLocationObjects { get; private set; } - public int PreviousItems { get; private set; } + public int PreviousItems_ { get; private set; } + public Dictionary PreviousItems { get; private set; } public GameLocation PreviousGameLocation { get; private set; } public IClickableMenu PreviousActiveMenu { get; private set; } @@ -217,8 +218,7 @@ namespace StardewModdingAPI.Inheritance Program.LogDebug("A custom location could not be created for: " + name); return null; } - - + public void UpdateEventCalls() { KStateNow = Keyboard.GetState(); @@ -264,10 +264,12 @@ namespace StardewModdingAPI.Inheritance PreviousFarmer = player; } - if(player != null && PreviousItems != player.items.GetHash()) + List changedItems; + if (player != null && HasInventoryChanged(player.items, out changedItems)) { - Events.PlayerEvents.InvokeInventoryChanged(player.items); - } + Events.PlayerEvents.InvokeInventoryChanged(player.items, changedItems); + PreviousItems = player.items.Where(n => n != null).ToDictionary(n => n, n => n.Stack); + } if(currentLocation != null && PreviousLocationObjects != currentLocation.objects.GetHash()) { @@ -299,5 +301,33 @@ namespace StardewModdingAPI.Inheritance PreviousYearOfGame = year; } } + + private bool HasInventoryChanged(List items, out List changedItems) + { + changedItems = new List(); + IEnumerable actualItems = items.Where(n => n != null); + foreach (var item in actualItems) + { + if (PreviousItems != null && PreviousItems.ContainsKey(item)) + { + if(PreviousItems[item] != item.Stack) + { + changedItems.Add(new ItemStackChange() { Item = item, StackChange = item.Stack - PreviousItems[item], ChangeType = ChangeType.StackChange }); + } + } + else + { + changedItems.Add(new ItemStackChange() { Item = item, StackChange = item.Stack, ChangeType = ChangeType.Added }); + } + } + + if (PreviousItems != null) + { + changedItems.AddRange(PreviousItems.Where(n => !actualItems.Any(i => i == n.Key)).Select(n => + new ItemStackChange() { Item = n.Key, StackChange = -n.Key.Stack, ChangeType = ChangeType.Removed })); + } + + return (changedItems.Any()); + } } } \ No newline at end of file diff --git a/StardewModdingAPI/Program.cs b/StardewModdingAPI/Program.cs index f7cb7c87..000ddab0 100644 --- a/StardewModdingAPI/Program.cs +++ b/StardewModdingAPI/Program.cs @@ -51,7 +51,7 @@ namespace StardewModdingAPI public static Thread consoleInputThread; public const string Version = "0.36 Alpha"; - public const bool debug = true; + public const bool debug = false; public static bool disableLogging { get; private set; } public static bool StardewInjectorLoaded { get; private set; } diff --git a/StardewModdingAPI/StardewModdingAPI.csproj b/StardewModdingAPI/StardewModdingAPI.csproj index 64f5a718..a223f6c2 100644 --- a/StardewModdingAPI/StardewModdingAPI.csproj +++ b/StardewModdingAPI/StardewModdingAPI.csproj @@ -88,6 +88,7 @@ + @@ -110,7 +111,7 @@ - +