/// <summary>
        ///
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static NpcShopDefinition TryLoadFromFile(string filePath)
        {
            NpcShopDefinition result = null;

            try
            {
                NpcShopsPlugin.Instance.LogPrint($"Loading NpcShop {filePath} ...", TraceLevel.Info);
                var txt = File.ReadAllText(filePath);
                result = JsonConvert.DeserializeObject <NpcShopDefinition>(txt);

                return(result);
            }
            catch (JsonReaderException jrex)
            {
                NpcShopsPlugin.Instance.LogPrint($"A json error occured while trying to load NpcShop {filePath}.", TraceLevel.Error);
                NpcShopsPlugin.Instance.LogPrint(jrex.Message, TraceLevel.Error);
            }
            catch (Exception ex)
            {
                NpcShopsPlugin.Instance.LogPrint($"An error occured while trying to load NpcShop {filePath}.", TraceLevel.Error);
                NpcShopsPlugin.Instance.LogPrint(ex.Message, TraceLevel.Error);
            }

            NpcShopsPlugin.Instance.LogPrint("Shop disabled.", TraceLevel.Error);

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="NpcShop" /> class with the specified definition.
        /// </summary>
        /// <param name="definition">The definition, which must not be <c>null</c>.</param>
        public NpcShop(NpcShopDefinition definition)
        {
            _definition = definition ?? throw new ArgumentNullException("NpcShopDefinition cannot be null.");

            if (!string.IsNullOrWhiteSpace(definition.RegionName))
            {
                var region = TShock.Regions.GetRegionByName(definition.RegionName);

                if (region == null)
                {
                    throw new Exception($"Could not find region named {definition.RegionName}.");
                }

                Rectangle = region.Area;
            }
            else
            {
                //ensure against nre's... create a dummy rectangle that will never be hit
                Rectangle = new Rectangle(-1, -1, 0, 0);
            }

            ParseNpcTypeOverrides(definition);

            //we have to create our products, and make sure they are in a valid state before we add them to the shop.
            ShopCommands = definition.ShopCommands.Select(sc => new ShopCommand(sc))
                           .Where(sc => sc.IsValid)
                           .ToList();

            ShopItems = definition.ShopItems.Select(si => new ShopItem(si))
                        .Where(si => si.IsValid)
                        .ToList();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Parses int and string ids for normal and custom npcs, and adds them to the NpcToShopMap.
        /// </summary>
        private void ParseNpcTypeOverrides(NpcShopDefinition definition)
        {
            if (definition.OverrideNpcTypes != null)
            {
                foreach (var npcType in definition.OverrideNpcTypes)
                {
                    string key = null;

                    if (npcType is string || npcType is long || npcType is int) //json.net will generate longs, but we add an int branch in case this changes for some reason...
                    {
                        key = npcType.ToString();
                    }
                    else
                    {
                        NpcShopsPlugin.Instance.LogPrint($"OverrideNpcType '{npcType.ToString()}' is not a string or int. Ignoring.", TraceLevel.Warning);
                    }

                    if (!string.IsNullOrWhiteSpace(key))
                    {
                        NpcToShopMap[key] = this;
                    }
                }
            }
        }