(SOLVED)Quick question: How do I display a message exclusively to a player

Discussion in 'Spigot Plugin Development' started by Ygx, Jul 10, 2015.

  1. Ygx

    Ygx

    Hey guys. I have some basic code here and I want it to send a string to the player upon the command "/faq". So far I have not been able to get it to work!
    Code (Text):

    [INDENT][INDENT]public boolean onCommand(Command cmd, CommandSender sender, String label, String args[]) {
            if(label.equalsIgnoreCase("faq")) {
                //Send the player a big message about frequently asked questions.
                Player p = (Player) sender;
                p.sendMessage("String goes here!");
            }
            return true;
        }
     
    This should be working right?
    What am I doing wrong? Whether or not I am op on the server doing /faq does nothing. When I do /help faq it shows up properly. The plugin is aknowledged but not working.
     
    #1 Ygx, Jul 10, 2015
    Last edited: Jul 10, 2015
  2. Please click the link in my signature unrelated to my plugins, Then try to update your post. You'll get more help if people can understand what the problem is immediately and, don't have to request stack traces. Also, Putting debug messages in your code will help find silent errors that don't generate stack traces.

    I would also suggest to look into pagination for displaying pages of faq. Rather then 100 single lines, That the player has to scroll through.
     
  3. Is the command in the plugin.yml?

    (guessing it is since /help shows it)
     
  4. Ygx

    Ygx

    Yes it is in the yml.
     
  5. Do cmd.getName().equalsIgnoreCase by the way, not label.
     
    • Informative Informative x 1
  6. Ygx

    Ygx

    I also want to know how to send messages to the player for future references. I could use another plugin but I want to get to learn the bukkit/spigot api. So far I have not found any resources that tell me how to do this.
     
  7. Ygx

    Ygx

    I did that but the message is still not being displayed. Help?
     
  8. I'm not too sure what you're trying to say here. Would you mind elaborating?
     
    • Winner Winner x 1
  9. Ygx

    Ygx

    I want the player who types the command (In this case /faq) to see a message. It does not need to be broadcast to the whole server, just the player who types the command. How can I do this?
     
  10. That's exactly what p.sendMessage() does. It won't broadcast the message to everyone.
     
  11. Ygx

    Ygx

    Are you sure? It's not doing that for me. How do i send the message? What's wrong with my code!
     
  12. The only scenario that may cause that to happen is if you're casting the console to a player. Which I'm not even sure would happen.

    You should be checking to see if the sender is a instanceof a player, Before casting the sender to a player. As the sender could be the console.

    And I'm about 10,000 lines of code sure that p.sendMessage() only shows a message to that player.
     
  13. Something like this. I am on mobile so the spacing may be incorrect.

    Code (Text):
      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("faq")){
                return true;
                if (!(sender instanceof Player)) {
                    sender.sendMessage("You are not a player.");
                } else {
                    Player player = (Player) sender;
                    // do something
                }
                return true;
            }
            return false;
        }
     
     
    • Agree Agree x 1
  14. Ygx

    Ygx

    That code gives me errors. "Unreachable code"
     
  15. You are doing it correctly. But you have some other problems...

    Code (Text):
    player.sendMessage("the message");
    This sends to only that player. But lets look at the rest of your code.

    Firstly, you are casting the sender to a Player without checking if it is indeed a player. If you were to try this command in the console you would generate a stacktrace because of that. To fix this, you can either check to be sure sender is a Player first and do something else if its not a Player (by checking if the instanceof sender is a Player). OR, you can simply send the message to the CommandSender so it works for console and command blocks as well as players.

    Code (Text):
    sender.sendMessage("the message");
    Secondly, you are checking the label to be "faq" while you should be checking if the command's name is "faq" instead. Checking the command will allow your command aliases to also work. Checking the label forces a certain alias/name to be used while all others will fail. Use cmd.getName() instead of label, unless otherwise specifically needed.

    Now, onto the real issue here. You are not overriding the correct method for running commands. If you had added the @Override annotation, your IDE would have thrown an error because the method you are using has no super method. Why? Simple. The method you are using is this:

    Code (Text):
    public boolean onCommand(Command cmd, CommandSender sender, String label, String args[]) {
    But the method you need to override is this:

    Code (Text):
    public boolean onCommand(Command cmd, CommandSender sender, String label, String[] args) {
    Yes, its that simple. Here is a sample to show the entire command:

    Code (Text):

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("faq")) {
                sender.sendMessage("the message");
                return true;
            }
            return false;
        }
     
    And here is an example for a command that requires a Player object:


    Code (Text):

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("faq")) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage("Player command only!");
                    return true;
                }

                Player player = (Player) sender;
                player.sendMessage("the message");
                return true;
            }
            return false;
        }
     
     
    #15 BillyGalbreath, Jul 10, 2015
    Last edited: Jul 10, 2015
    • Winner Winner x 1
    • Informative Informative x 1
  16. Check the return statements, as I said, I am on a mobile. It's very confusing on the screen.
     
    #16 Asyncline, Jul 10, 2015
    Last edited: Jul 18, 2015
  17. "Something like this" doesn't imply the code works. You'll have to figure it out on your own. Unless 1/100 member comes along and spoon feeds you code.
     
  18. Ygx

    Ygx

    I understand that p.sendMessage() does that. For me it isn't working. How would I make this work? How do I check the sender?
     
  19. Ygx

    Ygx

    Will do!
     
  20. Didn't read all of the above posts but you need to do two things.
    1. Register the command in your plugin.yml
    2. Paste this in
    Code (Text):
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label,
                String[] args) {
            if(cmd.getName().equalsIgnoreCase("faq")) {
                sender.sendMessage("Message here");
            }
            return true;
        }