Пример #1
0
        /// <summary>
        /// Removes an item from storage.
        /// </summary>
        /// <param name="storage">The storage to remove from.</param>
        /// <param name="amount">Amount to be removed, -1 for all.</param>
        /// <param name="predicate">The predicate to be used, can be null for none</param>
        /// <returns>true if something was removed and false if nothing was removed.</returns>
        public static bool DeleteInventoryItem <T>(IStorage storage, int amount = -1, Func <T, bool> predicate = null)
        {
            var item = typeof(T);

            if (storage.Inventory == null)
            {
                storage.Inventory = new List <IInventoryItem>();
            }
            if (amount == -1)
            {
                IInventoryItem[] items = predicate != null?storage.Inventory.FindAll(x => x.GetType() == item).Cast <T>().Where(predicate).Cast <IInventoryItem>().ToArray() : storage.Inventory.FindAll(x => x.GetType() == item).ToArray();

                foreach (var i in items)
                {
                    storage.Inventory.Remove(i);
                    OnStorageLoseItem?.Invoke(storage, new OnLoseItemEventArgs(i, amount));
                }
                OnStorageItemUpdateAmount?.Invoke(storage, new OnItemAmountUpdatedEventArgs(item, 0));

                if (GetInventoryFilledSlots(storage) <= storage.MaxInvStorage && storage.GetType() == typeof(Character) && ((Character)storage).Player.HasSharedData("OVERWEIGHT"))
                {
                    API.Shared.ResetEntitySharedData(((Character)storage).Player,
                                                     "OVERWEIGHT");
                }
                return(true);
            }

            IInventoryItem itm = predicate != null ? (IInventoryItem)storage.Inventory.Where(x => x.GetType() == item).Cast <T>().SingleOrDefault(predicate) : storage.Inventory.SingleOrDefault(x => x.GetType() == item);

            if (itm == null)
            {
                return(false);
            }

            itm.Amount -= amount;
            if (itm.Amount <= 0)
            {
                storage.Inventory.Remove(itm);
            }

            OnStorageLoseItem?.Invoke(storage, new OnLoseItemEventArgs(itm, amount));
            OnStorageItemUpdateAmount?.Invoke(storage, new OnItemAmountUpdatedEventArgs(item, itm.Amount));

            if (GetInventoryFilledSlots(storage) <= storage.MaxInvStorage && storage.GetType() == typeof(Character) && ((Character)storage).Player.HasSharedData("OVERWEIGHT"))
            {
                API.Shared.ResetEntitySharedData(((Character)storage).Player,
                                                 "OVERWEIGHT");
            }
            return(true);
        }
Пример #2
0
        /// <summary>
        /// Removes an item from storage.
        /// </summary>
        /// <param name="storage">The storage to remove from.</param>
        /// <param name="item">The item to remove.</param>
        /// <param name="amount">Amount to be removed, -1 for all.</param>
        /// <param name="predicate">The predicate to be used, can be null for none</param>
        /// <returns>true if something was removed and false if nothing was removed.</returns>
        public static bool DeleteInventoryItem(IStorage storage, Type item, int amount = -1, Func <IInventoryItem, bool> predicate = null)
        {
            if (storage.Inventory == null)
            {
                storage.Inventory = new List <IInventoryItem>();
            }
            if (amount == -1)
            {
                IInventoryItem[] items = predicate != null?storage.Inventory.FindAll(x => x.GetType() == item).Where(predicate).ToArray() : storage.Inventory.FindAll(x => x.GetType() == item).ToArray();

                foreach (var i in items)
                {
                    storage.Inventory.Remove(i);
                    OnStorageLoseItem?.Invoke(storage, new OnLoseItemEventArgs(i, amount));
                }
                OnStorageItemUpdateAmount?.Invoke(storage, new OnItemAmountUpdatedEventArgs(item, 0));
                LogManager.Log(LogManager.LogTypes.Storage, $"[{GetStorageInfo(storage)}] Removed Item '{ItemTypeToNewObject(item).LongName}', Amount: '{amount}'");

                if (GetInventoryFilledSlots(storage) <= storage.MaxInvStorage && storage.GetType() == typeof(Character) && ((Character)storage).Client.HasSharedData("OVERWEIGHT"))
                {
                    API.Shared.ResetEntitySharedData(((Character)storage).Client,
                                                     "OVERWEIGHT");
                }
                return(true);
            }

            IInventoryItem itm = predicate != null?storage.Inventory.Where(x => x.GetType() == item).SingleOrDefault(predicate) : storage.Inventory.SingleOrDefault(x => x.GetType() == item);

            if (itm == null)
            {
                return(false);
            }

            itm.Amount -= amount;
            if (itm.Amount <= 0)
            {
                storage.Inventory.Remove(itm);
            }

            OnStorageLoseItem?.Invoke(storage, new OnLoseItemEventArgs(itm, amount));
            OnStorageItemUpdateAmount?.Invoke(storage, new OnItemAmountUpdatedEventArgs(item, itm.Amount));
            LogManager.Log(LogManager.LogTypes.Storage, $"[{GetStorageInfo(storage)}] Removed Item '{ItemTypeToNewObject(item).LongName}', Amount: '{amount}'");

            if (GetInventoryFilledSlots(storage) <= storage.MaxInvStorage && storage.GetType() == typeof(Character) && ((Character)storage).Client.HasSharedData("OVERWEIGHT"))
            {
                API.Shared.ResetEntitySharedData(((Character)storage).Client,
                                                 "OVERWEIGHT");
            }
            return(true);
        }
Пример #3
0
        /// <summary>
        /// Gives player an item.
        /// NOTE: The item object is cloned, so the actual object passed isn't referenced.
        /// Use DeleteInventoryItem to take.
        /// </summary>
        /// <param name="storage">The storage you wanna add to.</param>
        /// <param name="sentitem">The item object, the Amount inside is ignored.</param>
        /// <param name="amount">The amount of this item to be added, item will be duplicated if not stackable.</param>
        /// <param name="ignoreBlocking">Add even if inventory is blocked due to big item.</param>
        /// <returns></returns>
        public static GiveItemErrors GiveInventoryItem(IStorage storage, IInventoryItem sentitem, int amount = 1, bool ignoreBlocking = false)
        {
            //We wanna clone and add it.
            var item = CloneItem(sentitem, amount);

            if (storage.Inventory == null)
            {
                storage.Inventory = new List <IInventoryItem>();
            }

            int maxAmount = -1;

            foreach (var amnt in item.MaxAmount)
            {
                if (amnt.Key == storage.GetType())
                {
                    maxAmount = amnt.Value;
                    break;
                }
            }

            int filled = GetInventoryFilledSlots(storage);

            if (storage.GetType() != typeof(Character))
            {
                filled += item.Amount * item.AmountOfSlots;
            }

            //Check if player has simliar item.
            var oldItem = storage.Inventory.FirstOrDefault(x => x.CommandFriendlyName == item.CommandFriendlyName);

            if (oldItem == null || oldItem.CanBeStacked == false)
            {
                if (maxAmount != -1 && (item.Amount + (oldItem?.Amount ?? 0) > maxAmount))
                {
                    return(GiveItemErrors.MaxAmountReached);
                }

                if (oldItem?.CommandFriendlyName == sentitem.CommandFriendlyName)
                {
                    return(GiveItemErrors.HasSimilarItem);
                }
                //Check if has enough space.

                if (filled <= storage.MaxInvStorage)
                {
                    //Set an id.
                    if (item.Id == ObjectId.Empty)
                    {
                        item.Id = ObjectId.GenerateNewId(DateTime.Now); sentitem.Id = item.Id;
                    }

                    //Add.
                    storage.Inventory.Add(item);
                    OnStorageGetItem?.Invoke(storage, new OnGetItemEventArgs(sentitem, amount));
                    OnStorageItemUpdateAmount?.Invoke(storage, new OnItemAmountUpdatedEventArgs(item.GetType(), item.Amount));
                    LogManager.Log(LogManager.LogTypes.Storage, $"[{GetStorageInfo(storage)}] Add Item '{item.LongName}', Amount: '{amount}'");

                    if (GetInventoryFilledSlots(storage) > storage.MaxInvStorage && storage.GetType() == typeof(Character))
                    {
                        API.Shared.SendChatMessageToPlayer(((Character)storage).Player, "You have gone overweight. You'll no longer be able to sprint or jump.");
                        API.Shared.SetEntitySharedData(((Character)storage).Player,
                                                       "OVERWEIGHT", true);
                    }
                    return(GiveItemErrors.Success);
                }
                else
                {
                    return(GiveItemErrors.NotEnoughSpace);
                }
            }
            else
            {
                if (maxAmount != -1 && item.Amount + (oldItem?.Amount ?? 0) > maxAmount)
                {
                    return(GiveItemErrors.MaxAmountReached);
                }

                //Make sure there is space again.
                if (filled <= storage.MaxInvStorage)
                {
                    //Add.
                    oldItem.Amount += item.Amount;
                    if (oldItem.Amount == 0)
                    {
                        storage.Inventory.Remove(oldItem);
                    }
                    OnStorageGetItem?.Invoke(storage, new OnGetItemEventArgs(sentitem, amount));
                    OnStorageItemUpdateAmount?.Invoke(storage, new OnItemAmountUpdatedEventArgs(item.GetType(), oldItem.Amount));
                    LogManager.Log(LogManager.LogTypes.Storage, $"[{GetStorageInfo(storage)}] Add Item '{item.LongName}', Amount: '{amount}'");

                    if (GetInventoryFilledSlots(storage) > storage.MaxInvStorage && storage.GetType() == typeof(Character))
                    {
                        API.Shared.SendChatMessageToPlayer(((Character)storage).Player, "You have gone overweight. You'll no longer be able to sprint or jump.");
                        API.Shared.SetEntitySharedData(((Character)storage).Player,
                                                       "OVERWEIGHT", true);
                    }
                    return(GiveItemErrors.Success);
                }
                else
                {
                    return(GiveItemErrors.NotEnoughSpace);
                }
            }
        }