namespace Minecraft.Server.FourKit.Inventory.Meta; using Minecraft.Server.FourKit.Enchantments; /// /// Represents the metadata of an , including display name and lore. /// public class ItemMeta { private string? _displayName; private Dictionary? _enchants; private List? _lore; /// /// Checks for existence of a display name. /// /// true if this has a display name. public bool hasDisplayName() => _displayName != null; /// /// Gets the display name that is set. /// Plugins should check that hasDisplayName() returns true before calling this method. /// /// The display name that is set. public string getDisplayName() => _displayName ?? string.Empty; /// /// Sets the display name. /// /// The name to set. public void setDisplayName(string? name) => _displayName = name; /// /// Checks for existence of lore. /// /// true if this has lore. public bool hasLore() => _lore != null && _lore.Count > 0; /// /// Gets the lore that is set. /// Plugins should check if hasLore() returns true before calling this method. /// /// A list of lore that is set. public List getLore() => _lore != null ? new List(_lore) : new List(); /// /// Sets the lore for this item. Removes lore when given null. /// /// The lore that will be set. public void setLore(List? lore) { _lore = lore != null ? new List(lore) : null; } /// /// Adds the specified enchantment to this item meta. /// /// Enchantment to add /// Level for the enchantment /// Indicates the enchantment should be applied, ignoring the level limit /// true if the item meta changed as a result of this call, false otherwise public bool addEnchant(EnchantmentType enchantment, int level, bool ignoreLevelRestriction) { if (_enchants == null) _enchants = new Dictionary(); if (!ignoreLevelRestriction) { Enchantment enchant = Enchantment.getByType(enchantment); if (enchant.getMaxLevel() < level) return false; } try { _enchants.Add(enchantment, level); return true; } catch { } return false; } /// /// Removes the specified enchantment from this item meta. /// /// Enchantment to remove /// true if the item meta changed as a result of this call, false otherwise public bool removeEnchant(EnchantmentType enchantment) { if (!hasEnchant(enchantment)) return false; return _enchants.Remove(enchantment); } /// /// Returns a copy of the enchantments in this ItemMeta. Returns an empty map if none. /// /// An immutable copy of the enchantments public Dictionary getEnchants() => _enchants != null ? new Dictionary(_enchants) { } : new Dictionary(); /// /// Checks for the level of the specified enchantment. /// /// Enchantment to check /// The level that the specified enchantment has, or 0 if none public int getEnchantLevel(EnchantmentType enchantment) { if (!hasEnchant(enchantment)) return 0; return _enchants[enchantment]; //this cant be invalid, we check above } /// /// Checks if the specified enchantment conflicts with any enchantments in this ItemMeta. /// /// Enchantment to test /// true if the enchantment conflicts, false otherwise public bool hasConflictingEnchant(EnchantmentType enchantment) { Enchantment enchantmentClass = Enchantment.getByType(enchantment); if (enchantmentClass == null) return false; //this should never happen foreach (KeyValuePair ench in _enchants) { Enchantment enchClass = Enchantment.getByType(ench.Key); if (enchClass == null) continue; //this should never happen if (enchClass.conflictsWith(enchantmentClass)) { return true; } } return false; } /// /// Checks for existence of the specified enchantment. /// /// Enchantment to check /// true if this enchantment exists for this meta public bool hasEnchant(EnchantmentType enchantment) => hasEnchants() && _enchants.ContainsKey(enchantment); /// /// Checks for the existence of any enchantments. /// /// true if an enchantment exists on this meta public bool hasEnchants() => _enchants != null && _enchants.Count > 0; /// /// Sets the enchantments for this item meta. /// /// The enchantments to set. public void setEnchants(Dictionary? enchants) { _enchants = enchants != null ? new Dictionary(enchants) : null; } public ItemMeta clone() { var copy = new ItemMeta(); copy._displayName = _displayName; copy._enchants = _enchants != null ? new Dictionary(_enchants) : null; copy._lore = _lore != null ? new List(_lore) : null; return copy; } internal bool isEmpty() { return _displayName == null && (_lore == null || _lore.Count == 0) && (_enchants == null || _enchants.Count == 0); } }