How do I create an object for every online player

Discussion in 'Plugin Development' started by qqifford, Nov 17, 2020.

  1. Offline


    I want to create a player object for every online player, how do I do this.
    For example, if there are 5 players online, I would be able to reference p1, p2, p3, p4, and p5 in my code.
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline


    @timtower got you covered, somthing like this, if you just need the player names you can replace the data type for the list from 'Player' to 'String' and instead of adding player 'p' you could add string p#getName() or somthing similiar.

    List<Player> playerList = new ArrayList<Player>();
    For (Player p : plugin.getServer().getOnlinePlayers()) {
    Last edited: Nov 18, 2020
  4. Offline


    it's bad practice to store the player variable, it is recommended to use UUIDs instead, as they do not change, unlike player names.

    What you are looking for is a HashMap, have key be the UUID and the value the Object
  5. Offline


    and if you do player.getUniqueID (or something like that() you can get their UUID to store in the HashMap.

Share This Page