Giter VIP home page Giter VIP logo

Comments (15)

kashike avatar kashike commented on July 19, 2024 1

Yes, it is working as intended -- if you look at the above JSON you'll see that the second-last object has empty ("") text and green colouring - this is your last &a in the prefix. To continue with that formatting, you'd need to be appending to the last child component, not the root component itself.

from adventure.

kashike avatar kashike commented on July 19, 2024

Could you provide the code you're using, please? Which version of text are you using?

from adventure.

Crypnotic avatar Crypnotic commented on July 19, 2024

Velocity version: 1.0.0-SNAPSHOT (Latest)
Text version: 1.12-1.6.5 (Packaged with Velocity)

Specific code chunks: https://pastebin.com/DULhHL7J

Appending in code: https://github.com/Crypnotic/Neutron/blob/refactor/announcement/src/main/java/me/crypnotic/neutron/module/announcement/Announcement.java#L65

Color method for serializing: https://github.com/Crypnotic/Neutron/blob/refactor/announcement/src/main/java/me/crypnotic/neutron/util/StringHelper.java#L37-L44

TypeSerializer that is used to load from config: https://github.com/Crypnotic/Neutron/blob/refactor/announcement/src/main/java/me/crypnotic/neutron/api/serializer/TextComponentSerializer.java

from adventure.

Crypnotic avatar Crypnotic commented on July 19, 2024

Text values used in the screenshot

Prefix text: &7[&bPrefix&7] &a

Message text: This entire messages should be &agreen

from adventure.

kashike avatar kashike commented on July 19, 2024

This is actually how this is intended to work:

color("&7[&bPrefix&7] &a").append(color("This entire messages should be &agreen"))
{
  "text": "",
  "extra": [
    {
      "text": "[",
      "color": "gray"
    },
    {
      "text": "Prefix",
      "color": "aqua"
    },
    {
      "text": "] ",
      "color": "gray"
    },
    {
      "text": "",
      "color": "green"
    },
    {
      "text": "This entire messages should be ",
      "extra": [
        {
          "text": "green",
          "color": "green"
        }
      ]
    }
  ]
}

The "prefix" is part of one root, and the root doesn't have any style - the last child, which has empty content, is styled with green.

from adventure.

Crypnotic avatar Crypnotic commented on July 19, 2024

So you're saying this is working as intended? That's strange since the same string appended beforehand would yield the proper result

from adventure.

Crypnotic avatar Crypnotic commented on July 19, 2024

Component children are immutable. When appending onto a child component, would it return the child component or the root component?

from adventure.

kashike avatar kashike commented on July 19, 2024

It would return the child component.


This is one of the reasons that use of the legacy serialiser is discouraged, in favour of the JSON serialiser (or custom ones - YAML, XML, etc).

from adventure.

Crypnotic avatar Crypnotic commented on July 19, 2024

So there is essentially no way to apply a prefix with a trailing color code to a TextComponent without creating multiple new components just to get there?

from adventure.

Crypnotic avatar Crypnotic commented on July 19, 2024

Side note: I would use JSON, but I can't expect most people to take the time to create a proper JSON message just to send announcements. That's just opening an avenue to get littered with issues that are user error

from adventure.

kashike avatar kashike commented on July 19, 2024

You can deserialise all as one (color(prefix + message)) to create a component, otherwise you'll have to do a dance.

from adventure.

Crypnotic avatar Crypnotic commented on July 19, 2024

Which means if I want to support JSON messages also, I would have to serialize, deserialize into legacy, append, reserialize. Either direction requires a dance

from adventure.

kashike avatar kashike commented on July 19, 2024

Assuming the last bit of the prefix defines the colour for the text coming up, yes, you'd need to do a dance still:

    final Component prefix = color("&7[&bPrefix&7] &a");
    final List<Component> prefixChildren = prefix.children();
    final Style prefixLastChildStyle = prefixChildren.get(prefixChildren.size() - 1).style();
    final Component message = color("This entire messages should be &agreen");
    final Component result = prefix.append(TextComponent.builder().style(prefixLastChildStyle).append(message));

results in a component that serialises into the following JSON:

{
  "text": "",
  "extra": [
    {
      "text": "[",
      "color": "gray"
    },
    {
      "text": "Prefix",
      "color": "aqua"
    },
    {
      "text": "] ",
      "color": "gray"
    },
    {
      "text": "",
      "color": "green"
    },
    {
      "text": "",
      "extra": [
        {
          "text": "This entire messages should be ",
          "extra": [
            {
              "text": "green",
              "color": "green"
            }
          ]
        }
      ],
      "color": "green"
    }
  ]
}

and into the following legacy text:

§7[§bPrefix§7] §aThis entire messages should be green

The above code is based on text 3.0.0 - if you're on a previous version you'll have to manually apply any colouring + decorations, as Style does not exist in previous versions.

from adventure.

Crypnotic avatar Crypnotic commented on July 19, 2024

It's a good thing Velocity packages 1.6.5 🥇

from adventure.

Crypnotic avatar Crypnotic commented on July 19, 2024

Solved for now I guess. In case anyone needs a method that works on versions < 3.0.0

Neutron - StringHelper#append()

from adventure.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.