Getting player from sender and sending string in game.

Discussion in 'Spigot Plugin Development' started by MinerDude010, Feb 8, 2017.

  1. Hey guys, I just started learning how to code some simple plugins for spigot. I was wondering how I could have a command that you could run in console, but the reply sends in the game.

    I have:

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

    if(cmd.getName().equalsIgnoreCase("test")) {
    sender.sendMessage("yes, test");
    }
    return false;
    }

    But when I do it in console, it says "yes, test" back to me in the console. I want it to send "yes, test" in the game instead of console. Thanks in advance! And sorry for the noob question
     
  2. If you want to broadcast it to everyone.. then

    Code (Java):

    Bukkit.broadcastMessage("example");
     
  3. That sends the message in game and in console, I want it JUST to send the message in game no matter if the command is used in console or in game
     
  4. Strahan

    Benefactor

    To one person:

    Code (Text):
    Player p = this.getServer().getPlayer("somebody");
    if (p != null) p.sendMessage("Whatever you want to say.");
    To all:

    Code (Text):
    for (Player p : Bukkit.getOnlinePlayers()) p.sendMessage("Whatever you want to say.");
     
  5. for(Player player : Bukkit.getOnlinePlayers())
    player.sendMessage(msg);

    Edit:

    Ooh too late :c
     
  6. Strahan

    Benefactor

    Yours is better anyway, as my all players example (before I edited it at least lol) won't work if you aren't in the main class.
     
  7. [​IMG][​IMG]

    i Think This is what You want
    Code (Text):
    //Console Command
    if(cmd.getName().equalsIgnoreCase("msg")){  
                        for(Player player2 : Bukkit.getServer().getOnlinePlayers() ){
                            player2.sendMessage("Hey");
                        }
                }    
    Late D:
     
  8. Strahan

    Benefactor

    Is .getServer() really necessary when .getOnlinePlayers() is already exposed on the Bukkit object? Also if you want to save some typing just do label.equalsIgnoreCase("msg") - label is the same thing as cmd.getName() after all.
     
  9. No is not, if you use label you can't use alias for commands!
     
  10. Strahan

    Benefactor

    Hmm. That's odd. In the plugin I was working on lately, my onCommand looks like this:

    Code (Text):
      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        try {
          Player p = ((sender instanceof Player)?(Player)sender:null);
          switch (label.toLowerCase()) {
          case "itemcmd":
          case "ic":
              if (args.length == 0) {
     
    My plugin.yml is:

    Code (Text):
    (snip)
    commands:
      itemcmd:
        description: Plugin control interface
        aliases: [ic]
    ...and that catches both itemcmd and ic. Or did you mean something other than aliases defined as I've done?
     
  11. Code (Java):

    Bukkit.getOnlinePlayers().forEach(player -> player.sendMessage("example"));
     
    • Agree Agree x 2
  12. Im pretty sure than somewhere i read that if you use label you cant use aliases but maybe now you can!
     
  13. Choco

    Moderator

    The String passed to the onCommand() method is the command alias that was used. i.e. let's say this is my command
    Code (YAML):
    commands:
        MyAwesomeCommand
    :
            description
    : It's pretty awesome
            usage
    : /<command>
            aliases
    : [somealias, someotheralias]
    If I were to execute the command, "/somealias", the String passed to onCommand() would be "somealias". Otherwise, if I were to execute the command, "/someotheralias", the String would instead be "someotheralias". This is why people instead use the cmd.getName() method because it's getting the Command object that was used, and getting the name it was registered under. Whether "somealias" or "someotheralias" was used, it's going to pass the same Command object. The Command's #getName() method returns the command that was registered, in this case, "MyAwesomeCommand".

    It allows for more flexibility and less typing in the long run. You wouldn't need to use a switch statement in the first place if you just check the Command's name. And on top of that, if you use a CommandExecutor, you don't even need to check for the name of the command at all.
     
    • Like Like x 1
    • Informative Informative x 1
  14. JanTuck

    Supporter

    *You can always have aliases.