Development Assistance Trying to announce what the player typed in the command

Discussion in 'Plugin Help/Development/Requests' started by PerezHD, Dec 22, 2014.

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

    PerezHD

    Hey guys, I am basically trying to make it so when a player types /stream <twitch> it will display a message with their twitch. But when I attempted to type /stream PerezHD in-game, it just told me the usage in code? I am guessing the args are incorrect? Please help me!

    Code:
      public boolean onCommand(CommandSender sender, Command cmd, String lable, String[] args)
      {
        Player p = (Player)sender;
        if (cmd.getName().equalsIgnoreCase("stream") && p.hasPermission("stream.announce")) {
           
            long lastUsed = 0;
            if (lastUsage.containsKey(sender.getName())){
                lastUsed = lastUsage.get(sender.getName());
            }
           
            int cdmillis = cTime * 1000;
           
            if (System.currentTimeMillis() - lastUsed > cdmillis){
               
                if (streaming.contains(p.getName())){
                    Bukkit.broadcastMessage(stream + ChatColor.RED + p.getName() + " has stopped streaming!");
                }
                else if (args.length < 2){
                    sender.sendMessage(ChatColor.RED + "Usage: /stream <twitch>");
                }
                else {
                    Bukkit.broadcastMessage(stream + ChatColor.GREEN + "" + ChatColor.BOLD + p.getName() + " is streaming" + ChatColor.AQUA + "" + ChatColor.BOLD + " Go watch @ www.twitch.tv/" + args[1] + "!");
                    lastUsage.put(sender.getName(), System.currentTimeMillis());
                }
               
            }
            else {
                int timeLeft = (int) (cTime -((System.currentTimeMillis() - lastUsed)/1000));
                sender.sendMessage(ChatColor.RED + "You must wait another " + timeLeft + "s.");
            }
        }
        return false;
      }
    }
     
  2. Offline

    adam753

    Firstly, the command itself isn't counted in the args, so the in the command "/stream twitch" args.length is only 1. And secondly, when onCommand returns false it will pring out the usage from plugin.yml, so you shouldn't do it all the time.
     
  3. Offline

    _Filip

  4. Offline

    teej107

    If you type from the console, it'll show you a ClassCastException.
     
  5. Offline

    PerezHD

    Well I am stuck on this part, I am basically trying to make it so on the <twitch> part, it will show anything that the player types for example /stream abc. It would show abc

    Code:
        if (cmd.getName().equalsIgnoreCase("stream")) {
            if (sender instanceof Player){
                Player p = (Player)sender;
                if (p.hasPermission("stream.announce")){
                  
                }
            }
            else {
                sender.sendMessage("You must be a player to type that command!");
                return false;
            }
          
            if (args.length > 2){
                sender.sendMessage("Too many arguments!");
                return false;
            }
          
            String msg = (args[0]);
    Im basically stuck at
    Code:
    String msg = (args[0]);
     
  6. Offline

    Techy4198

    First of all you need to put that under the line where you check if the player has the permission.
     
  7. Offline

    PerezHD

    Well I did a different sort of setup, here is what I did.

    Code:
      public boolean onCommand(CommandSender sender, Command cmd, String lable, String[] args)
      {
        if (cmd.getName().equalsIgnoreCase("stream")) {
            if (sender instanceof Player){
                Player p = (Player)sender;
                if (p.hasPermission("stream.announce")){
                  
                }
            }
            else {
                sender.sendMessage("You must be a player to type that command!");
                return false;
            }
          
            if (args.length > 0){
                sender.sendMessage(stream + ChatColor.RED + "You are adding more arguments then told!");
                return false;
            }
            if (args.length == 0){
                sender.sendMessage(stream + ChatColor.RED + "Usage: /stream <twitch>");
            }
            long lastUsed = 0;
            if (lastUsage.containsKey(sender.getName())){
                lastUsed = lastUsage.get(sender.getName());
              
                int cdmillis = cTime * 1000;
              
                if (System.currentTimeMillis() - lastUsed > cdmillis){
                  
                    if (args.length == 1){
                        Player p = (Player) sender;
                        Bukkit.broadcastMessage(stream + ChatColor.GREEN + "" + ChatColor.BOLD + p.getName() + " is streaming" + ChatColor.AQUA + "" + ChatColor.BOLD + " Go watch @ www.twitch.tv/" + args[0] + "!");
                        lastUsage.put(sender.getName(), System.currentTimeMillis());
                    }
                    else {
                        int timeLeft = (int) (cTime -((System.currentTimeMillis() - lastUsed)/1000));
                        sender.sendMessage(ChatColor.RED + "You must wait another " + timeLeft + "s.");
                    }
                }  
            }
        }
        return false;
      }
    Yet, when I do it in-game, it states that I am adding more arguments. Anyway to fix that?
     
  8. Offline

    JordyPwner

    This code dude. So wrong i believe
     
  9. Offline

    PerezHD

    Thats why I am here asking for help..... Do you honestly think you telling me that my code is wrong is going to make it any better. I know its wrong. Again, that's why I am here asking.
     
  10. Offline

    nverdier

    @PerezHD
    You have to put the
    Code:
    if (args.length > 0){
                sender.sendMessage(stream + ChatColor.RED + "You are adding more arguments then told!");
                return false;
            }
            if (args.length == 0){
                sender.sendMessage(stream + ChatColor.RED + "Usage: /stream <twitch>");
            }
            long lastUsed = 0;
            if (lastUsage.containsKey(sender.getName())){
                lastUsed = lastUsage.get(sender.getName());
         
                int cdmillis = cTime * 1000;
         
                if (System.currentTimeMillis() - lastUsed > cdmillis){
             
                    if (args.length == 1){
                        Player p = (Player) sender;
                        Bukkit.broadcastMessage(stream + ChatColor.GREEN + "" + ChatColor.BOLD + p.getName() + " is streaming" + ChatColor.AQUA + "" + ChatColor.BOLD + " Go watch @ www.twitch.tv/" + args[0] + "!");
                        lastUsage.put(sender.getName(), System.currentTimeMillis());
                    }
                    else {
                        int timeLeft = (int) (cTime -((System.currentTimeMillis() - lastUsed)/1000));
                        sender.sendMessage(ChatColor.RED + "You must wait another " + timeLeft + "s.");
                    }
                }
            }
        }
    INSIDE the permission check. I recommend learning how to use blocks and java in general before using bukkit.
     
  11. Offline

    PerezHD

    Thank you, I was wondering why the permission was empty.

    Okay well, I did exactly what you did, still getting this issue
    http://gyazo.com/aca6bfefc109c335bf5f498bf929c3b2

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 28, 2016
  12. Offline

    nverdier

    @PerezHD What you are doing is 'If the user entered no arguments: send them the usage', 'If the user entered more than 0 arguments: tell them too many args'. You need to change if (args.length > 0) to if (args.length > 1) and add an else statement after the end bracket to that if statement. Again, I would recommend learning Java before Bukkit.
     
    AdamQpzm likes this.
  13. Offline

    AdamQpzm

    @PerezHD Read the code you've written in english terms and the problem becomes clear ;)

    "if the length of args is greater than 0, send the player a message"
     
    bwfcwalshy and nverdier like this.
  14. Offline

    bwfcwalshy Retired Staff

    @PerezHD
    Check if it is a player
    Check if args are 0
    Check if args are 1
    Check if args are >= 2
    Add the rest of the code.

    You are checking if it is greater than 0 and not 2.
     
  15. Offline

    PerezHD

    lol so basically math right?
     
  16. Offline

    AdamQpzm

  17. Offline

    PerezHD

    @AdamQpzm @nverdier
    Well I attempted to do as you said to do. @nverdier I am confused on where you said the else statement.
    Code:
        if (cmd.getName().equalsIgnoreCase("stream")) {
            if (sender instanceof Player){
                Player p = (Player)sender;
                if (p.hasPermission("stream.announce")){
                    if (args.length > 1){
                        sender.sendMessage(stream + ChatColor.RED + "You are adding more arguments then told!");
                        return false;
                    }
                    else if (args.length == 0){
                        sender.sendMessage(stream + ChatColor.RED + "Usage: /stream <twitch>");
                    }
    As so, in-game, it does not tell me I am adding too many arguments anymore. Now it just doesn't want to display the broadcast message.
    Code:
      public boolean onCommand(CommandSender sender, Command cmd, String lable, String[] args)
      {
        if (cmd.getName().equalsIgnoreCase("stream")) {
            if (sender instanceof Player){
                Player p = (Player)sender;
                if (p.hasPermission("stream.announce")){
                    if (args.length > 1){
                        sender.sendMessage(stream + ChatColor.RED + "You are adding more arguments then told!");
                        return false;
                    }
                    else if (args.length == 0){
                        sender.sendMessage(stream + ChatColor.RED + "Usage: /stream <twitch>");
                    }
                    long lastUsed = 0;
                    if (lastUsage.containsKey(sender.getName())){
                        lastUsed = lastUsage.get(sender.getName());
                  
                        int cdmillis = cTime * 1000;
                  
                        if (System.currentTimeMillis() - lastUsed > cdmillis){
                      
                            if (args.length == 2){
                                Bukkit.broadcastMessage(stream + ChatColor.GREEN + "" + ChatColor.BOLD + p.getName() + " is streaming" + ChatColor.AQUA + "" + ChatColor.BOLD + " Go watch @ www.twitch.tv/" + args[1] + "!");
                                lastUsage.put(sender.getName(), System.currentTimeMillis());
                            }
                            else {
                                int timeLeft = (int) (cTime -((System.currentTimeMillis() - lastUsed)/1000));
                                sender.sendMessage(ChatColor.RED + "You must wait another " + timeLeft + "s.");
                            }
                        }
                    }
                }
            }
        }
        return false;
      }
    I understand that this may or may not be a bother to you. I am just looking for some help. Arguments is like really irritating to me sometimes.
     
  18. Offline

    AdamQpzm

    @PerezHD What you're doing now is that all of this code:
    Code:
     lastUsed = lastUsage.get(sender.getName());
                
                        int cdmillis = cTime * 1000;
                
                        if (System.currentTimeMillis() - lastUsed > cdmillis){
                    
                            if (args.length == 2){
                                Bukkit.broadcastMessage(stream + ChatColor.GREEN + "" + ChatColor.BOLD + p.getName() + " is streaming" + ChatColor.AQUA + "" + ChatColor.BOLD + " Go watch @ www.twitch.tv/" + args[1] + "!");
                                lastUsage.put(sender.getName(), System.currentTimeMillis());
                            }
                            else {
                                int timeLeft = (int) (cTime -((System.currentTimeMillis() - lastUsed)/1000));
                                sender.sendMessage(ChatColor.RED + "You must wait another " + timeLeft + "s.");
                            }
    is only executed if the lastUsage contains their key. It can't contain it by default, so nobody ever has it executed for them.
     
  19. Offline

    PerezHD

    You think it will work like this?
    Code:
      public boolean onCommand(CommandSender sender, Command cmd, String lable, String[] args)
      {
        if (cmd.getName().equalsIgnoreCase("stream")) {
            if (sender instanceof Player){
                Player p = (Player)sender;
                if (p.hasPermission("stream.announce")){
                    if (args.length > 1){
                        sender.sendMessage(stream + ChatColor.RED + "You are adding more arguments then told!");
                        return false;
                    }
                    else if (args.length == 0){
                        sender.sendMessage(stream + ChatColor.RED + "Usage: /stream <twitch>");
                    }
                    else if (args.length == 1){
                        long lastUsed = 0;
                        if (lastUsage.containsKey(sender.getName())){
                            lastUsed = lastUsage.get(sender.getName());
                     
                            int cdmillis = cTime * 1000;
                     
                            if (System.currentTimeMillis() - lastUsed > cdmillis){
                                Bukkit.broadcastMessage(stream + ChatColor.GREEN + "" + ChatColor.BOLD + p.getName() + " is streaming" + ChatColor.AQUA + "" + ChatColor.BOLD + " Go watch @ www.twitch.tv/" + args[1] + "!");
                                lastUsage.put(sender.getName(), System.currentTimeMillis());
                    }
                            else {
                                int timeLeft = (int) (cTime -((System.currentTimeMillis() - lastUsed)/1000));
                                sender.sendMessage(ChatColor.RED + "You must wait another " + timeLeft + "s.");
                            }
                        }
                    }
                }
            }
        }
        return false;
      }
    EDIT: Tested it, it doesn't work. IDK what I am doing wrong
     
  20. Offline

    AdamQpzm

    @PerezHD No, it's still inside the containsKey check
     
  21. Offline

    PerezHD

    @AdamQpzm Oh, I see
    Would I have to move
    Code:
                        long lastUsed = 0;
                        if (lastUsage.containsKey(sender.getName())){
                            lastUsed = lastUsage.get(sender.getName());
                      
                            int cdmillis = cTime * 1000;
    to the top or somewhere?
     
  22. Offline

    AdamQpzm

    @PerezHD Not necessarily, you just need to make sure that the check for the last used time and broadcasting of the message is outside the containsKey check :)
     
  23. Offline

    PerezHD

    Code:
      public boolean onCommand(CommandSender sender, Command cmd, String lable, String[] args)
      {
        if (cmd.getName().equalsIgnoreCase("stream")) {
            long lastUsed = 0;
            if (lastUsage.containsKey(sender.getName())){
                lastUsed = lastUsage.get(sender.getName());
          
                int cdmillis = cTime * 1000;
               
            if (sender instanceof Player){
                Player p = (Player)sender;
                if (p.hasPermission("stream.announce")){
                    if (args.length > 1){
                        sender.sendMessage(stream + ChatColor.RED + "You are adding more arguments then told!");
                        return false;
                    }
                    else if (args.length == 0){
                        sender.sendMessage(stream + ChatColor.RED + "Usage: /stream <twitch>");
                    }
                    else if (args.length == 1){
                        if (System.currentTimeMillis() - lastUsed > cdmillis){
                            Bukkit.broadcastMessage(stream + ChatColor.GREEN + "" + ChatColor.BOLD + p.getName() + " is streaming" + ChatColor.AQUA + "" + ChatColor.BOLD + " Go watch @ www.twitch.tv/" + args[1] + "!");
                            lastUsage.put(sender.getName(), System.currentTimeMillis());
                            }
                            else {
                                int timeLeft = (int) (cTime -((System.currentTimeMillis() - lastUsed)/1000));
                                sender.sendMessage(ChatColor.RED + "You must wait another " + timeLeft + "s.");
                            }
                        }
                    }
                }
            }
        }
        return false;
      }
    I swear I hope I did this right!
     
  24. Offline

    AdamQpzm

    @PerezHD Unfortunately you've now put even more code inside the check - now it won't do anything unless they're in the map!

    You need to pay attention to the braces - your indentation suggests that you're doing it right, but you're missing the } - and it's the } that's important :)
     
  25. Offline

    PerezHD

    So I was basically missing a brace?

    EDIT: Well, if I am, please tell me where!
     
  26. Offline

    AdamQpzm

    @PerezHD Yes you are - and you need to put it before the if(sender instanceof Player) part
     
  27. Offline

    PerezHD

    @AdamQpzm This may look really stupid, but like this right?

    Code:
      public boolean onCommand(CommandSender sender, Command cmd, String lable, String[] args)
      {
        if (cmd.getName().equalsIgnoreCase("stream")) {
            {
               
            }
            if (sender instanceof Player){
                Player p = (Player)sender;
                if (p.hasPermission("stream.announce")){
                    if (args.length > 1){
                        sender.sendMessage(stream + ChatColor.RED + "You are adding more arguments then told!");
                        return false;
                    }
                    else if (args.length == 0){
                        sender.sendMessage(stream + ChatColor.RED + "Usage: /stream <twitch>");
                    }
                    else if (args.length == 1){
                        long lastUsed = 0;
                        if (lastUsage.containsKey(sender.getName())){
                            lastUsed = lastUsage.get(sender.getName());
                      
                            int cdmillis = cTime * 1000;
                           
                            {
                       
                            }
                      
                            if (System.currentTimeMillis() - lastUsed > cdmillis){
                                Bukkit.broadcastMessage(stream + ChatColor.GREEN + "" + ChatColor.BOLD + p.getName() + " is streaming" + ChatColor.AQUA + "" + ChatColor.BOLD + " Go watch @ www.twitch.tv/" + args[1] + "!");
                                lastUsage.put(sender.getName(), System.currentTimeMillis());
                            }
                            else {
                                int timeLeft = (int) (cTime -((System.currentTimeMillis() - lastUsed)/1000));
                                sender.sendMessage(ChatColor.RED + "You must wait another " + timeLeft + "s.");
                            }
                        }
                    }
                }
            }
        }
        return false;
      }
    XD, its funny chatting with you, you make me laugh!
     
  28. Offline

    AdamQpzm

    @PerezHD Now you've got a couple of empty sections, the code is a bit messy to be honest. :( I think you need to take a step back and actually look at where your braces are and what they're doing.
     
    bwfcwalshy likes this.
  29. Offline

    PerezHD

    @AdamQpzm Dude, I know XD. Empty braces = no no. I know that. I am very familer with coding. Its just that when someone types on where to put it, I can't just go towards it. I don't know, my mind is like a visual learner. That's why its sort of hard.

    EDIT: Also are you asking me to get
    Code:
                                Bukkit.broadcastMessage(stream + ChatColor.GREEN + "" + ChatColor.BOLD + p.getName() + " is streaming" + ChatColor.AQUA + "" + ChatColor.BOLD + " Go watch @ www.twitch.tv/" + args[1] + "!");
                                lastUsage.put(sender.getName(), System.currentTimeMillis());
    out of
    Code:
                            if (System.currentTimeMillis() - lastUsed > cdmillis){
     
  30. Offline

    AdamQpzm

    @PerezHD Well, like I said: Step back and think about the flow of the code. If need be, manually check where each opening brace closes. Say to yourself "should this close here? Does this make it so that my if check encompasses too many statements, or too few?"
     
Thread Status:
Not open for further replies.

Share This Page