Thread: Better way of fetching player names

Results 1 to 5 of 5
  1. #1 Better way of fetching player names 
    Banned

    Join Date
    Oct 2012
    Posts
    4,716
    Thanks given
    1,682
    Thanks received
    1,105
    Rep Power
    0
    Dear reader,

    What is a better way instead of looping trough all the players in order to find an name.

    Current way:

    Code:
    case "resettask":
        		String reset_task = cmd[1];
                for (int i = 0; i < World.getWorld().getPlayers().capacity(); i++) {
                    if (World.getWorld().getPlayers().get(i) != null) {
                        if (World.getWorld().getPlayers().get(i).getName().equalsIgnoreCase(reset_task)) {
                            Player player_to_reset = World.getWorld().getPlayers().get(i);
                            player_to_reset.taskAmount = 0;
                            player_to_reset.slayerTask = 0;
                            player_to_reset.write(new SendMessagePacket("Your slayer task has been reset, please get another one."));
                            break;
                        }
                    }
                }
        		return true;
    Would like to know a better way, thanks in advance.

    Kind regards,

    Patrick
    Reply With Quote  
     

  2. #2  
    Registered Member
    Stanaveli's Avatar
    Join Date
    Aug 2014
    Posts
    1,489
    Thanks given
    179
    Thanks received
    651
    Rep Power
    1311
    How does your player collection look like?
    ~No honour among thieves.



    [Only registered and activated users can see links. ]

    Reply With Quote  
     

  3. Thankful user:


  4. #3  
    Contributor
    Kris's Avatar
    Join Date
    Jun 2016
    Age
    23
    Posts
    3,536
    Thanks given
    703
    Thanks received
    2,322
    Discord
    View profile
    Rep Power
    5000
    How else do you imagine doing this? I mean you certainly could write yourself a method to keep the code cleaner rather than writing this loop in tens of places wherever it's necessary but I can't imagine finding the certain player in any other way. I mean there are certainly different possible ways to do this but none of them would be any more efficient than a simple loop. Not to mention I cannot imagine you using this command very often since it's player-targeted. Also I cannot imagine looping a few hundred (I highly doubt any higher amount) entries is very memory consuming or whatever, it'll be done in a split second.
    Reply With Quote  
     

  5. #4  
    Respected Member

    Join Date
    Jan 2009
    Posts
    5,682
    Thanks given
    1,093
    Thanks received
    3,494
    Discord
    View profile
    Rep Power
    5000
    Code:
    Optional<Player> optional = World.getWorld().getPlayers().stream().filter(x -> x.getName().equalsIgnoreCase(reset_task)).findFirst()
    as you're using a list you shouldn't need the null check
    Reply With Quote  
     

  6. Thankful user:


  7. #5  
    Registered Member
    hc747's Avatar
    Join Date
    Dec 2013
    Age
    23
    Posts
    1,448
    Thanks given
    3,133
    Thanks received
    672
    Discord
    View profile
    Rep Power
    1029
    You could use a map instead, which would have constant time lookup (O(1)), as opposed to O lookup. You would want to do something like HashMap<String, Player> to properly utilise this - although the code is cleaner, speed wise you'll notice it more so with larger player counts.
    [Only registered and activated users can see links. ]
    [Only registered and activated users can see links. ]
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 19
    Last Post: 08-18-2012, 09:54 PM
  2. Replies: 25
    Last Post: 05-09-2011, 08:06 PM
  3. Better way of doing player ranks
    By tj007razor in forum Snippets
    Replies: 10
    Last Post: 06-02-2010, 06:27 AM
  4. Better way of loading stackable items
    By Sean in forum Tutorials
    Replies: 19
    Last Post: 11-02-2008, 12:54 PM
  5. Better way of blocking names with bad symbols
    By Graham in forum Tutorials
    Replies: 14
    Last Post: 10-10-2008, 07:16 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •