Solved Give every Player an Int

Discussion in 'Spigot Plugin Development' started by MrWallah, Jun 4, 2017.

  1. Hi,

    How can I give an int for every online Player?

    Player 1: Int 1
    Player 2: Int 2
    Player 3: Int 3

    Thank You
  2. Please learn Java.
    You can do this by creating a map.
    Code (Text):
    Map<UUID, Integer> players = new HashMap<>();

    Bukkit.getOnlinePlayers().stream().forEach(p -> players.put(p.getUniqueId(), 1));
    This will associate all online players with the value of 1.
  3. Either use pvprealm's 1 liner Java 8 method, or you could do
    Code (Text):
    int counter = 0;
    for(Player p : Bukkit.getOnlinePlayers()( {
    p.sendMessage("You are assigned the integer " + counter);
    • Agree Agree x 1
  4. But this wont give every Player an own int.

  5. Then just make an int, increment it and apply it to every player in a loop.
  6. Yes it will? Or are you after an identifier? Like a uuid? A complete random variable?

    If so, something like this:

    Code (Text):
    for (Player p : Bukkit.getOnlinePlayers) {

        List<Integer> used = new ArrayList<>();
        //add all values in the map to the list
        int i = 1;
        while(used.contains(i)) {
         //add value to map
         //also no point in actually creating a list, just getValues and loop through that


    Sent from my iPhone using Tapatalk
  7. He didn't even read mine ;(
    • Agree Agree x 1
  8. Thanks!