I need to know if there is a way to clear a players inventory when they run a command from another plugin. I'm thinking you can use the PlayerCommandPreprocessEvent but I have not been able to get it myself. I would like some help with my problem. Thank you :)
You're on the right track - create a listener for PlayerCommandPreprocessEvent, check the command is what you want, and then clear the player's inventory:
public class PlayerCommandPreprocessListener implements Listener {
@EventHandler
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
if (event.getMessage().toLowerCase().startsWith("/otherplugincommand")) {
event.getPlayer().getInventory().clear();
}
}
}
Remember to normalise the case before comparing (either call toUpperCase()
or toLowerCase()
on the message), as Bukkit's command processing is case insensitive.
Using startsWith()
as opposed to equals()
ignores any following arguments - if you need to check the arguments match exactly, use an equals()
call.
Past that, actually clearing the player's inventory is trivial, and can be done in a one-liner.