How do I create an object for every online player

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

Thread Status:
Not open for further replies.
  1. Offline

    qqifford

    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. Online

    timtower Administrator Administrator Moderator

  3. Offline

    jonthe445

    @qqifford
    @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.

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

    Chr0mosom3

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

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

    Dai_Kunai

    and if you do player.getUniqueID (or something like that() you can get their UUID to store in the HashMap.
     
Thread Status:
Not open for further replies.

Share This Page