Improved Inventory Changed command to show which items were added/removed/alteredQuantity

This commit is contained in:
ClxS 2016-03-04 20:20:58 +00:00
parent 210db71a92
commit 4712da9f2d
6 changed files with 74 additions and 12 deletions

View File

@ -1,5 +1,6 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
using StardewModdingAPI.Inheritance;
using StardewValley; using StardewValley;
using StardewValley.Menus; using StardewValley.Menus;
using System; using System;
@ -94,11 +95,17 @@ namespace StardewModdingAPI.Events
public class EventArgsInventoryChanged : EventArgs public class EventArgsInventoryChanged : EventArgs
{ {
public EventArgsInventoryChanged(List<Item> inventory) public EventArgsInventoryChanged(List<Item> inventory, List<ItemStackChange> changedItems)
{ {
Inventory = inventory; 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<Item> Inventory { get; private set; } public List<Item> Inventory { get; private set; }
public List<ItemStackChange> Added { get; private set; }
public List<ItemStackChange> Removed { get; private set; }
public List<ItemStackChange> QuantityChanged { get; private set; }
} }
public class EventArgsIntChanged : EventArgs public class EventArgsIntChanged : EventArgs

View File

@ -1,4 +1,5 @@
using StardewValley; using StardewModdingAPI.Inheritance;
using StardewValley;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -17,9 +18,9 @@ namespace StardewModdingAPI.Events
FarmerChanged.Invoke(null, new EventArgsFarmerChanged(priorFarmer, newFarmer)); FarmerChanged.Invoke(null, new EventArgsFarmerChanged(priorFarmer, newFarmer));
} }
public static void InvokeInventoryChanged(List<Item> inventory) public static void InvokeInventoryChanged(List<Item> inventory, List<ItemStackChange> changedItems)
{ {
InventoryChanged.Invoke(null, new EventArgsInventoryChanged(inventory)); InventoryChanged.Invoke(null, new EventArgsInventoryChanged(inventory, changedItems));
} }
} }
} }

View File

@ -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; }
}
}

View File

@ -45,7 +45,8 @@ namespace StardewModdingAPI.Inheritance
public int PreviousGameLocations { get; private set; } public int PreviousGameLocations { get; private set; }
public int PreviousLocationObjects { get; private set; } public int PreviousLocationObjects { get; private set; }
public int PreviousItems { get; private set; } public int PreviousItems_ { get; private set; }
public Dictionary<Item, int> PreviousItems { get; private set; }
public GameLocation PreviousGameLocation { get; private set; } public GameLocation PreviousGameLocation { get; private set; }
public IClickableMenu PreviousActiveMenu { get; private set; } public IClickableMenu PreviousActiveMenu { get; private set; }
@ -218,7 +219,6 @@ namespace StardewModdingAPI.Inheritance
return null; return null;
} }
public void UpdateEventCalls() public void UpdateEventCalls()
{ {
KStateNow = Keyboard.GetState(); KStateNow = Keyboard.GetState();
@ -264,9 +264,11 @@ namespace StardewModdingAPI.Inheritance
PreviousFarmer = player; PreviousFarmer = player;
} }
if(player != null && PreviousItems != player.items.GetHash()) List<ItemStackChange> 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()) if(currentLocation != null && PreviousLocationObjects != currentLocation.objects.GetHash())
@ -299,5 +301,33 @@ namespace StardewModdingAPI.Inheritance
PreviousYearOfGame = year; PreviousYearOfGame = year;
} }
} }
private bool HasInventoryChanged(List<Item> items, out List<ItemStackChange> changedItems)
{
changedItems = new List<ItemStackChange>();
IEnumerable<Item> 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());
}
} }
} }

View File

@ -51,7 +51,7 @@ namespace StardewModdingAPI
public static Thread consoleInputThread; public static Thread consoleInputThread;
public const string Version = "0.36 Alpha"; 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 disableLogging { get; private set; }
public static bool StardewInjectorLoaded { get; private set; } public static bool StardewInjectorLoaded { get; private set; }

View File

@ -88,6 +88,7 @@
<Compile Include="Events\Player.cs" /> <Compile Include="Events\Player.cs" />
<Compile Include="Events\Time.cs" /> <Compile Include="Events\Time.cs" />
<Compile Include="Extensions.cs" /> <Compile Include="Extensions.cs" />
<Compile Include="Inheritance\ItemStackChange.cs" />
<Compile Include="Inheritance\Menus\SBobberBar.cs" /> <Compile Include="Inheritance\Menus\SBobberBar.cs" />
<Compile Include="Inheritance\Menus\SGameMenu.cs" /> <Compile Include="Inheritance\Menus\SGameMenu.cs" />
<Compile Include="Inheritance\Menus\SInventoryPage.cs" /> <Compile Include="Inheritance\Menus\SInventoryPage.cs" />