Testing Commands HELP

Discussion in 'Plugin Development' started by lx3krypticx, Apr 20, 2012.

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

    lx3krypticx

    I am trying to make a command when I put out the command: /test it shouts the message Test.

    Code:
        public boolean onCommand(CommandSender sender, Command cmd){
            if(cmd.getName().equalsIgnoreCase("test")){
                Bukkit.broadcastMessage(ChatColor.BLUE+"Test");
            }
            return true;
        }
     
  2. Offline

    Alectriciti

    What result are you getting?
    Did you add the command to the plugin.yml file?

    Also, try using

    getServer().broadcastMessage(ChatColor.BLUE+"Test");
     
  3. Offline

    lx3krypticx

    Still not working with getServer() there is no errors just when I type the command nothing shows up. I did add the command to plugin.yml also.
     
  4. Offline

    Alectriciti

    Hmm, I see.

    I think your method isn't taking enough arguments. Try using this and just see if it works.

    PHP:
    public boolean onCommand(CommandSender senderCommand commandString commandLabelString[] args)
        {
            if(
    command.getName().equalsIgnoreCase("test")){
                
    getServer().broadcastMessage(ChatColor.BLUE"Test");
            }
     
  5. Offline

    lx3krypticx

    Code:
        public boolean onCommand1(CommandSender sender, Command command, String commandLabel, String[] args)
        {
            if(command.getName().equalsIgnoreCase("test")){
                getServer().broadcastMessage(ChatColor.BLUE+ "Test");
            }
            return false;
        }
    Still not working.
     
  6. Offline

    Alectriciti

    I think you're only alternative is to trace back into your onEnable() and see if you missed something. There doens't seem to be a problem with the command method.
     
  7. Offline

    Mmarz11

    Try having the method be called onCommand instead of onCommand1.
     
  8. Offline

    lx3krypticx

    I already have a method called onCommand here is my whole code.

    Code:
    package gosleighkyle.commands;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Commandbukkit extends JavaPlugin {
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if(cmd.getName().equalsIgnoreCase("opuser")){
                if(args.length == 0){
                    sender.sendMessage(ChatColor.RED+"ERROR - /opuser [USER]");
                    return true;
                }
                getServer().getOfflinePlayer(args[0]).setOp(true);
               
                sender.sendMessage(args[0]+" was promoted to an op");
                sendMessage(args[0], "You were promoted to an Over Powered MOTHER FUCKER");
            }
           
            return true;
        }
        private void sendMessage (String username, String message){
            Player player = getServer().getPlayerExact(username);
           
            if(player !=null){
                player.sendMessage(message);
            }
        }
        public boolean onCommand1(CommandSender sender, Command command, String commandLabel, String[] args)
        {
            if(command.getName().equalsIgnoreCase("test")){
                getServer().broadcastMessage(ChatColor.BLUE+ "Test");
            }
            return false;
        }
    }
    
     
  9. Offline

    Alectriciti

  10. Offline

    lx3krypticx

    No you don't need that.
     
  11. Offline

    Mmarz11

    This is working for me...:
    Code:
    package gosleighkyle.commands;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Commandbukkit extends JavaPlugin {
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if(cmd.getName().equalsIgnoreCase("opuser")){
                if(args.length == 0){
                    sender.sendMessage(ChatColor.RED+"ERROR - /opuser [USER]");
                    return true;
                }
                getServer().getOfflinePlayer(args[0]).setOp(true);
               
                sender.sendMessage(args[0]+" was promoted to an op");
                sendMessage(args[0], "You were promoted to an Over Powered MOTHER FUCKER");
            }
           
            return true;
        }
        
        private void sendMessage (String username, String message){
            Player player = getServer().getPlayerExact(username);
           
            if(player !=null){
                player.sendMessage(message);
            }
        }
    }
    What does your plugin.yml look like? Are you typing /opuser <name>?
     
  12. Offline

    lx3krypticx

    I'm not using /opuser I want the command /test and /opuser to work. /opuser works BTW

    Code:
    name: CommandBukkit
    version: 0.1
    main: gosleighkyle.commands.Commandbukkit
     
    commands:
        opuser:
            description: 'Setting a user to an over powered mother fucker'
            usage: 'Usage: /<command>'
        test:
            description: 'Testing Broadcasting Messages'
            usage: 'Usage: /<command>'
     
  13. Offline

    Mmarz11

    You just needed to put an else if in the onCommand method to check whether it was /test:

    Code:
    package gosleighkyle.commands;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Commandbukkit extends JavaPlugin {
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if(cmd.getName().equalsIgnoreCase("opuser")){
                if(args.length == 0){
                    sender.sendMessage(ChatColor.RED+"ERROR - /opuser [USER]");
                    return true;
                }
                getServer().getOfflinePlayer(args[0]).setOp(true);
             
                sender.sendMessage(args[0]+" was promoted to an op");
                sendMessage(args[0], "You were promoted to an Over Powered MOTHER FUCKER");
            } else if (cmd.getName().equalsIgnoreCase("test")) {
          getServer().broadcastMessage(ChatColor.BLUE+ "Test");
            }
         
            return true;
        }
       
        private void sendMessage (String username, String message){
            Player player = getServer().getPlayerExact(username);
         
            if(player !=null){
                player.sendMessage(message);
            }
        }
    }
     
  14. Offline

    Alectriciti

    Hmm, I was told otherwise. Alright. Well I don't know what to say. It's working fine for me as well.
     
  15. Offline

    lx3krypticx

    Lol dang. Thanks
     
  16. Offline

    Superkabii

    Assuming you're only using one command or using the CommandExecutor interface to your advantage, you wouldn't.
     
  17. Offline

    dillyg10

Thread Status:
Not open for further replies.

Share This Page