forked from neophoenix236/INVedit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInventory.cs
44 lines (42 loc) · 1.33 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
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using NBT;
namespace INVedit
{
public static class Inventory
{
public static void Load(Tag inventory, Dictionary<byte, ItemSlot> slots)
{
try {
foreach (ItemSlot slot in slots.Values) slot.Item = null;
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(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);
}
}
}
}