Solved Splitting up some strings, and creating pages

Discussion in 'Plugin Development' started by AdobeGFX, Aug 14, 2015.

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

    AdobeGFX

    Code (open)
    Code:
    public String testWarnings(OfflinePlayer target, int page) {
            String result = "";
            int x = 0;
            int y = 5;
    
            for(String warnings : SettingsManager.getInstance().getData().getConfigurationSection("uuid." + target.getUniqueId().toString() + ".warnings").getKeys(false)) {
               
            if(x > 0) {
            continue;
            }
    
            if(y == 0) {
            break;
            }
    
            result += warnings + "\n";
            x--;
            y--;
    
            }
            return result;
        }

    in theory this should work.. And it do...
    It works, as long as x = 0, if i change it to 5, or 3 then it doesn't work..

    This is what you find in the config:
    data.yml (open)

    Code:
    uuid:
      62b5a723-05ef-4ff3-b47e-e1a679b4a0fe:
        points: 200
        warnings:
          '3326':
            points: 30
            reason: noob
            warner: 62b5a723-05ef-4ff3-b47e-e1a679b4a0fe
          '1472':
            points: 25
            reason: noob
            warner: 62b5a723-05ef-4ff3-b47e-e1a679b4a0fe
          '3935':
            points: 25
            reason: noob
            warner: 62b5a723-05ef-4ff3-b47e-e1a679b4a0fe
          '5893':
            points: 25
            reason: noob
            warner: 62b5a723-05ef-4ff3-b47e-e1a679b4a0fe
          '9321':
            points: 25
            reason: noob
            warner: 62b5a723-05ef-4ff3-b47e-e1a679b4a0fe
          '1224':
            points: 25
            reason: noob
            warner: 62b5a723-05ef-4ff3-b47e-e1a679b4a0fe
          '1421':
            points: 25
            reason: noob
            warner: 62b5a723-05ef-4ff3-b47e-e1a679b4a0fe
          '5065':
            points: 20
            reason: noob
            warner: 62b5a723-05ef-4ff3-b47e-e1a679b4a0fe
          '6757':
            points: 0
            reason: noob
            warner: 62b5a723-05ef-4ff3-b47e-e1a679b4a0fe
          '722':
            points: 0
            reason: nr 10
            warner: 62b5a723-05ef-4ff3-b47e-e1a679b4a0fe


    I'm looping through the warnings, and sending them (pos x to y : The first, to the second)
    I can change y to whatever i want, but still not x.. When i change x, it returns nothing..
    Any one who can see why that happens?
    Or who can share a different way to do this..
     
  2. the problem is probably
    Code:
     if(x > 0) {
            continue;
            }
    The continue; keyword says that it needs to skip everything below it in the for loop and let it go to the next item. This makes the line that decreases the x not able to run, and thus it keeps using the continue keyword. You should also decrease the x just before you use the continue keyword.
     
  3. Offline

    adam753

    I'm very confused about what your code is doing, but...
    Code:
    if(x > 0) {
        continue;
    }
    If x starts above 0, it just skips every time. Check your logic.
     
  4. Offline

    AdobeGFX

    i find it confusing as well.. I know it's the logic.. If i change to if (x > 3) { then the first page will return the 3 first lines of the "warnings", and the second page will return the whole "warnings" string..

    I tried to decrease x before continue..
    I almost works now!
    result (open)
    first page: 1warn 2warn 3warn
    second page: 4warn 5warn 6warn 7warn
    third page: 8warn 9warn

    on the second page, it returned 4 warnings, which it shouldn't, but i think thats something to do with
    Code:
    if (x > 0) {
    and on the third page, it returned 2 warnings, and skipped the last one.

    i had to increase x at the end..
    Code:
    public String testWarnings(OfflinePlayer target, int page) {
            String result = "";
            int x = 3 * (page - 1);
            int y = x + 3;
    
            for(Object warnings : SettingsManager.getInstance().getData().getConfigurationSection("uuid." + target.getUniqueId().toString() + ".warnings").getKeys(false).toArray()) {
               
            if(x > 0) {
                x--;
            continue;
            }
    
            if(y == 0) {
            break;
            }
    
            result += warnings + "\n";
            x++;
            y--;
    
            }
            return result;
        }
    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Aug 14, 2015
  5. Offline

    adam753

    I got about this far in your post before deciding what my response would be: if your own code confuses you, scrap it and rewrite. Consider what this function is supposed to do, and this time write it in a way that isn't so bizarre.
     
  6. Offline

    AdobeGFX

    Think of it like this: x and y are positions for the strings in the for loop. x(top) to y(bottom)..
    the confusing part is just why it returns the way it does..

    Page 1;
    x = 0
    y = 3
    returns the first 3 strings
    Page 4;
    x = 9
    y= 12
    returns the 3 strings after the 8 first strings

    I JUST FOUND OUT SOMETHING
    for each page, it adds 3 more...
    the first page it returns 3 strings..
    the nest page it returns the 3 next strings.. + 3 more!

    Solved!
    i just needed to decrease y before continue on x > 0..

    Working code:
    Code:
    public String testWarnings(OfflinePlayer target, int page) {
            String result = "";
                    // 3 = how many lines per page
            int x = 3 * (page - 1);
            int y = x + 3;
            for(String warnings : SettingsManager.getInstance().getData().getConfigurationSection("uuid." + target.getUniqueId().toString() + ".warnings").getKeys(false)) {
               
            if(x > 0) {
                x--;
                y--;
            continue;
            }
    
            if(y == 0) {
            break;
            }
            result += warnings + "\n";
            x--;
            y--;
    
            }
            return result;
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
Thread Status:
Not open for further replies.

Share This Page