Wheres the getAllPlayers() method?

Discussion in 'Plugin Development' started by Hotshot, Oct 17, 2011.

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

    Hotshot

    Im making a plugin that pays players according to their group, but there us no built in function that gives all the players. (both online and offline) ....So, how do it do it?
    Thank you.
     
  2. Hm, you could make your plugin use the data folder of the server, which contains all players. You could then strip off the .dat ending and voilĂ , have a list of all players that ever played on the server (and were not deleted).
     
  3. Offline

    Hotshot

    Could you be so kind as to post some code on how I would do that...? Sorry, I'm still a newbie to plugins, this is actually my first one.
     
  4. Code:java
    1. private static HashSet<OfflinePlayer> getAllExistingPlayers() {
    2. final List<World> worlds = Bukkit.getServer().getWorlds();
    3. final HashSet<OfflinePlayer> allPlayers = new HashSet<OfflinePlayer>();
    4.  
    5. for (World w : worlds) {
    6. final File f = new File(w.getName() + File.separator + "players" + File.separator);
    7.  
    8. for (File playerFile : f.listFiles()) {
    9. allPlayers.add(Bukkit.getServer().getOfflinePlayer(playerFile.getName().substring(0, playerFile.getName().length() - 4)));
    10. }
    11. }
    12.  
    13. return allPlayers;
    14. }
     
  5. Make sure you use all worlds.
    looks good :)
     
  6. Offline

    TMAsantos

    how can I make my code look like that? xD I only have HTML and 2 more options :S
     
  7. Code:
    [syntax=java]Blub[/syntax]
     
    thehutch likes this.
  8. Offline

    TMAsantos

    ***** Thx :)
     
  9. Offline

    Hotshot

    Is it possible to get all the players into an array so i can easily loop through them with a for loop?
     
  10. Sure, but you can do that with a Set, List, or anything like that
    Code:java
    1.  
    2. for (Player p : Bukkit.getOnlinePlayers()) {
    3. if (p.getName().equalsIgnoreCase("tips48")) {
    4. System.out.println("Cool player found!");
    5. } else {
    6. System.out.println("Eww, its " + p.getName());
    7. }
    8. }
    9.  
     
    Walker Crouse and Hotshot like this.
  11. Offline

    Hotshot

    So...
    Code:
    [syntax=java]
    for(Player p : getAllExistingPlayers)
    {
        if(p.hasPermission("group.member")
        {
             code to pay user according to group
        }
        else if(p.hasPermission("group.admin")
        {
             code to pay user according to group
        }
    }
    [/syntax]
    Something that that would work? I put nodes in each of the groups so i could find out whose in which group according to nodes...
     
  12. I would check admin before member, but yes. it should work :)
     
  13. Offline

    Hotshot

    Oh, because of inheritance?
     
  14. Yep
     
  15. Offline

    Hotshot

    -__- its telling me to change type of p to OfflinePlayer...
     
  16. Your using the wrong method then :)
    Use Server.getOnlinePlayers()
     
  17. Offline

    Hotshot

    in place of getAllExsistingPlayers()?

    Heres a pic on the segment of my code:
    [​IMG]
    Inserting a pic doesnt seem to be working so, http://www.freeimagehosting.net/9073f
     
  18. Yes
     
  19. Offline

    Hotshot

    But I need to get all players, not just online ones.
     
  20. Oh?
    Then do what you had and change to OfflinePlayer
     
  21. Offline

    Hotshot

    How would I loop through the output of that and check each player for a certain permission?
     
Thread Status:
Not open for further replies.

Share This Page