Accounting players?

Discussion in 'Plugin Development' started by tuskiomi, May 23, 2013.

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

    tuskiomi

    Hello all, i have made a tiny, effecient method for accounting an array of players.
    basicaly you feed it 3 things: a counter (private int that starts at Zero), an array of players (For me its 30, but you can make it as big as you like), and a player to search for (through parameters).
    The code is :
    Code:
        private int getIndex(Player p) {
            if (!(Pcount == 0)) {//Pcount is the counter varible
                for (int x = 0; x < Pcount; x++) {
                    if (p == Plist[x]) {//if The name is found return with the number. don't increase.
                        //another note Plist is the array (Just makin sure you took note of that :P)
                        return x;
                    }
                }//if not found in for loop then make new record
                Pcount++;
                return Pcount - 1;
            } else { //if pcount == 0 then bump it 1 and return 0
                Pcount++;
                return Pcount - 1;
            }
        }
    the problem is
    unless it's the first record it just increases by 1
    it never finds a player even if is the same one.
    how would I fix this to make the method find a player correctly? I have already tried usernames and that doesn't work.
     
  2. Offline

    Minnymin3

    Do for loops stop when you return a value? Try adding break; after returning x.
     
  3. Offline

    CubixCoders

    Minnymin3 is right, if you do return in it, it will stop the for loop and wont continue
     
  4. Offline

    Tirelessly

    Use .equals to compare objects.

    !(x == y) is better written as x != y
     
  5. Offline

    tuskiomi

    It gives me the warning "unreachable code"
     
  6. Offline

    Minnymin3

    Ok then ya returns stops the loop.
     
  7. Offline

    tuskiomi

    Okay, anybody else want to take a stab at it?

    Anybody?

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

Share This Page