-
Notifications
You must be signed in to change notification settings - Fork 101
/
ItemTypeOrderedSet.cs
112 lines (93 loc) · 2.44 KB
/
ItemTypeOrderedSet.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System.Collections.Generic;
using System.Linq;
using Terraria;
using Terraria.ModLoader;
using Terraria.ModLoader.Config;
using Terraria.ModLoader.IO;
namespace MagicStorage
{
public class ItemTypeOrderedSet
{
public static ItemTypeOrderedSet Empty => new("Empty");
private const string Suffix = "~v2";
private const string Suffix3 = "~v3";
private readonly string _name;
private List<Item> _items = new();
private List<ItemDefinition> _unloadedItems = new();
private HashSet<int> _set = new();
public int Count => _items.Count;
public IEnumerable<Item> Items => _items;
public ItemTypeOrderedSet(string name)
{
_name = name;
}
public bool Add(Item item) => Add(item.type);
public bool Add(int type)
{
Item item = new();
item.SetDefaults(type);
if (_set.Add(item.type))
{
_items.Add(item);
return true;
}
return false;
}
public bool Contains(int type) => _set.Contains(type);
public bool Contains(Item item) => _set.Contains(item.type);
public bool Remove(Item item) => Remove(item.type);
public bool Remove(int type)
{
if (_set.Remove(type))
{
_items.RemoveAll(x => x.type == type);
return true;
}
return false;
}
public void Clear()
{
_set.Clear();
_items.Clear();
}
public bool RemoveAt(int index)
{
Item item = _items[index];
if (_set.Remove(item.type))
{
_items.RemoveAt(index);
return true;
}
return false;
}
public void Save(TagCompound c)
{
c.Add(_name + Suffix3, _items.Select(x => new ItemDefinition(x.type)).Concat(_unloadedItems).ToList());
}
public void Load(TagCompound tag)
{
if (tag.GetList<TagCompound>(_name) is { Count: > 0 } listV1)
{
_items = listV1.Select(Utility.SafelyLoadItem).Where(static i => !i.IsAir).ToList();
}
else if (tag.GetList<int>(_name + Suffix) is { Count: > 0 } listV2)
{
_items = listV2
.Where(x => x < ItemLoader.ItemCount) // Unable to reliably restore invalid IDs; just ignore them
.Select(x => new Item(x))
.Where(x => !x.IsAir) // Filters out deprecated items
.ToList();
}
else if (tag.GetList<ItemDefinition>(_name + Suffix3) is { Count: > 0 } listV3)
{
_items = listV3.Where(x => !x.IsUnloaded).Select(x => new Item(x.Type)).ToList();
_unloadedItems = listV3.Where(x => x.IsUnloaded).ToList();
}
else
{
_items = new List<Item>();
}
_set = new HashSet<int>(_items.Select(x => x.type));
}
}
}