Skip to content

Commit

Permalink
v2.6.1.1 - Patched some logic errors with FixPurchasedItemInfo()
Browse files Browse the repository at this point in the history
  • Loading branch information
hamstar0 committed Nov 26, 2021
1 parent a412b31 commit 6017ab8
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Durability.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</ItemGroup>
<ItemGroup>
<Reference Include="HamstarHelpers">
<HintPath>..\..\Mod Helpers\v5 (tML11)\HamstarHelpers\bin\Release\net452\HamstarHelpers.dll</HintPath>
<HintPath>..\..\Mod Helpers\Project\HamstarHelpers\bin\Release\net452\HamstarHelpers.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
8 changes: 5 additions & 3 deletions DurabilityMod.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Terraria.GameContent.Achievements;
using System;
using Microsoft.Xna.Framework.Graphics;
using Terraria.GameContent.Achievements;
using Terraria.ModLoader;
using Terraria;
using Microsoft.Xna.Framework.Graphics;
using System;
using HamstarHelpers.Helpers.TModLoader.Mods;


Expand Down Expand Up @@ -31,6 +31,8 @@ public DurabilityMod() {
////////////////

public override void Load() {
DurabilityMod.Instance = this;

if( Main.netMode != 2 ) { // Not server
this.DestroyedTex = ModContent.GetTexture( "Terraria/MapDeath" );
}
Expand Down
12 changes: 6 additions & 6 deletions DurabilityPlayer_Update.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,16 @@ private void UpdateInventoryAndShopSnapshots() {

private void FixPurchasedItemInfo() {
Item[] currShop = Main.instance.shop[Main.npcShop].item;
Item[] currInv = (Item[])this.player.inventory.Clone();
currInv = currInv.Concat<Item>( new Item[] { Main.mouseItem.Clone() } ).ToArray();
Item[] currInv = (Item[])this.player.inventory.ToArray();
currInv = currInv.Concat<Item>(
new Item[] { Main.mouseItem.Clone() }
).ToArray();

var shopChanges = ItemFinderHelpers.FindChanges( this.PrevShop, currShop );
var invChanges = ItemFinderHelpers.FindChanges( this.PrevInventory, currInv );
var shopChanges = Durability.Helpers.Items.ItemFinderHelpers.FindChanges( this.PrevShop, currShop );
var invChanges = Durability.Helpers.Items.ItemFinderHelpers.FindChanges( this.PrevInventory, currInv );

if( shopChanges.Count != 1 || invChanges.Count != 1 ) { return; }

int prevItemIdx = 0;
Item prevItem = null;
Item currItem = null;

Expand All @@ -157,7 +158,6 @@ private void FixPurchasedItemInfo() {

if( changeAmt < 0 ) { return; }
prevItem = this.PrevShop[itemIdx];
prevItemIdx = itemIdx;
}
foreach( var kv in invChanges ) {
int itemIdx = kv.Key;
Expand Down
79 changes: 79 additions & 0 deletions Libraries/Helpers/Items/ItemFinderHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Terraria;
using Terraria.ID;
using HamstarHelpers.Classes.Errors;
using HamstarHelpers.Helpers.DotNET.Extensions;


namespace Durability.Helpers.Items {
public partial class ItemFinderHelpers {
/// <summary>
/// Finds changes between 2 same-sized arrays of items.
/// </summary>
/// <param name="prevItems"></param>
/// <param name="currItems"></param>
/// <param name="skipCoins"></param>
/// <returns>Set of array indices of changed items between the 2 collections. Mapped to a "direction" indicator of
/// changes.</returns>
public static IDictionary<int, int> FindChanges( Item[] prevItems, Item[] currItems, bool skipCoins=true ) {
if( prevItems.Length != currItems.Length ) {
throw new ModHelpersException( "Mismatched item array sizes." );
}

IDictionary<int, int> changes = new Dictionary<int, int>();
int len = currItems.Length;
Item prevItem, currItem;

for( int i = 0; i < len; i++ ) {
prevItem = prevItems[i];
currItem = currItems[i];
bool prevItemOn = prevItem?.IsAir == false;
bool currItemOn = currItem?.IsAir == false;

if( prevItemOn != currItemOn ) {
changes[i] = currItemOn
? 1
: -1;

continue;
} else if( !currItemOn ) { // both not active
continue;
}

if( prevItem.type != currItem.type || prevItem.stack != currItem.stack ) {
changes[i] = prevItem.stack < currItem.stack
? 1
: -1;
}
}

if( skipCoins ) {
foreach( (int itemIdx, int changeDir) in changes.ToArray() ) {
currItem = currItems[itemIdx];
prevItem = prevItems[itemIdx];
bool prevItemOn = prevItem?.IsAir == false;
bool currItemOn = currItem?.IsAir == false;

if( !currItemOn ) { // is not active
if( prevItemOn ) {
if( prevItem.type >= ItemID.CopperCoin && prevItem.type <= ItemID.PlatinumCoin ) { // was money
changes.Remove( itemIdx );
}
}
} else { // is active
if( currItem.type >= ItemID.CopperCoin || currItem.type <= ItemID.PlatinumCoin ) { // is money
// was not active or was money:
if( !prevItemOn || (prevItem.type >= ItemID.CopperCoin && prevItem.type <= ItemID.PlatinumCoin) ) {
changes.Remove( itemIdx );
}
}
}
}
}

return changes;
}
}
}
2 changes: 1 addition & 1 deletion build.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
author = hamstar
version = 2.6.1
version = 2.6.1.1
displayName = Durability
modReferences = [email protected]
buildIgnore = *.csproj, *.user, *.bat, obj\*, bin\*, .vs\*, .git\*
Expand Down

0 comments on commit 6017ab8

Please sign in to comment.