Adding a second command

Discussion in 'Plugin Development' started by coobro123, Jan 16, 2013.

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

    coobro123

    Hello!

    I am coding a plugin, But I want to get another command. So my "/staff" does one message and I want another one to be "/hi" that sends a welcome message in the same format.

    Coding:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    if(cmd.getName().equalsIgnoreCase("staff")){
    String player = ((Player) sender).getName();
    Player Player = Bukkit.getPlayerExact(player);
    String msg = "";
    for (int words = 0; words < args.length; words++) {
    msg = msg + args[words] + " ";
    }
    if (null==chat.getPlayerPrefix(Player)){
    String world = Player.getWorld().getName();
    String group = perms.getPrimaryGroup(Player);
    prefix = chat.getGroupPrefix(world, group);
    }
    else{
    prefix = chat.getPlayerPrefix(Player);
    }
    for(int i = 0; i < prefix.length(); i++)
    {
    if(prefix.charAt(i) == '&')
    MSG1 = MSG1 + '§';
    else
    MSG1 = MSG1 + prefix.charAt(i);
    }
    Bukkit.broadcastMessage(ChatColor.YELLOW+"[G] "+MSG1+Player.getDisplayName() + ChatColor.YELLOW + ": " + ChatColor.DARK_GRAY + "Need Help? Message me, " + ChatColor.YELLOW + "Can help with anything!");
    MSG1 = "";
    returntrue;
    }
    returntrue;
     
  2. Offline

    RainoBoy97

    after the closing bracket for the command, just make another one the same way :p
     
  3. Offline

    coobro123

    So with the onCommand method? and do it after return true; thing?
     
  4. Offline

    H2NCH2COOH

    try to use CommandExecutor

    Code:
    public class MyCommandExecutor implements CommandExecutor
    {
        public MinescriptCommandExecutor()
        {
     
        }
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
        {
            Do what ever you want
        }
    }
    In your main class

    Code:
    getCommand("mycommand").setExecutor(new MyCommandExecutor());
    For every command you have, create a command executor class and register it in your main class
     
  5. Offline

    Kodfod

    Like this:
    Code:Java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    2. if (cmd.getName().equalsIgnoreCase("Command1")) {
    3. //it was command 1
    4. }
    5. if (cmd.getName().equalsIgnoreCase("Command2")) {
    6. //it was command2
    7. }
    8. return false;
    9. }


    So yours would look like...

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    2. if(cmd.getName().equalsIgnoreCase("staff")){
    3. String player = ((Player) sender).getName();
    4. Player Player = Bukkit.getPlayerExact(player);
    5. String msg = "";
    6. for (int words = 0; words < args.length; words++) {
    7. msg = msg + args[words] + " ";
    8. }
    9. if (null==chat.getPlayerPrefix(Player)){
    10. String world = Player.getWorld().getName();
    11. String group = perms.getPrimaryGroup(Player);
    12. prefix = chat.getGroupPrefix(world, group);
    13. }
    14. else{
    15. prefix = chat.getPlayerPrefix(Player);
    16. }
    17. for(int i = 0; i < prefix.length(); i++)
    18. {
    19. if(prefix.charAt(i) == '&')
    20. MSG1 = MSG1 + '§';
    21. else
    22. MSG1 = MSG1 + prefix.charAt(i);
    23. }
    24. Bukkit.broadcastMessage(ChatColor.YELLOW+"[G] "+MSG1+Player.getDisplayName() + ChatColor.YELLOW + ": " + ChatColor.DARK_GRAY + "Need Help? Message me, " + ChatColor.YELLOW + "Can help with anything!");
    25. MSG1 = "";
    26. return true;
    27. }
    28. if (cmd.getName().equalsIgnoreCase("Command2")) {
    29. sender.sendMessage("Hey a second command!!");
    30. return true;
    31. }
    32. return false;
    33. }


    then in your plugin.yml:
    Code:YAML
    1. commands:
    2. staff:
    3. description: does stuff
    4. usage: /<command>
    5. Command2:
    6. description: a second command!
    7. usage: /<command>
     
  6. Offline

    coobro123

    I get an error, where it repeats the player prefix.
     
  7. Offline

    fireblast709

Thread Status:
Not open for further replies.

Share This Page