Spongeapi: Simplifying the procedure of sending message through MessageReceiver interface.

Created on 14 Jan 2017  路  3Comments  路  Source: SpongePowered/SpongeAPI

It would be very good to simplify this process by adding another method into this interface.
The method could look like default void sendMessage(Object... message) and then process all the objects using Text.of method to later use the default sendMessage method to handle it.

input wanted text

Most helpful comment

I personally agree with @kashike here. In my opinion, a method like this would be only useful for temporary debugging. In all other cases you should either:

  • Define a static constant for the Text or TextTemplate instead of recreating it every time the message is sent.
  • Load the messages from a configuration to allow users to change (e.g. to translate) them.

Another reason against the new method is that Object... allows passing literally everything to the sendMessage method. There is no longer any validation of the parameters because all combinations are valid.

This is a problem if you actually want to call one of the other methods. Right now you get a compile error if you forget to build a Text object.
Let's assume we have an object of a class called Mail. With the new method, player.sendMessage(mail) is silently compiled using the Object... method and will send com.example.plugin.Mail@78308db1 to the player. We actually wanted to call player.sendMessage(mail.getText()), but there is no way to notice this until we test this at runtime.

It might sound stupid to forget to call mail.getText() but you can run into these cases very easily:

  • You want to send multiple messages and call sendMessage(Text.of("A"), Text.of("B")) instead of sendMessages(Text.of("A"), Text.of("B")). The first call will send AB instead of A and B on separate lines.

  • You want to call sendMessage(TextTemplate, Map<String, TextElement>) but you pass a map with an incompatible generic signature. Even though Text extends TextElement, the following is silently compiled using the Object... method instead of sending the formatted TextTemplate:

    Map<String, Text> arguments = ImmutableMap.of("argument1", Text.of("Test"));
    player.sendMessage(template, arguments);
    

We've had a similar discussion several times before, primarily for the addition of a String... overload: https://github.com/SpongePowered/SpongeAPI/issues/763

The reason against an Object... overload is similar: one of the purposes for the additional Text.of call is to make it absolutely clear that we can only send instances of Text. You can neither send raw color codes like on Bukkit nor throw all your API objects into the chat - you will always have to build a properly formatted Text.

I'm going to close this issue for now, feel free to comment here if you would like to discuss this further.

All 3 comments

You're too lazy to do sendMessage(Text.of(...))? I don't agree with adding such a method at all.

Well I will just give a link to the discussion on forum: https://forums.spongepowered.org/t/lets-simplify-messagereceivers-method-sendmessage

I personally agree with @kashike here. In my opinion, a method like this would be only useful for temporary debugging. In all other cases you should either:

  • Define a static constant for the Text or TextTemplate instead of recreating it every time the message is sent.
  • Load the messages from a configuration to allow users to change (e.g. to translate) them.

Another reason against the new method is that Object... allows passing literally everything to the sendMessage method. There is no longer any validation of the parameters because all combinations are valid.

This is a problem if you actually want to call one of the other methods. Right now you get a compile error if you forget to build a Text object.
Let's assume we have an object of a class called Mail. With the new method, player.sendMessage(mail) is silently compiled using the Object... method and will send com.example.plugin.Mail@78308db1 to the player. We actually wanted to call player.sendMessage(mail.getText()), but there is no way to notice this until we test this at runtime.

It might sound stupid to forget to call mail.getText() but you can run into these cases very easily:

  • You want to send multiple messages and call sendMessage(Text.of("A"), Text.of("B")) instead of sendMessages(Text.of("A"), Text.of("B")). The first call will send AB instead of A and B on separate lines.

  • You want to call sendMessage(TextTemplate, Map<String, TextElement>) but you pass a map with an incompatible generic signature. Even though Text extends TextElement, the following is silently compiled using the Object... method instead of sending the formatted TextTemplate:

    Map<String, Text> arguments = ImmutableMap.of("argument1", Text.of("Test"));
    player.sendMessage(template, arguments);
    

We've had a similar discussion several times before, primarily for the addition of a String... overload: https://github.com/SpongePowered/SpongeAPI/issues/763

The reason against an Object... overload is similar: one of the purposes for the additional Text.of call is to make it absolutely clear that we can only send instances of Text. You can neither send raw color codes like on Bukkit nor throw all your API objects into the chat - you will always have to build a properly formatted Text.

I'm going to close this issue for now, feel free to comment here if you would like to discuss this further.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Lergin picture Lergin  路  5Comments

connorhartley picture connorhartley  路  3Comments

Cybermaxke picture Cybermaxke  路  4Comments

ryantheleach picture ryantheleach  路  5Comments

Katrix picture Katrix  路  3Comments