[Noob Question] Commands aren't recognized in-game

Discussion in 'Plugin Development' started by JoetheMango, Sep 16, 2012.

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

    JoetheMango

    Just a warning, I only recently started playing around with bukkit plugins and Java so be nice!

    I set up a simple command with a simple permission node and exported it to debug. It exported and built fine, no errors came up on the server console, however when I used the command in-game as a player it said "Unknown Command. Type "help" for help.".

    This is my plugin.yml:
    Code:
    name: TakeOne
    version: 1.0.1
    description: This plugin allows the controlled distribution of items via chests.
    author: JoetheMango
    load: STARTUP
    main: com.hotmail.joethemango.takeone.Takeone
    database: false
     
    permissions:
        takeone.*:
            description: Gives access to Takeone commands.
            children:
                takeone.set: true
        takeone.set:
            description: Allows you to set a TakeOne chest.
            default: op
    commands:
      takeone set:
          description: This is the command to set a distribution chest
          usage: takeone set
          permission: takeone.set
          permission-message: You don't have permission to do this!
    
    And this is my main class: Takeone.java
    Code:
    package com.hotmail.joethemango.takeone;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    public class Takeone extends JavaPlugin{
       
        public static String ActiveString;
     
     
        @Override
        public void onEnable(){
            getLogger().info("TakeOne 1.0.1 has been loaded!");
        }
     
        @Override
        public void onDisable() {
            getLogger().info("TakeOne 1.0.1 has been stopped!");
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if (cmd.getName().equalsIgnoreCase("takeone set")){
                if (sender instanceof Player) {
                    Player p = (Player) sender;
               
                      if(p.hasPermission("takeone.set")){
                          Variables.ActivePlayer = p ;
                          ActiveString = p.getName();
                          getLogger().info(Takeone.ActiveString); //remove this its just for debugging
                            // do something
                      }
                 
                    } else {
                      sender.sendMessage("You must be a player to set a Takeone chest!");
                      return false;
                    }
            }
                    // do something
                    return false;
        }
    }
    
    And this is my only other class: Variables.java
    Code:
    package com.hotmail.joethemango.takeone;
     
    import org.bukkit.entity.Player;
     
    public class Variables {
    public static Player ActivePlayer;
     
     
     
    }
    
    Why won't my command be recognised / did I do anything horribly wrong? Any help is appreciated :)
     
  2. Offline

    Tzeentchful

    I dont think you can have spaces in your commands. Anything after the command is in the args array.
    Code:
    if (cmd.getName().equalsIgnoreCase("takeone")){
      if(args[0].equalsIgnoreCase("set")){
      //bla bla
      }
    }
    also remember to return true after you are done doing stuff with the command.
     
  3. Offline

    LimitedWard

    Spaces are not allowed in command names. You must work with each word as though they were separate arguments in the command string.
     
  4. Offline

    JoetheMango

    Oh OK, but how would I do that? Tzeen what do you mean by "args array"?
     
  5. Offline

    Sabersamus

    The String array parameter in onCommand().
    You know, public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){}
     
  6. Offline

    zack6849

    Anything after the first word is stored in an array or strings called "args" you'd want to do something along the lines of
    Code:
    if(cmd.getName().equalsIgnoreCase("takeone") && (args.length >= 1){
         if(args[0].equalsIgnoreCase("set"){
            //your code here
            return true;
            }
        }
    
     
  7. Offline

    JoetheMango

    thank you ! :)

    Zack, on a fairly unrelated note this block break event is not working and I have no clue why, it's the bit after @EventHandler


    Code:
    package com.hotmail.joethemango.takeone;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
    public class Takeone extends JavaPlugin implements Listener{
       
        public static String ActiveString;
     
     
        @Override
        public void onEnable(){
            getLogger().info("TakeOne 1.0.1 has been loaded!");
        }
     
        @Override
        public void onDisable() {
            getLogger().info("TakeOne 1.0.1 has been stopped!");
           
        }
     
        @EventHandler
          public void PlayerBreaksBlock(BlockBreakEvent event) {
          getLogger().info("A block has been broken"); // just for debugging
            }
     
     
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if (cmd.getName().equalsIgnoreCase("takeone_set")){
                if (sender instanceof Player) {
                   
                    Player p = (Player) sender;
               
                      if(p.hasPermission("takeone.set")){
                          Variables.ActivePlayer = p ;
                          ActiveString = p.getName();
                          getLogger().info(Takeone.ActiveString + " has enabled set TakeOne chest"); //remove this its just for debugging
                            // do something
                          return true;
                      }
                 
                    } else {
                      sender.sendMessage("You must be a player to set a TakeOne chest!");
                      return false;
                    }
            }
                    // do something
                    return false;
        }
       
    }
     
     
               
       
     
     
       
     
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  8. Offline

    zack6849

    Register it in your onEnable, and im not sure about using event handlers in the main
    i always use seperate classes for EventHandlers.
     
  9. Offline

    jacklin213

    doesnt he need to have something in
    public void PlayerBreaksBlock(BlockBreakEvent event) {

    to trigger the event? or does it work when any block is broken
     
    zack6849 likes this.
  10. Offline

    zack6849

    When the event handler is called if it is set up correctly it runs whatever code is there, so when he does get it working it should print to the console.
     
Thread Status:
Not open for further replies.

Share This Page