sendMessage doesn't seem to do anything

Discussion in 'Plugin Development' started by zootal, Aug 15, 2012.

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

    zootal

    This is such a stupid problem I hesitate to post this, but I have no clue why this does not work. I'm building a simple plugin using the tutorial found at bukkit.org.This is my .java file. I can export it to a jar and load it on the server, but when I type /zoot, all I get is the usage line from the yml file. I would expect the sendMessage line that outputs "bork bork..." to at least send to the console, but it does not. The fact that it outputs the usage line from the yml file leads me to believe that the if statement

    Code:
    if(cmd.getName().equalsIgnoreCase("zoot"))
    is returning false (because it outputs the usage line, and that only happens if the if statement returns false, right?)

    So I'm trying to understand 1) why the if statement is returning false and 2) why the send.sendMessage doesn't send anything. I'm a total n00b so be gentle with me :)


    Code:
    package plugin_1;
     
    import org.bukkit.command.CommandSender;
     
        public class Plugin_1 extends org.bukkit.plugin.java.JavaPlugin {
           
            public void onEnable()
                {
                    getLogger().info("minecraft_plugin_1 has been enabled!");
                }
               
                public void onDisable()
                {
                    getLogger().info("minecraft_plugin_1 has been disabled!");
                }
     
                public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
                {
                   
                    if(cmd.getName().equalsIgnoreCase("zoot"))
                    {
                        // If the player typed /zoot then do the following...
                        // doSomething
                        sender.sendMessage("minecraft_plugin_1 test. /zoot was typed. Success");
                        return true;
                    } //If this has happened the function will break and return true. if this hasn't happened the a value of false will be returned.
                   
                    // Output something anyhow just to see if this works or not
                    sender.sendMessage("bork bork bork bork");
                                 
                    return false;
                }
               
               
     
        }
    
     
  2. Offline

    Asdjke

    there is no need for cmd.getName().equalsIgnoreCase("zoot");
    you only need cmg.equalsIgnoreCase("zoot");
     
  3. Offline

    devilquak

    First, make sure everything, including any commands, are registered in your plugin.yml.

    Code:
    if(cmd.getName().equalsIgnoreCase("zoot"))
                    {
                        sender.sendMessage("minecraft_plugin_1 test. /zoot was typed. Success");
                        return true;
                    }
                    else
                    {
                    sender.sendMessage("bork bork bork bork");
                    return true;
                    }
                    return false;
                }
    }
    
    Add an else statement and another return true;, otherwise it'll return false and give you an ugly command suggestion after whatever else it does.

    If that doesn't work, add

    Code:
    Player player = (Player) sender;
    below the command statement, and change all every "sender" to a player, and see if that works.
     
  4. Offline

    Asdjke

    Yeah add in Player player = (Player) sender; then do for example player.sendMessage("test"); insted of sender.sendMessage("test");
     
  5. Offline

    Malikk

    You can use sender.sendMessage just fine. CommandSender has both Player and the ConsoleCommandSender as subinterfaces. Either way, it will send the message correctly. This isn't the issue here.
     
  6. Offline

    jacklin213

    or you could do
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] args)
                {
               
                    if(commandLabel.equalsIgnoreCase("zoot"))
                    {
                        // If the player typed /zoot then do the following...
                        // doSomething
                        sender.sendMessage("minecraft_plugin_1 test. /zoot was typed. Success");
                        return true;
                    } //If this has happened the function will break and return true. if this hasn't happened the a value of false will be returned.
                else{
                    // Output something anyhow just to see if this works or not
                    sender.sendMessage("bork bork bork bork");
                              }
                    return false;
                }
              
    me believes that the else statement works here as well as making (Sting label) to (commandLabel)
     
  7. Offline

    zootal

    Thanks everyone for your input. At the end of the day, the problem was really quite simple. The tutorial neglected to mention a very vital step, that is to add this to the top of your class....

    Code:
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    Problem solved :)

    I'm not a java expert, I do c++ and PHP, but I guess I still should have known better.....

    Also cmd.equalsIgnoreCase("zoot") did not work because cmd is an instance of Command, and (AFAIK) you can't do equalsIgnoreCase on the Command class, you do it on the results of Command.getName()

    I'm getting there...slowly....
     
  8. Offline

    jacklin213

    tip: you see a red line Hover over it 5/10 your problem is solved (import from "whatever")
     
  9. Offline

    Coelho

    That is bad practice.
     
  10. Offline

    jacklin213

    ^ i agree its bad but what does practice mean lol
     
  11. Offline

    Coelho

  12. Offline

    jacklin213

    i ment in terms of what u just said
     
  13. Offline

    zootal

    Yeah I eventually figured that out LOL :)
     
Thread Status:
Not open for further replies.

Share This Page