[Solved] Sending Players A Message For My Plugin!!

Discussion in 'Plugin Development' started by HotelManager24, Nov 25, 2011.

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

    HotelManager24

    Hey, I was trying to send a player a message when he/she would use a command. But, it didn't work.. :S

    I do believe this is how one sends a command to a player:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args) {
    Player player = (Player) sender;
    if(cmd.getName().equalsIgnoreCase("example")) {
    player.sendMessage(ChatColor.LIGHT_PURPLE + "This is the example message when using the example command!");
    
    }
    But, it doesn't seem to work. Please help!
     
  2. Well... What doesn't work? Error? Or just no message.
    The code looks good if your using the /example command.
     
  3. Offline

    HotelManager24

    No error, there is just no message. I am using the "/example" command.. :S
     
  4. Offline

    Waffletastic

    Are you trying to send a message to the sender of a command? If so use
    Code:
    sender.sendMessage() 
    
    So it would look like this:
    Code:
    [SIZE=13px][FONT=Consolas]public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args) {[/FONT][/SIZE]
    [SIZE=13px][FONT=Consolas][/FONT][/SIZE][SIZE=13px][FONT=Consolas]if(cmd.getName().equalsIgnoreCase("example")) {[/FONT][/SIZE]
    [SIZE=13px][FONT=Consolas][/FONT][/SIZE][SIZE=13px][FONT=Consolas]sender.sendMessage(ChatColor.LIGHT_PURPLE + "This is the example message when using the example command!");[/FONT][/SIZE]
    [SIZE=13px][FONT=Consolas]}[/FONT][/SIZE] 
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2016
  5. Does your onCommand even fire?

    Put this in onCommand:
    Code:
    Logger.getLogger("Minecraft").info("onCommand fired!");
    
    Now you should get an console message with "onCommand fired!"
     
  6. Offline

    HotelManager24

    Yes, I am trying to send it to the sender, I change the code to use "sender.sendMessage(ChatColor.LIGHT_PURPLE + "This is the example message when using the example command!");" but it still won't work.
     
  7. Offline

    Waffletastic

    I guess you could do that, but it doesn't send the message to the player. And sorry my message got messed up. idk what happened
     
  8. Offline

    HotelManager24

    So I changed the code to be:
    Code:
    if(cmd.getName().equalsIgnoreCase("example")) {[/FONT][/SIZE][/FONT][/SIZE]
    [SIZE=4][FONT=arial][SIZE=4][FONT=arial]log.info("Command example used!");[/FONT][/SIZE][/FONT][/SIZE]
    [SIZE=4][FONT=arial][SIZE=4][FONT=arial]sender.sendMessage(ChatColor.LIGHT_PURPLE + "This is the example message when using the example command!");[/FONT][/SIZE][/FONT][/SIZE]
    [SIZE=4][FONT=arial][SIZE=4][FONT=arial]return true;[/FONT][/SIZE][/FONT][/SIZE]
    [SIZE=4][FONT=arial][SIZE=4][FONT=arial]}
    [/FONT][/SIZE]

    But, "Command example used!" doesn't appear on the console..[/CODE][/CODE][/CODE][/FONT][/SIZE][/CODE][/FONT][/SIZE]

    Sorry about the messed up thing.. :S[/CODE][/FONT][/SIZE]

    Also, when I enter in "Logger log = Logger.getLogger("Minecraft").info("onCommand fired!");", Eclipse gives me an error.

    Code:
    Type Mismatch: Cannot convert from void to logger.
    Any advice?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2016
  9. Offline

    theguynextdoor

    Have you registered the command in your main class, and in your plugin.yml?
     
  10. Offline

    Slamakans

    use Label instead of cmd at line 4, and don't use .getName() after it, just .equalsIgnoreCase()

    Here's what I use for one of my plugins... you may edit it to your liking.
    Code:

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    
            Player player = (Player) sender;
    
            if(commandLabel.equalsIgnoreCase("shelter")){
                toggleVision(player);
            }
    
            return true;
    
        }
     
  11. Try this
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args) {
      Logger.getLogger("Minecraft").info("onCommand fired!");
      Player player = (Player) sender; if(cmd.getName().equalsIgnoreCase("example")) {
        player.sendMessage(ChatColor.LIGHT_PURPLE + "This is the example message when using the example command!");
      }
    PS: Don't forget to import logger
    Code:
    import java.util.logging.Logger;
    PS2: The addition of the Logger just adds some debugging. If it does print out that line in the console then you know onCommand has been fired. If not, then that's the problem!
     
  12. Offline

    HotelManager24

    I tried this but when I run the command in-game, it continues responding with "Unknown command"
     
  13. Offline

    theguynextdoor

    Try something like this if it's in your main class

    Code:java
    1.  
    2. this.getCommand("example").setExecutor(new CommandExecutor() {
    3.  
    4. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    5. if (cmd.getName().toLowerCase().equals("example")) {
    6. sender.sendMessage(ChatColor.LIGHT_PURPLE + "This is the example message when using the example command!");
    7. return true;
    8. }
    9. return false;
    10. }
    11. });


    This can go in your onEnable()

    And don't forget to put it in your plugin.yml aswell.

    Code:
    name: Example
    main: <package name>.<Main class name>
    version: 0.1
    commands:
      example:
          description: derp
          usage: /<command>
    
     
    dadaemon likes this.
  14. Offline

    HotelManager24

    Ok, so here it is:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args {
    Player player = (Player) sender;
    if(commandLabel.equalsIgnoreCase("example")) {
    Logger.getLogger("Minecraft").info("onCommand fired!");
    sender.sendMessage(ChatColor.LIGHT_PURPLE + "[SIZE=13px][FONT=Consolas]This is the example message when using the example command![/FONT][/SIZE]");
    return true;
    }
    When I type "/example" in-game, it tells me Unknown Command. Also, nothing appears on the console.

    @theguynextdoor You were right. My plugin.yml layout for the command was wrong. Thank you, guys!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2016
  15. Offline

    theguynextdoor

  16. Offline

    HotelManager24

    Hey, one last thing.. I got this error when I ran the plugin.

    Code:
    [SEVERE] Error occurred while enabling StopLookingAtMyPlugins v1.0 (Is it up to date?): null
    java.lang.NullPointerException
    	at me.hotelmanager.slamp.slampmain.onEnable(slampmain.java:27)
    	at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:174)
    	at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:957)
    	at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:280)
    	at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:171)
    	at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:154)
    	at net.minecraft.server.MinecraftServer.e(MinecraftServer.java:297)
    	at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:284)
    	at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:152)
    	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:348)
    	at net.minecraft.server.ThreadServerApplication.run(SourceFile:417)
     
  17. Offline

    theguynextdoor

    Post me your code, and specify which is line 27
     
  18. Offline

    HotelManager24

    I sent the code in a private message.
     
  19. Offline

    Daniel Heppner

    [/FONT][/SIZE]
    [/CODE][/quote]Please remove all the stupid [font] tags and stuff in there. Why would they even be in there? They don't work in code blocks.[/CODE][/FONT][/SIZE][/quote]
     
Thread Status:
Not open for further replies.

Share This Page