internal void ImportModdedItems(IJsonAssetsAPI API, BagConfig Target) { try { if (HasImportedItems) { return; } IModHelper Helper = ItemBagsMod.ModInstance.Helper; // Index all BagTypes by their names Dictionary <string, BagType> IndexedTypes = new Dictionary <string, BagType>(); foreach (BagType Type in Target.BagTypes) { if (!IndexedTypes.ContainsKey(Type.Name)) { IndexedTypes.Add(Type.Name, Type); } else { ItemBagsMod.ModInstance.Monitor.Log(string.Format("Warning - multiple BagTypes were found with the name: '{0}'\nDid you manually edit your bagconfig.json file or do you have multiple Modded Bags with the same name?", Type.Name), LogLevel.Warn); } } Dictionary <string, int> AllBigCraftableIds = new Dictionary <string, int>(); foreach (System.Collections.Generic.KeyValuePair <int, string> KVP in Game1.bigCraftablesInformation) { string ObjectName = KVP.Value.Split('/').First(); if (!AllBigCraftableIds.ContainsKey(ObjectName)) { AllBigCraftableIds.Add(ObjectName, KVP.Key); } } Dictionary <string, int> AllObjectIds = new Dictionary <string, int>(); foreach (System.Collections.Generic.KeyValuePair <int, string> KVP in Game1.objectInformation) { string ObjectName = KVP.Value.Split('/').First(); if (!AllObjectIds.ContainsKey(ObjectName)) { AllObjectIds.Add(ObjectName, KVP.Key); } } IDictionary <string, int> JABigCraftableIds = API.GetAllBigCraftableIds(); IDictionary <string, int> JAObjectIds = API.GetAllObjectIds(); // Import items from each ModAddon foreach (ModAddon ModAddon in ModAddons) { if (Helper.ModRegistry.IsLoaded(ModAddon.UniqueId)) { foreach (BagAddon BagAddon in ModAddon.BagAddons) { if (!IndexedTypes.TryGetValue(BagAddon.Name, out BagType TargetType)) { ItemBagsMod.ModInstance.Monitor.Log(string.Format("Warning - no BagType found with Name = '{0}'. Modded items for this bag will not be imported.", BagAddon.Name), LogLevel.Warn); } else { foreach (ModdedItem Item in BagAddon.Items) { int Id = -1; if (Item.ObjectId.HasValue) { Id = Item.ObjectId.Value; } else { if ((Item.IsBigCraftable && !JABigCraftableIds.TryGetValue(Item.Name, out Id) && !AllBigCraftableIds.TryGetValue(Item.Name, out Id)) || (!Item.IsBigCraftable && !JAObjectIds.TryGetValue(Item.Name, out Id) && !AllObjectIds.TryGetValue(Item.Name, out Id))) { string Message = string.Format("Warning - no item with Name = '{0}' was found. This item will not be imported to Bag '{1}'.", Item.Name, BagAddon.Name); ItemBagsMod.ModInstance.Monitor.Log(Message, LogLevel.Warn); continue; } } foreach (BagSizeConfig SizeConfig in TargetType.SizeSettings.Where(x => x.Size >= Item.Size)) { SizeConfig.Items.Add(new StoreableBagItem(Id, Item.HasQualities, null, Item.IsBigCraftable)); } } } } } } } catch (Exception ex) { ItemBagsMod.ModInstance.Monitor.Log(string.Format("Failed to import modded items. Error: {0}", ex.Message), LogLevel.Error); } finally { HasImportedItems = true; } }
private static void OnJsonAssetsIdsFixed(IJsonAssetsAPI API, BagConfig Target) { try { ItemBagsMod.ModdedItems.ImportModdedItems(API, ItemBagsMod.BagConfig); if (ItemBagsMod.TemporaryModdedBagTypes.Any()) { Dictionary <string, int> AllBigCraftableIds = new Dictionary <string, int>(); foreach (System.Collections.Generic.KeyValuePair <int, string> KVP in Game1.bigCraftablesInformation) { string ObjectName = KVP.Value.Split('/').First(); if (!AllBigCraftableIds.ContainsKey(ObjectName)) { AllBigCraftableIds.Add(ObjectName, KVP.Key); } } Dictionary <string, int> AllObjectIds = new Dictionary <string, int>(); foreach (System.Collections.Generic.KeyValuePair <int, string> KVP in Game1.objectInformation) { string ObjectName = KVP.Value.Split('/').First(); if (!AllObjectIds.ContainsKey(ObjectName)) { AllObjectIds.Add(ObjectName, KVP.Key); } } IDictionary <string, int> JABigCraftableIds = API.GetAllBigCraftableIds(); IDictionary <string, int> JAObjectIds = API.GetAllObjectIds(); // Now that JsonAssets has finished loading the modded items, go through each one, and convert the items into StoreableBagItems (which requires an Id instead of just a Name) foreach (System.Collections.Generic.KeyValuePair <ModdedBag, BagType> KVP in ItemBagsMod.TemporaryModdedBagTypes) { foreach (BagSizeConfig SizeCfg in KVP.Value.SizeSettings) { HashSet <string> FailedItemNames = new HashSet <string>(); List <StoreableBagItem> Items = new List <StoreableBagItem>(); foreach (ModdedItem DesiredItem in KVP.Key.Items.Where(x => x.Size <= SizeCfg.Size)) { StoreableBagItem Item = DesiredItem.ToStoreableBagItem(JABigCraftableIds, JAObjectIds, AllBigCraftableIds, AllObjectIds); if (Item == null) { FailedItemNames.Add(DesiredItem.Name); } else { Items.Add(Item); } } SizeCfg.Items = Items; if (FailedItemNames.Any()) { int MaxNamesShown = 5; string MissingItems = string.Format("{0}{1}", string.Join(", ", FailedItemNames.Take(MaxNamesShown)), FailedItemNames.Count <= MaxNamesShown ? "" : string.Format(" + {0} more", (FailedItemNames.Count - MaxNamesShown))); string WarningMsg = string.Format("Warning - {0} items could not be found for modded bag '{1} {2}'. Missing Items: {3}", FailedItemNames.Count, SizeCfg.Size.ToString(), KVP.Key.BagName, MissingItems); ItemBagsMod.ModInstance.Monitor.Log(WarningMsg, LogLevel.Warn); } } } ItemBag.GetAllBags(true).ForEach(x => x.OnJsonAssetsItemIdsFixed(API, true)); } } catch (Exception ex) { ItemBagsMod.ModInstance.Monitor.Log(string.Format("Failed to import modded bags. Error: {0}", ex.Message), LogLevel.Error); } }