How does your player collection look like?
|
|

Dear reader,
What is a better way instead of looping trough all the players in order to find an name.
Current way:
Would like to know a better way, thanks in advance.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;
Kind regards,
Patrick



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.
as you're using a list you shouldn't need the null checkCode:Optional<Player> optional = World.getWorld().getPlayers().stream().filter(x -> x.getName().equalsIgnoreCase(reset_task)).findFirst()

You could use a map instead, which would have constant time lookup (O(1)), as opposed to Olookup. 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.
| « #129 Raids Animations | Vencillio Enchanting » |
| Thread Information |
Users Browsing this ThreadThere are currently 1 users browsing this thread. (0 members and 1 guests) |