improve this, make more efficient, cache repeated calls in variables, less nesting too,…
- User
improve this, make more efficient, cache repeated calls in variables, less nesting too, use c# codeblock:
/// /// Checks whether or not this bank instance has enough items to accept a take operation. /// /// The ItemId to see if it can be taken away. /// The quantity of above item to see if we can take away. /// Whether or not the item can be taken away from the bank in the requested quantity. public bool CanTakeItem(Guid itemId, int quantity) => FindItemQuantity(itemId) >= quantity;
public bool TryDepositItem(int slot, int amount, int destSlot = -1, bool sendUpdate = true) { //Permission Check if (mGuild != null) { var rank = Options.Instance.Guild.Ranks[Math.Max(0, Math.Min(Options.Instance.Guild.Ranks.Length - 1, mPlayer.GuildRank))]; if (!rank.Permissions.BankDeposit && mPlayer.GuildRank != 0) { PacketSender.SendChatMsg(mPlayer, Strings.Guilds.NotAllowedDeposit.ToString(mGuild.Name), ChatMessageType.Bank, CustomColors.Alerts.Error); return false; } } var itemBase = mPlayer.Items[slot].Descriptor; if (itemBase != null) { if (mPlayer.Items[slot].ItemId != Guid.Empty) { if ((!itemBase.CanBank && mGuild == null) || (!itemBase.CanGuildBank && mGuild != null)) { PacketSender.SendChatMsg(mPlayer, Strings.Items.nobank, ChatMessageType.Bank, CustomColors.Items.Bound); return false; } lock (mLock) { // if stackable, make sure the user actually has this many items or more. if (itemBase.IsStackable) { if (!mPlayer.CanTakeItem(itemBase.Id, amount)) { amount = mPlayer.FindInventoryItemQuantity(itemBase.Id); } } else { amount = 1; } var bankSlot = mBank?[destSlot]; if (CanStoreItem(new Item(itemBase.Id, amount), destSlot) && itemBase.IsStackable && itemBase.Id == bankSlot.ItemId && amount + bankSlot.Quantity <= bankSlot.Descriptor.MaxBankStack) { PutItem(new Item(itemBase.Id, amount), destSlot, sendUpdate); mPlayer.TryTakeItem(itemBase.Id, amount, ItemHandling.Normal, sendUpdate); if (sendUpdate) { PacketSender.SendInventoryItemUpdate(mPlayer, slot); } if (mGuild != null) { DbInterface.Pool.QueueWorkItem(mGuild.Save); } return true; } // Bank slot is full or item is not stackable, find another slot! if (!IsSlotOpen(destSlot)) { destSlot = FindOpenSlot(); } if (destSlot < 0 || destSlot > Options.Instance.Bank.MaxSlots) // Bank is full. { PacketSender.SendChatMsg(mPlayer, Strings.Banks.banknospace, ChatMessageType.Bank, CustomColors.Alerts.Error); return false; } PutItem(mPlayer.Items[slot], destSlot, sendUpdate); mPlayer.Items[slot].Set(Item.None); mPlayer.EquipmentProcessItemLoss(slot); if (sendUpdate) { PacketSender.SendInventoryItemUpdate(mPlayer, slot); } if (mGuild != null) { DbInterface.Pool.QueueWorkItem(mGuild.Save); } return true; } } } PacketSender.SendChatMsg(mPlayer, Strings.Banks.depositinvalid, ChatMessageType.Bank, CustomColors.Alerts.Error); return false; } - Assistant
/// /// Checks whether or not this bank instance has enough items to accept a take operation. /// /// The ItemId to see if it can be taken away. /// The quantity of above item to see if we can take away. /// Whether or not the item can be taken away from the bank in the requested quantity. public bool CanTakeItem(Guid itemId, int quantity) { int itemQuantity = FindItemQuantity(itemId); return itemQuantity >= quantity; }
public bool TryDepositItem(int slot, int amount, int destSlot = -1, bool sendUpdate = true) { //Permission Check if (mGuild != null && !Options.Instance.Guild.Ranks[Math.Max(0, Math.Min(Options.Instance.Guild.Ranks.Length - 1, mPlayer.GuildRank))].Permissions.BankDeposit && mPlayer.GuildRank != 0) { PacketSender.SendChatMsg(mPlayer, Strings.Guilds.NotAllowedDeposit.ToString(mGuild.Name), ChatMessageType.Bank, CustomColors.Alerts.Error); return false; }
ItemDescriptor itemBase = mPlayer.Items[slot].Descriptor; if (itemBase == null || mPlayer.Items[slot].ItemId == Guid.Empty) { PacketSender.SendChatMsg(mPlayer, Strings.Banks.depositinvalid, ChatMessageType.Bank, CustomColors.Alerts.Error); return false; } if ((!itemBase.CanBank && mGuild == null) || (!itemBase.CanGuildBank && mGuild != null)) { PacketSender.SendChatMsg(mPlayer, Strings.Items.nobank, ChatMessageType.Bank, CustomColors.Items.Bound); return false; } lock (mLock) { int maxAmount = itemBase.IsStackable ? mPlayer.FindInventoryItemQuantity(itemBase.Id) : 1; amount = Math.Min(amount, maxAmount); if (!CanStoreItem(new Item(itemBase.Id, amount), destSlot)) { PacketSender.SendChatMsg(mPlayer, Strings.Banks.banknospace, ChatMessageType.Bank, CustomColors.Alerts.Error); return false; } if (itemBase.IsStackable && destSlot != -1 && mBank[destSlot].ItemId == itemBase.Id && mBank[destSlot].Quantity + amount <= mBank[destSlot].Descriptor.MaxBankStack) { PutItem(new Item(itemBase.Id, amount), destSlot, sendUpdate); mPlayer.TryTakeItem(itemBase.Id, amount, ItemHandling.Normal, sendUpdate); if (sendUpdate) { PacketSender.SendInventoryItemUpdate(mPlayer, slot); } if (mGuild != null) { DbInterface.Pool.QueueWorkItem(mGuild.Save); } return true; } if (destSlot == -1 || destSlot > Options.Instance.Bank.MaxSlots || !IsSlotOpen(destSlot)) { destSlot = FindOpenSlot(); } if (destSlot == -1 || destSlot > Options.Instance.Bank.MaxSlots) { PacketSender.SendChatMsg(mPlayer, Strings.Banks.banknospace, ChatMessageType.Bank, CustomColors.Alerts.Error); return false; } PutItem(mPlayer.Items[slot], destSlot, sendUpdate); mPlayer.Items[slot].Set(Item.None); mPlayer.EquipmentProcessItemLoss(slot); if (sendUpdate) { PacketSender.SendInventoryItemUpdate(mPlayer, slot); } if (mGuild != null) { DbInterface.Pool.QueueWorkItem(mGuild.Save); } return true; }}