Plugin Help Need Help with Array List

Discussion in 'Plugin Help/Development/Requests' started by BestMcPlayers, Apr 25, 2015.

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

    BestMcPlayers

    I'm using an Array list to save files with Minecraft usernames and UUIDs. When I save an username to a file the name shows up as [CraftPlayer{name=USERNAME}].

    The thing is I can't use the method player.getName();

    Good code:
    Code:
      final static ArrayList<Player> killAdmins= new ArrayList<Player>()
    
      public static void addKillAdmin(Player player)
      {
           killAdmins.add(player);
           save();
      }

    When I try to use the code below I get an error
    Code:
        public static void addKillAdmin(Player player)
        {
            String getName = player.getName();
          
                killAdmins.add(getName);
                save();
        }
        
    How would I convert this?
     
  2. @BestMcPlayers The first problem is that you're storing players in your arraylist. Using players can cause massive memory leaks, so the best thing to do is store the player's UUID
    Code:
    (killAdmins.add(player.getUniqueId());
    and then to retrieve the player again, you'll use
    Code:
    Bukkit.getPlayer(UUID);
    The reason you can't use player.getName() is because your ArrayList<Player> cannot accept Strings, only Player objects.
     
  3. Offline

    BestMcPlayers

    @DJSkepter

    The thing is I want to use both usernames and UUIDs. I'm storing UUIDs in another file. I can't convert string to player?
     
    Last edited: Apr 30, 2015
  4. @BestMcPlayers You convert UUID to player, (Bukkit.getPlayer(UUID), then you can use Player.getName()
     
    BestMcPlayers likes this.
  5. Offline

    BestMcPlayers

    @DJSkepter

    Thanks works like a charm. You wouldn't know how to save an Array list under strings would you? For example:

    Admins
    - ArrayList
    - ArrayList
    Admins2
    - ArrayList2
    - ArrayList2
    - ArrayList2
     
  6. @BestMcPlayers Why try to store multiple arraylists under an admin when they'd both contain similar data? Just have one arraylist for each group:

    Code:
    Admins
      - ArrayList
    Admins2
      - ArrayList
    With a FileConfiguration, you can use the set() method to set the value and getStringList() to retrieve it.
     
Thread Status:
Not open for further replies.

Share This Page