1010import org .machinemc .scriptive .style .TextFormat ;
1111
1212import java .util .*;
13+ import java .util .function .Consumer ;
1314
1415public abstract class BaseComponent implements Component {
1516
1617 private final List <Component > siblings ;
17- private @ Nullable Colour color ;
18- private @ Nullable Boolean bold , italic , underlined , strikethrough , obfuscated ;
19- private @ Nullable String font , insertion ;
18+ private TextFormat textFormat ;
19+ private @ Nullable String insertion ;
2020 private @ Nullable ClickEvent clickEvent ;
21- private @ Nullable HoverEvent hoverEvent ;
21+ private @ Nullable HoverEvent <?> hoverEvent ;
2222
2323 protected BaseComponent () {
24- this (new ArrayList <>());
24+ this (new ArrayList <>(), new TextFormat () );
2525 }
2626
27- protected BaseComponent (List <Component > siblings ) {
27+ protected BaseComponent (List <Component > siblings , TextFormat textFormat ) {
2828 this .siblings = siblings ;
29+ this .textFormat = textFormat ;
30+ }
31+
32+ @ Override
33+ public TextFormat getTextFormat () {
34+ return textFormat ;
35+ }
36+
37+ @ Override
38+ public void setTextFormat (TextFormat textFormat ) {
39+ this .textFormat = Objects .requireNonNull (textFormat , "textFormat" );
2940 }
3041
3142 public Optional <Colour > getColor () {
32- return Optional . ofNullable ( color );
43+ return textFormat . getColor ( );
3344 }
3445
3546 public void setColor (@ Nullable Colour color ) {
36- this . color = color ;
47+ textFormat . setColor ( color ) ;
3748 }
3849
3950 public Optional <Boolean > isBold () {
40- return Optional . ofNullable ( bold );
51+ return textFormat . getStyle ( ChatStyle . BOLD );
4152 }
4253
4354 public void setBold (@ Nullable Boolean bold ) {
44- this . bold = bold ;
55+ textFormat . setStyle ( ChatStyle . BOLD , bold ) ;
4556 }
4657
4758 public Optional <Boolean > isObfuscated () {
48- return Optional . ofNullable ( obfuscated );
59+ return textFormat . getStyle ( ChatStyle . OBFUSCATED );
4960 }
5061
5162 public void setObfuscated (@ Nullable Boolean obfuscated ) {
52- this . obfuscated = obfuscated ;
63+ textFormat . setStyle ( ChatStyle . OBFUSCATED , obfuscated ) ;
5364 }
5465
5566 public Optional <Boolean > isItalic () {
56- return Optional . ofNullable ( italic );
67+ return textFormat . getStyle ( ChatStyle . ITALIC );
5768 }
5869
5970 public void setItalic (@ Nullable Boolean italic ) {
60- this . italic = italic ;
71+ textFormat . setStyle ( ChatStyle . ITALIC , italic ) ;
6172 }
6273
6374 public Optional <Boolean > isUnderlined () {
64- return Optional . ofNullable ( underlined );
75+ return textFormat . getStyle ( ChatStyle . UNDERLINED );
6576 }
6677
6778 public void setUnderlined (@ Nullable Boolean underlined ) {
68- this . underlined = underlined ;
79+ textFormat . setStyle ( ChatStyle . UNDERLINED , underlined ) ;
6980 }
7081
7182 public Optional <Boolean > isStrikethrough () {
72- return Optional . ofNullable ( strikethrough );
83+ return textFormat . getStyle ( ChatStyle . STRIKETHROUGH );
7384 }
7485
7586 public void setStrikethrough (@ Nullable Boolean strikethrough ) {
76- this . strikethrough = strikethrough ;
87+ textFormat . setStyle ( ChatStyle . STRIKETHROUGH , strikethrough ) ;
7788 }
7889
7990 public Optional <String > getFont () {
80- return Optional . ofNullable ( font );
91+ return textFormat . getFont ( );
8192 }
8293
8394 public void setFont (@ Nullable String font ) {
84- this . font = font ;
95+ textFormat . setFont ( font ) ;
8596 }
8697
8798 @ Override
@@ -105,12 +116,12 @@ public void setClickEvent(@Nullable ClickEvent clickEvent) {
105116 }
106117
107118 @ Override
108- public Optional <HoverEvent > getHoverEvent () {
119+ public Optional <HoverEvent <?> > getHoverEvent () {
109120 return Optional .ofNullable (hoverEvent );
110121 }
111122
112123 @ Override
113- public void setHoverEvent (@ Nullable HoverEvent hoverEvent ) {
124+ public void setHoverEvent (@ Nullable HoverEvent <?> hoverEvent ) {
114125 this .hoverEvent = hoverEvent ;
115126 }
116127
@@ -120,7 +131,17 @@ public void setHoverEvent(@Nullable HoverEvent hoverEvent) {
120131 }
121132
122133 @ Override
123- public Component append (Component component ) {
134+ public BaseComponent append (String literal ) {
135+ return (BaseComponent ) Component .super .append (literal );
136+ }
137+
138+ @ Override
139+ public BaseComponent append (String literal , TextFormat textFormat ) {
140+ return (BaseComponent ) Component .super .append (literal , textFormat );
141+ }
142+
143+ @ Override
144+ public BaseComponent append (Component component ) {
124145 siblings .add (component .clone ());
125146 return this ;
126147 }
@@ -130,101 +151,60 @@ public void clearSiblings() {
130151 siblings .clear ();
131152 }
132153
154+ @ Override
155+ public void inheritFrom (Component parent ) {
156+ textFormat .inheritFrom (parent .getTextFormat ());
157+ getInsertion ().ifPresentOrElse (k -> {}, () -> setInsertion (parent .getInsertion ().orElse (null )));
158+ getClickEvent ().ifPresentOrElse (k -> {}, () -> setClickEvent (parent .getClickEvent ().orElse (null )));
159+ getHoverEvent ().ifPresentOrElse (k -> {}, () -> setHoverEvent (parent .getHoverEvent ().orElse (null )));
160+ }
161+
133162 @ Override
134163 public void merge (Component other ) {
135164 if (other .hasSiblings ()) {
136165 clearSiblings ();
137166 other .getSiblings ().forEach (sibling -> append (sibling .clone ()));
138167 }
139- if (other .getColor ().isPresent ())
140- setColor (other .getColor ().get ());
141- if (other .isBold ().isPresent ())
142- setBold (other .isBold ().get ());
143- if (other .isItalic ().isPresent ())
144- setItalic (other .isItalic ().get ());
145- if (other .isUnderlined ().isPresent ())
146- setUnderlined (other .isUnderlined ().get ());
147- if (other .isStrikethrough ().isPresent ())
148- setStrikethrough (other .isStrikethrough ().get ());
149- if (other .isObfuscated ().isPresent ())
150- setObfuscated (other .isObfuscated ().get ());
151- if (other .getFont ().isPresent ())
152- setFont (other .getFont ().get ());
153- if (other .getInsertion ().isPresent ())
154- setInsertion (other .getInsertion ().get ());
155- if (other .getClickEvent ().isPresent ())
156- setClickEvent (other .getClickEvent ().get ());
157- if (other .getHoverEvent ().isPresent ())
158- setHoverEvent (other .getHoverEvent ().get ());
159- }
160-
161- @ Override
162- public TextFormat getFormat () {
163- Map <ChatStyle , @ Nullable Boolean > map = new HashMap <>();
164- isObfuscated ().ifPresent (obfuscated -> map .put (ChatStyle .OBFUSCATED , obfuscated ));
165- isBold ().ifPresent (bold -> map .put (ChatStyle .BOLD , bold ));
166- isStrikethrough ().ifPresent (strikethrough -> map .put (ChatStyle .STRIKETHROUGH , strikethrough ));
167- isUnderlined ().ifPresent (underlined -> map .put (ChatStyle .UNDERLINED , underlined ));
168- isItalic ().ifPresent (italic -> map .put (ChatStyle .ITALIC , italic ));
169- return new TextFormat (getColor ().orElse (null ), map );
170- }
171-
172- @ Override
173- public void applyFormat (TextFormat format ) {
174- format .getColor ().ifPresent (this ::setColor );
175- format .getStyle (ChatStyle .BOLD ).ifPresent (this ::setBold );
176- format .getStyle (ChatStyle .OBFUSCATED ).ifPresent (this ::setObfuscated );
177- format .getStyle (ChatStyle .STRIKETHROUGH ).ifPresent (this ::setStrikethrough );
178- format .getStyle (ChatStyle .UNDERLINED ).ifPresent (this ::setUnderlined );
179- format .getStyle (ChatStyle .ITALIC ).ifPresent (this ::setItalic );
168+
169+ textFormat .merge (other .getTextFormat ());
170+
171+ other .getInsertion ().ifPresent (this ::setInsertion );
172+ other .getClickEvent ().ifPresent (this ::setClickEvent );
173+ other .getHoverEvent ().ifPresent (this ::setHoverEvent );
180174 }
181175
182176 @ Override
183177 public String toLegacyString () {
184- List <Component > components = separatedComponents ();
178+ List <Component > components = toFlatList ();
185179 StringBuilder builder = new StringBuilder ();
186- for (Component component : components )
187- builder .append (toLegacyString (component ));
188- return builder .toString ();
189- }
190-
191- private static String toLegacyString (Component component ) {
192- StringBuilder builder = new StringBuilder ();
193- TextFormat format = component .getFormat ();
194- format .getColor ().ifPresent (color -> {
195- if (color .isDefaultColor ()) {
196- builder .append (color );
197- } else {
198- builder .append ("&x&" ).append (String .join ("&" , color .getHexString ().split ("" )));
199- }
200- });
201- for (ChatStyle style : format .getStyles (true ))
202- builder .append (style );
203- builder .append (component .flatten ());
180+ for (Component component : components ) {
181+ TextFormat format = component .getTextFormat ();
182+ format .getColor ().ifPresent (color -> {
183+ if (color .isDefaultColor ()) {
184+ builder .append (color );
185+ } else {
186+ builder .append ("&x&" ).append (String .join ("&" , color .getHexString ().split ("" )));
187+ }
188+ });
189+ for (ChatStyle style : format .getStyles (true ))
190+ builder .append (style );
191+ builder .append (component .getString ());
192+ }
204193 return builder .toString ();
205194 }
206195
207196 @ Override
208- public List <Component > separatedComponents () {
197+ public List <Component > toFlatList () {
209198 List <Component > components = new LinkedList <>();
210- addSeparatedComponents (clone (), components );
199+ addSeparatedComponents (clone (), components :: add );
211200 return components ;
212201 }
213202
214- private static void addSeparatedComponents (Component parent , List <Component > components ) {
215- components . add (parent );
203+ private static void addSeparatedComponents (Component parent , Consumer <Component > consumer ) {
204+ consumer . accept (parent );
216205 for (Component child : parent .getSiblings ()) {
217- child .getColor ().ifPresentOrElse (k -> {}, () -> child .setColor (parent .getColor ().orElse (null )));
218- child .isBold ().ifPresentOrElse (k -> {}, () -> child .setBold (parent .isBold ().orElse (null )));
219- child .isItalic ().ifPresentOrElse (k -> {}, () -> child .setItalic (parent .isItalic ().orElse (null )));
220- child .isUnderlined ().ifPresentOrElse (k -> {}, () -> child .setUnderlined (parent .isUnderlined ().orElse (null )));
221- child .isStrikethrough ().ifPresentOrElse (k -> {}, () -> child .setStrikethrough (parent .isStrikethrough ().orElse (null )));
222- child .isObfuscated ().ifPresentOrElse (k -> {}, () -> child .setObfuscated (parent .isObfuscated ().orElse (null )));
223- child .getFont ().ifPresentOrElse (k -> {}, () -> child .setFont (parent .getFont ().orElse (null )));
224- child .getInsertion ().ifPresentOrElse (k -> {}, () -> child .setInsertion (parent .getInsertion ().orElse (null )));
225- child .getClickEvent ().ifPresentOrElse (k -> {}, () -> child .setClickEvent (parent .getClickEvent ().orElse (null )));
226- child .getHoverEvent ().ifPresentOrElse (k -> {}, () -> child .setHoverEvent (parent .getHoverEvent ().orElse (null )));
227- addSeparatedComponents (child , components );
206+ child .inheritFrom (parent );
207+ addSeparatedComponents (child , consumer );
228208 }
229209 parent .clearSiblings ();
230210 }
@@ -234,13 +214,7 @@ private static void addSeparatedComponents(Component parent, List<Component> com
234214
235215 @ Override
236216 public Map <String , Object > asMap () {
237- Map <String , Object > map = new HashMap <>();
238- getColor ().ifPresent (color -> map .put ("color" , color .getName ()));
239- isBold ().ifPresent (bold -> map .put ("bold" , bold ));
240- isItalic ().ifPresent (italic -> map .put ("italic" , italic ));
241- isUnderlined ().ifPresent (underlined -> map .put ("underlined" , underlined ));
242- isStrikethrough ().ifPresent (strikethrough -> map .put ("strikethrough" , strikethrough ));
243- isObfuscated ().ifPresent (obfuscated -> map .put ("obfuscated" , obfuscated ));
217+ Map <String , Object > map = new HashMap <>(textFormat .asMap ());
244218 getInsertion ().ifPresent (insertion -> map .put ("insertion" , insertion ));
245219 getClickEvent ().ifPresent (clickEvent -> map .put ("clickEvent" , clickEvent .asMap ()));
246220 getHoverEvent ().ifPresent (hoverEvent -> map .put ("hoverEvent" , hoverEvent .asMap ()));
0 commit comments