Player Arrays?/Add Teams to plugin.

Discussion in 'Plugin Development' started by chenr1, Jun 16, 2012.

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

    chenr1

    Hey guys i'm trying to make to teams. I figured the best way to do teams was to put the players names into an array. But I couldnt seem to figure out how to do that. Please give me a simple/effecient way to put all the players in an array. OR give an alternative to make a team. Keeping in mind I will have to teleport all the players on one team to a specefic location. THANKS
     
  2. I recommend that you use an Set for team names:
    1 object/name cannot be more than 1 time inside the set
    size is dynamic, dont need to create an new array is the old is full
     
  3. Offline

    chenr1

    Cool!! But how do I get the players in there?
    Also thanks for your response.

    Can anyone help me with the Sets? I sor of understand it but dont understand how to implement it into bukkit to add players to it.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  4. Offline

    FTWin01Gurl

    For this, I recommend using ArrayLists because having a HashMap would get a little confusing, but separating teams into their own respective ArrayList will make it easier to detect which player is on which team. For that, you would look at the API from the Javadocs below.

    http://docs.oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html

    Also, I discourage Player instances because they do memory leaks. I'd suggest you to replace Player with String, that gets the name of the player (not display, as it could be changed anytime).
     
  5. Offline

    RobotA69

  6. Offline

    chenr1

    Thank you BOTH!! In on my phone now but as soon as I get to a pc I will try this
     
  7. I belive the best way would be a HashMap with a Set value.

    This would work splendidly:
    Code:
    HashMap<String, Set<String>> teams = new HashMap<String, Set<String>>();
    If you need extra stuff stored in team, you can make a custom class that'll hold the Set<String> and your other stuff, then just use that instead of the Set in the hashmap :)

    Be sure to add the a team as:
    Code:
    teams.put("Team name", new HashSet<String>());
    And then you can just:
    Code:
    teams.get("Team name").add(player.getName());
    
    // ...
    
    if(teams.get("Team name").contains(player.getName()))
    {
        // ...
    }
    
    //...
    
    teams.get("Team name").remove(player.getName());
    But don't hardcode team names in codes, use variables ofc!
     
  8. Offline

    Everdras

    Why would you use a HashMap to store only two values, especially when the OP will only ever have 2 teams?

    Just have two seperate HashMap<String, Boolean>.

    Code:
    HashMap<String, Boolean> team = new Hashmap<>();
     
    //To add a player to a team:
    team.put(playerName, true);
     
    //To remove:
    team.remove(playerName);
     
    //To check if a player is on the team:
    Boolean result = team.get(playerName);
    return result == null ? false : result;
     
    //To get all players on the team:
    return team.keySet();
     
  9. Seriously, you're discarding my hashmap for a BOOLEAN hashmap ? That's one of the most inefficient types of hashmap values you can use.
    But no I didn't knew he only needed 2 teams, my code was for an undefined number of teams....

    chenr1
    But if you only need 2 teams, you can use 2 List variables:
    Code:
    List<String> team1 = new ArrayList<String>();
    List<String> team2 = new ArrayList<String>();
    
    // code from hashmap applies, but ofc without the hashmap...
    
    team1.add(player.getName());
    
    // ...
    
    if(team1.contains(player.getName()))
    {
        // ...
    }
    
    //...
    
    team1.remove(player.getName());
    
    // teleporting is easy:
    
    Player player;
    for(String name : team1)
    {
        player = Bukkit.getPlayerExact(name);
    
        if(player != null)
            player.telerport(whererver);
    }
    EDIT: Changed from Set to List because I noticed you want to iterate through all of the team members for teleporting.
     
  10. Offline

    chenr1

    Digi
    Everdras
    THANKS FOR BOTH OF OUR INPUT!!

    But I have a problem I get an error at player.getname So i added this: Player player = event.getPlayer();
    At the top. But theni get an error on event. Heres my code
    Code:
    Player player = event.getPlayer();
                       
                        ArrayList<String> team1 = new ArrayList<String>();
                        ArrayList<String> team2 = new ArrayList<String>();
     
                        // code from hashmap applies, but ofc without the hashmap...
     
                        team1.add(player.getName());
     
                        // ...
     
                        if(team1.contains(player.getName()))
                        {
                            // ...
                        }
     
                        //...
     
                        team1.remove(player.getName());
     
                        // teleporting is easy:
     
                        Player player;
                        for(String name : team1)
                        {
                            player = Bukkit.getPlayerExact(name);
     
                            if(player != null)
                                player.telerport(whererver);
                        }
     
  11. Offline

    ItsHarry

    It would help if you say what error.
    Also, using an ArrayList is the best approach, good job =)
     
  12. Offline

    chenr1

    I get an error under Player player = event.getPlayer();
    event the error says "event cannot be resolved."
     
  13. Offline

    Xemos

    I see 2 ways of doing this:

    Method 1: You can do a hashmap where the key is the team captain's name then you can add all player's names as a string to it. then whenever you need to do something with the team, reverse the flow use the team captain's name to pull a player's name out and can convert the string of the name into a player object if needed.

    Method 2: Can use a bukkit config file to store the names in an organized structure, easy to put data in, easy to pull data out.


    If you like either of those I could elaborate a lot on the methods as I use both in 2 different plugins.
     
  14. Offline

    ItsHarry

    Dude he already has the solution to that... Using Arraylists..

    Show your entire source code, it can't find the variable event.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  15. Offline

    chenr1

    Wait what do you mean my entire code.
     
  16. Your entire class... but I noticed you just copy pasted my examples without reading them... those are not meant to be used all at once and you should not create the team arrays inside the event...
     
  17. Offline

    chenr1

    Ok i fixed it I moved the decleration to the actuall declaration area and added Player player; Thanks To you ALL!<!!!!!
    Digi
    ItsHarry
    Xemos
    Everdras
    RobotA69
    I'm tagging you so i can say thanks.
     
  18. Offline

    Everdras

    What's wrong with a boolean hashmap? How is it "the most inefficient"? What would you use instead?
     
  19. Offline

    -_Husky_-

    I save a string list of the players in the teamname.yml file, then fetch it.
     
  20. Why use a Map forboolean when you can just use a List/Set ? It's more complicated and uses more memory to use a Map just for boolean value.
     
  21. Offline

    ItsHarry

    I personally just make a list and put names in there. The boolean value is simply whether the name is in there or not. Way easier and way more efficient.
     
  22. Offline

    Everdras

    I will give you this; a List or Set will use less memory as they are typically dynamically allocated- they use only exactly as much memory as they need.

    However, using a HashMap over a LinkedList is a classic example of a time-space tradeoff. Memory is cheap, so we make our data structure more space-complex in order to decrease the time-complexity of certain operations on the elements in the structure.

    Specifically, I assume here we will have two main things we'll ever want to care about:

    -Who's on the team? (i.e. get a list or set of all names of who is on the team)
    -Is a specific player on the team?

    Before we go further, we can't work with pure ADTs. We need specific implementations. So I'll assume we're using a LinkedList versus a HashMap.

    For the first point, the operation will generally be O(1) for both. With a LinkedList, you can just iterate over it directly. For a HashMap, I *believe* it dynamically updates an internally-organized Set of keys. So that too is O(1). If it's not, it's O(n) if it needs to iterate over all entries in the table to assemble the Set. So, assuming the HashMap prefers the time-efficient method, their time-complexity is the same here.

    For the second point, this is where the HashMap shines. Checking if a player is on the team will generally involve calling some sort of "contains()" method. For the LinkedList, it's contains(T elem). For the HashMap, you can use "containsKey(K key)" or "get(K key)".

    contains() on a LinkedList is O(n), since it has to iterate over every element in it in worst-case to determine if it contains the element you want.

    containsKey() and get() on a HashMap are both O(1), since it uses a hashing algorithm to index into its underlying array and access the value.

    So the HashMap is a better choice of data structure since it offers O(1) time-complexity for one of the operations we care most about, whereas the other structure is much less efficient (O(n)).

    TLDR: HashMap is more time-efficient than LinkedList or Set when it comes to checking if an element is present. So we prefer it since memory is cheap cheap cheap.
     
  23. Offline

    ItsHarry

    The problem with hashmaps is, why would you have a key and a value when only the key is needed. That's why a List is easier.
     
  24. Offline

    Everdras

    You would be right if all we ever cared about was keeping the names, and we would never need to perform operations on arbitrary elements in it. I.e. the only time you ever used it was when you wanted to iterate over every single element in it.

    With the way the OP phrased his questions and the intent of his plugin, it will very likely be that he will need to operate on arbitrary elements within the list, whether that be checking if a player is on a team or mutating some value pertaining to the player.

    See my above comment and reply for more detail.
     
  25. Offline

    ItsHarry

    Oh like that you mean. Well that is a possibility but wouldn't it be easier to just have 2 lists, one for team 1 and one for team 2?
     
  26. Offline

    Everdras

    Easier, yes. More time-efficient, no. Be lazy if you want.

    Like I said, this is a time-space tradeoff. Memory is cheap, this is why they tell you in the main plugin intro tutorials to use HashMaps as much as you can. They want to basically force you to use data structures that favor computational efficiency over memory efficiency.

    Seriously, there's a reason why Algorithms and Data Structure classes teach about time-complexity instead of space-complexity. Yes, avoid memory leaks, but even as far back as when they were designing the GCC initially, the recommendation was to read in entire files because "with most computers having over 512MB of RAM", it's preferable to take up more RAM and compile in half the time.

    Most computer systems now have at least 4GB of main memory. My sister's crappy laptop from Best Buy has 4 gigs and it cost like 300 bucks.

    It's a lot easier to double your RAM than it is to double your CPU clock.

    Time is much more valuable than space.
     
  27. Offline

    ItsHarry

    k you win. D:
     
  28. Offline

    Everdras

    It doesn't matter. I'm only ever talking about one team.

    Each team is represented by a HashMap, that maps player names (Strings) to a Boolean object that determines whether or not theyre on the team.
     
  29. Hmm, I understand now.

    Well, this is one downside from "learning by experience", you can get bad experience from other people and that stuff spreads :/
     
Thread Status:
Not open for further replies.

Share This Page