<1 word in an arguement

Discussion in 'Plugin Development' started by lubbs31, Jul 30, 2015.

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

    lubbs31

    Hello everyone, I'm making a bukkit plugin that requires more than one word in an argument.

    Example: /report [blah blah he hurt me]

    How would one start on doing this?
     
  2. Offline

    teej107

  3. Offline

    lubbs31

    Can you give me an example? I have looked through many, many threads and google, and I still cannot figure this out.
     
  4. Offline

    teej107

    @lubbs31 Example of what? You can just use StringUtils#join() which I linked to in my first post.
     
  5. Offline

    cfil360

    @lubbs31 You can also do.
    Code:
    String full = arg1 + arg2 + arg3 + arg4
     
  6. Offline

    teej107

    That is not recommended seeing as there could be any amount of arguments.
     
  7. Offline

    cfil360

    @teej107 You could always do a for loop that uses the length of args.
     
  8. Offline

    teej107

    @cfil360 Yes. That is why I suggested using a StringBuilder and even the StringUtils which can do it in one line of code.
     
  9. Offline

    cfil360

    @teej107 Sure thing. Either of the ways works. Just offering multiple solutions :)
     
  10. Offline

    lubbs31

    This is my code:
    Code:
    public class Main extends JavaPlugin {
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("911")) {
                Player p = (Player) sender;
                p.sendMessage(ChatColor.BLACK + "[" + ChatColor.DARK_GREEN + "911" + ChatColor.BLACK + "]" + ChatColor.GREEN + " 9-1-1, What is your emergency?");
                p.sendMessage(ChatColor.BLACK + "[" + ChatColor.DARK_GREEN + "911" + ChatColor.BLACK + "]" + ChatColor.GREEN + " Usage: /911 [report]");
                return false; }
            if(args.length >= 0) {
                Player p = (Player) sender;
                String prefix = ChatColor.BLACK + "[" + ChatColor.DARK_GREEN + "911" + ChatColor.BLACK + "]";
                p.sendMessage(prefix + ChatColor.GREEN + "You're report has been sent!");
                p.sendMessage(prefix + ChatColor.GREEN + args[0]);
                p.playSound(p.getLocation(), Sound.CHEST_CLOSE, 2F, 1F);
                String message = StringUtils.join(args, ' ', 0, args.length);
                p.sendMessage("Bug reported by " + sender.getName() + ": " + message);
            }
            return false;
        }
    }
           
    Something is so wrong... Can someone help?
     
  11. Offline

    teej107

    Well it would be nice if I knew what was wrong. Let me guess. Is it your unchecked casting? Returning false every time?
     
  12. Offline

    lubbs31

    When I type /911 [reason] in game, it just shows me the usage thing I put everytime...
     
  13. Offline

    teej107

    Return true
     
    cfil360 likes this.
  14. Offline

    lubbs31

  15. Offline

    cfil360

    @lubbs31 you should never return false if you handled any type of action. If you output an error message true. If the code worked correctly true.
     
  16. Offline

    lubbs31

    I changed all the returns to true, but it still just givings me the error message (There are no 'real errors' in to console)
    Code:
    public class Main extends JavaPlugin {
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("911")) {
                Player p = (Player) sender;
                p.sendMessage(ChatColor.BLACK + "[" + ChatColor.DARK_GREEN + "911" + ChatColor.BLACK + "]" + ChatColor.GREEN + " 9-1-1, What is your emergency?");
                p.sendMessage(ChatColor.BLACK + "[" + ChatColor.DARK_GREEN + "911" + ChatColor.BLACK + "]" + ChatColor.GREEN + " Usage: /911 [report]");
                return true;
                }
            if(args.length >= 1) {
                Player p = (Player) sender;
                String prefix = ChatColor.BLACK + "[" + ChatColor.DARK_GREEN + "911" + ChatColor.BLACK + "]";
                p.sendMessage(prefix + ChatColor.GREEN + "You're report has been sent!");
                p.sendMessage(prefix + ChatColor.GREEN + args[0]);
                p.playSound(p.getLocation(), Sound.CHEST_CLOSE, 2F, 1F);
                String message = StringUtils.join(args, ' ', 1, args.length);
                if (p.hasPermission("911.recieve")) {
                    p.sendMessage("Bug reported by " + sender.getName() + ": " + message);
                }
            }
            return true;
        }
    }
     
  17. @lubbs31 You end the command before you even do the arguments. Remove the closing bracket after the command check.
     
  18. Offline

    lubbs31

    Im now getting an "java.lang.ArrayIndexOutOfBoundsException: 1" whenever I do /911 (blah blah)
    Code
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("911")) {
                Player p = (Player) sender;
                p.sendMessage(ChatColor.BLACK + "[" + ChatColor.DARK_GREEN + "911" + ChatColor.BLACK + "]" + ChatColor.GREEN + " 9-1-1, What is your emergency?");
                p.sendMessage(ChatColor.BLACK + "[" + ChatColor.DARK_GREEN + "911" + ChatColor.BLACK + "]" + ChatColor.GREEN + " Usage: /911 [report]");
                if(args.length >= 1) {
                    String prefix = ChatColor.BLACK + "[" + ChatColor.DARK_GREEN + "911" + ChatColor.BLACK + "]";
                    p.sendMessage(prefix + ChatColor.GREEN + "You're report has been sent!");
                    p.sendMessage(prefix + ChatColor.GREEN + args[1]);
                    p.playSound(p.getLocation(), Sound.CHEST_CLOSE, 2F, 1F);
                    String message = StringUtils.join(args, ' ', 1, args.length);
                    if (p.hasPermission("911.recieve")) {
                        p.sendMessage("Bug reported by " + sender.getName() + ": " + message);
                    }
                }
                return true;
            }
            return false;
        }
    }   
     
  19. Offline

    hammy2899

    Code:
    String message = "";
    for(String s : args) message = message + " " + s;
    player.sendMessage(message.replaceFirst(" ", "");
    
     
  20. Offline

    PDKnight

    I'm using this. Try it :)
    Code:
    String sentence = "";
    for (int i = 0; i < args.length; i++)
      sentence += i == 0 ? args[i] : " " + args[i];
    Anyway, if you want to start with second sentence, this will help you :)
    Code:
    String sentence = "";
    int startAt = 2; // <-- second word
    for (int i = startAt-1; i < args.length; i++)
      sentence += i == startAt-1 ? args[i] : " " + args[i];
     
Thread Status:
Not open for further replies.

Share This Page