Solved Question: List Pages

Discussion in 'Plugin Development' started by hanahouhanah, Nov 3, 2013.

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

    hanahouhanah

    The title is a bit weird, sorry, but I don't know how to sum it up in a title (but I tried because I had to).
    I have a log of many things in separate yamls, and eventually, the list will be really long (it could get long). Due to this and Minecraft limiting how much you can see in chat, it won't work out if I have each string listed out.
    So plugins like logblock, when you get a log and there's a lot of logs, it'll separate the logs into pages, maybe 10 lines per page. It would look something like:

    Logs:
    Pages 1 of ___
    First line
    Second line
    Third line
    etc...
    Use /page # to navigate through pages.

    How would I do something like this (also, how would I separate the string into separate lines)?
    Thanks.
     
  2. Offline

    Chinwe

    Have a look at this resource :)
     
  3. Offline

    hanahouhanah

    Chinwe
    Thanks!
    How would I get this to work with a list, though? With this I can make a page of one string, so how would I put "many strings" on there?

    So I got it to work... Sorta. Now my list shows up like this:
    [​IMG]
    instead of List: Page (1 of 1)
    - asdf
    - asdf

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

    Chinwe

  5. Offline

    hanahouhanah

    Chinwe
    I'm straight coyping that method.
    Code:java
    1. public void paginate(CommandSender sender, SortedMap<Integer, String> map,
    2. int page, int pageLength) {
    3. sender.sendMessage(ChatColor.YELLOW + "List: Page (" + String.valueOf(page) + " of " + (((map.size() % pageLength) == 0) ? map.size() / pageLength : (map.size() / pageLength) + 1) + ")");
    4. int i = 0, k = 0;
    5. page--;
    6. for (final Entry<Integer, String> e : map.entrySet()) {
    7. k++;
    8. if ((((page * pageLength) + i + 1) == k) && (k != ((page * pageLength) + pageLength + 1))) {
    9. i++;
    10. sender.sendMessage(ChatColor.YELLOW + " - " + e.getValue());
    11. }
    12. }
    13. }


    Then to actually use this, I'm doing this:
    Code:java
    1. for(String s : ad){
    2. SortedMap<Integer, String> map = new TreeMap<Integer, String>();
    3. map.put(1, s);
    4. paginate(sender, map, 1, 10);
    5. }
     
  6. Offline

    Chinwe

    You're making a whole new map for each value in the list, do something like this instead:
    Code:
    int i = 0; // counter
    SortedMap<bleh> map = blah blah; // new TreeMap
    for (String s :ad)
      map.put(i++, s); // add each one once
    paginate(sender, map, 1, 10); // then paginate once, with everything in the list in one map
    
     
  7. Offline

    hanahouhanah

    Chinwe
    Still the same error. Also, how would I change pages then load the things that would only be for that page?

    bump

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

    Coelho

    Code:
    String[] lines = ...
    int page = ...
    int perLine = 10;
    int fromLine = (page - 1) * perLine;
    int toLine = (fromLine + perLine > lines.length - 1) ? pages.length : (fromLine + perLine);
    for(int i = fromLine; i < toLine; i++) {
      System.out.println(line[i]);
    }
    
    I believe that is right.
     
  9. Offline

    hanahouhanah

    Coelho
    Hmm.. Can you explain how this works?
    Thanks for contributing by the way.

    --Edit--
    Fixed.

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

Share This Page