public override void Initialize(EntityProperties properties, ICoreAPI api, long InChunkIndex3d) { base.Initialize(properties, api, InChunkIndex3d); if (Inventory == null) { Inventory = new InventoryTrader("traderInv", "" + EntityId, api); } if (api.Side == EnumAppSide.Server) { try { string json = Properties.Server.Attributes["tradeProps"].ToJsonToken(); TradeProps = new JsonObject(json).AsObject <TradeProperties>(); } catch (Exception e) { api.World.Logger.Error("Failed deserializing TradeProperties for trader {0}, exception logged to verbose debug", properties.Code); api.World.Logger.VerboseDebug("Failed deserializing TradeProperties: {0}", e); api.World.Logger.VerboseDebug("================="); api.World.Logger.VerboseDebug("Tradeprops json:"); api.World.Logger.VerboseDebug("{0}", Properties.Server.Attributes["tradeProps"].ToJsonToken()); } if (OutfitCodes == null) { OutfitCodes = api.ModLoader.GetModSystem <TraderOutfits>().GetRandomOutfit(); } } else { talkUtil = new TalkUtil(api as ICoreClientAPI, this); } try { Inventory.LateInitialize("traderInv-" + EntityId, api, this); } catch (Exception e) { api.World.Logger.Error("Failed initializing trader inventory. Will recreate. Exception logged to verbose debug"); api.World.Logger.VerboseDebug("Failed initializing trader inventory. Will recreate. Exception {0}", e); WatchedAttributes.RemoveAttribute("traderInventory"); Inventory = new InventoryTrader("traderInv", "" + EntityId, api); Inventory.LateInitialize("traderInv-" + EntityId, api, this); RefreshBuyingSellingInventory(); } (AnimManager as TraderAnimationManager).Personality = this.Personality; this.Personality = this.Personality; // to update the talkutil }
public override void Initialize(EntityProperties properties, ICoreAPI api, long InChunkIndex3d) { base.Initialize(properties, api, InChunkIndex3d); if (Inventory == null) { Inventory = new InventoryTrader("traderInv", "" + EntityId, api); } if (api.Side == EnumAppSide.Server) { try { string json = Properties.Server.Attributes["tradeProps"].ToJsonToken(); TradeProps = new JsonObject(json).AsObject <TradeProperties>(); } catch (Exception e) { api.World.Logger.Error("Failed deserializing TradeProperties, exception logged to verbose debug"); api.World.Logger.VerboseDebug("Failed deserializing TradeProperties: " + e); } } else { talkUtil = new TalkUtil(api as ICoreClientAPI, this); } try { Inventory.LateInitialize("traderInv-" + EntityId, api, this); } catch (Exception e) { api.World.Logger.Error("Failed initializing trader inventory. Will recreate. Exception logged to verbose debug"); api.World.Logger.VerboseDebug("Failed initializing trader inventory. Will recreate. Exception {0}", e); WatchedAttributes.RemoveAttribute("traderInventory"); Inventory = new InventoryTrader("traderInv", "" + EntityId, api); Inventory.LateInitialize("traderInv-" + EntityId, api, this); RefreshBuyingSellingInventory(); } }
private void Event_LevelFinalize() { foreach (var entitytype in capi.World.EntityTypes) { TradeProperties tradeProps = null; if (entitytype.Attributes?["tradeProps"].Exists == true) { try { tradeProps = entitytype.Attributes["tradeProps"].AsObject <TradeProperties>(); } catch (Exception e) { capi.World.Logger.Error("Failed deserializing tradeProps attribute for entitiy {0}, exception logged to verbose debug", entitytype.Code); capi.World.Logger.VerboseDebug("Failed deserializing TradeProperties: {0}", e); capi.World.Logger.VerboseDebug("================="); capi.World.Logger.VerboseDebug("Tradeprops json:"); capi.World.Logger.VerboseDebug("{0}", entitytype.Server?.Attributes["tradeProps"].ToJsonToken()); } } if (tradeProps == null) { continue; } foreach (var val in tradeProps.Selling.List) { if (val.Resolve(capi.World, "tradehandbookinfo", true)) { var collobj = val.ResolvedItemstack.Collectible; if (collobj.Attributes == null) { collobj.Attributes = new JsonObject(JToken.Parse("{}")); } ExtraHandbookSection section; var bh = collobj.GetBehavior <CollectibleBehaviorHandbookTextAndExtraInfo>(); string title = Lang.Get("Sold by"); section = bh.ExtraHandBookSections?.FirstOrDefault(ele => ele.Title == title); if (section == null) { section = new ExtraHandbookSection() { Title = title, TextParts = new string[0] }; collobj.GetBehavior <CollectibleBehaviorHandbookTextAndExtraInfo>().ExtraHandBookSections = bh.ExtraHandBookSections == null ? new ExtraHandbookSection[] { section } : bh.ExtraHandBookSections.Append(section); } section.TextParts = section.TextParts.Append(Lang.Get(entitytype.Code.Domain + ":item-creature-" + entitytype.Code.Path)); } } foreach (var val in tradeProps.Buying.List) { if (val.Resolve(capi.World, "tradehandbookinfo", true)) { var collobj = val.ResolvedItemstack.Collectible; if (collobj.Attributes == null) { collobj.Attributes = new JsonObject(JToken.Parse("{}")); } ExtraHandbookSection section; var bh = collobj.GetBehavior <CollectibleBehaviorHandbookTextAndExtraInfo>(); string title = Lang.Get("Purchased by"); section = bh?.ExtraHandBookSections?.FirstOrDefault(ele => ele.Title == title); if (section == null) { section = new ExtraHandbookSection() { Title = title, TextParts = new string[0] }; collobj.GetBehavior <CollectibleBehaviorHandbookTextAndExtraInfo>().ExtraHandBookSections = bh.ExtraHandBookSections == null ? new ExtraHandbookSection[] { section } : bh.ExtraHandBookSections.Append(section); } section.TextParts = section.TextParts.Append(Lang.Get(entitytype.Code.Domain + ":item-creature-" + entitytype.Code.Path)); } } } }