diff --git a/EXILED/Exiled.API/Features/Items/Firearm.cs b/EXILED/Exiled.API/Features/Items/Firearm.cs index 00630fad3..004ea01f9 100644 --- a/EXILED/Exiled.API/Features/Items/Firearm.cs +++ b/EXILED/Exiled.API/Features/Items/Firearm.cs @@ -360,9 +360,11 @@ public static Firearm Create(FirearmType type) /// The to add. public void AddAttachment(AttachmentIdentifier identifier) { + uint fallbackCode = AvailableAttachments[FirearmType].FirstOrDefault(attId => attId.Name == identifier.Name).Code; + // Fallback addedCode onto AvailableAttachments' code in case it's 0 - uint addedCode = identifier.Code == 0 - ? AvailableAttachments[FirearmType].FirstOrDefault(attId => attId.Name == identifier.Name).Code + uint addedCode = identifier.Code == 0 || fallbackCode != identifier.Code + ? fallbackCode : identifier.Code; // Look for conflicting attachment (attachment that occupies the same slot) diff --git a/EXILED/Exiled.API/Features/Player.cs b/EXILED/Exiled.API/Features/Player.cs index 693f949d7..d1e39aadd 100644 --- a/EXILED/Exiled.API/Features/Player.cs +++ b/EXILED/Exiled.API/Features/Player.cs @@ -2871,7 +2871,7 @@ public Item AddItem(ItemType itemType) { if (itemType.GetFirearmType() is not FirearmType.None) { - return AddItem(itemType.GetFirearmType(), null); + return AddItem(itemType.GetFirearmType(), new List()); } Item item = Item.Create(itemType, this); @@ -2881,6 +2881,39 @@ public Item AddItem(ItemType itemType) return item; } + /// + /// Adds the amount of items of the specified type with default durability(ammo/charge) and no mods to the player's inventory. + /// + /// The item to be added. + /// The amount of items to be added. + /// An containing the items given. + public List AddItem(ItemType itemType, int amount) + { + List items = new(amount > 0 ? amount : 0); + if (amount > 0) + { + for (int i = 0; i < amount; i++) + items.Add(AddItem(itemType)); + } + + return items; + } + + /// + /// Adds the list of items of the specified type with default durability(ammo/charge) and no mods to the player's inventory. + /// + /// The list of items to be added. + /// An containing the items given. + public List AddItem(IEnumerable items) + { + List returnedItems = new(items.Count()); + + foreach (ItemType type in items) + returnedItems.Add(AddItem(type)); + + return returnedItems; + } + /// /// Adds a firearm of the specified type with default durability(ammo/charge) and no mods to the player's inventory. /// @@ -2905,18 +2938,43 @@ public Item AddItem(FirearmType firearmType, IEnumerable i } /// - /// Adds the amount of items of the specified type with default durability(ammo/charge) and no mods to the player's inventory. + /// Adds a firearm of the specified type with default durability(ammo/charge) and no mods to the player's inventory. /// - /// The item to be added. + /// The firearm to be added. + /// The attachments to be added to the item. + /// The given to the player. + public Item AddItem(FirearmType firearmType, IEnumerable identifiers) + { + Item item = Item.Create(firearmType.GetItemType()); + + if (item is Firearm firearm) + { + if (identifiers is not null) + firearm.AddAttachment(identifiers); + else if (Preferences is not null && Preferences.TryGetValue(firearmType, out AttachmentIdentifier[] attachments)) + firearm.Base.ApplyAttachmentsCode(attachments.GetAttachmentsCode(), true); + } + + AddItem(item); + + return item; + } + + /// + /// Adds the amount of firearms of the specified type with default durability(ammo/charge) and no mods to the player's inventory. + /// + /// The item to be added. /// The amount of items to be added. - /// An containing the items given. - public IEnumerable AddItem(ItemType itemType, int amount) + /// The attachments to be added to the item. + /// An containing the items given. + public List AddItem(FirearmType firearmType, int amount, IEnumerable identifiers) { List items = new(amount > 0 ? amount : 0); + if (amount > 0) { for (int i = 0; i < amount; i++) - items.Add(AddItem(itemType)); + items.Add(AddItem(firearmType, identifiers)); } return items; @@ -2928,17 +2986,15 @@ public IEnumerable AddItem(ItemType itemType, int amount) /// The item to be added. /// The amount of items to be added. /// The attachments to be added to the item. - /// An containing the items given. - public IEnumerable AddItem(FirearmType firearmType, int amount, IEnumerable identifiers) + /// An containing the items given. + public List AddItem(FirearmType firearmType, int amount, IEnumerable identifiers) { List items = new(amount > 0 ? amount : 0); if (amount > 0) { - IEnumerable attachmentIdentifiers = identifiers.ToList(); - for (int i = 0; i < amount; i++) - items.Add(AddItem(firearmType, attachmentIdentifiers)); + items.Add(AddItem(firearmType, identifiers)); } return items; @@ -2947,48 +3003,76 @@ public IEnumerable AddItem(FirearmType firearmType, int amount, IEnumerabl /// /// Adds the list of items of the specified type with default durability(ammo/charge) and no mods to the player's inventory. /// - /// The list of items to be added. - /// An containing the items given. - public IEnumerable AddItem(IEnumerable items) + /// The of and of to be added. + /// An containing the items given. + public List AddItem(Dictionary> items) { - List enumeratedItems = ListPool.Pool.Get(items); - List returnedItems = new(enumeratedItems.Count); + List returnedItems = new(items.Count); - foreach (ItemType type in enumeratedItems) - returnedItems.Add(AddItem(type)); + foreach (KeyValuePair> item in items) + returnedItems.Add(AddItem(item.Key, item.Value)); - ListPool.Pool.Return(enumeratedItems); return returnedItems; } /// /// Adds the list of items of the specified type with default durability(ammo/charge) and no mods to the player's inventory. /// - /// The of and of to be added. - /// An containing the items given. - public IEnumerable AddItem(Dictionary> items) + /// The of and of to be added. + /// An containing the items given. + public List AddItem(Dictionary> items) { List returnedItems = new(items.Count); - foreach (KeyValuePair> item in items) - returnedItems.Add(AddItem(item.Key, item.Value)); + foreach (KeyValuePair> item in items) + returnedItems.Add(AddItem(item.Key, AttachmentIdentifier.Get(item.Key, item.Value))); return returnedItems; } + /// + /// Adds the list of items to the player's inventory. + /// + /// The of and of to be added. + public void AddItem(Dictionary> firearms) + { + if (firearms.Count > 0) + { + foreach (KeyValuePair> item in firearms) + AddItem(item.Key, item.Value); + } + } + + /// + /// Adds the list of items to the player's inventory. + /// + /// The of and of to be added. + public void AddItem(Dictionary> firearms) + { + if (firearms.Count > 0) + { + foreach (KeyValuePair> item in firearms) + AddItem(item.Key, item.Value); + } + } + /// /// Adds an item to the player's inventory. /// /// The item to be added. - public void AddItem(Item item) + /// The attachments to be added to the item. + public void AddItem(Firearm item, IEnumerable identifiers) { try { + if (identifiers is not null) + item.AddAttachment(identifiers); + AddItem(item.Base, item); } - catch (Exception e) + catch (Exception exception) { - Log.Error($"{nameof(Player)}.{nameof(AddItem)}(Item): {e}"); + Log.Error($"{nameof(Player)}.{nameof(AddItem)}(Item): {exception}"); } } @@ -2997,7 +3081,7 @@ public void AddItem(Item item) /// /// The item to be added. /// The attachments to be added to the item. - public void AddItem(Firearm item, IEnumerable identifiers) + public void AddItem(Firearm item, IEnumerable identifiers) { try { @@ -3015,10 +3099,18 @@ public void AddItem(Firearm item, IEnumerable identifiers) /// /// Adds an item to the player's inventory. /// - /// The of the item to be added. - /// The reason the item was added. + /// The of the item to be added. + /// The attachments to be added to of the item. /// The that was added. - public Item AddItem(Pickup pickup, ItemAddReason addReason = ItemAddReason.AdminCommand) => Item.Get(Inventory.ServerAddItem(pickup.Type, addReason, pickup.Serial, pickup.Base)); + public Item AddItem(FirearmPickup pickup, IEnumerable identifiers) + { + Firearm firearm = Item.Get(Inventory.ServerAddItem(pickup.Type, ItemAddReason.PickedUp, pickup.Serial, pickup.Base)); + + if (identifiers is not null) + firearm.AddAttachment(identifiers); + + return firearm; + } /// /// Adds an item to the player's inventory. @@ -3026,9 +3118,9 @@ public void AddItem(Firearm item, IEnumerable identifiers) /// The of the item to be added. /// The attachments to be added to of the item. /// The that was added. - public Item AddItem(FirearmPickup pickup, IEnumerable identifiers) + public Item AddItem(FirearmPickup pickup, IEnumerable identifiers) { - Firearm firearm = Item.Get(Inventory.ServerAddItem(pickup.Type, ItemAddReason.PickedUp, pickup.Serial, pickup.Base)); + Firearm firearm = Item.Get(Inventory.ServerAddItem(pickup.Type, ItemAddReason.AdminCommand, pickup.Serial, pickup.Base)); if (identifiers is not null) firearm.AddAttachment(identifiers); @@ -3036,6 +3128,30 @@ public Item AddItem(FirearmPickup pickup, IEnumerable iden return firearm; } + /// + /// Adds an item to the player's inventory. + /// + /// The item to be added. + public void AddItem(Item item) + { + try + { + AddItem(item.Base, item); + } + catch (Exception e) + { + Log.Error($"{nameof(Player)}.{nameof(AddItem)}(Item): {e}"); + } + } + + /// + /// Adds an item to the player's inventory. + /// + /// The of the item to be added. + /// The reason the item was added. + /// The that was added. + public Item AddItem(Pickup pickup, ItemAddReason addReason = ItemAddReason.AdminCommand) => Item.Get(Inventory.ServerAddItem(pickup.Type, addReason, pickup.Serial, pickup.Base)); + /// /// Adds an item to the player's inventory. /// @@ -3082,19 +3198,6 @@ public void AddItem(IEnumerable items) AddItem(item); } - /// - /// Adds the list of items to the player's inventory. - /// - /// The of and of to be added. - public void AddItem(Dictionary> firearms) - { - if (firearms.Count > 0) - { - foreach (KeyValuePair> item in firearms) - AddItem(item.Key, item.Value); - } - } - /// /// Gives the player a specific candy. Will give the player a bag if they do not already have one. /// diff --git a/EXILED/Exiled.API/Structs/AttachmentIdentifier.cs b/EXILED/Exiled.API/Structs/AttachmentIdentifier.cs index cc27b57a0..24b891399 100644 --- a/EXILED/Exiled.API/Structs/AttachmentIdentifier.cs +++ b/EXILED/Exiled.API/Structs/AttachmentIdentifier.cs @@ -131,16 +131,17 @@ internal AttachmentIdentifier(uint code, AttachmentName name, AttachmentSlot slo /// /// Converts the string representation of a to its equivalent. - /// A return value indicates whether the conversion is succeeded or failed. + /// A return value indicates whether the conversion succeeded or failed. /// - /// The to convert. - /// The converted . + /// The to convert. + /// The to get the from. + /// The converted . /// if was converted successfully; otherwise, . - public static bool TryParse(string s, out AttachmentIdentifier identifier) + public static bool TryParse(string name, FirearmType firearm, out AttachmentIdentifier identifier) { identifier = default; - foreach (AttachmentIdentifier attId in Features.Items.Firearm.AvailableAttachments.Values.SelectMany(kvp => kvp.Where(kvp2 => kvp2.Name.ToString() == s))) + foreach (AttachmentIdentifier attId in Features.Items.Firearm.AvailableAttachments[firearm].Where(kvp2 => kvp2.Name.ToString() == name)) { identifier = attId; return true; @@ -149,25 +150,9 @@ public static bool TryParse(string s, out AttachmentIdentifier identifier) return false; } - /// - /// Gets a by name. - /// - /// Weapons . - /// Attachment name. - /// instance. - public static AttachmentIdentifier Get(FirearmType type, AttachmentName name) => Features.Items.Firearm.AvailableAttachments[type].FirstOrDefault(identifier => identifier.Name == name); - - /// - /// Gets the all 's for type, by slot. - /// - /// Weapons . - /// Attachment slot. - /// instance. - public static IEnumerable Get(FirearmType type, AttachmentSlot slot) => Features.Items.Firearm.AvailableAttachments[type].Where(identifier => identifier.Slot == slot); - /// /// Converts the string representation of a to its equivalent. - /// A return value indicates whether the conversion is succeeded or failed. + /// A return value indicates whether the conversion succeeded or failed. /// /// The to convert. /// The converted . @@ -188,6 +173,56 @@ public static bool TryParse(string s, out AttachmentName name) return false; } + /// + /// Gets a by name. + /// + /// Weapons . + /// Attachment name. + /// instance. + public static AttachmentIdentifier Get(FirearmType type, AttachmentName name) => Features.Items.Firearm.AvailableAttachments[type].FirstOrDefault(identifier => identifier.Name == name); + + /// + /// Gets a IEnumerable of by name. + /// + /// Weapons . + /// Attachments name. + /// instances. + public static IEnumerable Get(FirearmType type, IEnumerable names) => Features.Items.Firearm.AvailableAttachments[type].Where(identifier => names.Contains(identifier.Name)); + + /// + /// Gets a by the Attachment's code. + /// + /// Weapons . + /// Attachment code. + /// instance. + public static AttachmentIdentifier Get(FirearmType type, uint code) => Features.Items.Firearm.AvailableAttachments[type].FirstOrDefault(identifier => identifier.Code == code); + + /// + /// Gets a IEnumerable of by the Attachment's code. + /// + /// Weapons . + /// Attachments codes. + /// instances. + public static IEnumerable Get(FirearmType type, IEnumerable codes) => Features.Items.Firearm.AvailableAttachments[type].Where(identifier => codes.Contains(identifier.Code)); + + /// + /// Gets the all 's for type, by slot. + /// + /// Weapons . + /// Attachment slot. + /// instance. + public static IEnumerable Get(FirearmType type, AttachmentSlot slot) => Features.Items.Firearm.AvailableAttachments[type].Where(identifier => identifier.Slot == slot); + + /// + /// Gets a by the Attachment's code. + /// A return value indicates whether the conversion succeeded or failed. + /// + /// The code of the attachment. + /// The to get the from. + /// The converted . + /// if the was found; otherwise, . + public static bool TryGet(uint code, FirearmType firearm, out AttachmentIdentifier identifier) => (identifier = Get(firearm, code)).Code != 0; + /// public override bool Equals(object obj) => base.Equals(obj);