-
Notifications
You must be signed in to change notification settings - Fork 29
/
Inventory.cs
54 lines (52 loc) · 1.65 KB
/
Inventory.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
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using NBT;
namespace INVedit
{
public static class Inventory
{
public static void Load(string filename, Dictionary<byte, ItemSlot> slots)
{
Load(Tag.Load(filename)["Data"]["Player"]["Inventory"], slots);
}
public static void Load(Tag inventory, Dictionary<byte, ItemSlot> slots)
{
try {
foreach (ItemSlot slot in slots.Values) slot.Clear();
foreach (Tag tag in inventory) {
short id = (short)tag["id"];
byte slot = (byte)tag["Slot"];
byte count = (byte)tag["Count"];
if (count == 0) continue;
if (!slots.ContainsKey(slot)) {
MessageBox.Show("Unknown slot '"+slot+"', discarded item.",
"Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
continue;
}
ItemSlot itemSlot = slots[slot];
itemSlot.Item = new Item(id, count, slot, (short)tag["Damage"]);
}
} finally { foreach (ItemSlot slot in slots.Values) slot.Refresh(); }
}
public static void Save(string filename, Dictionary<byte, ItemSlot> slots)
{
Tag tag = Tag.Load(filename);
Save(tag["Data"]["Player"], slots);
tag.Save(filename);
}
public static void Save(Tag parent, Dictionary<byte, ItemSlot> slots)
{
if (parent.Contains("Inventory")) parent["Inventory"].Remove();
Tag inventory = parent.AddList("Inventory", TagType.Compound);
foreach (ItemSlot slot in slots.Values) {
if (slot.Item == null) continue;
Tag item = inventory.AddCompound();
item.Add("id", slot.Item.ID);
item.Add("Slot", slot.Slot);
item.Add("Count", slot.Item.Count);
item.Add("Damage", slot.Item.Damage);
}
}
}
}