public static ICraftingTreeItem CreateCraftingTreeItem(int itemId, int requiredAmount, int currentAmount = 0) { var mcItem = _minecraftItemRepository.GetMinecraftItemById(itemId); var recipe = _craftingRecipeRepository.GetRecipesByMinecraftId(mcItem.MinecraftId).FirstOrDefault(); ICraftingTreeItem treeItem; if (recipe == null) { var smeltingRecipe = _smeltingRecipes.FirstOrDefault(x => x.ResultId == mcItem.Id); if (smeltingRecipe != null) { treeItem = CreateSmeltingItem(mcItem, smeltingRecipe, requiredAmount, currentAmount, parent: null); } else { treeItem = new CraftingTreeSimpleItem(item: mcItem, requiredAmount: requiredAmount, currentAmount: currentAmount, parent: null); } } else { treeItem = CreateRootCompoundItem(mcItem, recipe, requiredAmount, currentAmount); } return(treeItem); }
private static ICraftingTreeItem CreateChildItem(MinecraftItem mcItem, CraftingRecipe parentRecipe, int requiredAmount, int currentAmount) { ICraftingTreeItem childItem; var createSimpleItem = true; var recipe = _craftingRecipeRepository.GetRecipesByMinecraftId(mcItem.MinecraftId).FirstOrDefault(); var smeltingRecipe = _smeltingRecipes.FirstOrDefault(x => x.ResultId == mcItem.Id); if (smeltingRecipe != null) { var recipeAmount = parentRecipe.Ingredients.Where(x => x.Id == mcItem.Id).First().Count; childItem = CreateSmeltingItem(mcItem, smeltingRecipe, requiredAmount, currentAmount, recipeAmount: recipeAmount); return(childItem); } if (recipe != null) { recipe.Ingredients = _craftingRecipeRepository.GetIngredientsByRecipeId(recipe.Id).ToList(); recipe.Ingredients.ToList().ForEach(x => x = MinecraftIdConversion(x)); createSimpleItem = recipe.Ingredients.Select(x => x.IngredientMinecraftId).Contains(parentRecipe.ResultMinecraftId); } if (createSimpleItem) { var recipeAmount = parentRecipe.Ingredients.Where(x => x.Id == mcItem.Id).First().Count; childItem = new CraftingTreeSimpleItem(item: mcItem, recipeAmount: recipeAmount, currentAmount: currentAmount); } else { List <ICraftingTreeItem> ingredients = new List <ICraftingTreeItem>(); foreach (var ingredient in recipe.Ingredients) { var childMCItem = _minecraftItemRepository.GetMinecraftItemById(ingredient.Id); var ingredientItem = CreateChildItem(childMCItem, recipe, ingredient.Count, 0); ingredients.Add(ingredientItem); } var recipeAmount = parentRecipe.Ingredients.Where(x => x.Id == mcItem.Id).First().Count; childItem = new CraftingTreeCompoundItem(item: mcItem, ingredients: ingredients, recipeResultCount: recipe.ResultCount, recipeAmount: recipeAmount, currentAmount: currentAmount); ingredients.ForEach(x => x.Parent = childItem as ICraftingTreeCompoundItem); } return(childItem); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var jsonObject = JObject.Load(reader); var itemReader = jsonObject.CreateReader(); ICraftingTreeItem item = new CraftingTreeSimpleItem(); var hasIngredients = jsonObject.TryGetValue("Ingredients", out JToken ingredientsValue); if (hasIngredients) { item = new CraftingTreeCompoundItem(); } serializer.Populate(itemReader, item); return(item); }
private static ICraftingTreeItem CreateSmeltingIngredient(MinecraftItem mcItem, int currentAmount) { ICraftingTreeItem childItem; var createSimpleItem = true; var recipe = _craftingRecipeRepository.GetRecipesByMinecraftId(mcItem.MinecraftId).FirstOrDefault(); if (recipe != null) { createSimpleItem = false; } if (createSimpleItem) { var recipeAmount = 1; childItem = new CraftingTreeSimpleItem(item: mcItem, recipeAmount: recipeAmount, currentAmount: currentAmount, isSmeltingIngredient: true); } else { recipe.Ingredients = _craftingRecipeRepository.GetIngredientsByRecipeId(recipe.Id); recipe.Ingredients.ToList().ForEach(x => x = MinecraftIdConversion(x)); List <ICraftingTreeItem> ingredients = new List <ICraftingTreeItem>(); foreach (var ingredient in recipe.Ingredients) { var childMCItem = _minecraftItemRepository.GetMinecraftItemById(ingredient.Id); var ingredientItem = CreateChildItem(childMCItem, recipe, ingredient.Count, 0); ingredients.Add(ingredientItem); } var recipeAmount = 1; childItem = new CraftingTreeCompoundItem(item: mcItem, ingredients: ingredients, recipeResultCount: recipe.ResultCount, recipeAmount: recipeAmount, currentAmount: currentAmount, isSmeltingIngredient: true); ingredients.ForEach(x => x.Parent = childItem as ICraftingTreeCompoundItem); } return(childItem); }