Add a method to get the plain text of a Text object, without the plain text of it's childrens.
Maybe move the getContent() method from LiteralText to Text?
This text should return "Apple":
Text text = Text.builder("Apple").color(TextColors.RED).append(Text.of(TextColors.GREEN, "tree")).build();
Text{format=TextFormat{color=red, style=TextStyle{}}, children=[Text{format=TextFormat{color=green, style=TextStyle{}}, tree}], Apple}
Mind explaining this a little bit more in detail?
Whats wrong with LiteralText#getContent() : String?
What is that method supposed to return for ScoreText or the other types?
Although I must admit that it could be handy to have a method to get a (copy) of a Text(Builder) instance without children, because currently you cannot ever remove them.
@ST-DDT There is text.newBuilder().removeAll().build()
Whats wrong with LiteralText#getContent() : String?
It only works for LiteralText, not for other Text types
What is that method supposed to return for ScoreText or the other types?
The same as it would normally return with the toPlain() method. This method already works for all types of Texts.
There is text.newBuilder().removeAll().build()
That is a nice beginning.
So this method would just return text.newBuilder().removeAll().build().toPlain(), altough I still feel like getContent() should be available for all Text types so I am not closing this issue.
@ST-DDT There is text.newBuilder().removeAll().build()
@Minecrell Ups must have missed that, while writing the Text simplify code (for https://github.com/SpongePowered/SpongeAPI/issues/1312).
What is that method supposed to return for ScoreText or the other types?
The same as it would normally return with the toPlain() method. This method already works for all types of Texts.
@Bammerbom So you want a toPlainSelf() or toPlainSingle():
public String toPlainSelf() {
if (this instanceof LiteralText) {
return ((LiteralText) this).getContent();
} else {
// return "":
return TextSerializers.PLAIN.serialize(this.newBuilder().removeAll().build());
}
}
Yes, altough why wouldn't you just call it getContent() and make LiteralText override it?
@Bammerbom
No, because you don't get the content but rather a plain representation of the content. The content of a ScoreText is the score, not the way it is represented in plain text.
Most helpful comment
Whats wrong with LiteralText#getContent() : String?
It only works for LiteralText, not for other Text types
What is that method supposed to return for ScoreText or the other types?
The same as it would normally return with the toPlain() method. This method already works for all types of Texts.
There is text.newBuilder().removeAll().build()
That is a nice beginning.
So this method would just return text.newBuilder().removeAll().build().toPlain(), altough I still feel like getContent() should be available for all Text types so I am not closing this issue.