Skip to content

MessageSender

Florian Fülling edited this page Jun 3, 2021 · 1 revision

The MessageSender provides a central object to send messages to players.

Setup

To setup a MessageSender for your plugin you need to create a instance.
Every Plugin can have one MessageSender instance. Or use the default MessageSender.

To create a MessageSender you have to call the create() Method.

MessageSender sender = MessageSender.create(pluginInstance, "§6[P]", '2', 'c');

This will create a MessageSender with the prefix §6[P], default color §2 and the error color §c; There is also a overloaded method where you can pass more formatting codes for normal and error messages

If you call the create method again you will change the settings of your current instance. You can't create a seconds MessageSender.

The instance is now registered and you can get it every time again by calling:

MessageSender sender = MessageSender.getPluginMessageSender(pluginInstance);

If you call get without creating a message sender for your plugin you will get the default message sender.

Usage

The message sender can simply send messages.
It will always add the prefix in front of your message.
If you use the §r code in your message it will change it to §r plus your default color. So you don't have to worry about weird coloring.

Currently you can use the sendMessage(player, "Message") and sendError(player, "Message") methods.

Localized Messages

The methods which have the localized word in their names provide a automatic localization.

If you use only one locale code in your message you can simply use:
messageSender.sendLocalizedMessage(player, "locale.code")

Of course it is also possible to add multiple locale codes:
messageSender.sendLocalizedMessage(player, "$locale.code$ and $another.code$ with some more text")

In this case both locale codes will be replaced.\

It is also possible to add replacements. Replacements will be applied after the locale codes are resolved.

Clone this wiki locally