List several items on one line.

Discussion in 'Plugin Development' started by BizarreAlex, Jul 8, 2014.

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

    BizarreAlex

    I have this code I wrote that lists all items separately on one line. How would I make it so that multiple items would be listed on one line. For example: Kit1 Kit2 Kit 3 Kit4 instead of

    Kit1
    Kit2
    Kit3
    Kit4


    CODE:

    Code:java
    1. if (cmd.getLabel().equalsIgnoreCase("kits")) {
    2. Player p = (Player) sender;
    3. p.sendMessage("");
    4. p.sendMessage(ChatColor.GRAY + "----=" + ChatColor.DARK_AQUA + "" + ChatColor.BOLD + " Your Kits " + ChatColor.RESET + "" + ChatColor.GRAY + "=----");
    5. int kAmount = 0;
    6. for (KitType t : KitType.values()) {
    7. if (t != KitType.NONE) {
    8. if (p.hasPermission(t.getPerm())) {
    9. ++kAmount;
    10. p.sendMessage(ChatColor.GRAY + t.getName());
    11. }
    12.  
    13. }
    14. }
    15. }
    16. Player p = (Player) sender;
    17. p.sendMessage(ChatColor.GRAY + "----=" + ChatColor.DARK_AQUA + "" + ChatColor.BOLD + " Your Kits " + ChatColor.RESET + "" + ChatColor.GRAY + "=----");
    18. p.sendMessage("");
    19. return false;
    20. }
     
  2. Offline

    techboy291

    Use a StringBuilder and .append() to it in the for loop instead of sending the message for each kit. Then send a message to the player with the StringBuilder instance's toString() as the argument after the loop is finished.
     
  3. Offline

    BizarreAlex

    techboy291
    How?
     
  4. Offline

    Kazzababe

    BizarreAlex
    This isn't the full code, but to send the message, you could so something like this:
    Code:java
    1. StringBuilder sb = new StringBuilder();
    2. for(KitType kit : KitType.values()) {
    3. if(p.hasPermission(kit.getPerm())) {
    4. sb.append(ChatColor.GRAY + kit.getName()).append(" ");
    5. }
    6. }
    7. p.sendMessage(sb.toString().trim());
     
  5. Code:java
    1. String all = "";
    2. for (KitType t : KitType.values()) {
    3. if (t != KitType.NONE) {
    4. if (p.hasPermission(t.getPerm())) {
    5. ++kAmount;
    6. all+= t.getName() + ", ";
    7. }
    8. }
    9. p.sendMessage(ChatColor.GRAY + all);


    Kazzababe
    You are adding ChatColor.GRAY each time, it'd be nicer to add it once.

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

    BizarreAlex

    adventuretc Kazzababe I tried this:

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String tag, String[] args)
    2.  
    3.  
    4. {
    5. Player p = (Player) sender;
    6. int kAmount = 0;
    7. for (KitType t : KitType.values()) {
    8. if (t != KitType.NONE) {
    9. if (p.hasPermission(t.getPerm())) {
    10. ++kAmount;
    11. }
    12. }
    13. }
    14.  
    15. if (cmd.getLabel().equalsIgnoreCase("kits")) {
    16. p.sendMessage(ChatColor.GRAY + "----=" + ChatColor.DARK_AQUA + "" + ChatColor.BOLD + " Your Kits " + ChatColor.RESET + "" + ChatColor.GRAY + "=----");
    17. p.sendMessage(ChatColor.GRAY + "You own " + kAmount + " kits.");
    18. StringBuilder sb = new StringBuilder();
    19. for(KitType kit : KitType.values()) {
    20. if (kit != KitType.NONE) {
    21. if(p.hasPermission(kit.getPerm())) {
    22. sb.append(ChatColor.GRAY + kit.getName()).append(" ");
    23. }
    24. }
    25. p.sendMessage(sb.toString().trim());
    26. {
    27. {
    28. {
    29. }
    30.  
    31.  
    32. }
    33. }
    34.  
    35. }
    36. }
    37. p.sendMessage(ChatColor.GRAY + "----=" + ChatColor.DARK_AQUA + "" + ChatColor.BOLD + " Your Kits " + ChatColor.RESET + "" + ChatColor.GRAY + "=----");
    38. p.sendMessage("");
    39. return false;
    40. }
    41. }



    I end up getting something that looks like this though:

    You own 4 kits.
    Kit1
    Kit1
    Kit1 Kit2
    Kit1 Kit2 Kit3
    Kit1 Kit2 Kit3 Kit4


    Any help?
     
  7. Offline

    Kazzababe

    BizarreAlex
    You're sending the player the message inside the for loop. Put the line where you're sending the player the StringBuilder message outside of it.

    If he wants to change the color of each kit name for any certain reason, he's able to.
     
Thread Status:
Not open for further replies.

Share This Page