Skip to content

Instantly share code, notes, and snippets.

@Swedz
Last active January 15, 2020 13:45
Show Gist options
  • Save Swedz/63055e05461535fb76f9c65535610b07 to your computer and use it in GitHub Desktop.
Save Swedz/63055e05461535fb76f9c65535610b07 to your computer and use it in GitHub Desktop.
Much simpler version of registering commands.
package net.swedz.lib.bukkit;
import net.swedz.lib.ArgumentReader;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.event.Listener;
import java.util.List;
public abstract class BukkitCommand implements CommandExecutor, TabCompleter, Listener {
protected final String label;
protected final String[] permissions;
/**
* Instantiate a new bukkit command.
*
* @param label the exact first word of the command (excluding arguments) that was entered by the sender
* @param permissions the array of permissions that are required to run the command
*/
public BukkitCommand(String label, String...permissions) {
this.label = label;
this.permissions = permissions;
}
/**
* Instantiate a new bukkit command.
*
* @param label the exact first word of the command (excluding arguments) that was entered by the sender
* @param permission the permission that is required to run the command
*/
public BukkitCommand(String label, String permission) {
this(label, new String[] {permission});
}
/**
* Instantiate a new bukkit command.
*
* @param label the exact first word of the command (excluding arguments) that was entered by the sender
*/
public BukkitCommand(String label) {
this(label, (String[]) null);
}
/**
* Get the label of the command.
*
* @return the label of the command
*/
public String getLabel() { return this.label; }
/**
* Get the required permissions of the command.
*
* @return the required permissions of the command
*/
public String[] getPermissions() { return this.permissions; }
/**
* Check whether the CommandSender has the permission to run this command.
*
* @param sender the {@link CommandSender} instance that is to be checked upon
* @return true if it can be run, false otherwise
*/
public boolean hasPermission(CommandSender sender) {
// If there is no requirement, let them run it
if(this.permissions == null || this.permissions.length == 0) return true;
// Make sure all of the given permissions are applied to this sender
for(String perm : this.permissions)
if(!sender.hasPermission(perm))
return false;
// They are all applied! We can run it
return true;
}
/**
* Get the usage of the command for a given sender and their attempted arguments.
*
* @param sender the {@link CommandSender} of the command
* @param label the exact first word of the command (excluding arguments) that was entered by the sender
* @param args the argument reader for the command
* @return the formatted string of the correct usage of the command
*/
public abstract String getUsage(CommandSender sender, String label, ArgumentReader args);
/**
* Listen to all executions of this command from users who have permission to run it.
* This already pre-accounts for permission. No need to check if they have permission again (unless you did not supply permissions in the constructor).
*
* @param sender the {@link CommandSender} of the command
* @param label the exact first word of the command (excluding arguments) that was entered by the sender
* @param args the argument reader for the command
* @return true when the arguments are all valid, false otherwise
*/
public abstract boolean executeCommand(CommandSender sender, String label, ArgumentReader args);
/**
* Get the list of valid arguments for the current argument.
*
* @param sender the {@link CommandSender} of the command
* @param label the exact first word of the command (excluding arguments) that was entered by the sender
* @param args the argument reader for the command
* @return list of valid arguments
*/
public abstract List<String> tabComplete(CommandSender sender, String label, ArgumentReader args);
/**
* Send a message to the command sender that displays the correct format for the commands.
*
* @param sender the {@link CommandSender} of the command
* @param label the exact first word of the command (excluding arguments) that was entered by the sender
* @param args the argument reader for the command
*/
protected void sendInvalidArguments(CommandSender sender, String label, ArgumentReader args) {
sender.sendMessage(ChatColor.RED + "Invalid arguments. Try: " + this.getUsage(sender, label, args));
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
// Make sure the sender has permission
if(!this.hasPermission(sender)) {
sender.sendMessage(ChatColor.RED + "You do not have permission to do that!");
return true;
}
// Make sure the user supplied valid arguments
if(!this.executeCommand(sender, label, new ArgumentReader(args))) {
this.sendInvalidArguments(sender, label, new ArgumentReader(args));
return true;
}
// The command was fully run
return true;
}
@Override
public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] args) {
// Make sure the sender has permission
if(!this.hasPermission(sender)) return null;
// They have permission, so let's give them a few suggestions on what to do
return this.tabComplete(sender, label, new ArgumentReader(args));
}
}
@Swedz
Copy link
Author

Swedz commented Sep 16, 2018

Take note that this makes use of the ArgumentReader class.

An example for a command class would be something like this:

// Command will only be able to be executed by players due to the "false" input for the second variable of the constructor.
public class TestCommand extends BukkitCommand {
	public TestCommand() {
		super("test", false);
	}
	
	@Override
	public void execute(Player player, ArgumentReader args) {
		player.sendMessage("This is a test command!");
	}
}

Register the command like so:

TestCommand command = new TestCommand();
// Register the actual command
PluginCommand cmd = plugin.getCommand(command.getLabel());
cmd.setExecutor(command);
cmd.setTabCompleter(command);
// Register the listener
plugin.getServer().getPluginManager().registerEvents(command, plugin);

It is suggested to put this registry in a method where the command and plugin are arguments so that it can be reused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment