示例#1
0
        /// <summary>
        /// Use a JSON Object to build the corresponding string
        /// </summary>
        /// <param name="data">JSON object to convert</param>
        /// <param name="colorcode">Allow parent color code to affect child elements (set to "" for function init)</param>
        /// <param name="links">Container for links from JSON serialized text</param>
        /// <returns>returns the Minecraft-formatted string</returns>
        private static string JSONData2String(Json.JSONData data, string colorcode, List <string> links)
        {
            string extra_result = "";

            switch (data.Type)
            {
            case Json.JSONData.DataType.Object:
                if (data.Properties.ContainsKey("color"))
                {
                    colorcode = TextColor.Color2tag(JSONData2String(data.Properties["color"], "", links));
                }
                if (data.Properties.ContainsKey("clickEvent") && links != null)
                {
                    Json.JSONData clickEvent = data.Properties["clickEvent"];
                    if (clickEvent.Properties.ContainsKey("action") &&
                        clickEvent.Properties.ContainsKey("value") &&
                        clickEvent.Properties["action"].StringValue == "open_url" &&
                        !String.IsNullOrEmpty(clickEvent.Properties["value"].StringValue))
                    {
                        links.Add(clickEvent.Properties["value"].StringValue);
                    }
                }
                if (data.Properties.ContainsKey("extra"))
                {
                    Json.JSONData[] extras = data.Properties["extra"].DataArray.ToArray();
                    foreach (Json.JSONData item in extras)
                    {
                        extra_result = extra_result + JSONData2String(item, colorcode, links) + "§r";
                    }
                }
                if (data.Properties.ContainsKey("text"))
                {
                    return(colorcode + JSONData2String(data.Properties["text"], colorcode, links) + extra_result);
                }
                else if (data.Properties.ContainsKey("translate"))
                {
                    List <string> using_data = new List <string>();
                    if (data.Properties.ContainsKey("using") && !data.Properties.ContainsKey("with"))
                    {
                        data.Properties["with"] = data.Properties["using"];
                    }
                    if (data.Properties.ContainsKey("with"))
                    {
                        Json.JSONData[] array = data.Properties["with"].DataArray.ToArray();
                        for (int i = 0; i < array.Length; i++)
                        {
                            using_data.Add(JSONData2String(array[i], colorcode, links));
                        }
                    }
                    return(colorcode + TranslateString(JSONData2String(data.Properties["translate"], "", links), using_data) + extra_result);
                }
                else
                {
                    return(extra_result);
                }

            case Json.JSONData.DataType.Array:
                string result = "";
                foreach (Json.JSONData item in data.DataArray)
                {
                    result += JSONData2String(item, colorcode, links);
                }
                return(result);

            case Json.JSONData.DataType.String:
                return(colorcode + data.StringValue);
            }

            return("");
        }