Editing aliases of a command

Discussion in 'Plugin Development' started by CorrieKay, Aug 3, 2012.

  1. Offline

    CorrieKay

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Ive got a system in place where if you type /<channelname> <message> it will shoot a message to another channel, without having to focus your chat to that channel.

    The last time i made this , i hardcoded the channels into my server, and therefore, hardcoded the aliases into the plugin.

    However i'm looking for a bit more flexibility this time around, so whenever a channel is created, in its constructor, it grabs the command, and edits and sets the aliases:

    Code:
        Command cmd = Mane.instance.getCommand("channel");
        List<String> aliases = cmd.getAliases();
        aliases.add(quick);
        aliases.add(channelName);
        cmd.setAliases(aliases);
    Unfortunately, this isnt working. debug prints out all aliases, and theyre getting the correct aliases, but the server is still saying its an unrecognized command..
  2. Offline

    pzxc

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    The way bukkit works, only commands specifically defined in your plugin.yml file are sent to the onCommand function of your plugin for processing.

    You can get around this, by using a PlayerCommandPreprocessEvent listener, which fires for every command entered by a player, even if handled by another plugin or even if an invalid command.

    @EventHandler
    public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e) {
    if (e.getMessage().toLowerCase().startsWith("/mychannel ") {
    sendToChannel(); // handle it
    e.setCancelled(true); // if you don't want another plugin to try to process the same command
    }
    }

    This post has been edited 1 time. It was last edited by pzxc Aug 3, 2012.
  3. Offline

    CorrieKay

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I figured, but if that's how it works, what is the point to editing the aliases?

    This post has been edited 2 times. It was last edited by CorrieKay Aug 3, 2012.
  4. Offline

    pzxc

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    As I said, ALL commands are sent to PlayerCommandPreprocess
    Whereas only commands defined specifically in plugin.yml are sent to onCommand

    PlayerCommandPreprocess doesn't need to use the literal string "/mychannel"

    Rather, it can check dynamically...

    for (String alias : aliases) if (e.getMessage().toLowerCase().startsWith("/" + alias + " ") {
    ...
    }
  5. Offline

    pzxc

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Oh I see what you're asking, what is the point of even having a setAliases method in the API

    That, I do not know the answer to
  6. Offline

    CorrieKay

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    That doesnt seem correct, as any alias i put into "commands: "command": aliases [alias]" will trigger for the parent command name on cmd.getName()

    if a command "derp" has an alias of "herp" and you type /herp, the command will fire as cmd.getName() == "derp" while String label == "herp"
  7. Offline

    chaseoes BukkitDev Staff

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Do you have to set them in onEnable?
  8. Offline

    CorrieKay

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    i have them set into a constructor that technically is firing during onEnable. That said, it really shouldnt matter when you set the aliases.
  9. Offline

    nisovin

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Have you tried setting aliases in onLoad instead of onEnable?
  10. Offline

    CorrieKay

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    the alias list, or command, seems to be null at this point.
  11. Offline

    nisovin

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    It seems like if you want to do this, you'd have to use reflection to modify the command's CommandMap. It would probably be easier to just use the PlayerCommandPreprocessEvent.
  12. Offline

    CorrieKay

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    *shrug* If ive gotta go so far as to use that event, ill just hardcode the aliases myself.

Share This Page