/// <summary> /// Adds an item to the inventory. /// </summary> /// <param name="item">The item to add.</param> /// <returns>True if the item was added, false otherwise.</returns> public bool Add(Models.Items.Item item) { if (_items.Count >= 40) { Player.SendSystemMessage("INVENTORY_FULL"); return(false); } if (_items.TryAdd(item.ClientId, item)) { if (item.DbOwnerItem == null) { item.DbOwnerItem = new Database.Models.DbOwnerItem { PlayerId = Player.DbPlayer.Id, ItemId = (uint)item.DbItem.Id }; item.DbOwnerItem.MaxDura = item.DbItem.Amount; item.DbOwnerItem.CurrentDura = item.DbOwnerItem.MaxDura; item.Gem1 = Enums.Gem.NoSocket; item.Gem2 = Enums.Gem.NoSocket; item.DbOwnerItem.OwnedBy.AddItem(Player.Name); if (!item.DbOwnerItem.Create(Database.Models.DbOwnerItem.Inventories)) { return(false); } } else if (item.DbOwnerItem.Id == 0) { item.DbOwnerItem.PlayerId = Player.DbPlayer.Id; item.DbOwnerItem.OwnedBy.AddItem(Player.Name); if (!item.DbOwnerItem.Create(Database.Models.DbOwnerItem.Inventories)) { return(false); } } else { item.DbOwnerItem.PlayerId = Player.DbPlayer.Id; item.DbOwnerItem.OwnedBy.AddItem(Player.Name); if (!item.DbOwnerItem.Update(Database.Models.DbOwnerItem.Inventories)) { return(false); } } item.Position = Enums.ItemPosition.Inventory; item.UpdateClient(Player, Enums.ItemUpdateAction.Add); return(true); } return(false); }
/// <summary> /// Loses dura when the player is attacked. /// </summary> /// <param name="damage">The damage.</param> public void LoseDefenseDura(uint damage) { if (Player.Battle != null) { return; } if (damage <= 1 && Tools.CalculationTools.ChanceSuccessBig(10) || damage >= (Player.MaxHP / 2) && Tools.CalculationTools.ChanceSuccess(90) || Tools.CalculationTools.ChanceSuccessBig(15)) { Models.Items.Item duraItem = null; if (Tools.CalculationTools.ChanceSuccess(75)) { duraItem = Player.Equipments.Get(Enums.ItemPosition.Armor, false); } if (duraItem == null && Tools.CalculationTools.ChanceSuccess(75)) { duraItem = Player.Equipments.Get(Enums.ItemPosition.Head, false); } if (duraItem == null && Tools.CalculationTools.ChanceSuccess(75)) { duraItem = Player.Equipments.Get(Enums.ItemPosition.Necklace, false); } if (duraItem == null && Tools.CalculationTools.ChanceSuccess(75)) { duraItem = Player.Equipments.Get(Enums.ItemPosition.Ring, false); } if (duraItem == null && Tools.CalculationTools.ChanceSuccess(75)) { duraItem = Player.Equipments.Get(Enums.ItemPosition.Boots, false); } if (Data.Constants.GameMode.AllowTower && duraItem == null && Tools.CalculationTools.ChanceSuccess(75)) { duraItem = Player.Equipments.Get(Enums.ItemPosition.Tower, false); } if (Data.Constants.GameMode.AllowFan && duraItem == null && Tools.CalculationTools.ChanceSuccess(75)) { duraItem = Player.Equipments.Get(Enums.ItemPosition.Fan, false); } if (duraItem != null && duraItem.DbOwnerItem.CurrentDura > 0) { duraItem.DbOwnerItem.CurrentDura -= 100; duraItem.UpdateDatabase(); duraItem.UpdateClient(Player, Enums.ItemUpdateAction.Update); Player.UpdateBaseStats(); } } }
/// <summary> /// Updates the client with the changes. /// </summary> /// <param name="player">The player composing.</param> /// <param name="mainItem">The main item for the composition.</param> /// <remarks>It's necessary to remove and add to support the old composition way.</remarks> private static void UpdateClient(Models.Entities.Player player, Models.Items.Item mainItem) { if (mainItem != null) { player.ClientSocket.Send(new Models.Packets.Items.ItemActionPacket { Action = Enums.ItemAction.Remove, ClientId = mainItem.ClientId }); mainItem.UpdateClient(player, Enums.ItemUpdateAction.Add); } }
/// <summary> /// Equips an item. /// </summary> /// <param name="item">The item to equip.</param> /// <param name="position">The position to equip it at.</param> /// <param name="unequip">Set to true if existing equipment should be unequipped.</param> /// <param name="forceEquip">Set to true if the equipping should be forced.</param> /// <param name="init">Set to true if the equipping is during player initialization.</param> /// <returns>True if the equipment was a success, false otherwise.</returns> public bool Equip(Models.Items.Item item, Enums.ItemPosition position, bool unequip, bool forceEquip = false, bool init = false) { if (!Player.Alive) { return(false); } if (Player.Battle != null) { return(false); } if (!init) { #region Player Validation if (!forceEquip) { #region Gender Check if (item.IsFemale && !Player.IsFemale) { Player.SendSystemMessage("ITEM_FEMALE_ONLY"); return(false); } #endregion #region Job Check if (item.DbItem.Job > 0 && Tools.JobTools.GetBaseJob((Enums.Job)item.DbItem.Job) != Tools.JobTools.GetBaseJob(Player.Job)) { if (Player.Reborns == 0 || item.DbItem.Level > 70) { Player.SendSystemMessage("ITEM_INVALID_JOB"); return(false); } } #endregion #region Level Check if (Player.Level < item.DbItem.Level) { Player.SendSystemMessage("ITEM_INVALID_LEVEL"); return(false); } #endregion #region Stats Check if (Player.Strength < item.DbItem.Strength || Player.Agility < item.DbItem.Agility || Player.Vitality < item.DbItem.Vitality || Player.Spirit < item.DbItem.Spirit) { Player.SendSystemMessage("ITEM_STATS_LOW"); return(false); } #endregion #region Prof Check if (!Player.Spells.ContainsProficiency(item.DbItem.BaseId)) { var prof = Player.Spells.GetOrCreateProficiency(item.DbItem.BaseId); if (prof.Level < item.DbItem.WeaponSkill) { if (Player.Reborns == 0 || item.DbItem.Level > 70) { Player.SendSystemMessage("ITEM_INVALID_PROF"); return(false); } } } #endregion } #endregion #region Position Validation if (!forceEquip) { if (_maskedEquipments.ContainsKey(position)) { SendInvalidEquipMessage(1, item, position); return(false); } switch (position) { #region Head case Enums.ItemPosition.Head: { if (!item.IsHead) { SendInvalidEquipMessage(2, item, position); return(false); } break; } #endregion #region Necklace case Enums.ItemPosition.Necklace: { if (!item.IsNecklace) { SendInvalidEquipMessage(3, item, position); return(false); } break; } #endregion #region Ring case Enums.ItemPosition.Ring: { if (!item.IsRing) { SendInvalidEquipMessage(4, item, position); return(false); } break; } #endregion #region Armor case Enums.ItemPosition.Armor: { if (!item.IsArmor) { SendInvalidEquipMessage(5, item, position); return(false); } break; } #endregion #region Boots case Enums.ItemPosition.Boots: { if (!item.IsBoots) { SendInvalidEquipMessage(6, item, position); return(false); } break; } #endregion #region Bottle case Enums.ItemPosition.Bottle: { if (!item.IsBottle) { SendInvalidEquipMessage(7, item, position); return(false); } break; } #endregion #region Garment case Enums.ItemPosition.Garment: { if (!item.IsGarment) { SendInvalidEquipMessage(8, item, position); return(false); } break; } #endregion #region Steed case Enums.ItemPosition.Steed: { if (!Data.Constants.GameMode.AllowSteed) { SendInvalidEquipMessage(9, item, position); return(false); } if (Player.ContainsStatusFlag(Enums.StatusFlag.Riding)) { SendInvalidEquipMessage(10, item, position); return(false); } if (!item.IsSteed) { SendInvalidEquipMessage(11, item, position); return(false); } break; } #endregion #region SteedArmor case Enums.ItemPosition.SteedArmor: { if (!Data.Constants.GameMode.AllowSteed) { SendInvalidEquipMessage(12, item, position); return(false); } if (Player.ContainsStatusFlag(Enums.StatusFlag.Riding)) { SendInvalidEquipMessage(13, item, position); return(false); } if (!item.IsMountArmor) { SendInvalidEquipMessage(14, item, position); return(false); } break; } #endregion #region Fan case Enums.ItemPosition.Fan: { if (!Data.Constants.GameMode.AllowFan) { SendInvalidEquipMessage(15, item, position); return(false); } if (!item.IsFan) { SendInvalidEquipMessage(16, item, position); return(false); } break; } #endregion #region Tower case Enums.ItemPosition.Tower: { if (!Data.Constants.GameMode.AllowTower) { SendInvalidEquipMessage(17, item, position); return(false); } if (!item.IsTower) { SendInvalidEquipMessage(18, item, position); return(false); } break; } #endregion #region Right case Enums.ItemPosition.WeaponR: { if (!item.IsOneHand && !item.IsTwoHand) { SendInvalidEquipMessage(19, item, position); return(false); } if (_equipments.ContainsKey(Enums.ItemPosition.WeaponL) && item.IsTwoHand) { SendInvalidEquipMessage(20, item, position); return(false); } break; } #endregion #region Left case Enums.ItemPosition.WeaponL: { if (!item.IsOneHand && !item.IsShield && !item.IsArrow) { SendInvalidEquipMessage(21, item, position); return(false); } Models.Items.Item rightItem; if (!TryGetItem(Enums.ItemPosition.WeaponR, out rightItem)) { SendInvalidEquipMessage(22, item, position); return(false); } if (item.IsArrow && !rightItem.IsBow) { SendInvalidEquipMessage(23, item, position); return(false); } if (item.IsShield && !rightItem.IsOneHand) { SendInvalidEquipMessage(24, item, position); return(false); } break; } #endregion #region default default: { SendInvalidEquipMessage(-1, item, position); return(false); } #endregion } } #endregion if (!forceEquip) { if (!Player.Inventory.Remove(item.ClientId)) { return(false); } if (unequip && _equipments.ContainsKey(position)) { if (!Unequip(position)) { return(false); } } } } if (forceEquip && !init || _equipments.TryAdd(position, item)) { item.Position = position; if (!forceEquip && !init) { if (!item.DbOwnerItem.Create(Database.Models.DbOwnerItem.Equipments)) { return(false); } } if (Player.LoggedIn) { item.UpdateClient(Player, Enums.ItemUpdateAction.Add); Player.ClientSocket.Send(new Models.Packets.Items.ItemActionPacket { Player = this.Player, ClientId = item.ClientId, Action = Enums.ItemAction.Equip, Data1Low = (ushort)position }); SendGears(); } Player.UpdateBaseStats(); return(true); } return(false); }