sendMessage() not working

Discussion in 'Plugin Development' started by Callum.K, Dec 30, 2013.

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

    Callum.K

    Hello I have just learning the BukkitAPI and I've made a problem but can't see it, I made a very basic command that sends the sender a message but it doesn't send the message. The plugin loads up fine and when I type the command it doesn't say unknown command but it doesn't do anything. Here's my code:
    Code:java
    1. package me.callum_keleher;
    2.  
    3. import org.bukkit.material.Command;
    4. import org.bukkit.command.CommandSender;
    5. import java.util.logging.Logger;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.PluginDescriptionFile;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class Callum extends JavaPlugin{
    12. public final Logger logger = Logger.getLogger("Minecraft");
    13. public static Callum plugin;
    14.  
    15. @Override
    16. public void onDisable() {
    17. PluginDescriptionFile ymlFile = this.getDescription();
    18. this.logger.info("Plugin: " + ymlFile.getName() + " Version: " + ymlFile.getVersion() + " has been disabled.");
    19. }
    20.  
    21. @Override
    22. public void onEnable() {
    23. PluginDescriptionFile ymlFile = this.getDescription();
    24. this.logger.info("Plugin: " + ymlFile.getName() + " Version: " + ymlFile.getVersion() + " has been enabled.");
    25. }
    26.  
    27. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    28. if(sender instanceof Player) {
    29. Player p = (Player) sender;
    30. if(commandLabel.equalsIgnoreCase("hello")) {
    31. p.sendMessage(ChatColor.GREEN + "Hello Callum!");
    32. }
    33. }
    34. return false;
    35. }
    36. }
     
  2. Offline

    ItsOneAndTwo

    try
    Code:
    sender.sendMessage(ChatColor.GREEN + "Hello Callum!");
     
  3. Offline

    bobthefish

    Callum.K
    make sure that you put that command in the plugin.yml file too if you havent.

    oh, and maybe make it return true down at the bottom
     
  4. Offline

    Zethien

    Doesn't the Command cmd part refer to the command the player gives?

    Seems like your if statement should be

    if(cmd.getName().equalsIgnoreCase("hello")){

    And take out that if(sender instanceof player){ I think you are doing that with Player p = (Player) sender;

     
  5. Offline

    Callum.K

    ItsOneAndTwo
    Neither of them worked I'm afraid.

    I get "cannot find symbol" on getName();
     
  6. Offline

    Chinwe

    Nope, it's needed: you'll get an exception if you try to cast a ConsoleCommandSender to a Player :oops:

    Callum.K
    Have you registered the command in your plugin.yml? Something like:
    Code:
    name: bleh
    version: bleh
    main: bleh
     
    commands:
      hello:
        usage: /hello (optional)
        description: Says hello back (also optional)
     
  7. Offline

    Callum.K

    I have.
    Code:java
    1. name: Callums Plugin Package
    2. main: me.callum_keleher.Callum
    3. version: 1.0
    4. description: >
    5. Callums first plugin
    6. commands:
    7. hello:
    8. description: sends a basic message saying Hello Callum!
     
  8. Offline

    Zethien

    Well I don't mean to "argue" as I am new to this but... This works 110%

    Code:java
    1. package me.Zethien.Test;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.plugin.PluginDescriptionFile;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12.  
    13.  
    14. public class Test extends JavaPlugin{
    15. public final Logger logger = Logger.getLogger("Minecraft");
    16.  
    17. public static Test plugin;
    18.  
    19. public void onDisable(){
    20. PluginDescriptionFile pdfFile = this.getDescription();
    21. this.logger.info(pdfFile.getName() + " Has Been Disabled!");
    22.  
    23. }
    24.  
    25. @Override
    26. public void onEnable(){
    27. PluginDescriptionFile pdfFile = this.getDescription();
    28. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled!");
    29.  
    30. }
    31.  
    32.  
    33. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    34. Player player = (Player) sender;
    35.  
    36. if (commandLabel.equalsIgnoreCase("test")){ //or if(cmd.getName().equalsIgnoreCare("test"))
    37.  
    38. player.sendMessage(ChatColor.RED + "" + player.getName() + " <--- This is your name!!");
    39. }
    40.  
    41. return false;
    42.  
    43. }
    44. }
     
  9. Offline

    Chinwe

    Zethien
    Try running it from the console ;)
     
  10. Offline

    Zethien

    It's not made to be run from console. It's made to be run as a player. :eek:
     
  11. Offline

    Callum.K

    I've fixed my issue, I believe it was the order of my imports or it could have been that I put player after instaneof player instead of before.
     
  12. Offline

    Zethien

    At any rate I see what you're saying but he has his player command condition in his console command if statement.

    You can keep that the way it is but it needs to look slightly different:

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    2. if (commandLabel.equalsIgnoreCase("hello")){
    3.  
    4. if (!(sender instanceof Player)) {
    5.  
    6. sender.sendMessage("This command can only be run by a player.");
    7.  
    8. } else {
    9.  
    10. Player player = (Player) sender;
    11. player.sendMessage((ChatColor.GREEN + "Hello " + player.getName());
    12.  
    13. }
    14. return true;
    15. }
    16. return false;
    17. }


    Callum.K Excellent!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
Thread Status:
Not open for further replies.

Share This Page