public static Recipe BuildRecipe(RecipeStub rs, ObjectDB odb) { var recipe = ScriptableObject.CreateInstance <Recipe>(); recipe.m_item = rs.Item; recipe.m_craftingStation = odb.GetItemPrefab(rs.CraftingStation)?.GetComponentInChildren <CraftingStation>(true); if (recipe.m_craftingStation == null) { recipe.m_craftingStation = ZNetScene.instance?.GetPrefab(rs.CraftingStation)?.GetComponentInChildren <CraftingStation>(true); if (recipe.m_craftingStation == null) { Plugin.Log.LogInfo("BuildRecipe couldn't find crafting station " + rs.CraftingStation + " for " + rs.Name); return(null); } } if (!string.IsNullOrWhiteSpace(rs.RepairStation)) { recipe.m_repairStation = odb.GetItemPrefab(rs.RepairStation)?.GetComponentInChildren <CraftingStation>(true); if (recipe.m_repairStation == null) { recipe.m_repairStation = ZNetScene.instance?.GetPrefab(rs.RepairStation)?.GetComponentInChildren <CraftingStation>(true); if (recipe.m_repairStation == null) { Plugin.Log.LogInfo("BuildRecipe couldn't find declared repair station " + rs.RepairStation + " for " + rs.Name); return(null); } } if (recipe.m_repairStation == null) { return(null); } } recipe.m_minStationLevel = Mathf.Max(1, rs.MinStationLevel); List <Piece.Requirement> reqs = new List <Piece.Requirement>(); foreach (RecipeStub.RequirementStub r in rs.Requirements) { ItemDrop id = odb.GetItemPrefab(r.Component)?.GetComponentInChildren <ItemDrop>(true); if (!id) { Plugin.Log.LogInfo("BuildRecipe couldn't get requirement component " + r.Component + " for " + rs.Name); return(null); } Piece.Requirement pr = new Piece.Requirement(); pr.m_resItem = id; pr.m_amount = r.Amount; pr.m_amountPerLevel = r.AmountPerLevel; pr.m_recover = r.Recoverable; reqs.Add(pr); } recipe.m_resources = reqs.ToArray(); return(recipe); }
static void Postfix(ObjectDB __instance) { if (__instance.m_items.Count == 0 || __instance.GetItemPrefab("Amber") == null) { Utils.Log("Waiting for game to initialize before adding prefabs."); return; } MapSharingMadeEasy.instance.TryRegisterItems(); }
public static void ObjectDB_Awake_QuickSmeltPatch(ref ObjectDB __instance) { if (!TripleBronze.CraftBarsInForgeEnabled) { return; } if (!IsObjectDBGood()) { return; } List <CraftingStation> craftingStations = __instance.m_recipes .Where(r => r != null) .Select(i => i.m_craftingStation) .Where(r => r != null) .Distinct() .ToList(); ItemDrop woodDrop = __instance.GetItemPrefab(@"Wood")?.GetComponent <ItemDrop>(); ItemDrop coalDrop = __instance.GetItemPrefab(@"Coal")?.GetComponent <ItemDrop>(); ItemDrop copperOreDrop = __instance.GetItemPrefab(@"CopperOre")?.GetComponent <ItemDrop>(); ItemDrop copperDrop = __instance.GetItemPrefab(@"Copper")?.GetComponent <ItemDrop>(); ItemDrop tinOreDrop = __instance.GetItemPrefab(@"TinOre")?.GetComponent <ItemDrop>(); ItemDrop tinDrop = __instance.GetItemPrefab(@"Tin")?.GetComponent <ItemDrop>(); ItemDrop ironOreDrop = __instance.GetItemPrefab(@"IronOre")?.GetComponent <ItemDrop>(); ItemDrop ironScrapDrop = __instance.GetItemPrefab(@"IronScrap")?.GetComponent <ItemDrop>(); ItemDrop ironDrop = __instance.GetItemPrefab(@"Iron")?.GetComponent <ItemDrop>(); ItemDrop silverOreDrop = __instance.GetItemPrefab(@"SilverOre")?.GetComponent <ItemDrop>(); ItemDrop silverDrop = __instance.GetItemPrefab(@"Silver")?.GetComponent <ItemDrop>(); ItemDrop blackMetalScrapDrop = __instance.GetItemPrefab(@"BlackMetalScrap")?.GetComponent <ItemDrop>(); ItemDrop blackMetalDrop = __instance.GetItemPrefab(@"BlackMetal")?.GetComponent <ItemDrop>(); ItemDrop flametalOreDrop = __instance.GetItemPrefab(@"FlametalOre")?.GetComponent <ItemDrop>(); ItemDrop flametalDrop = __instance.GetItemPrefab(@"Flametal")?.GetComponent <ItemDrop>(); if (woodDrop == null) { logger.LogError($"woodDrop is null!"); return; } if (coalDrop == null) { logger.LogError($"coalDrop is null!"); return; } if (copperOreDrop == null) { logger.LogError($"copperOreDrop is null!"); return; } if (copperDrop == null) { logger.LogError($"copperDrop is null!"); return; } if (tinOreDrop == null) { logger.LogError($"tinOreDrop is null!"); return; } if (tinDrop == null) { logger.LogError($"tinDrop is null!"); return; } if (ironScrapDrop == null) { logger.LogError($"ironScrapDrop is null!"); return; } if (ironOreDrop == null) { logger.LogError($"ironOreDrop is null!"); return; } if (ironDrop == null) { logger.LogError($"ironDrop is null!"); return; } if (silverOreDrop == null) { logger.LogError($"silverOreDrop is null!"); return; } if (silverDrop == null) { logger.LogError($"silverDrop is null!"); return; } if (blackMetalScrapDrop == null) { logger.LogError($"blackMetalScrapDrop is null!"); return; } if (blackMetalDrop == null) { logger.LogError($"blackMetalDrop is null!"); return; } if (flametalOreDrop == null) { logger.LogError($"flametalOreDrop is null!"); return; } if (flametalDrop == null) { logger.LogError($"flametalDrop is null!"); return; } CraftingStation forge = craftingStations.Find(c => c.m_name.Contains(@"forge")); CraftingStation workbench = craftingStations.Find(c => c.m_name.Contains(@"workbench")); Recipe coalRecipe = MakeRecipe(Tuple.Create(workbench, 2), null, Tuple.Create(coalDrop, 1), Tuple.Create(woodDrop, 1)); Recipe copperBarRecipe = MakeRecipe(Tuple.Create(forge, 2), null, Tuple.Create(copperDrop, 1), Tuple.Create(coalDrop, (int)TripleBronze.CoalPerBar), Tuple.Create(copperOreDrop, 1)); Recipe tinBarRecipe = MakeRecipe(Tuple.Create(forge, 3), null, Tuple.Create(tinDrop, 1), Tuple.Create(coalDrop, (int)TripleBronze.CoalPerBar), Tuple.Create(tinOreDrop, 1)); Recipe ironBarRecipe1 = MakeRecipe(Tuple.Create(forge, 4), null, Tuple.Create(ironDrop, 1), Tuple.Create(coalDrop, (int)TripleBronze.CoalPerBar), Tuple.Create(ironScrapDrop, 1)); Recipe ironBarRecipe2 = MakeRecipe(Tuple.Create(forge, 4), null, Tuple.Create(ironDrop, 1), Tuple.Create(coalDrop, (int)TripleBronze.CoalPerBar), Tuple.Create(ironOreDrop, 1)); Recipe silverBarRecipe = MakeRecipe(Tuple.Create(forge, 4), null, Tuple.Create(silverDrop, 1), Tuple.Create(coalDrop, (int)TripleBronze.CoalPerBar), Tuple.Create(silverOreDrop, 1)); Recipe blackMetalBarRecipe = MakeRecipe(Tuple.Create(forge, 5), null, Tuple.Create(blackMetalDrop, 1), Tuple.Create(coalDrop, (int)TripleBronze.CoalPerBar), Tuple.Create(blackMetalScrapDrop, 1)); Recipe flametalBarRecipe = MakeRecipe(Tuple.Create(forge, 4), null, Tuple.Create(flametalDrop, 1), Tuple.Create(coalDrop, (int)TripleBronze.CoalPerBar), Tuple.Create(flametalOreDrop, 1)); Recipe[] recipes = new[] { coalRecipe, copperBarRecipe, tinBarRecipe, ironBarRecipe1, ironBarRecipe2, silverBarRecipe, blackMetalBarRecipe, flametalBarRecipe }; var howManyRecipesAlreadyHave = __instance.m_recipes.Count(recipe => recipes.Contains(recipe)); if (howManyRecipesAlreadyHave < recipes.Length) { __instance.m_recipes.RemoveAll(recipe => recipes.Contains(recipe)); } __instance.m_recipes.AddRange(recipes); }
/// <summary> /// Register our custom building pieces to their respective ingame items or stations /// </summary> private void TryRegisterPieces(ObjectDB instance) { Logger.LogMessage($"Registering custom pieces in ObjectDB {instance}"); // Get CraftingStations if necessary Instance.InitCraftingStations(instance); // Collect all current PieceTables from the items in ObjectDB var pieceTables = new List <PieceTable>(); foreach (var itemPrefab in instance.m_items) { var item = itemPrefab.GetComponent <ItemDrop>().m_itemData; if (item.m_shared.m_buildPieces != null && !pieceTables.Contains(item.m_shared.m_buildPieces)) { pieceTables.Add(item.m_shared.m_buildPieces); } } // Go through all registered Pieces and try to obtain references // to the actual objects defined as strings in PieceDef foreach (var entry in RegisteredPieces) { Logger.LogDebug($"GameObject: {entry.Key.name}"); var prefab = entry.Key; var pieceDef = entry.Value; var piece = prefab.GetComponent <Piece>(); if (piece == null) { Logger.LogError("GameObject has no Piece attached"); continue; } if (pieceDef == null) { Logger.LogError("No PieceDef available"); continue; } // Assign the piece to the actual PieceTable if not already in there var pieceTable = pieceTables.Find(x => x.name == pieceDef.PieceTable); if (pieceTable == null) { Logger.LogWarning($"Could not find piecetable: {pieceDef.PieceTable}"); continue; } if (pieceTable.m_pieces.Contains(prefab)) { Logger.LogDebug($"Piece already added to PieceTable {pieceDef.PieceTable}"); continue; } pieceTable.m_pieces.Add(prefab); // Assign the CraftingStation for this piece, if needed if (!string.IsNullOrEmpty(pieceDef.CraftingStation)) { var pieceStation = CraftingStations.GetValueSafe(pieceDef.CraftingStation); if (pieceStation == null) { Logger.LogWarning($"Could not find crafting station: {pieceDef.CraftingStation}"); var stationList = string.Join(", ", CraftingStations.Keys); Logger.LogDebug($"Available Stations: {stationList}"); } else { piece.m_craftingStation = pieceStation; } } // Assign all needed resources for this piece var resources = new List <Piece.Requirement>(); foreach (var resource in pieceDef.Resources) { var resourcePrefab = instance.GetItemPrefab(resource.Item); if (resourcePrefab == null) { Logger.LogError($"Could not load requirement item: {resource.Item}"); continue; } resources.Add(new Piece.Requirement { m_resItem = resourcePrefab.GetComponent <ItemDrop>(), m_amount = resource.Amount }); } piece.m_resources = resources.ToArray(); // Try to assign the effect prefabs of another extension defined in ExtendStation var stationExt = prefab.GetComponent <StationExtension>(); if (stationExt != null && !string.IsNullOrEmpty(pieceDef.ExtendStation)) { var stationPrefab = pieceTable.m_pieces.Find(x => x.name == pieceDef.ExtendStation); if (stationPrefab != null) { var station = stationPrefab.GetComponent <CraftingStation>(); stationExt.m_craftingStation = station; } var otherExt = pieceTable.m_pieces.Find(x => x.GetComponent <StationExtension>() != null); if (otherExt != null) { var otherStationExt = otherExt.GetComponent <StationExtension>(); var otherPiece = otherExt.GetComponent <Piece>(); stationExt.m_connectionPrefab = otherStationExt.m_connectionPrefab; piece.m_placeEffect.m_effectPrefabs = otherPiece.m_placeEffect.m_effectPrefabs.ToArray(); } } // Otherwise just copy the effect prefabs of any piece within the table else { var otherPiece = pieceTable.m_pieces.Find(x => x.GetComponent <Piece>() != null).GetComponent <Piece>(); piece.m_placeEffect.m_effectPrefabs.AddRangeToArray(otherPiece.m_placeEffect.m_effectPrefabs); } Logger.LogInfo($"Registered Piece {prefab.name}"); } }
/// <summary> /// Create a <see cref="Recipe" /> for a prefab based on the <see cref="ItemDef" /> of this custom item /// </summary> /// <param name="prefab">The item for which the recipe will be created</param> /// <param name="itemDef"></param> /// <param name="craftingStations"> /// List of stations which are allowed to act as the crafting and repair station for this /// item /// </param> /// <returns></returns> private Recipe CreateRecipe(ObjectDB instance, GameObject prefab, ItemDef itemDef) { var newRecipe = ScriptableObject.CreateInstance <Recipe>(); newRecipe.name = $"Recipe_{prefab.name}"; newRecipe.m_amount = itemDef.Amount; newRecipe.m_minStationLevel = itemDef.MinStationLevel; newRecipe.m_item = prefab.GetComponent <ItemDrop>(); newRecipe.m_enabled = itemDef.Enabled; // Assign the crafting station for this Recipe if defined in ItemDef if (!string.IsNullOrEmpty(itemDef.CraftingStation)) { var craftingStation = CraftingStations.GetValueSafe(itemDef.CraftingStation); if (craftingStation == null) { Logger.LogWarning($"Could not find crafting station: {itemDef.CraftingStation}"); var stationList = string.Join(", ", CraftingStations.Keys); Logger.LogDebug($"Available Stations: {stationList}"); } else { newRecipe.m_craftingStation = craftingStation; } } // Assign the repair station for this recipe if defined in ItemDef if (!string.IsNullOrEmpty(itemDef.RepairStation)) { var repairStation = CraftingStations.GetValueSafe(itemDef.RepairStation); if (repairStation == null) { Logger.LogWarning($"Could not find repair station: {itemDef.RepairStation}"); var stationList = string.Join(", ", CraftingStations.Keys); Logger.LogDebug($"Available Stations: {stationList}"); } else { newRecipe.m_repairStation = repairStation; } } // Create a requirement list and assign instances of the requirement prefabs to it var reqs = new List <Piece.Requirement>(); foreach (var requirement in itemDef.Resources) { var reqPrefab = instance.GetItemPrefab(requirement.Item); if (reqPrefab == null) { Logger.LogError($"Could not load requirement item: {requirement.Item}"); continue; } reqs.Add(new Piece.Requirement { m_amount = requirement.Amount, m_resItem = reqPrefab.GetComponent <ItemDrop>() }); } newRecipe.m_resources = reqs.ToArray(); return(newRecipe); }