Adding commands on-the-fly

Discussion in 'Plugin Development' started by neko259, Jun 10, 2011.

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

    neko259

    I have to make configurable command name for my plugin, but it does not work if I don't define it in plugin.yml. How can I assign some command to my plugin during onEnable?
     
  2. Offline

    shadrxninga

    Have you tried doing this in the plugin.yml?
    Code:
    aliases: [spawn,setspawn,spawnset]
    You could try this.

    Code:
    commands:
      some_thing:
        description: Does Something...
        aliases: setspawn
        usage: /<command>
      something_else:
        description: Does Something Else
        aliases: spawn
        usage: /<command>
    In your command method put this.
    if(cmd.getName().equalsignorecase("some_thing"){
    //Do stuff
    }
    To change the command without having to edit the code all you have to do is edit the aliases field.

    Hope that helps :D
     
  3. Offline

    neko259

    I have to add alias from the config.yml
    For example if user specifies command-name: somename if config.yml, I need to use somename. But I can't add it to plugin.yml because I don't know what name would user choose.
     
  4. Offline

    Jayjay110

    What I think of is making an external file editor to modify the settings and update the files! Make a program to do it or sumthing
     
  5. Offline

    neko259

    You mean modify plugin.yml? That's too much for me now :)
     
  6. Offline

    Shamebot

    use onPlayerCommandPreprocess
     
  7. Offline

    garbagemule

    Either I'm the only one getting this, or I'm the only one not getting it. What I take from this is that the guy needs dynamic commands.

    If so, forget plugin.yml, forget onPlayerCommandPreprocess (which shouldn't be used for actual commands anyway, there's a reason it's called "Preprocess"). What you need to do is have an external file, it could be something like config.yml if you want, which makes it easy to grab, add, alter and remove data.

    The reason I say "forget onPlayerCommandPreprocess" is because you should use an alias for all your commands. If you register /set or /add or something, you're bound to run into conflicts with other plugins. Grab an alias, and process all your commands based on that alias. Then, if the first part of the player command is your alias, you can scan the file/Configuration for the values in the arguments.
     
  8. Offline

    neko259

    Can you show me a code sample how to add a working alias to my command during the program without adding this alias to plugin.yml?
     
  9. Offline

    Shamebot

    @garbagemule
    With Command.setAliases(...)? Yeah that's better, I forgot about it.
     
  10. Offline

    neko259

    And it doesn't need to add this alias to plugin.yml?
     
  11. Offline

    Shamebot

    Try this, never tested it:
    Code:
    getServer().getPluginCommand("commandname").setAliases(...);
    Edit:fixed mistake in getPluginCommand
     
  12. Offline

    garbagemule

    @Shamebot - Never knew about this command! I'll be interested to know how it works out.
     
  13. Offline

    Shamebot

    It won't work, I'm pretty sure. I didn't try it, but looked at the Bukkit source. setAliases is for setting them when reading the .yml, they are then added as commands in the CommandMap class.
    So the only way I can think of without using CommandPreprocess is casting the Server to CraftServer and getting the commandMap via reflection to register a new command.
     
Thread Status:
Not open for further replies.

Share This Page