Reputation: 89
For my plugin I have to intercept messages and commands sent (msg or command ex: "Hello word" or "/kill player1") by players or the console and check it before sending / executing they.
Is there any way to do it?
I'm using Spigot 1.18.1
Upvotes: 1
Views: 1813
Reputation: 4908
You should use event. Firstly, register it in your onEnable()
like that :
getServer().getPluginManager().registerEvent(new ManageChatAndCommandListener(), this);
To manage message, you should use AsyncPlayerChatEvent
(Documentation) like that :
@EventHandler
public void onChat(AsyncPlayerChatEvent e){
if(e.getMessage().equalsIgnoreCase("My not allowed message")) {
e.setCancelled(true); // don't send the message
}
}
To manage commands, you should use PlayerCommandPreprocessEvent
(Documentation) like that :
@EventHandler
public void onCommand(PlayerCommandPreprocessEvent e){
if(e.getMessage().equalsIgnoreCase("/not-allowed-command")) {
e.setCancelled(true); // don't run the command
}
}
Warn: the message correspond to the command, with the /
. Even if most of people use /
, you should just ignore the first char and not the /
itself
Upvotes: 1