@@ -215,11 +215,29 @@ public BaseComponent duplicateWithoutFormatting()
215
215
public static String toLegacyText (BaseComponent ... components )
216
216
{
217
217
StringBuilder builder = new StringBuilder ();
218
+ ComponentStyle currentLegacy = new ComponentStyle ();
219
+ currentLegacy .setColor ( ChatColor .RESET );
220
+
221
+ toLegacyText ( builder , currentLegacy , components );
222
+ return builder .toString ();
223
+ }
224
+
225
+ /**
226
+ * Converts the components to a string that uses the old formatting codes
227
+ * ({@link net.md_5.bungee.api.ChatColor#COLOR_CHAR}
228
+ *
229
+ * @param builder the StringBuilder to append to
230
+ * @param currentLegacy the style at the end of {@code builder}
231
+ * @param components the components to convert
232
+ * @return the style at the end of the legacy string
233
+ */
234
+ public static ComponentStyle toLegacyText (StringBuilder builder , ComponentStyle currentLegacy , BaseComponent ... components )
235
+ {
218
236
for ( BaseComponent msg : components )
219
237
{
220
- builder . append ( msg .toLegacyText () );
238
+ currentLegacy = msg .toLegacyText ( builder , currentLegacy );
221
239
}
222
- return builder . toString () ;
240
+ return currentLegacy ;
223
241
}
224
242
225
243
/**
@@ -273,15 +291,20 @@ public void setColor(ChatColor color)
273
291
*/
274
292
public ChatColor getColor ()
275
293
{
276
- if ( !style .hasColor () )
294
+ return getColor ( ChatColor .WHITE );
295
+ }
296
+
297
+ ChatColor getColor (ChatColor def )
298
+ {
299
+ if ( style .hasColor () )
277
300
{
278
- if ( parent == null )
279
- {
280
- return ChatColor .WHITE ;
281
- }
282
- return parent .getColor ();
301
+ return style .getColor ();
283
302
}
284
- return style .getColor ();
303
+ if ( parent == null )
304
+ {
305
+ return def ;
306
+ }
307
+ return parent .getColor ( def );
285
308
}
286
309
287
310
/**
@@ -651,46 +674,148 @@ void toPlainText(StringBuilder builder)
651
674
public String toLegacyText ()
652
675
{
653
676
StringBuilder builder = new StringBuilder ();
654
- toLegacyText ( builder );
677
+ ComponentStyle currentLegacy = new ComponentStyle ();
678
+ currentLegacy .setColor ( ChatColor .RESET );
679
+ toLegacyText ( builder , currentLegacy );
655
680
return builder .toString ();
656
681
}
657
682
658
- void toLegacyText (StringBuilder builder )
683
+ /**
684
+ * Converts the component to a string that uses the old formatting codes
685
+ * ({@link net.md_5.bungee.api.ChatColor#COLOR_CHAR}
686
+ *
687
+ * @param currentLegacy the style at the end of the string the result of this method will be appended to
688
+ * @return the string in the old format
689
+ */
690
+ public String toLegacyText (ComponentStyle currentLegacy )
659
691
{
660
- if ( extra != null )
692
+ StringBuilder builder = new StringBuilder ();
693
+ toLegacyText ( builder , currentLegacy );
694
+ return builder .toString ();
695
+ }
696
+
697
+ /**
698
+ * Converts the component to a string that uses the old formatting codes
699
+ * ({@link net.md_5.bungee.api.ChatColor#COLOR_CHAR}
700
+ *
701
+ * @param builder the StringBuilder to append to
702
+ * @param currentLegacy the style at the end of {@code builder}
703
+ * @return the style at the end of the legacy string
704
+ */
705
+ public ComponentStyle toLegacyText (StringBuilder builder , ComponentStyle currentLegacy )
706
+ {
707
+ currentLegacy = currentLegacy .clone ();
708
+ if ( !currentLegacy .hasColor () )
661
709
{
662
- for ( BaseComponent e : extra )
663
- {
664
- e .toLegacyText ( builder );
665
- }
710
+ currentLegacy .setColor ( ChatColor .RESET );
711
+ }
712
+ return toLegacyText ( builder , currentLegacy .hasColor () ? currentLegacy .getColor () : ChatColor .RESET , currentLegacy );
713
+ }
714
+
715
+ /**
716
+ * Converts the component to a string that uses the old formatting codes
717
+ * ({@link net.md_5.bungee.api.ChatColor#COLOR_CHAR}
718
+ *
719
+ * @param builder the StringBuilder to append to
720
+ * @param baseColor the color to use if no color is set, but a format downgrade is needed
721
+ * @param currentLegacy the style at the end of {@code builder}
722
+ * @return the new current style at the end of the {@code builder}
723
+ */
724
+ ComponentStyle toLegacyText (StringBuilder builder , ChatColor baseColor , ComponentStyle currentLegacy )
725
+ {
726
+ if ( extra == null )
727
+ {
728
+ return currentLegacy ;
666
729
}
730
+ for ( BaseComponent e : extra )
731
+ {
732
+ currentLegacy = e .toLegacyText ( builder , baseColor , currentLegacy );
733
+ }
734
+ return currentLegacy ;
667
735
}
668
736
669
- void addFormat ( StringBuilder builder )
737
+ private static boolean colorEquals ( ChatColor a , ChatColor b )
670
738
{
671
- if ( style . hasColor () || parent != null )
739
+ if ( a == b )
672
740
{
673
- builder . append ( getColor () ) ;
741
+ return true ;
674
742
}
675
- if ( isBold () )
743
+ if ( a == null || b == null )
676
744
{
677
- builder . append ( ChatColor . BOLD ) ;
745
+ return false ;
678
746
}
679
- if ( isItalic ( ) )
747
+ if ( ChatColor . RESET . equals ( a ) )
680
748
{
681
- builder . append ( ChatColor .ITALIC );
749
+ return ChatColor . WHITE . equals ( b ) || ChatColor .RESET . equals ( b );
682
750
}
683
- if ( isUnderlined ( ) )
751
+ if ( ChatColor . RESET . equals ( b ) )
684
752
{
685
- builder . append ( ChatColor .UNDERLINE );
753
+ return ChatColor . WHITE . equals ( a ) || ChatColor .RESET . equals ( a );
686
754
}
687
- if ( isStrikethrough () )
755
+ return a .equals ( b );
756
+ }
757
+
758
+ /**
759
+ * @param builder the StringBuilder to append to
760
+ * @param baseColor the color to use if no color is set, but a format downgrade is needed
761
+ * @param currentLegacy the style at the end of {@code builder}
762
+ * @return the new current style at the end of {@code builder}
763
+ */
764
+ ComponentStyle addFormat (StringBuilder builder , ChatColor baseColor , ComponentStyle currentLegacy )
765
+ {
766
+ // Check if we can skip adding color code
767
+ if ( colorEquals ( getColor (), currentLegacy .getColor () ) && currentLegacy .isLegacyFormattingUpgrade ( style ) )
768
+ {
769
+ if ( isBold () && !currentLegacy .isBold () )
770
+ {
771
+ builder .append ( ChatColor .BOLD );
772
+ }
773
+ if ( isItalic () && !currentLegacy .isItalic () )
774
+ {
775
+ builder .append ( ChatColor .ITALIC );
776
+ }
777
+ if ( isUnderlined () && !currentLegacy .isUnderlined () )
778
+ {
779
+ builder .append ( ChatColor .UNDERLINE );
780
+ }
781
+ if ( isStrikethrough () && !currentLegacy .isStrikethrough () )
782
+ {
783
+ builder .append ( ChatColor .STRIKETHROUGH );
784
+ }
785
+ if ( isObfuscated () && !currentLegacy .isObfuscated () )
786
+ {
787
+ builder .append ( ChatColor .MAGIC );
788
+ }
789
+ } else
688
790
{
689
- builder .append ( ChatColor .STRIKETHROUGH );
791
+ builder .append ( getColor ( baseColor ) == null ? baseColor : getColor ( baseColor ) );
792
+ if ( isBold () )
793
+ {
794
+ builder .append ( ChatColor .BOLD );
795
+ }
796
+ if ( isItalic () )
797
+ {
798
+ builder .append ( ChatColor .ITALIC );
799
+ }
800
+ if ( isUnderlined () )
801
+ {
802
+ builder .append ( ChatColor .UNDERLINE );
803
+ }
804
+ if ( isStrikethrough () )
805
+ {
806
+ builder .append ( ChatColor .STRIKETHROUGH );
807
+ }
808
+ if ( isObfuscated () )
809
+ {
810
+ builder .append ( ChatColor .MAGIC );
811
+ }
690
812
}
691
- if ( isObfuscated () )
813
+ currentLegacy = style ;
814
+ if ( currentLegacy .getColor () == null )
692
815
{
693
- builder .append ( ChatColor .MAGIC );
816
+ currentLegacy = style .clone ();
817
+ currentLegacy .setColor ( getColor ( baseColor ) == null ? baseColor : getColor ( baseColor ) );
694
818
}
819
+ return currentLegacy ;
695
820
}
696
821
}
0 commit comments