Commands and Send Message

I’m currently looking at @sk89q’s example plugin for the command syntax and by looking at it it seems I need a new class for each command. Would I be right in assuming this or can I have multiple commands in the one class?

Also, the sendMessage syntax confuses me a fair bit and I was wondering how I’d send a coloured message to a player. I’ve currently got the below though there’s something telling me it’s wrong.

player.sendMessage(TextColors.DARK_AQUA + 'This is a string');

If the above syntax is wrong, could someone please correct me. Thanks.

As for the player message you are missing a chat type you can find theme in the sponge API chat types located here.
As far as I can tell you do need class per command though I could be wrong on this.

So the following then?

player.sendMessage(ChatType.CHAT, TextColors.DARK_AQUA + 'This is a string');

Neither, guys.

player.sendMessage(
  Messages.builder("This is a string").color(TextColors.DARK_AQUA).build()
);

EDIT: Although now I see we really need a tutorial on chat. Will add to documentation.

5 Likes

Cheers for that.

What about something such as multiple message colors?

1 Like

Logically it would be the following though I could very well be wrong. (I probably am wrong)

player.sendMessage(
  Messages.builder("This is ").color(TextColors.DARK_AQUA)
          .builder("a string").color(TextColors.GOLD).build()
);

Not sure how that’s logical, but

player.sendMessage(
    Message.builder("This is ").color(TextColors.DARK_AQUA).append(
        Message.builder("a string").color(TextColors.GOLD).build()
    .build()
);

Appending more messages after that is easier:

Message.builder("This is ").color(TextColors.DARK_AQUA).append(
    Message.builder("a string").color(TextColors.GOLD).build())
).append(
    Message.builder("and another string").color(TextColors.GREEN).build())
.build()

Would you guys prefer something like:

Message.withColor("This is", TextColors.DARK_AQUA, 
    Message.withColor("another string", TextColors.GOLD),
    Message.withColor("and another string", TextColors.GREEN)
)

? that could save on some typing.

5 Likes

That would be much nicer.

Yes, that definitely looks nicer. Also, what @Pink__Slime said is logical, actually makes a lot of sense. Many builders for different Objects do it that way (ex: The artemis entity builder / Entity.edit()). They return a builder Object after you add something so you can chain the methods.

Usage Example

MessageBuilder builder = new MessageBuilder();
Message message = builder.color(TextColors.GOLD)
    .text("This is a string...")
    .append(yourExistingMessage)
    .color(TextColors.GREEN)
    .text("Green text.")
    .link("http://www.google.com")
        .color(TextColors.AQUA)
        .text("This is link text added to the link mesage builder.")
    .close()
    .text("TNT!!!!")
    .build()

The link method returns a new specific MessageBuilder for the links label. By calling close, you end the link and make it return the parent MessageBuilder.

4 Likes