/// <summary> /// Save <paramref name="slots" /> items from <paramref name="inventory" /> to the <paramref name="bb" />. /// </summary> /// <param name="bb">The writer for storing data</param> /// <param name="inventory">The array of items</param> /// <param name="slots">The amount of items in the inventory to save</param> /// <param name="stack">Whether or not the stack size should be saved</param> /// <param name="favourited">Whether or not the favourited state should be saved</param> static void SaveItemSlots(BinBuffer bb, Item[] inventory, int slots, bool stack, bool favourited) { for (int i = 0; i < slots; i++) { if (inventory[i].type < ItemID.Count) { bb.Write(String.Empty); // write an empty string instead of 'Vanilla' } else { // Save basic item data ItemDef item = Handler.ItemDef.DefsByType[inventory[i].type]; bb.Write(item.Mod.InternalName); bb.Write(item.InternalName); // why, vanilla writes these already? // only type + mod data is needed imo (and prefix type (+ data?) later on) if (stack) { bb.Write(inventory[i].stack); } bb.WriteByte(inventory[i].prefix); if (favourited) { bb.Write(inventory[i].favorited); } } // Save Mod Data if (inventory[i].P_BHandler != null) { ItemBHandler handler = (ItemBHandler)inventory[i].P_BHandler; handler.Save(bb); } else { bb.Write(0); } } }
internal static void OnSetDefaults(Item item, int type, bool noMatCheck) { Dictionary <string, BinBuffer> data = null; if (item.P_BHandler != null) { data = ((ItemBHandler)item.P_BHandler).data; } item.P_BHandler = null; item.P_UseSound = null; if (ModLoader.Reloading && !RecipeDefHandler.SettingUpRecipes) { item.RealSetDefaults(type, noMatCheck); if (!FillingVanilla) { Logging.LogWarning("Tried to call SetDefaults on an Item while [re|un]?loading mods."); } return; } ItemBHandler h = null; // will be set to <non-null> only if a behaviour handler will be attached item.RealSetDefaults(0, noMatCheck); if (Handler.ItemDef.DefsByType.ContainsKey(type)) { var d = Handler.ItemDef.DefsByType[type]; item.type = item.netID = type; item.width = item.height = 16; item.stack = item.maxStack = 1; Handler.ItemDef.CopyDefToEntity(d, item); if (RecipeDefHandler.SettingUpRecipes) { return; } if (d.CreateBehaviour != null) { h = new ItemBHandler(); var b = d.CreateBehaviour(); if (b != null) { b.Mod = d.Mod == PrismApi.VanillaInfo ? null : ModData.mods[d.Mod]; h.behaviours.Add(b); } } } else { item.RealSetDefaults(type, noMatCheck); } if (RecipeDefHandler.SettingUpRecipes) { return; } var bs = ModData.mods.Values .Select(m => new KeyValuePair <ModDef, ItemBehaviour>(m, m.ContentHandler.CreateGlobalItemBInternally())) .Where(kvp => kvp.Value != null) .Select(kvp => { kvp.Value.Mod = kvp.Key; return(kvp.Value); }); if (!bs.IsEmpty() && h == null) { h = new ItemBHandler(); } if (h != null) { h.behaviours.AddRange(bs); if (data != null) { h.data = data; } h.Create(); item.P_BHandler = h; foreach (var b in h.Behaviours) { b.Entity = item; } h.OnInit(); } }
internal static void OnSetDefaults(Item item, int type, bool noMatCheck) { Dictionary<string, BinBuffer> data = null; if (item.P_BHandler != null) data = ((ItemBHandler)item.P_BHandler).data; item.P_BHandler = null; item.P_UseSound = null; if (ModLoader.Reloading && !RecipeDefHandler.SettingUpRecipes) { item.RealSetDefaults(type, noMatCheck); if (!FillingVanilla) Logging.LogWarning("Tried to call SetDefaults on an Item while [re|un]?loading mods."); return; } ItemBHandler h = null; // will be set to <non-null> only if a behaviour handler will be attached item.RealSetDefaults(0, noMatCheck); if (Handler.ItemDef.DefsByType.ContainsKey(type)) { var d = Handler.ItemDef.DefsByType[type]; item.type = item.netID = type; item.width = item.height = 16; item.stack = item.maxStack = 1; Handler.ItemDef.CopyDefToEntity(d, item); if (RecipeDefHandler.SettingUpRecipes) return; if (d.CreateBehaviour != null) { h = new ItemBHandler(); var b = d.CreateBehaviour(); if (b != null) { b.Mod = d.Mod == PrismApi.VanillaInfo ? null : ModData.mods[d.Mod]; h.behaviours.Add(b); } } } else item.RealSetDefaults(type, noMatCheck); if (RecipeDefHandler.SettingUpRecipes) return; var bs = ModData.mods.Values .Select(m => new KeyValuePair<ModDef, ItemBehaviour>(m, m.ContentHandler.CreateGlobalItemBInternally())) .Where(kvp => kvp.Value != null) .Select(kvp => { kvp.Value.Mod = kvp.Key; return kvp.Value; }); if (!bs.IsEmpty() && h == null) h = new ItemBHandler(); if (h != null) { h.behaviours.AddRange(bs); if (data != null) h.data = data; h.Create(); item.P_BHandler = h; foreach (var b in h.Behaviours) b.Entity = item; h.OnInit(); } }
internal static void OnSetDefaults(Item i, int type, bool noMatCheck) { ItemBHandler h = null; i.SetDefaults(0, noMatCheck); if (DefFromType.ContainsKey(type)) { var d = DefFromType[type]; i.type = i.netID = type; i.width = i.height = 16; i.stack = i.maxStack = 1; CopyDefToItem(i, d); h = new ItemBHandler(); if (d.CreateBehaviour != null) { var b = d.CreateBehaviour(); if (b != null) h.behaviours.Add(b); } i.BHandler = h; } //TODO: add global hooks here (and check for null) if (h != null) { h.Create(); i.BHandler = h; foreach (var b in h.Behaviours) b.Entity = i; h.OnInit(); } }