⌨️Commands
Since 1.0.0
This is one of the most powerful yet easy to use features MCUtils has to offer, as now you can register commands that support sub commands, have argument conversion methods, can filter between players or the console and don't require to be added on your plugin.yml (You can still add them optionally). Now let's see how to actually use them.
Creating an MCCommand
As the title suggests, the class used for commands is called MCCommand. MCCommands are linked to and MCPlugin, which is generally the one that will register the command. Let's assume we have an MCPlugin called ClanPlugin for this example.
About setSenderClass
While this method is useful to restrict players or the console from executing our commands in just one line, you may wonder what message will be displayed to the senders if they aren't allowed to use it. Well, if your MCPlugin has a registered MessagesFileHolder so MCPlugin#getMessages
doesn't return null
, it will send the message on the path "commands.noPlayer" for players and "commands.noConsole" for the console. If the holder is null
, MCUtils will just send "[!] This command cannot be executed by <sender>", where "<sender>" can be either "players" or "the console". You can of course override this behavior with your own by overriding the MCCommand#sendInvalidSenderMsg(CommandSender)
, where you can send a message to the sender.
Registering commands
Registering commands is very easy and can be done in one line with multiple commands, this is generally done on the onEnable
method of your MCPlugin, so that is what we are going to do.
You might have noticed that the plugin.yml file wasn't modified to register this command. That is because with MCUtils this step is not required, but optional, which means that you can still define your commands on your plugin.yml to add a description, aliases or whatever you want to add, but this is no longer required as MCUtils will detect that the command you are trying to register is not present on your plugin.yml and will register it directly to the Bukkit command map by using a bit of reflection.
Limiting access
One important thing we can do to make sure that all playes that execute our MainClanCommand command actually have a clan is to override the hasAccess
method as follows, this will NOT break the sender class restrictions.
The message
boolean basically tells you whether you should inform the sender about not having access to the command, this will be false
if the method is being called via onTab
and true
if the method is being called via onCommand
, as expected. However, this is not the most optimal way to create conditions for a group of commands, that should be done by creating a generic class for clan commands. Which is what will be explained on the "Custom command class" page.
Last updated