Combining Texts objects into one Texts object

Have been doing a lot of poking and playing with things trying to incoroporate pieces of info that is out there and learn the power of the systems (loving the command handling powers)…

Have come up short multiple different ways trying to combine Text objects created with a Texts.builder()… .build() system, into a single Text object.

Something like this works by nesting builds in builds:

    colorlist = Texts
               .builder("RED ").color(TextColors.RED).append(
                 Texts.builder("BLUE ").color(TextColors.BLUE).append(
                   Texts.builder("GOLD " ).color(TextColors.GOLD).append(
                    Texts.builder("GREEN ").color(TextColors.GREEN).build() )
                   .build() )
                 .build() )
               .build()  ;

But this is a bit clumsy, and limited to hardcoding.

I want to have a list of Texts programmatically generated, all being similar format, but with programmatic color, hover stuff, and onclick stuff associated with each one … and then combine them all together to make one Texts object to send as a message.

I cant figure out how to use a builder inside a

for (Text t: textobjects) { }

structure to ‘concatenate’ them together.

Well, I can, from the working example, if I coded a recursive function to append the called function, and close the call with a build - the algorithmic equivalent of the hardcoding - but even then, breaking up the builder object …how to keep it connected together across code boundaries?

I cant show what i’ve tried - I’ve erased and replaced so many variations of things and come up with an empty final object, i’ve lost track of which methods.

Perhaps Texts.join() is what you are looking for?…-

Jeeze… I got tunnel-vision on trying to build the object iteratively, and couldn’t picture how to pass a set of texts into it iteratively … but really its “pass AN ITERATIVE COLLECTION” as parameter…

Text main = Texts.join(textobjects) 

bingo bango done. Time to admit its time to leave coding to the weekends instead of after work :smile:

\Thanks for the point in the right direction

No problem. I haven’t done much with building Texts yet, but I have a feeling I would have had a similar question in the near future. I agree that the builder-append format feels clumsy and I’m glad SpongeAPI has got us covered.

There’s a separate “developer rambling” thread, but I was thinking earlier that a “SpongeAPI hidden gems” thread could be cool. A lot of smart people are thinking about this stuff and I think the little niceties of the API should get recognition in addition to the big features.

You can overload the of method:

Texts.of("Test ", TextColors.RED, "Message", TextColors.GOLD);



1 Like