Iwin Posted October 11, 2016 Posted October 11, 2016 public boolean checkforPlayers(int area) { java.util.List<Player> playerss = players.getAll(); for (Player p : playerss) { if (myPlayer().getArea(area).contains(p) && !p.getName().equals(myPlayer().getName())) { return true; } } return false; } Returns true if there's players around your area. (excluding yourself) I use this generally like this: if(checkforPlayers(7) && random(0,25) == 0){ /* do world hop*/ } 1
Abuse Posted October 11, 2016 Posted October 11, 2016 (edited) Nitpicking, but performance-wise you could do this: EDIT: Do not use this, is 10x slower return (getPlayers().filter(n -> myPlayer().getArea(area).contains(n)).size() > 1); Edited October 11, 2016 by Abuse
Khaleesi Posted October 11, 2016 Posted October 11, 2016 Nitpicking, but performance-wise you could do this: return (getPlayers().filter(n -> myPlayer().getArea(area).contains(n)).size() > 1); Performance wise that's probably exact the same. It just looks more neat ^^ 2
Abuse Posted October 11, 2016 Posted October 11, 2016 (edited) Performance wise that's probably exact the same. It just looks more neat ^^ It's no longer comparing strings nor reserving memory for the entire list, thats a slight performance benefit Edited October 11, 2016 by Abuse
Khaleesi Posted October 11, 2016 Posted October 11, 2016 It's no longer comparing strings nor reserving memory for the entire list, thats a slight performance benefit It gets converted to the exact same java byte code when its compiled. The only benefit is that it looks more neat ^^ 1
Abuse Posted October 11, 2016 Posted October 11, 2016 It gets converted to the exact same java byte code when its compiled. The only benefit is that it looks more neat ^^ Nevermind, I take my suggestion back. After some testing it appears that filters are highly inefficient (300 ms vs 3000ms for 10000 loops) 1
venetox Posted October 11, 2016 Posted October 11, 2016 Nevermind, I take my suggestion back. After some testing it appears that filters are highly inefficient (300 ms vs 3000ms for 10000 loops) Do remember though, at most there can be 2000 players on and therefore can at most be 2000 loops for it. And worst case probably like ~600 anyway. It looks neat asf so worth. 2