-
Notifications
You must be signed in to change notification settings - Fork 3
Parsing Messages
The most important file of RoseChat's API is the RoseChatAPI class, which can be found here.
This class contains many functions which you may find useful.
The most convenient way of parsing a message is to use the RoseChatAPI#parse()
function. This function will take a given string and parse it using RoseChat's message wrapper and message tokenizer system. This will allow colors (including hex, rainbow and gradient), emojis, tags, and everything else included in RoseChat, to be parsed.
There are a few variations of this function:
This method simply parses the message.
BaseComponent[] parse(RoseSender sender, RoseSender viewer, String message)
This method allows you to use placeholders in the message.
BaseComponent[] parse(RoseSender sender, RoseSender viewer, String message, StringPlaceholders placeholders)
This method allows using a MessageLocation to check for permissions.
BaseComponent[] parse(MessageLocation location, RoseSender sender, RoseSender viewer, String message)
The RoseSender object is used by RoseChat to give more information to a Player object. When you need to use a RoseSender, you can just create a new RoseSender()
. This object has a few different constructors, but passing a Player object through is the most common.
This is an example of how to use these together, for parsing a message when the player joins.
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
RoseChatAPI rosechat = RoseChatAPI.getInstance();
Player player = event.getPlayer();
RoseSender roseSender = new RoseSender(player);
BaseComponent[] message = rosechat.parse(roseSender, roseSender, "<r:0.5>Hi %player_name% &r:rosewood:");
Bukkit.spigot().broadcast(message);
}
An alternative way to parse a message is by creating a new MessageWrapper()
.
This class is useful as by default, parsed messages do not filter things like URLs and spam.
An example of this used within RoseChat:
MessageWrapper messageWrapper = new MessageWrapper(roseSender, MessageLocation.CHANNEL, channel, message).filter().applyDefaultColor();
This creates a new message wrapper with the given RoseSender. A message location is specified so that permissions can be checked. A channel or group also needs to be provided.
Then, the message is filtered and the player's chat color is applied.
Once this message wrapper is created, it needs to be parsed before it can do anything.
Before parsing the message, checking if the message canBeSent()
may be useful to see if the filter system caught anything that should not be sent. The function getFilterType()
can provide you with the reason why the message was filtered.
BaseComponent[] message = messageWrapper.parse(format, viewer);
The parse
function parses the message using the provided format and viewer. A format is not needed, and the message should be parsed for each player that is going to receive it (however this may not be needed if you know that your message will 100% not change with a different player viewing it).
Two other parsing functions are also available, parseFromDiscord
which assumes the message comes from Discord, and things like Discord channels (#general), get parsed in here too. There is also parseToDiscord
, which acts similarly but things like spoilers (||spoiler||) are parsed here.
Now that the message is parsed, functions such as getTaggedPlayers()
become available to use.
Tokenizers are used in RoseChat to convert an input to an output. For example, converting &c
into a colour, or changing %player_name%
into a player's name.