send List<String> to players

Discussion in 'Plugin Development' started by G4meM0ment, Aug 23, 2012.

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

    G4meM0ment

    Hi,
    I've got a list of Strings I wanna send the player.
    It should be formatted like this:
    --- Header ---
    1. Message
    2. Message
    3. Message

    I wanna array these messages after some numbers, can you tell me how to array it? (Got numbers which are greater than others)

    But the main problem is I dont know how to send it to the player. My beginning of an tryal:

    Code:java
    1.  
    2. if(args.length > 0 && args[0].equalsIgnoreCase("list")) {
    3. List<String> bountys = bounty.getAllBountys();
    4. int size = bountys.size();
    5. player.sendMessage("--------> *** BOUNTYS *** <--------");
    6. player.sendMessage("More information: /bounty info <name>");
    7. while(size > 0) {
    8.  
    9. }
    10. }
    11.  


    Thanks for your help.

    ~Julian
     
  2. Offline

    zack6849

    player.sendMessage(bountys);
    just a guess?
    or a for loop of some sort to send them line by line
     
  3. Offline

    sternmin8or

    it would be:
    Code:
    for(int i=0;i<size;i++){
    player.sendMessage((i+1)+". "+bountys.get(i))
    }
     
    zack6849 likes this.
  4. Offline

    Sabersamus

    Code:java
    1.  
    2. String[] stringArray = (String[])bountys.toArray();
    3. player.sendMessage(stringArray);
    4.  


    Player has to sendMessage 's one takes a String for a parameter, the other takes a String[]
     
  5. Offline

    Icyene

    Or better, in my opinion:

    Code:
    int i = 0;
    for(String message: bountys) {
    ++i;
    player.sendMessage(i + ". " + message);
    }
    
     
    sternmin8or likes this.
  6. Offline

    sternmin8or

    He needed the numbers.
    I like doing it that way too, but he had allready set it up for the other way :p
     
  7. Offline

    Sabersamus

    Yeah i realized that after i posted it
     
  8. Offline

    sternmin8or

    I still love you:D
     
  9. Offline

    G4meM0ment

    I did something similar before, but can to tell me how to sort numbers with the greatest on top?
    Ive got a method getting all variables, but I've got one variable in it which is the "sorting"-variable.
     
  10. Offline

    Sabersamus

    You want the greatest number on top?

    Code:java
    1. for(int i=bountys.size(); i>=0;--i;){
    2. player.sendMessage(i + ". " + bountys.get(i);
    3. }
     
  11. Offline

    G4meM0ment

    No its other wise, I know now how to send the messages but I got a bunch of numbers (can be 5 or 1000) and I want to sort them.
     
  12. Offline

    sternmin8or

    Where are the numbers.
     
  13. Offline

    G4meM0ment

    These are the numbers:

    Code:java
    1.  
    2. @SuppressWarnings("null")
    3. public List<String> getAllBountys() {
    4. Player[] players = plugin.getServer().getOnlinePlayers();
    5. List<String> bountys = null;
    6. int i = 1;
    7. for(Player player : players) {
    8. if(existsBounty(player)) {
    9. String money = ""+getCustomConfig().getInt("bountys." + player.getName() + ".amount");
    10. String type = getCustomConfig().getString("bountys." + player.getName() + ".type");
    11. bountys.add(i + ": " + player.getName() + " | " + type + " | " + money);
    12. i++;
    13. }
    14. }
    15. return bountys;
    16. }
    17.  


    The number I wanna use is the amount (money as String)
     
  14. Offline

    sternmin8or

    remove your int i = 1 line and replace the inside of that if statement with this

    Code:
    int money = getCustonConfig...etc
    int i = 1;
    if(bountys.size()!=0){
      for(String s:bountys){
        if s.split("|")[2]<=money{
          bountys.add(player.getName()+ " | " + type + " | " + money,i)
          break;
        }
        i++
      }
    }
    else{bountys.add(player.getName() + " | " + type + " | " + money)
    }
     
  15. Offline

    G4meM0ment

    Will that sort the numbers by Size, with the biggest on top.
     
  16. Offline

    sternmin8or

    It wont "sort" any array, but it will ensure that your strings are sorted on the way in.
     
  17. Offline

    G4meM0ment

    I dont wanna sort any arrays, I just wanna sort these entries in the List by Size.
    SO what is this

    bountys.add(player.getName()+ " | " + type + " | " + money,i)

    so whats that money,i good for?
     
  18. Offline

    sternmin8or

    that satement adds the string player.getName()+ " | " + type + " | " + money
    to the list bountys
    at the index i
    meaning that as it iterates down the list, its checking to see where money belongs, and then inserting it there
     
  19. Offline

    G4meM0ment

    and how to read it out in the right direction then?
     
  20. Offline

    sternmin8or

    It is allready in order, this should read it out and add numbers
    for(int i=0;i<bountys.size;i++){
    player.sendMessage((i+1)+". "+bountys.get(i))
    }
     
  21. Offline

    G4meM0ment

    This don't works it's string and int, did you made a mistake or are changes needed?
    And can you also explain me, what this split and stuff does?
     
  22. Offline

    Sabersamus

    String.split(String); creates an Array from the string, with the ParamString being the separator

    an example:
    String string = "Hello how are you?";
    String[] split = string.split(" ");
    its the equivalent of String[] split = {"Hello", "how", "are", "you?"};
     
  23. Offline

    sternmin8or

    lol it doesnt work because I forgot to put it in parenthesis. and i forgot to Integer.parseInt it. It should look like:
    if (Integer.parseInt(s.split("|")[2])<=money)

    Saber is correct, but this split in particular is meant to get the money value of the current string it is iterating over. Because the string is saved in the format:player.getName() + " | " + type + " | " + money
    splitting it around "|" returns a list of [player.getName(),type,money]. Since the only one I am interested in is the value of money, that is the one I use. Now, I cant compare that string to a int unless I parse it first, which I forgot to do lol
     
  24. Offline

    G4meM0ment

    Ok should work, do you know how to check if a number of an array exists? For example in a onCommand with args[1] how to check if that arg exists?

    Another problem is that bountys is null when its default, but that throws an npe when it trys to check: if(bountys.size() != null)
     
  25. Offline

    sternmin8or

    to check if an arg exists simply use if (args.length >= whatever number arg you are trying to check).
    Instead of initializing bountys as null, intialize it ias
    List<String> bountys = new List<String>();
     
  26. Offline

    G4meM0ment

    That dont works too it says: Can not instaniate the type List<String>
     
  27. Offline

    Sabersamus

    List<String> bountys = new ArrayList<String>();

    ArrayList implements List

    Also, you cant check if(bountys.size() != null){ because size() returns an integer, which can never be null, but it can return 0.

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

    travja

    What I would do is have:
    Code:java
    1. int i = 0;
    2. while(bountys.size()<= i){
    3. player.sendMessage(bountys.get(i));
    4. i = i+1;
    5. }


    That would go through all of the bountys and will stop when it gets above the size of the bountys.... When the command is run you can set i back to 0 so it runs properly.

    You may need to see if bountys.isEmpty() to avoid possible errors.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
Thread Status:
Not open for further replies.

Share This Page