What's the best way to send a message with the number of inputted args?

Discussion in 'Plugin Development' started by SkaterSam200, Apr 21, 2014.

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

    SkaterSam200

    So here's the problem, I'm creating a report plugin and I'm not sure how to broadcast it with the correct number of args (args being the description of what a user is doing) without doing a for loop to broadcast the message with each new arg which of course would be spammy.

    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    3.  
    4. Player reporter = (Player) sender;
    5. Location reporterLocation = reporter.getLocation();
    6. int reporterX = (int)reporterLocation.getX();
    7. int reporterY = (int)reporterLocation.getY();
    8. int reporterZ = (int)reporterLocation.getZ();
    9.  
    10. Bukkit.broadcastMessage(ChatColor.RED + "[REPORT] " + reporter.getName() + " Reported " + args[0] + " at location " + reporterX + " " + reporterY + " " + reporterZ + ". For reason: " + args[1]);
    11. return false;
    12. }


    I know it's possible to easily broadcast the message no matter how many, or how few arguments there are, I just need to find it.

    EDIT: Also if there's a way to do it that's sending players a message that's fine as well because I'm going to be using permissions and messages later so everyone doesn't see it.
     
  2. Offline

    Weasel_Squeezer

    Try this. It will format an array of strings (you can easily convert it to accept a Collection) and append the elements together to a single string with a sentence like structure. so with 'and' and commas. Also with color highlighting the arguments.
    I have not tested this, but it should work.
    Code:java
    1. public String formatArgs(String[] args) {
    2. String result = "";
    3. if (args.length > 0) {
    4. int i = 0;
    5. for (String arg : args) {
    6. String val = ChatColor.DARK_RED + arg;
    7. result += (i++ != 0 ? ChatColor.RED + (i == args.length ? " and " : ", ") + val : val);
    8. }
    9. } else {
    10. result = ChatColor.RED + "none";
    11. }
    12.  
    13. return result;
    14. }
     
  3. Offline

    Rocoty

  4. Offline

    Weasel_Squeezer

    No.
    Considering that the arguments SkaterSam200 is printing to the players is less than 10 (even 100), using StringBuilder would most definitely be micro optimization which is pointless. The difference in process time would be mere nanoseconds.
    As the co-founder of Stack-Exchange Jeff Atwood said: "Arguments about which method results in code that is easier to read and easier to maintain will be gladly entertained. Arguments about speed will not. Stop micro-optimizing and start macro-optimizing"
     
  5. Offline

    RainoBoy97

    String report = StringUtils.join(args, " ", 1, args.length);
    Should do the trick.
     
  6. or this :
    Code:java
    1. for (int i = 1; i < args.length; i++) {
    2. msg += args[I] + " ";[/I]
    3. [I] }[/I]
     
  7. Offline

    Rocoty

Thread Status:
Not open for further replies.

Share This Page