Example #1
0
        public override string GenerateJson(bool topLevel)
        {
            var b = new StringBuilder(base.GenerateJson(topLevel));

            if (!string.IsNullOrWhiteSpace(CustomName))
            {
                b.AppendFormat("CustomName:\"{0}\",", CustomName.EscapeJsonString());
            }
            if (!string.IsNullOrWhiteSpace(CustomName) && CustomNameVisible)
            {
                b.Append("CustomNameVisible:1b,");
            }
            if (CanPickUpLoot)
            {
                b.Append("CanPickUpLoot:1b,");
            }
            if (PersistanceRequired)
            {
                b.Append("PersistanceRequired:1b,");
            }
            if (NoAi)
            {
                b.Append("NoAI:1b,");
            }

            var holding  = Holding.GenerateJson(false);
            var boots    = Boots.GenerateJson(false);
            var leggings = Leggings.GenerateJson(false);
            var chest    = Chestplate.GenerateJson(false);
            var helm     = Helmet.GenerateJson(false);

            if (holding != string.Empty)
            {
                holding = holding.Remove(holding.Length - 1, 1);
            }

            if (boots != string.Empty)
            {
                boots = boots.Remove(boots.Length - 1, 1);
            }

            if (leggings != string.Empty)
            {
                leggings = leggings.Remove(leggings.Length - 1, 1);
            }

            if (chest != string.Empty)
            {
                chest = chest.Remove(chest.Length - 1, 1);
            }

            if (helm != string.Empty)
            {
                helm = helm.Remove(helm.Length - 1, 1);
            }

            if (holding != string.Empty || boots != string.Empty || leggings != string.Empty || chest != string.Empty || helm != string.Empty)
            {
                b.AppendFormat("Equipment:[0:{{{0}}},1:{{{1}}},2:{{{2}}},3:{{{3}}},4:{{{4}}}],", holding, boots, leggings, chest, helm);
            }

            if (DefaultHealth.HasValue && Health != DefaultHealth)
            {
                b.AppendFormat("HealF:{0}f,", Health);
            }

            StringBuilder aBuilder;

            if (PotionEffects.Count > 0)
            {
                aBuilder = new StringBuilder();
                for (var i = 0; i < PotionEffects.Count; i++)
                {
                    var attribute = PotionEffects[i];

                    var generateJson = attribute.GenerateJson(false);
                    if (!string.IsNullOrWhiteSpace(generateJson))
                    {
                        aBuilder.AppendFormat("{0}:{{{1}}},", i, generateJson);
                    }
                }

                aBuilder.Remove(aBuilder.Length - 1, 1);

                if (aBuilder.Length > 0)
                {
                    b.AppendFormat("ActiveEffects:[{0}],", aBuilder);
                }
            }


            if (Attributes.Count > 0)
            {
                aBuilder = new StringBuilder();
                for (var i = 0; i < Attributes.Count; i++)
                {
                    var attribute = Attributes[i];

                    var generateJson = attribute.GenerateJson(false);
                    if (!string.IsNullOrWhiteSpace(generateJson))
                    {
                        aBuilder.AppendFormat("{0}:{{{1}}},", i, generateJson);
                    }
                }

                aBuilder.Remove(aBuilder.Length - 1, 1);

                if (aBuilder.Length > 0)
                {
                    b.AppendFormat("Attributes:[{0}],", aBuilder);
                }
            }


            if (Math.Abs(DropChanceFloats[0].Value) > 0.001 || Math.Abs(DropChanceFloats[1].Value) > 0.001 ||
                Math.Abs(DropChanceFloats[2].Value) > 0.001 || Math.Abs(DropChanceFloats[3].Value) > 0.001 ||
                Math.Abs(DropChanceFloats[4].Value) > 0.001)
            {
                b.AppendFormat("DropChances:[0:{0:0.##}f,1:{1:0.##}f,2:{2:0.##}f,3:{3:0.##}f,4:{4:0.##}f],",
                               DropChanceFloats[0].Value, DropChanceFloats[1].Value, DropChanceFloats[2].Value, DropChanceFloats[3].Value,
                               DropChanceFloats[4].Value);
            }

            return(b.ToString());
        }