下面列出了怎么用org.bukkit.command.CommandExecutor的API类实例代码及写法,或者点击链接到github查看源代码。
public void onEnable() {
instance = this;
//Configuration
this.saveDefaultConfig(); //���Ĭ������
//Listener
Bukkit.getPluginManager().registerEvents(new PlayerLimitListener(), this); //ע�������
Bukkit.getPluginManager().registerEvents(new PlayerLoginCommand(), this);
Bukkit.getPluginManager().registerEvents(new PlayerTipListener(), this);
//Commands
CommandExecutor ce = new PlayerLoginCommand(); //ע��ָ���������ָ���ͬһ��Executor
Bukkit.getPluginCommand("login").setExecutor(ce);
Bukkit.getPluginCommand("register").setExecutor(ce);
getLogger().info("StupidLogin���������");
}
/**
* Unregisters a CommandExecutor with the server
*
* @param command the command instance
* @param <T> the command executor class type
* @return the command executor
*/
@Nonnull
public static <T extends CommandExecutor> T unregisterCommand(@Nonnull T command) {
CommandMap map = getCommandMap();
try {
//noinspection unchecked
Map<String, Command> knownCommands = (Map<String, Command>) KNOWN_COMMANDS_FIELD.get(map);
Iterator<Command> iterator = knownCommands.values().iterator();
while (iterator.hasNext()) {
Command cmd = iterator.next();
if (cmd instanceof PluginCommand) {
CommandExecutor executor = ((PluginCommand) cmd).getExecutor();
if (command == executor) {
cmd.unregister(map);
iterator.remove();
}
}
}
} catch (Exception e) {
throw new RuntimeException("Could not unregister command", e);
}
return command;
}
@Override
public void registerCommand(String commandName, CommandExecutor executor, String description, String usage, String... aliases) {
PluginCommand command = getCommand(commandName);
if(command == null) {
forceRegisterCommand(commandName, executor, description, usage, aliases);
return;
}
command.setExecutor(executor);
if(executor instanceof TabCompleter) {
TabCompleter completer = (TabCompleter) executor;
command.setTabCompleter(completer);
}
if(executor instanceof Listener) {
Listener listener = (Listener) executor;
PluginManager manager = Bukkit.getPluginManager();
manager.registerEvents(listener, this);
}
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length == 0) {
sendMenu(sender);
} else {
String subCommand = args[0].toLowerCase();
CommandExecutor subCommandExec = commands.get(subCommand);
if (subCommandExec == null) {
sendMenu(sender);
} else if (!sender.hasPermission("holograms." + subCommand)) {
sender.sendMessage(ChatColor.RED + command.getPermissionMessage());
} else {
return subCommandExec.onCommand(sender, command, label, args);
}
}
return false;
}
public BukkitCommand(
Plugin plugin, CommandExecutor executor, TabCompleter completer, CommandMapping command) {
super(
command.getPrimaryAlias(),
command.getDescription().getShortDescription(),
"/" + command.getPrimaryAlias() + " " + command.getDescription().getUsage(),
Lists.newArrayList(command.getAllAliases()));
this.plugin = plugin;
this.executor = executor;
this.completer = completer;
this.permissions = command.getDescription().getPermissions();
super.setPermission(Joiner.on(';').join(permissions));
}
/**
* Registers a CommandExecutor with the server
*
* @param plugin the plugin instance
* @param command the command instance
* @param permission the command permission
* @param permissionMessage the message sent when the sender doesn't the required permission
* @param description the command description
* @param aliases the command aliases
* @param <T> the command executor class type
* @return the command executor
*/
@Nonnull
public static <T extends CommandExecutor> T registerCommand(@Nonnull Plugin plugin, @Nonnull T command, String permission, String permissionMessage, String description, @Nonnull String... aliases) {
Preconditions.checkArgument(aliases.length != 0, "No aliases");
for (String alias : aliases) {
try {
PluginCommand cmd = COMMAND_CONSTRUCTOR.newInstance(alias, plugin);
getCommandMap().register(plugin.getDescription().getName(), cmd);
getKnownCommandMap().put(plugin.getDescription().getName().toLowerCase() + ":" + alias.toLowerCase(), cmd);
getKnownCommandMap().put(alias.toLowerCase(), cmd);
cmd.setLabel(alias.toLowerCase());
if (permission != null) {
cmd.setPermission(permission);
if (permissionMessage != null) {
cmd.setPermissionMessage(permissionMessage);
}
}
if (description != null) {
cmd.setDescription(description);
}
cmd.setExecutor(command);
if (command instanceof TabCompleter) {
cmd.setTabCompleter((TabCompleter) command);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return command;
}
private void registerCommand(String commandName, CommandExecutor executor){
PluginCommand command = UhcCore.getPlugin().getCommand(commandName);
if (command == null){
Bukkit.getLogger().warning("[UhcCore] Failed to register " + commandName + " command!");
return;
}
command.setExecutor(executor);
}
private void forceRegisterCommand(String commandName, CommandExecutor executor, String description, String usage, String... aliases) {
if(commandName == null || executor == null || description == null || usage == null || aliases == null) return;
PluginManager manager = Bukkit.getPluginManager();
CustomCommand command = new CustomCommand(commandName, executor, description, usage, aliases);
manager.registerEvents(command, this);
if(executor instanceof Listener) {
Listener listener = (Listener) executor;
manager.registerEvents(listener, this);
}
}
@Nonnull
@Override
public <T extends CommandExecutor> T registerCommand(@Nonnull T command, String permission, String permissionMessage, String description, @Nonnull String... aliases) {
return CommandMapUtil.registerCommand(this, command, permission, permissionMessage, description, aliases);
}
private void registerCommand(String commandName, CommandExecutor executor) {
registerCommand(commandName, executor, null, null);
}
public CustomCommand(String command, CommandExecutor executor, String description, String usage, String... aliases) {
super(command, description, usage, Util.newList(aliases));
this.executor = executor;
}
protected void setupCommand(CommandExecutor executor, String... commands) {
for (final String command : commands) {
getCommand(command).setExecutor(executor);
}
}
public ShopSubCommand(String name, boolean playerCommand, CommandExecutor executor, TabCompleter tabCompleter) {
this.name = name;
this.playerCommand = playerCommand;
this.executor = executor;
this.tabCompleter = tabCompleter;
}
/**
* Registers a CommandExecutor with the server
*
* @param command the command instance
* @param aliases the command aliases
* @param <T> the command executor class type
* @return the command executor
*/
@Nonnull
default <T extends CommandExecutor> T registerCommand(@Nonnull T command, @Nonnull String... aliases) {
return registerCommand(command, null, null, null, aliases);
}
/**
* Registers a CommandExecutor with the server
*
* @param command the command instance
* @param permission the command permission
* @param permissionMessage the message sent when the sender doesn't the required permission
* @param description the command description
* @param aliases the command aliases
* @param <T> the command executor class type
* @return the command executor
*/
@Nonnull
<T extends CommandExecutor> T registerCommand(@Nonnull T command, String permission, String permissionMessage, String description, @Nonnull String... aliases);
/**
* Registers a CommandExecutor with the server
*
* @param plugin the plugin instance
* @param command the command instance
* @param aliases the command aliases
* @param <T> the command executor class type
* @return the command executor
*/
@Nonnull
public static <T extends CommandExecutor> T registerCommand(@Nonnull Plugin plugin, @Nonnull T command, @Nonnull String... aliases) {
return registerCommand(plugin, command, null, null, null, aliases);
}
/**
* Register a command to CombatLogX
* @param commandName The name of the command
* @param executor The executor class of the command
* @param description The description of the command
* @param usage The usage of the command
* @param aliases The alias list of the command
* @see CommandExecutor
*/
void registerCommand(String commandName, CommandExecutor executor, String description, String usage, String... aliases);