Exemple #1
0
        /**
         * Converts the old formatting system that used
         * {@link net.md_5.bungee.api.ChatColor#COLOR_CHAR} into the new json based
         * system.
         *
         * @param message the text to convert
         * @param defaultColor color to use when no formatting is to be applied
         * (i.e. after ChatColor.RESET).
         * @return the components needed to print the message to the client
         */
        public static BaseComponent[] FromLegacyText(String message, TextColor defaultColor)
        {
            List <BaseComponent> components = new List <BaseComponent>();
            StringBuilder        builder    = new StringBuilder();
            TextComponent        component  = new TextComponent();
            MatchCollection      matcher    = _urlRegex.Matches(message);

            TextComponent old;

            for (int i = 0; i < message.Length; i++)
            {
                char c = message[i];
                if (c == '§')
                {
                    if (++i >= message.Length)
                    {
                        break;
                    }

                    c = message[i];

                    if (c >= 'A' && c <= 'Z')
                    {
                        c += (char)32;
                    }

                    TextColor format = TextColor.GetColor(c);
                    if (format == null)
                    {
                        continue;
                    }
                    if (builder.Length > 0)
                    {
                        old       = component;
                        component = new TextComponent(old);
                        old.Text  = builder.ToString();
                        //old.setText(builder.toString());
                        builder = new StringBuilder();
                        components.Add(old);
                    }
                    switch (format.ToString())
                    {
                    case ChatFormatting.Bold:
                        component.Bold = true;
                        break;

                    case ChatFormatting.Italic:
                        component.Italic = true;
                        break;

                    case ChatFormatting.Underline:
                        component.Underlined = true;
                        break;

                    case ChatFormatting.Strikethrough:
                        component.Strikethrough = true;
                        break;

                    case ChatFormatting.Obfuscated:
                        component.Obfuscated = true;
                        break;

                    default:
                        format    = defaultColor;
                        component = new TextComponent(format.ToString());
                        break;
                    }
                    continue;
                }
                int pos = message.IndexOf(' ', i);
                if (pos == -1)
                {
                    pos = message.Length;
                }
                if (_urlRegex.Match(message, i, pos).Success)
                {                 //Web link handling
                    if (builder.Length > 0)
                    {
                        old       = component;
                        component = new TextComponent(old);
                        old.Text  = builder.ToString();
                        builder   = new StringBuilder();
                        components.Add(old);
                    }

                    old       = component;
                    component = new TextComponent(old);
                    string urlString = message.Substring(i, pos);
                    component.Text       = urlString;
                    component.ClickEvent = new ClickEvent(ClickEvent.Action.OpenUrl,
                                                          urlString.StartsWith("http") ? urlString : "http://" + urlString);

                    components.Add(component);
                    i        += pos - i - 1;
                    component = old;
                    continue;
                }
                builder.Append(c);
            }

            component.Text = builder.ToString();            // .setText(builder.toString());
            components.Add(component);

            return(components.ToArray());
        }
Exemple #2
0
 /**
  * Creates a TextComponent with formatting and text from the passed
  * component
  *
  * @param textComponent the component to copy from
  */
 public TextComponent(TextComponent textComponent) : base(textComponent)
 {
     //copyFormatting(textComponent, FormatRetention.ALL, true);
     //text = textComponent.text;
 }