Yarn: Text -> TextComponent

Created on 26 Aug 2019  路  23Comments  路  Source: FabricMC/yarn

Why was this renamed? Component is the Mojang name, and calling it just "Text" makes code like this confusing:

    public Text method_2164(int mouseX) {
        if (message == null) {
            return null;
        }

        int width = minecraft.textRenderer.getStringWidth(message.asFormattedString());
        int x1 = this.width / 2 - width / 2;
        int x2 = this.width / 2 + width / 2;
        int x = x1;
        if (mouseX < x1 || mouseX > x2) {
            return null;
        }

        for (Text t : message) {
            x += minecraft.textRenderer.getStringWidth(TextComponentUtil.getRenderChatMessage(t.asString(), false));
            if (x > mouseX) {
                return t;
            }
        }

        return null;
    }

Looking at it quickly, you might think that message is a list of texts (for example lines), but actually, the code is iterating through the different components of message (plain text, styled text, links, etc.) to get the component the mouse is over.

discussion refactor

Most helpful comment

I have never ONCE used iteration over a message, and I doubt many modders have, either. I'm fine with TextComponent but Component is vague and unhelpful for 99.9% of the places people actually use it in mods.

All 23 comments

Why was this renamed?

737 #650

I disagree with Component because it is too generic.

I still uphold player's suggestion for Text for its brevity and clarity. Moreover, we are moving away from other such ambiguous terms (e.g. Container -> Menu proposal)

In your example, I believe renaming TextComponentUtil class to TextRenderUtil/TextRenderHelper and its getRenderChatMessage method to handleColor is a better solution to renaming Text class.

Component objectively sucks. We went over this a dozen times. We are not going over it again.

Clarity and usability should always come first above consistency with Mojang. The latter is nice to have, but never, ever at the cost of the former.

Clarity and usability

Have you even read the issue? The problem is that it's confusing.

It doesn't make sense to "iterate through the texts of a message". It does make sense to iterate through the components of the message.

there's a (soft) policy of using Mojang names where we know them

keyword is soft. If we decide that a name is bad for understandability, we won't use it.

Honestly, I personally wouldn't pick Text as the name either. That said, it's a hell of a lot better than Component, because it actually describes what it's used for (text) rather than being a generic name for "a piece of something" that makes zero sense out of context. TextComponent would be a nice middle ground, but I'm guessing that's not exactly going to be popular knowing this community.

keyword is soft.

Menu is a terrible name and you want to use it. (Text)Component is a good name and you don't want to use it.

Can you tell me which makes the most sense to you?

  • for (Component component : message) {
  • for (Text text : message) {

I have never ONCE used iteration over a message, and I doubt many modders have, either. I'm fine with TextComponent but Component is vague and unhelpful for 99.9% of the places people actually use it in mods.

It doesn't make sense to "iterate through the texts of a message". It does make sense to iterate through the components of the message.

Iterating through a json text or a rich text makes sense then.

for (Component component : message) {
The problem is that the thing is not a message; it is siblings instead. The first text with all these subparts in the stream is indeed the whole text in memory.

@Boundarybreaker Yarn doesn't just map what you use.

I don't have a problem with TextComponent either.

I like both Text and TextComponent, but Component is too vague.

Yarn is a system for understanding Minecraft code for people to be able to create mods, so where and when people use a class is key to how we should decide on what names to use. Understandability for the most common uses of a class has higher precedence to a name being more accurate to Mojang's internal name.

Yarn is also used by people who want to understand how the vanilla code works.

Understandability for the most common uses of a class has higher precedence to a name being more accurate to Mojang's internal name.

Sure, but there's a name that does both: TextComponent

At this point we really have to consider the benefits of changing the name of this class for the third time.
Arguments to change it:

  • Slightly closer to mojang's name. Why is this a good point ? It is not even the actual name so why bother ?
  • It makes it clearer that it is not always the full text. Is this required ? Most of the time they are actually used as the full text, making Component just as inaccurate. Text can be nested, nothing in its definition states that it has to be the full thing.
  • It makes the behaviour of the Style clearer. This is a good point, and it can be solved by changing the name of the individual styling methods rather than the name of the whole class.

Arguments against changing it:

  • Text is shorter than any alternative. Given how often this class is used, I think it matters.
  • This class' name has been changed too many times already. No matter what we set it to, it has been shown that some people will complain. Enough people think that yarn names change all the time for no reason already; you have to prove that the current name actually hinders development and that changing it would solve every issue. My personal observation is that no one complained about Text in regular development contexts.

We already discussed this on Discord, so I'm going to reiterate my suggestions:

for (Fragment fragment : message)

And

for (TextFragment fragment : message)

This fits with the narrative of what a message is. Any piece of Text can be considered a fragment, and any Text can be broken up into other fragments of text. Also in language a segment of text that is not itself a full sentence, if not a phrase, is called a "fragment."

A Fragment is also a piece, or a part, a shard. i.e. _Fragmentation_.

You can hear a _Fragment_ of a conversation, or write it down as a _fragment_ of text. Then tear up the page and throw away the _fragments._

This isn't so say I don't like Text, I'm fine with it. Text has all the benefits of TextFragment and is even shorter. All things considered, just as good as TextFragment, _definitely_ better than TextComponent and _waaaaaaay_ better than just Component. I wouldn't change it.

TextFragment is slightly better than TextComponent, but it is as far from the mojang name as Text. I think Text is indeed the superior option of the two.

I'd name it TextTreeNode as this is the name that matches its behavior the most closely.

   TextTreeNode setStyle(Style var1);

   Style getStyle();

   TextTreeNode addChild(String string_1);

   TextTreeNode addChild(TextTreeNode var1);

   String getContent(); // only self

   String getString(); // whole tree, overrides brigadier message api, no format

   String getString(int maxLength); // whole tree, truncated result possibly

   String getFormattedString(); // whole tree

   List<TextTreeNode> getChildren();

   Stream<TextTreeNode> streamTree();

   Stream<TextTreeNode> streamCopiedTreeNodes();

   Iterator<TextTreeNode> treeIterator();

   TextTreeNode copy(); // copy this node

   TextTreeNode formattedDeepCopy(); // copy the whole tree with style

   TextTreeNode format(Consumer<Style> consumer_1); // accept the style

   TextTreeNode format(Formatting... formattings_1); // mutates the node

   TextTreeNode format(Formatting formatting_1); // mutates the node

   static TextTreeNode formattedCopy(TextTreeNode text_1); // copy this node with style

But even though they are indeed tree nodes, we don't have a class that is restricted to present the whole tree, hence I still uphold the name Text.

Ok, it time to end this argument. I assume we can safely close this issue given the amount of opposition of the issue

@liach Many comments on here and Discord agree that Text is not a good name. If not TextComponent, it should be renamed to something else. Maybe TextPiece?

Compare these:

  • I'm iterating through the texts that make up this text.
  • I'm iterating through the pieces of text that make up this piece of text.

Mind making a vote in a new issue? Though minecraft text is ugly, but there is only one type of text in minecraft, so I'd say the name "text" suffices, while there are other component-like stuff (e.g. FoodComponent)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Juuxel picture Juuxel  路  5Comments

altrisi picture altrisi  路  4Comments

asiekierka picture asiekierka  路  3Comments

ChloeDawn picture ChloeDawn  路  6Comments

Runemoro picture Runemoro  路  4Comments