Want to code a plugin that tps you to a player under y20

Discussion in 'Spigot Plugin Development' started by SpaceBob, May 12, 2016.

  1. Hey!

    I need to code a plugin where when you do /tpl if teleports you to a random player under y20. If someone could help that would be much appreciated!
  2. Should be fairly straight forward. Iterate through the online players, adding their UUID to a list if their Y is under 20, then select a random player from that list by generating a random number between 0 and the length of the list - 1.
    • Agree Agree x 2
  3. Create a new random getting all online players under y20 , get the location and tp to him
  4. Code (Java):
    private List<Player> underTwenty = Bukkit.getOnlinePlayers().stream().filter(p -> p.getLocation().getY() <= 20).collect(Collectors.toList());
    Player chosen = underTwenty.get(ThreadLocalRandom.current().nextInt(underTwenty.size()));
    Would require Java 8 ;)
    • Like Like x 3
    • Winner Winner x 2
  5. Gota love those new lambda and stream features :D ("new")
  6. Good. Stahp using 7.
  7. Aaand ThreadLocalRandom.
    I'm being kinda annoyed on those who doesn't use it xD
    Why make a new when one is supplied to you at command?

    Java 7 was outdated a year or two ago, soo xD
  8. How does streaming and stuff work like that..?
  9. Thanks for backing me up...?
    • Agree Agree x 1