Esempio n. 1
0
		/// <summary>
		/// Remove ItemStacks from the Container.
		/// </summary>
		/// <param name="stacks">ItemStacks to remove</param>
		/// <returns>Removed ItemStacks</returns>
		public ItemStack[] Remove(params ItemStack[] stacks)
		{
			bool containerChanged = false;
			List<ItemStack> removedItemStacks = new List<ItemStack>();
			for (int i = 0, j = stacks.Length; i < j; i++)
			{
				var stack = stacks[i];
				if (stack == null || stack.Amount <= 0) continue;

				ItemStack removedItemStack = new ItemStack(stack.Item, 1, false, true);

				for (int k = 0, l = Items.Length; k < l; k++)
				{
					var tempStack = Get(k);
					if (tempStack != null && tempStack.Item.GetType() == stack.Item.GetType())
					{
						int amountToRemove = Mathf.Min(tempStack.Amount, stack.Amount);
						tempStack.Amount -= amountToRemove;
						stack.Amount -= amountToRemove;
						removedItemStack.Amount += amountToRemove;
						containerChanged = true;
						if (tempStack.Amount == 0)
						{
							Items[k] = null;
						}
						if (stack.Amount == 0)
						{
							break;
						}
					}
				}

				removedItemStack.Amount--;

				removedItemStacks.Add(removedItemStack.GetPersistant());
			}

			if (containerChanged && Changed != null)
			{
				Changed.Invoke();
				SaveToDb();
			}

			return removedItemStacks.ToArray();
		}
Esempio n. 2
0
		/// <summary>
		/// Removes a set amount of a given item from the container
		/// </summary>
		/// <param name="item">Item type</param>
		/// <param name="amount">Amount to remove</param>
		/// <returns>ItemStack containing the removed items</returns>
		public ItemStack Remove(Item item, int amount)
		{
			bool containerChanged = false;

			ItemStack removedItemStack = new ItemStack(item, 1, false, true);

			for (int k = 0, l = Items.Length; k < l; k++)
			{
				var tempStack = Get(k);
				if (tempStack != null && tempStack.Item.GetType() == item.GetType())
				{
					int amountToRemove = Mathf.Min(tempStack.Amount, amount);
					tempStack.Amount -= amountToRemove;
					amount -= amountToRemove;
					removedItemStack.Amount += amountToRemove;
					containerChanged = true;
					if (tempStack.Amount == 0)
					{
						Items[k] = null;
					}
					if (amount == 0)
					{
						break;
					}
				}
			}

			removedItemStack.Amount--;

			if (containerChanged && Changed != null)
			{
				Changed.Invoke();
				SaveToDb();
			}

			return removedItemStack.GetPersistant();
		}
Esempio n. 3
0
		/// <summary>
		/// Add ItemStack to Container at specific slot.
		/// If it's already used, but samt Item, it will try to add the amount to the stack.
		/// </summary>
		/// <param name="index">Index of slot</param>
		/// <param name="stack">ItemStack to add</param>
		/// <returns>Excess or null if the whole ItemStack was added</returns>
		public ItemStack Add(int index, ItemStack stack)
		{
			if (stack == null)
			{
				return null;
			}

			if (stack.IsTemp)
			{
				stack = stack.GetPersistant();
			}

			//If the index is null just add the new items
			if (!Items[index].HasValue)
			{
				Items[index] = stack.GetPersistant().Id;
				return null;
			}
			else //If it's not null find a different index to place it at
			{
				ItemStack tmp = Get(index);
				if (tmp.Item.GetType() == stack.Item.GetType() && (tmp.Amount < tmp.Item.StackSize || !tmp.IsLimited))
				{
					int amountToAdd = stack.Amount;
					if (tmp.IsLimited)
					{
						amountToAdd = Mathf.Min(tmp.Item.StackSize - tmp.Amount, stack.Amount);
					}

					tmp.Amount += amountToAdd;
					stack.Amount -= amountToAdd;

					if (stack.Amount == 0)
					{
						return null;
					}
				}
			}

			return stack;
		}