Skip to content

Commit

Permalink
Fix enchantments
Browse files Browse the repository at this point in the history
  • Loading branch information
Tides committed Dec 17, 2024
1 parent a9427ab commit ca3c85c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Obsidian.API/Inventory/ItemMeta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public struct ItemMeta : IEquatable<ItemMeta>

public bool Unbreakable { get; internal set; }

public IReadOnlyDictionary<EnchantmentType, Enchantment> Enchantments { get; internal set; }
public IReadOnlyDictionary<EnchantmentType, Enchantment> StoredEnchantments { get; internal set; }
public IReadOnlyDictionary<int, Enchantment> Enchantments { get; internal set; }
public IReadOnlyDictionary<int, Enchantment> StoredEnchantments { get; internal set; }

public IReadOnlyList<string> CanDestroy { get; internal set; }

Expand Down
38 changes: 26 additions & 12 deletions Obsidian.API/Inventory/ItemMetaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ public class ItemMetaBuilder

public bool Unbreakable { get; internal set; }

public IReadOnlyDictionary<EnchantmentType, Enchantment> Enchantments { get; }
public IReadOnlyDictionary<EnchantmentType, Enchantment> StoredEnchantments { get; }
public IReadOnlyDictionary<int, Enchantment> Enchantments { get; }
public IReadOnlyDictionary<int, Enchantment> StoredEnchantments { get; }

public IReadOnlyList<string> CanDestroy { get; }

public IReadOnlyList<ChatMessage> Lore { get; }

private readonly Dictionary<EnchantmentType, Enchantment> enchantments = [];
private readonly Dictionary<EnchantmentType, Enchantment> storedEnchantments = [];
private readonly Dictionary<int, Enchantment> enchantments = [];
private readonly Dictionary<int, Enchantment> storedEnchantments = [];

private readonly List<string> canDestroy = [];

private readonly List<ChatMessage> lore = [];

public ItemMetaBuilder()
{
Enchantments = new ReadOnlyDictionary<EnchantmentType, Enchantment>(enchantments);
StoredEnchantments = new ReadOnlyDictionary<EnchantmentType, Enchantment>(storedEnchantments);
Enchantments = new ReadOnlyDictionary<int, Enchantment>(enchantments);
StoredEnchantments = new ReadOnlyDictionary<int, Enchantment>(storedEnchantments);
CanDestroy = new ReadOnlyCollection<string>(canDestroy);
Lore = new ReadOnlyCollection<ChatMessage>(lore);
}
Expand Down Expand Up @@ -91,9 +91,10 @@ public ItemMetaBuilder IsUnbreakable(bool unbreakable)

public ItemMetaBuilder AddEnchantment(EnchantmentType type, int level)
{
enchantments.Add(type, new Enchantment
var enchantmentId = type.GetHashCode();
enchantments.Add(enchantmentId, new Enchantment
{
Type = type,
Id = enchantmentId,
Level = level
});

Expand All @@ -102,9 +103,22 @@ public ItemMetaBuilder AddEnchantment(EnchantmentType type, int level)

public ItemMetaBuilder AddStoredEnchantment(EnchantmentType type, int level)
{
storedEnchantments.Add(type, new Enchantment
var enchantmentId = type.GetHashCode();

storedEnchantments.Add(enchantmentId, new Enchantment
{
Id = enchantmentId,
Level = level
});

return this;
}

public ItemMetaBuilder AddStoredEnchantment(int id, int level)
{
storedEnchantments.Add(id, new Enchantment
{
Type = type,
Id = id,
Level = level
});

Expand All @@ -121,8 +135,8 @@ public ItemMeta Build()
Durability = Durability,
Unbreakable = Unbreakable,

Enchantments = new ReadOnlyDictionary<EnchantmentType, Enchantment>(new Dictionary<EnchantmentType, Enchantment>(enchantments)),
StoredEnchantments = new ReadOnlyDictionary<EnchantmentType, Enchantment>(new Dictionary<EnchantmentType, Enchantment>(storedEnchantments)),
Enchantments = new ReadOnlyDictionary<int, Enchantment>(new Dictionary<int, Enchantment>(enchantments)),
StoredEnchantments = new ReadOnlyDictionary<int, Enchantment>(new Dictionary<int, Enchantment>(storedEnchantments)),

CanDestroy = new ReadOnlyCollection<string>(new List<string>(canDestroy))
};
Expand Down

0 comments on commit ca3c85c

Please sign in to comment.