Esempio n. 1
0
		public void ItemStackMaxStackSizeTest()
		{
			Item item = common.GetItem();
			ItemStack itemStack = new ItemStack();
			itemStack.Item = item;
			itemStack.Amount = Common.ItemStackSize * 2;
			Assert.AreEqual(item.StackSize, itemStack.Amount);
		}
Esempio n. 2
0
		public void ItemStackLimitlessSizeTest()
		{
			Item item = common.GetItem();
			ItemStack itemStack = new ItemStack();
			itemStack.Item = item;
			itemStack.IsLimited = false;
			itemStack.Amount = Common.ItemStackSize * 2;
			Assert.AreEqual(item.StackSize * 2, itemStack.Amount);
		}
Esempio n. 3
0
		public void ItemStackTypeTest()
		{
			Item item = common.GetItem();
			ItemStack itemStack = new ItemStack();
			Assert.AreEqual(null, itemStack.Item);
			itemStack.Item = item;
			Assert.AreEqual(item, itemStack.Item);
			itemStack.Amount = 0;
			Assert.AreEqual(null, itemStack.Item);
		}
Esempio n. 4
0
		/// <summary>
		/// Make clones of multiple ItemStacks
		/// </summary>
		/// <param name="stacks">ItemStacks</param>
		/// <returns>Cloned ItemStacks</returns>
		public static ItemStack[] CloneMultiple(bool temp = false, params ItemStack[] stacks)
		{
			ItemStack[] clonedStacks = new ItemStack[stacks.Length];
			for (int i = 0, j = stacks.Length; i < j; i++)
			{
				var stack = stacks[i];
                clonedStacks[i] = stack == null ? null : stack.Clone(temp);
			}
			return clonedStacks;
		}
		public void IncorrectShapelessInputTest()
		{
			var stick = FrameworkRegistry.GetItem("Stick");
			var stone = FrameworkRegistry.GetItem("Stone");

			var stoneStack = new ItemStack(stone, 30);
			var stickStack = new ItemStack(stick, 30);

			var recipe = new TestSpadeRecipe();

			var craftingField = new Container(16) { Width = 4 };
			craftingField.Add(1, stoneStack);
			craftingField.Add(2, stickStack);
			
			Assert.IsFalse(recipe.CheckRecipe(craftingField));
		}
		public void CorrectShapelessRecipeTest()
		{
			var stick = FrameworkRegistry.GetItem("Stick");
			var stone = FrameworkRegistry.GetItem("Stone");
			
			var stoneStack = new ItemStack(stone, 12);
			var stoneStack2 = new ItemStack(stone, 12);
			var stickStack = new ItemStack(stick, 12);
			
			var recipe = new TestSpadeRecipe();
			
			var craftingField = new Container(16) { Width = 4 };
			craftingField.Add(0, stoneStack);
			craftingField.Add(4, stoneStack2);
			craftingField.Add(12, stickStack);

			Assert.IsTrue(recipe.CheckRecipe(craftingField));
		}
Esempio n. 7
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. 8
0
		/// <summary>
		/// Called when an ItemStack empties
		/// </summary>
		/// <param name="itemStack"></param>
		private void onItemStackEmpty(ItemStack itemStack)
		{
			var id = itemStack.Id;
			if (id != Guid.Empty)
			{
				if (Items.Contains(id))
				{
					items[System.Array.IndexOf(items, id)] = null;
				}
			}
		}
Esempio n. 9
0
		public ItemStack Clone(bool temp = false)
		{
			ItemStack clone = new ItemStack(Item, Amount, false, temp);
			// Apply amount again if not limited, as ItemStacks are defaulted to limited
			if (!isLimited)
			{
				clone.IsLimited = false;
				clone.Amount = amount;
			}
			return clone;
		}
Esempio n. 10
0
		public void ItemContainerEmptyTest()
		{
			ItemStack i1 = new ItemStack(FrameworkRegistry.GetItem("Spade"), false, true);
			Container container = new Container(1);
			Assert.AreEqual(1, container.Items.Length);
			
			container.Add(i1);

			Assert.AreNotEqual(null, container.Get(0));

            container.Get(0).Amount = 0;

			Assert.AreEqual(null, container.Get(0));
		}
Esempio n. 11
0
		public void ItemContainerValidatorDeniedTest()
		{
			ItemStack i1 = new ItemStack(FrameworkRegistry.GetItem("Spade"), false, true);
			Container container = new Container(1);

			container.Validator += (int index, ItemStack itemStack, System.ComponentModel.CancelEventArgs args) =>
			{
				args.Cancel = true;
			};

			container.Add(i1);

			Assert.AreEqual(null, container.Get(0));
		}
Esempio n. 12
0
		public ShapedTestingRecipe() : base()
		{
			RecipeIngredients = new ItemStack[]
			{
					new ItemStack(FrameworkRegistry.GetItem("Stone"), 1, true, true),
					new ItemStack(FrameworkRegistry.GetItem("Stick"), 1, true, true),
					new ItemStack(FrameworkRegistry.GetItem("Stick"), 12, true, true)
            };
			width = 1;
			Output = new[]
			{
					new ItemStack()
					{
						Item = FrameworkRegistry.GetItem("Spade"),
						Amount = 1
					}
				};
		}
Esempio n. 13
0
		public TestSpadeRecipe() : base()
		{
			RecipeIngredients = new ItemStack[3]
			{
					new ItemStack(FrameworkRegistry.GetItem("Stick"), 10, true, true),
					new ItemStack(FrameworkRegistry.GetItem("Stone"), 10, true, true),
					new ItemStack(FrameworkRegistry.GetItem("Stone"), 3, true, true)
			};

			Output = new ItemStack[1]
			{
					new ItemStack(FrameworkRegistry.GetItem("Spade"), true, true)
			};
		}
Esempio n. 14
0
		public CraftingRecipe[] GetRecipes(ItemStack[] output)
		{
			return Recipes.Where(x => x.Output == output).ToArray();
		}
Esempio n. 15
0
		public CraftingRecipe[] GetRecipes(ItemStack[] input, ItemStack[] output)
		{
			return Recipes.Where(x => x.RecipeIngredients == input && x.Output == output).ToArray();
		}
Esempio n. 16
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;
		}
Esempio n. 17
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. 18
0
		/// <summary>
		/// Removes a set amount from an ItemStack at a given index and returns them as a new ItemStack
		/// </summary>
		/// <param name="index">Slot index</param>
		/// <param name="amount">Amount to remove from ItemStack</param>
		/// <returns>ItemStack containing the removed items</returns>
		public ItemStack Remove(int index, int amount)
		{
			//If there are no items to remove, remove no items
			if (!Items[index].HasValue)
			{
				return null;
			}

			//If asked to remove the amount or more of the stack, just remove the whole stack
			var tempStack = Get(index);
			if (amount >= tempStack.Amount)
			{
				Items[index] = null;

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

				return tempStack;
			}

			//If asked to remove part of the stack, create a new stack of the requested amount and remove said amount from the original stack
			var returnStack = new ItemStack
			{
				Item = tempStack.Item,
				Amount = amount
			};

			tempStack.Amount -= amount;

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

			return returnStack;
		}
Esempio n. 19
0
		/// <summary>
		/// Replaces an ItemStack at a given index with a new ItemStack
		/// </summary>
		/// <param name="index">Slot index</param>
		/// <param name="type">ItemStack to replace it with</param>
		/// <returns>Current ItemStack at given index</returns>
		public ItemStack Replace(int index, ItemStack type)
		{
			ItemStack tempStack = Get(index);
			Items[index] = type.Id;
			if (Changed != null)
			{
				Changed.Invoke();
			}
			return tempStack;
		}
Esempio n. 20
0
		/// <summary>
		/// Add ItemStacks to the Container.
		/// Fills already containing ItemStacks of same Item first.
		/// </summary>
		/// <param name="stacks">ItemStacks to add</param>
		/// <returns>Excess ItemStacks</returns>
		public ItemStack[] Add(params ItemStack[] stacks)
		{
			List<ItemStack> clonedStacks = new List<ItemStack>(ItemStack.CloneMultiple(true, stacks));
			bool containerChanged = false;
			for (int u = 0, j = clonedStacks.Count; u < j; u++)
			{
				ItemStack stack = clonedStacks[u];
				if (stack == null || stack.Item == null) continue;

				// First add to already existing stacks
				for (int i = 0, l = Items.Length; i < l; i++)
				{
					if (Items[i].HasValue)
					{
						ItemStack tempStack = Get(i);
						if (tempStack.Item.GetType() == stack.Item.GetType())
						{
							if (tempStack.Amount < tempStack.Item.StackSize)
							{
								int amountToAdd = Mathf.Min(tempStack.Item.StackSize - tempStack.Amount, stack.Amount);
								tempStack.Amount += amountToAdd;
								stack.Amount -= amountToAdd;
								containerChanged = true;

								if (stack.Item == null || stack.Amount == 0)
								{
									break;
								}
							}
						}
					}
				}

				// If the amount have been put into the Container, continue to next ItemStack
				if (stack.Item == null || stack.Amount == 0)
				{
					continue;
				}

				// Then find the first empty slot
				for (int k = 0, l = Items.Length; k < l; k++)
				{
					if (!Items[k].HasValue)
					{
						// Validate the ItemStack
						if (Validator != null)
						{
							var eventArgs = new CancelEventArgs();
							Validator(k, stack, eventArgs);
							if (eventArgs.Cancel)
							{
								continue;
							}
						}

						if (stack.Amount > stack.Item.StackSize)
						{
							Items[k] = new ItemStack(stack.Item, stack.Item.StackSize).Id;
							stack.Amount -= stack.Item.StackSize;
						}
						else
						{
							Items[k] = stack.GetPersistant().Id;
							containerChanged = true;
							stack.Amount = 0;
							break;
						}
					}
				}
			}
			ItemStack.TryDestroy(stacks);
			if (containerChanged)
			{
				if (Changed != null)
				{
					Changed.Invoke();
					SaveToDb();
				}
			}
			return clonedStacks.ToArray();
		}