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.
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:
Text or TextTemplate instead of recreating it every time the message is sent.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.
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:
TextorTextTemplateinstead of recreating it every time the message is sent.Another reason against the new method is that
Object...allows passing literally everything to thesendMessagemethod. 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
Textobject.Let's assume we have an object of a class called
Mail. With the new method,player.sendMessage(mail)is silently compiled using theObject...method and will sendcom.example.plugin.Mail@78308db1to the player. We actually wanted to callplayer.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 ofsendMessages(Text.of("A"), Text.of("B")). The first call will sendABinstead ofAandBon separate lines.You want to call
sendMessage(TextTemplate, Map<String, TextElement>)but you pass a map with an incompatible generic signature. Even thoughTextextendsTextElement, the following is silently compiled using theObject...method instead of sending the formattedTextTemplate:We've had a similar discussion several times before, primarily for the addition of a
String...overload: https://github.com/SpongePowered/SpongeAPI/issues/763The reason against an
Object...overload is similar: one of the purposes for the additionalText.ofcall is to make it absolutely clear that we can only send instances ofText. 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 formattedText.I'm going to close this issue for now, feel free to comment here if you would like to discuss this further.