Exemplo n.º 1
0
        private static ChatComponent ParseCompoent(JObject jsonObject)
        {
            ChatComponent component = null;

            // Construct the special part
            if (jsonObject.SelectToken("selector") != null)
            {
                component = new SelectorComponent(jsonObject.SelectToken("selector").Value <string>());
            }
            else if (jsonObject.SelectToken("score") != null)
            {
                var chatScore = new ChatScore(
                    jsonObject.SelectToken("score.name").Value <string>(),
                    jsonObject.SelectToken("score.objective").Value <string>());
                if (jsonObject.SelectToken("score.value") != null)
                {
                    chatScore.Value = jsonObject.SelectToken("score.value").Value <int>();
                }

                component = new ScoreComponent(chatScore);
            }
            else if (jsonObject.SelectToken("text") != null)
            {
                component = new StringComponent(jsonObject.SelectToken("text").Value <string>());
            }
            else if (jsonObject.SelectToken("translate") != null)
            {
                component = new TranslationComponent(jsonObject.SelectToken("translate").Value <string>());
                if (jsonObject.SelectToken("with") != null)
                {
                    // There should be elements when the `with` is not null
                    ((TranslationComponent)component).With = new List <ChatComponent>();
                    var with = (JArray)jsonObject.SelectToken("with");
                    foreach (var comp in with)
                    {
                        ((TranslationComponent)component).With.Add(ParseCompoent((JObject)comp));
                    }
                }
            }
            else if (jsonObject.SelectToken("keybind") != null)
            {
                component = new KeybindComponent((KeyBindType)jsonObject.SelectToken("keybind").Value <int>());
            }

            if (component == null)
            {
                throw new JsonException("Invalid JSON string.");
            }

            // Construct the common part
            ConstructCommonPart(jsonObject, component);

            return(component);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ScoreComponent"/> class ChatScore class.
 /// </summary>
 /// <param name="score">A ChatScore class</param>
 public ScoreComponent(ChatScore score)
 {
     Score = score;
 }