get number of players in arena?

Discussion in 'Plugin Development' started by thewalkingplay, Jun 30, 2015.

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

    thewalkingplay

    Hi , I'm making a minigame plugin and I need to check the number of players in an arena, the players are in a HashMap:
    Code:
    static HashMap<String, String> playersInArena = new HashMap<String, String>();
    the sequence of strings is Player, Arena

    to get he number i has used:
    Code:
    public static int getPlayersInArena(String arena) {
            int n = 0;
            for (Player p : Bukkit.getOnlinePlayers()) {
                if (playersInArena.get(p.getName()) == arena) {
                    n++;
                }
            }
            return n;
        }
    But, if are 2,3,4,5 players in the arena it aways returns 1.
    Sorry for my english, I'm brazilian
    can anyone help me?
     
  2. @thewalkingplay

    Simple. Get the amount of hashmaps there is.

    playersInArena.keySet().size();

    this gets how many keys(the first value of the hashmap) there is, which is essentially how many entries there are in a hashmap.
     
  3. Offline

    567legodude

    @HeyAwesomePeople That wouldn't work because that just tells how many total players are in an arena, they want to be able to find the number of players in a specific arena.

    @thewalkingplay You want to loop through the map, and every time the arena matches, you add one to the count.
    Example (open)
    Code:
    public int numPlayers(String arena) {
    // Don't use static because you need an instance of the class for this.
        int n = 0;
        for (String s : playersInArena.keySet()) {
            if (playersInArena.get(s).equals(arena)) {
                n++;
            }
        }
        return n;
    }
     
  4. Offline

    terturl890

    The easiest thing to do would probaby make Arena it's own instance. So you would do:

    new Arena("name")

    So that your ArrayList would be List<Arena> arenas = new ArrayList<Arena>();

    So that you would be able to get certain arenas and get the players in that arena with an arraylist inside the arena class:

    List<String> players = new ArrayList<String>();

    So you could do:

    Code:
    public void getPlayersInArena(String name) {
    
        Arena a = new Arena(name); (Probably should make away to get arenas without having to create a new one)
    
        for(String s : a.players) {
    
            Player p = Bukkit.getPlayer(s);
    
            //do stuff
    
        }
    
    }
     
    Last edited: Jun 30, 2015
  5. Offline

    567legodude

    @terturl890 You can't do "for (String s : playersInArena) {" because playersInArena is not a string list, it's a map.
    And we don't need to get the player because we only need to check the things that are in the map.
     
  6. Offline

    terturl890

  7. Offline

    thewalkingplay

    now it's saying that cannot make reference for a non-static method. But the class that calls the method isn't static
    @567legodude
     
  8. Offline

    mythbusterma

    @thewalkingplay

    That's more of a Java question, not really related to Bukkit. May I recommend: The Java™ Tutorials

    Also, you should really make a class to represent your Arenas, it makes much more sense.
     
  9. Offline

    567legodude

    @thewalkingplay @mythbusterma is right. For one plugin that I made where players can fight each other, each fight is a Duel object. With methods to get info about it. When someone requests to duel, a Request object is created with the request info, if they accept, the request object is sent to the ArenaManager, where it takes their request and puts them into an arena with the correct settings.
     
Thread Status:
Not open for further replies.

Share This Page