onCommand not working

Discussion in 'Plugin Development' started by CraftRay, Dec 30, 2015.

Thread Status:
Not open for further replies.
  1. When i wrote and send /pexcraft text, /pexcraft, /pexcraft text text - nothing
    happened. Where is my mistake?

    Code:
    package me.xcraftrayx.pl;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    
    public class PexCmd implements Listener {
      
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if(cmd.getName().equalsIgnoreCase("pexcraft")){
                if(sender instanceof Player){
                    Player player = (Player) sender;
                    if(args.length > 1){
                        sender.sendMessage(ChatColor.RED + "Podałeś za dużo argumentów");
                    }
                    if(args.length == 1){
                        username = args[0];
                        System.out.println("Lista:");
                        CrConfig.selectUser();
                    }
                    else{
                        sender.sendMessage("test");
                    }
                }
                else{
                    System.out.println("Musisz być na serwerze");
                }
            }
            return true;
        }
        public static String username;
    }
    
     
  2. Yeah, you might wanna look into args.length and args[] more...
    This is just some messy code ;-;
     
  3. Offline

    mythbusterma

    @CraftRay

    Most of it is a mistake, that's some pretty awful code. But I'm willing to bet you just didn't add it as a command handler for your command.

    EDIT: You have to extend CommandHandler for your commands to work, in addition you have register it as the command handler for that particular command.

    In Polish: Musisz rozszerzyć CommandHandler dla poleceń do pracy, dodatkowo trzeba zarejestrować go jako commandhandler dla danego polecenia.

    Might I recommend: Dowiedz się Java?
     
    Last edited: Dec 30, 2015
  4. Offline

    mcdorli

    You won, the class doesn't even extends CommandExecutor.
     
  5. Offline

    mythbusterma

    @CraftRay

    Re-read my post.

    Ponownie przeczytać mojej wiadomości.
     
  6. @mythbusterma Could you me to explain: extend CommandHandler
     
  7. Offline

    mythbusterma

  8. @mythbusterma So I first time make it, i add that in my main class: getCommand("pexcraft").setExecutor((CommandExecutor) new PexCmd());
    And why I have that mistake:
    Code:
    Error occurred while enabling PexCraft v1.0 (Is it up to date?)
    java.lang.ClassCastException: me.xcraftrayx.pl.PexCmd cannot be cast to org.bukkit.command.CommandExecutor
            at me.xcraftrayx.pl.CrConfig.onEnable(CrConfig.java:44) ~[?:?]
     
  9. Offline

    mythbusterma

    @CraftRay

    Because it still doesn't extend CommandExecutor.
     
  10. @mythbusterma Aaaa... thanks for all :)

    I must write "implements CommandExecutor" i thought "extends CommandExecutor" :p
    It working! :D
     
  11. Offline

    PresentingTulip

    Also, it's generally a better idea to return false rather than true, when the command doesn't go through.
     
  12. Offline

    Zombie_Striker

    Mark thread as solved if solved.

    But, two things I want to add:
    • The "usename" field should not be static,
    • Fields should not be below methods.
     
Thread Status:
Not open for further replies.

Share This Page