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
        private static void ConstructCommonPart(JObject jsonObject, ChatComponent component)
        {
            if (jsonObject.SelectToken("bold") != null)
            {
                component.Bold = jsonObject.SelectToken("bold").Value <bool>();
            }

            if (jsonObject.SelectToken("itatic") != null)
            {
                component.Itatic = jsonObject.SelectToken("itatic").Value <bool>();
            }

            if (jsonObject.SelectToken("underlined") != null)
            {
                component.Underlined = jsonObject.SelectToken("underlined").Value <bool>();
            }

            if (jsonObject.SelectToken("strikethrough") != null)
            {
                component.Strikethrough = jsonObject.SelectToken("strikethrough").Value <bool>();
            }

            if (jsonObject.SelectToken("obfuscated") != null)
            {
                component.Obfuscated = jsonObject.SelectToken("obfuscated").Value <bool>();
            }

            if (jsonObject.SelectToken("insertion") != null)
            {
                component.Insertion = jsonObject.SelectToken("insertion").Value <string>();
            }

            if (jsonObject.SelectToken("color") != null)
            {
                component.Color = (ColorType)jsonObject.SelectToken("color").Value <int>();
            }

            if (jsonObject.SelectToken("clickEvent") != null)
            {
                component.ClickEvent = new ChatClickEvent(
                    (ClickEventType)jsonObject.SelectToken("clickEvent.action").Value <int>(),
                    jsonObject.SelectToken("clickEvent.value"));
            }

            if (jsonObject.SelectToken("hoverEvent") != null)
            {
                component.HoverEvent = new ChatHoverEvent(
                    (HoverEventType)jsonObject.SelectToken("hoverEvent.action").Value <int>(),
                    jsonObject.SelectToken("hoverEvent.value"));
            }

            if (jsonObject.SelectToken("extra") != null)
            {
                component.Extra = new List <ChatComponent>();
                var extra = (JArray)jsonObject.SelectToken("extra");
                foreach (var comp in extra)
                {
                    component.Extra.Add(ParseCompoent((JObject)comp));
                }
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Chat"/> class with a string.
 /// It is convenient to initialize a Chat class with a StringComponent.
 /// </summary>
 /// <param name="text">The text of the StringComponent</param>
 public Chat(string text)
 {
     Component = new StringComponent(text);
 }
Exemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Chat"/> class with the specified component.
 /// </summary>
 /// <param name="component">The top-level component of this object.</param>
 public Chat(ChatComponent component)
 {
     Component = component;
 }