Class MessageSender
- All Implemented Interfaces:
Copyable<MessageSender>
MessageSender is designed for use in Minecraft plugins to process messages before sending.
It supports dynamic message formatting, placeholder replacement, function-based text modifications,
and conditional logging. Messages are formatted based on a chain of text appliers, and can be sent
to one or more target players or even via webhook if no target is provided.
Key features:
- Manage a collection of
Placeholderobjects that are dynamically replaced in messages. - Support for custom
PlayerFormatterfunctions to alter messages based on player context. - Flexible target setting: messages can be sent to individual players, collections of players, or broadcast as system messages.
- Flag filtering using
Channel.Flagto control which message types are sent. - Automatic logging of messages if enabled, with an error prefix for unsent messages.
Example usage:
TakionLib lib = TakionLib.fromPlugin(plugin);
MessageSender sender = new MessageSender(lib, player);
sender.addPlaceholder("{player}", player.getName())
.addFunctions(s -> s.toUpperCase())
.setFlags(Channel.Flag.CHAT)
.setLogger(true)
.setErrorPrefix("invalid input: '&c'[ERROR]invalid input: '&'7 ");
// Send a message to the player
sender.send("Hello, {player}! Welcome to the server.");
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionMessageSender(MessageSender sender) Copy constructor to create a newMessageSenderfrom an existing instance.MessageSender(TakionLib lib) Constructs a newMessageSenderwith the specified library instance and no target players.MessageSender(TakionLib lib, Collection<? extends org.bukkit.entity.Player> targets) Constructs a newMessageSenderwith the specified library instance and target players.MessageSender(TakionLib lib, Collection<? extends org.bukkit.entity.Player> targets, org.bukkit.entity.Player parser) Constructs a newMessageSenderwith the specified library instance, target players, and parser.MessageSender(TakionLib lib, org.bukkit.entity.Player player) Constructs a newMessageSenderwith the specified library instance and a single player. -
Method Summary
Modifier and TypeMethodDescriptionfinal MessageSenderaddFunctions(UnaryOperator<String>... operators) Adds one or moreUnaryOperatorfunctions to modify messages.addFunctions(PlayerFormatter... functions) Adds one or morePlayerFormatterfunctions to modify messages.<T> MessageSenderaddPlaceholder(String key, T value) Adds a placeholder with a constant value.<T> MessageSenderaddPlaceholder(Placeholder<T> placeholder) Adds aPlaceholderto be applied in the message.final <T> MessageSenderaddPlaceholders(String[] keys, T... values) Adds placeholders using parallel arrays of keys and values.addPlaceholders(Collection<? extends Placeholder<?>> placeholders) Adds multiple placeholders from a collection.addPlaceholders(Placeholder<?>... placeholders) Adds multiple placeholders from an array.@NotNull MessageSendercopy()Creates a copy of thisMessageSender.booleanSends an array of messages to the target players.booleanSends a list of messages to the target players.setFlags(Channel.Flag... flags) Sets the flags that determine which types of messages are allowed to be sent.setTargets(Collection<? extends org.bukkit.entity.Player> targets) Sets the target players for message sending.setTargets(org.bukkit.command.CommandSender... senders) Sets the target players from an array ofCommandSenderobjects.setTargets(org.bukkit.entity.Player... targets) Sets the target players from an array ofPlayerobjects.
-
Constructor Details
-
MessageSender
public MessageSender(TakionLib lib, Collection<? extends org.bukkit.entity.Player> targets, org.bukkit.entity.Player parser) Constructs a newMessageSenderwith the specified library instance, target players, and parser.- Parameters:
lib- the TakionLib instance (must not benull)targets- the collection of target players; may benullor emptyparser- the player used as the formatting context; may benull
-
MessageSender
Constructs a newMessageSenderwith the specified library instance and target players.- Parameters:
lib- the TakionLib instancetargets- the collection of target players
-
MessageSender
Constructs a newMessageSenderwith the specified library instance and a single player.- Parameters:
lib- the TakionLib instanceplayer- the player to target (ifnull, no target is set)
-
MessageSender
Constructs a newMessageSenderwith the specified library instance and no target players.- Parameters:
lib- the TakionLib instance
-
MessageSender
Copy constructor to create a newMessageSenderfrom an existing instance.- Parameters:
sender- theMessageSenderto copy
-
-
Method Details
-
setTargets
Sets the target players for message sending.If the provided collection is
nullor empty, the targets are not modified.- Parameters:
targets- the collection of players to set as targets- Returns:
- this
MessageSenderinstance for chaining
-
setTargets
Sets the target players from an array ofCommandSenderobjects.Only those that are instances of
Playerare retained.- Parameters:
senders- an array of command senders- Returns:
- this
MessageSenderinstance for chaining
-
setTargets
Sets the target players from an array ofPlayerobjects.- Parameters:
targets- an array of players- Returns:
- this
MessageSenderinstance for chaining
-
addFunctions
Adds one or morePlayerFormatterfunctions to modify messages.- Parameters:
functions- an array of player formatters- Returns:
- this
MessageSenderinstance for chaining
-
addFunctions
Adds one or moreUnaryOperatorfunctions to modify messages.Each operator is wrapped as a
PlayerFormatterthat ignores the player parameter.- Parameters:
operators- an array of unary operators to modify messages- Returns:
- this
MessageSenderinstance for chaining
-
setFlags
Sets the flags that determine which types of messages are allowed to be sent.- Parameters:
flags- an array ofChannel.Flagvalues- Returns:
- this
MessageSenderinstance for chaining
-
addPlaceholder
Adds aPlaceholderto be applied in the message.- Type Parameters:
T- the type of the placeholder value- Parameters:
placeholder- the placeholder to add (its sensitive flag is updated to match this sender)- Returns:
- this
MessageSenderinstance for chaining
-
addPlaceholder
Adds a placeholder with a constant value.- Type Parameters:
T- the type of the placeholder value- Parameters:
key- the placeholder keyvalue- the constant value for the placeholder- Returns:
- this
MessageSenderinstance for chaining
-
addPlaceholders
Adds multiple placeholders from a collection.- Parameters:
placeholders- a collection of placeholders to add- Returns:
- this
MessageSenderinstance for chaining
-
addPlaceholders
Adds multiple placeholders from an array.- Parameters:
placeholders- an array of placeholders to add- Returns:
- this
MessageSenderinstance for chaining
-
addPlaceholders
Adds placeholders using parallel arrays of keys and values.Both arrays must have the same length and be applicable for replacement.
- Type Parameters:
T- the type of the placeholder values- Parameters:
keys- an array of placeholder keysvalues- an array of placeholder values- Returns:
- this
MessageSenderinstance for chaining - Throws:
NullPointerException- if keys and values are not applicable for replacement
-
copy
Creates a copy of thisMessageSender.- Specified by:
copyin interfaceCopyable<MessageSender>- Returns:
- a new
MessageSenderinstance with the same configuration
-
send
Sends a list of messages to the target players.Each message is processed through formatting (placeholder replacement, function application, etc.) and then sent individually. If no target players are defined, messages are logged or sent via webhook if allowed.
- Parameters:
strings- a list of raw message strings- Returns:
trueif at least one message was sent successfully;falseotherwise
-
send
Sends an array of messages to the target players.- Parameters:
strings- an array of raw message strings- Returns:
trueif at least one message was sent successfully;falseotherwise
-