at main 1.1 kB view raw
1using System.Text.Json; 2using System.Text.Json.Serialization; 3using Electropunk.Content.Items; 4using Electropunk.World; 5 6namespace Electropunk.Containers; 7 8public class ItemInstance(ushort id, Dictionary<string, object>? data = null) 9{ 10 public ushort ItemID = id; 11 public Dictionary<string, object> Data = data ?? []; 12 13 [JsonIgnore] 14 public Item Item 15 { 16 get => Item.Items[ItemID]; 17 set => ItemID = value.ID; 18 } 19 20 public ItemInstance(Item item, Dictionary<string, object>? data = null) : this(item.ID, data) 21 { 22 } 23 24 public bool Matches(ItemInstance other) => other.ItemID == ItemID; 25 public bool Matches(Item other) => other.ID == ItemID; 26 public bool Matches(ushort otherID) => otherID == ItemID; 27 28 public bool IsEmpty() => Matches(GameItems.Air); 29 public bool IsSomething() => !Matches(GameItems.Air); 30 31 public ItemInstance Copy() => new(ItemID, new(Data)); 32 public void Clear() => Item = GameItems.Air; 33 34 public string Serialize() => JsonSerializer.Serialize(this); 35 36 public static ItemInstance Deserialize(string json) => 37 JsonSerializer.Deserialize<ItemInstance>(json)!; 38}