Solved Command Aliases

Discussion in 'Plugin Development' started by random_username, Oct 5, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    random_username

    Hello, I was wondering how can I make an alias for a command. I dont have a clue of how to do this, i.e. here is some of my onCommand:
    Code:java
    1. public boolean onCommand(CommandSender Sender, Command cmd, String commandLabel, String[] args){
    2. Player player = (Player) Sender;
    3. if(commandLabel.equalsIgnoreCase("plugins")){
    4. if(Sender.hasPermission("ecore.pl")){
    5. player.sendMessage("Plugins " + getConfig().getString("plnum") + ": " + getConfig().getString("pllist").replace("&", "§"));
    6.  
    7. }else{
    8. player.sendMessage(getConfig().getString("Prefix").replace("&", "§") + getConfig().getString("noperm").replace("&", "§"));
    9. }
    10. }
    11. }

    How can I make that command (/plugins) work when someone makes /pl? Thanks for the help :)
     
  2. Offline

    adam753

    An alias is simply another name for a command which will fire the same command to your plugin. You declare these inside your plugin.yml file like so:
    Code:
    commands:
      flagrate:
        description: Set yourself on fire.
        aliases: [combust_me, combustMe]
        permission: inferno.flagrate
        usage: Syntax error! Simply type /<command> to ignite yourself.
    
    (taken from the official wiki)
    So if you do this, when someone types /combustMe, the command "flagrate" will be sent to your onCommand function. If for whatever reason you want to see what alias was actually used, you can use onCommand's commandLabel parameter.
     
  3. Offline

    random_username

    I have tried that, but the strings wont appear in chat, only when i type the command that appears in the oncommand.
     
  4. Offline

    adam753

    random_username Ah, that's because you're using commandLabel to check what the command is. As I said, commandLabel shows the actual alias that was used when typing the command. You should try to use cmd.getName() when checking the command - there is no difference, unless you're trying to use aliases.
     
    Maulss likes this.
  5. Offline

    random_username

    So how would I write down my onCommand? Sorry, I'm a newbie at this v.v
     
  6. Offline

    adam753

    Instead of
    Code:
    if(commandLabel.equalsIgnoreCase("plugins")){
    
    you do this:
    Code:
    if(cmd.getName().equalsIgnoreCase("plugins")){
    
    and then it will be true if the command is "plugins" or any of its aliases.
     
  7. Offline

    random_username

    Thanks so much, It worked with almost all my commands! :D Well, although there is one that isn't working, it is /plugins. /pl is working fine, but any idea why /plugins isn't? :eek:
     
  8. Offline

    adam753

  9. Offline

    random_username

    Sure:
    Code:
    name: Corecommands
    main: me.Kait18.Corecommands.Corecommands
    version: 5.0
    description: >
                Some basic Commands.
    commands:
      plugins:
        description: Gives you the plugins list.
        aliases: [pl]
      help:
        description: The help Command.
        aliases: [?]
      Features:
        description: The Features on the server.
    When I try /plugins only my real plugin list appears, instead the one I made in my config (which is in my onCommand)
     
  10. Offline

    adam753

    random_username It's probably not causing a problem, but when you only write one alias, you don't have to put the square brackets around it. Other than that I'm afraid I'm stumped.
     
  11. Offline

    MrSparkzz

    random_username & adam753
    one thing I like to do, to save a bit of time when creating multiple commands in one method is to set the Command to command instead of cmd, then do this
    Code:java
    1.  
    2. cmd = command.getName();
    3.  

    it might not be much, but it's pretty nice.

    Another thing; the reason your /pl command works, but not your /plugins command, is because it's a default Bukkit command. I've done it before with the same command. Here's my code.

    Main class (onEnable method)
    Code:java
    1.  
    2. Bukkit.getPluginmanager().RegisterEnents(CommandOverrideListener(), this);
    3.  

    CommandOverrideListener Class
    Code:java
    1.  
    2. // overrides the default commands
    3. @EventHandler(priority = EventPriority.LOWEST)
    4. public void onCommandPreProcess(PlayerCommandPreprocessEvent event){
    5. if(event.getMessage().toLowerCase().startsWith("/plugins")){
    6. event.setCancelled(true);
    7. event.getPlayer().sendMessage(getPluginList());
    8. }
    9. }
    10.  
    11. // get the list of plugins, then generate it
    12. private String getPluginList() {
    13. StringBuilder pluginList = new StringBuilder();
    14. Plugin[] plugins = Bukkit.getPluginManager().getPlugins();
    15.  
    16. for (Plugin plugin : plugins) {
    17. // splits each plugin with a white comma
    18. if (pluginList.length() > 0) {
    19. pluginList.append(ChatColor.WHITE);
    20. pluginList.append(", ");
    21. }
    22.  
    23. // green plugins are enabled, red are not
    24. pluginList.append(plugin.isEnabled() ? ChatColor.GREEN : ChatColor.RED);
    25. pluginList.append(plugin.getName());
    26. }
    27.  
    28. /*
    29.   * outputs a string that lists how many plugins there are
    30.   * then the plugins themselves
    31.   */
    32. return ChatColor.DARK_GRAY + "(" + ChatColor.LIGHT_PURPLE + plugins.length + ChatColor.DARK_GRAY + "): " + pluginList.toString();
    33. }
    34.  
     
  12. Offline

    random_username

    Thanks! :D
     
Thread Status:
Not open for further replies.

Share This Page