Even More Modifiers  1.0.0.0
A mod for rolling various bonus stats on items
ItemUtils.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using Terraria;
5 using Terraria.ModLoader;
6 
7 namespace Loot.Api.Ext
8 {
12  public static class ItemUtils
13  {
14  public static bool IsModifierRollableItem(this Item item) => item.maxStack == 1 && item.IsWeapon() || item.IsAccessory() || item.IsArmor();
15  public static bool IsWeapon(this Item item) => item.damage > 0;
16  public static bool IsAccessory(this Item item) => item.accessory && !item.vanity;
17  public static bool IsArmor(this Item item) => (item.headSlot >= 0 || item.bodySlot >= 0 || item.legSlot >= 0) && !item.vanity;
18 
19  public static IEnumerable<T> GetDistinctModItems<T>(this Item[] inventory) where T : ModItem
20  => inventory
21  .Where(x => !x.IsAir
22  && x.modItem is T)
23  .GroupBy(x => x.type)
24  .Select(g => (T)g.First().modItem);
25 
26  public static int CountModItemStack<T>(this Item[] inventory, bool includeMouseItem = false) where T : ModItem
27  => CountItemStack(inventory, includeMouseItem, item => item.modItem is T);
28 
29  public static int CountItemStack(this Item[] inventory, int type, bool includeMouseItem = false)
30  => CountItemStack(inventory, includeMouseItem, item => item.type == type);
31 
32  public static int CountItemStack(this Item[] inventory, bool includeMouseItem, Func<Item, bool> predicate)
33  {
34  int size = includeMouseItem ? 59 : 58;
35  return Main.LocalPlayer.inventory.Take(size)
36  .Where(predicate)
37  .Select(x => x.stack)
38  .Sum();
39  }
40  }
41 }
static bool IsWeapon(this Item item)
static int CountItemStack(this Item[] inventory, bool includeMouseItem, Func< Item, bool > predicate)
Definition: ItemUtils.cs:32
Defines a set of utility methods for Item class
Definition: ItemUtils.cs:12