-
Notifications
You must be signed in to change notification settings - Fork 1
API
This is the page for all of the API tutorials, like where how to hook into the plugin etc.
This can be easily implemented, and can be used in almost any chat plugin.
Firstly, go into the config and scroll down to chat.format where it has the chat formatting.
By default, the format should be: '&6%displayname%&7: &f%message%'
For this example, we'll call the custom tag %my_tag%
Once you've decided on the tag name, head over to your IDE and make a new class extending listener and create the basic AsyncPlayerChatEvent
By now, your class should look a bit like this:
public class MyListener implements Listener {
@EventHandler
public void onPlayerChat(AsyncPlayerChatEvent e) {
}
}Now, lets say I have a config setting called cust-tag in my plugin, and thats what I want to set the %my_tag% to. So, lets make a variable!
public class MyListener implements Listener {
MyPlugin pl = MyPlugin.getInstance(); // Create the instance of our plugin for config purposes
@EventHandler
public void onPlayerChat(AsyncPlayerChatEvent e) {
// This is where our new tag will be specified V, we use \\ to seperate special characters for the regex string
// After the first parameter, we specify what we want to change the tag to
String format = e.getFormat().replaceFirst("\\%my_tag\\%", pl.getConfig().getString("cust-tag"));
}
}If we want to add colour code support, we simply set the format string to:
String format = e.getFormat().replaceFirst("\\%my_tag\\%", ChatColor.translateAlternateColorCodes('&', pl.getConfig().getString("cust-tag"));
The '&' symbol is the symbol we want to replace to § which is the minecraft colour code prefix
Now, lets set the new format: (this will be our final code!)
public class MyListener implements Listener {
MyPlugin pl = MyPlugin.getInstance();
@EventHandler
public void onPlayerChat(AsyncPlayerChatEvent e) {
String format = e.getFormat().replaceFirst("\\%my_tag\\%", pl.getConfig().getString("cust-tag"));
e.setFormat(format);
}
}Easy right? Now go onto the next step!
Easily, we go into our main class and go into the onEnable method and add this line to it:
Bukkit.getPluginManager().registerEvents(new MyListener(), this);
Now, we're done and now try it out!