Skip to content

Messages

Alberto Migliorato edited this page Sep 7, 2020 · 5 revisions

Messages Management

Instantiating a Message

To obtain a Message object, you can simply instantiate it with a String (or a List) inside the constructor:

Message msg = new Message("&cThis is a message!");

With a Message you can simplify tons of actions:

  • Automatically colors the text;
  • Allows you to support multiple-lines messages;
  • Allows you to simply send it to someone or broadcast;

Sending a Message

Sending a message to a player or to the console is quite easy: the #send(CommandSender) method can be invoked whenever you want to.

msg.send(Bukkit.getPlayer("Player"));

Broadcasting a Message

You can easily broadcast a message with a very useful option: you can decide whether only people with certain permissions should receive it or not. To do it, just invoke the #broadcast() method or the #broadcast(String) method, in which the permission is expressed as String.

Message m = new Message("&cYo staffers, there's a cheater in KitPvP server! Go and SS it!");
m.broadcast("permission.needed.to.receive.the.broadcast");

Using placeholders

If you want to add some custom variables or placeholders in your message, you might need to use the #addPlaceHolder(String, Object) syntax. Nothing but replacing a placeholder String with a specific Object chosen by yourself: pretty easy, isn't it?

Message m = new Message("&cHello! My name is Alberto!").addPlaceHolder("Alberto", "Francesco");
String myString = m.getText();    // Returns "&cHello! My name is Francesco!"

Example Message Class

Finally, here's an example of a complete Message class usage:

package it.mycraft.powerlibexample;

import it.mycraft.powerlib.chat.Message;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;

import java.util.List;

public class ExampleMessage {

    public void isOp(Player player) {
        if (player.isOp())
            new Message("&4%player is a Server Operator")
                    .addPlaceHolder("%player", player.getName())
                    .send(player);
    }

    public void sendMessage(Player player, FileConfiguration fileConfiguration) {
        new Message(fileConfiguration.getString("This.is.a.path"))
                .addPlaceHolder("%player", player.getName())
                .send(player);
    }

    public void sendListMessages(ConsoleCommandSender consoleCommandSender) {
        new Message("&6Hi Console", "&eHow are you?")
                .send(consoleCommandSender);
    }

    public String getText() {
        return new Message("&4Hi %player")
                .addPlaceHolder("%player", "Pompieregay")
                .getText();
    }

    public void textList() {
        List<String> s = new Message(getText(), "I'm fine")
                .getTextList();
        s.forEach(System.out::println);
    }

    public void setText() {
        System.out.println(
                new Message()
                        .set("&5Hello Shrek, today is: %day")
                        .addPlaceHolder("%day", "Somebody once told me the world is gonna roll me"));
    }
}