ArrayLists of players

Discussion in 'Plugin Development' started by AGuyWhoSkis, Sep 11, 2013.

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

    AGuyWhoSkis

    NOTE: This is solved! Turned out to just be a stupid mistake on my part.

    Hey guys, I need your help! For the life of me I can't figure out why this code here isn't working:
    Code:
                      if (s.getLine(1).equals("Game 1")) {
                            if (!(Game1S.size() > 2)) {
                                if (!Game1S.contains(p.getDisplayName())) {
                                    Game1.add(p);
                                    Game1S.add(p.getName());
                                    Game1.set(0, p);
                                    Bukkit.broadcastMessage("Added "+ p.getName());
                                    String players = BlockHandler.visPlayers(s.getLine(2));
                                    s.setLine(2, ChatColor.LIGHT_PURPLE + players + "/20");
                                    s.update();
                                    p.sendMessage(ChatColor.GREEN + "You have been added to the "+ChatColor.DARK_AQUA + "Game 1" + ChatColor.GREEN +" queue. "+ChatColor.WHITE + "(" + Game1S.size()+")");
                                } else {
                                    p.sendMessage(ChatColor.RED + "You have already joined the queue. Please wait for the game to start!");
                                }
                    
    To further explain, Game1 and Game1S are arraylists of players. Whenever someone is "added" to it, it says "player was added" in chat, BUT it ISN'T actually added! No errors, nothing! Halp! (Note: It really isn't added. I've checked the size and it doesn't go up)
     
  2. Offline

    xXSniperzzXx_SD

    Well before i read what the code is, don't use players in the list, use their names.
    Also why are you checking for their display name then adding their actual name?
     
    AGuyWhoSkis likes this.
  3. Offline

    AGuyWhoSkis

    Initially I did it because I didn't remember how to get a player object through a string. Also, I've fiddled with the code and tried variations of it. This is just one of them, I've tried adding names, display names, etc. I'm sure I'm just missing one little thing here.
     
  4. Offline

    xXSniperzzXx_SD

    AGuyWhoSkis likes this.
  5. Offline

    WauloK

    Game1S is storing player names but you are comparing against DisplayNames..
     
    AGuyWhoSkis likes this.
  6. Offline

    AGuyWhoSkis

    Tried changing it, still doesn't work :(

    Updated code, Game1 ArrayList is now a list of strings:
    Code:
                        if (s.getLine(1).equals("Game 1")) {
                            if (!(Game1.size() > 2)) {
                                if (!Game1.contains(p.getName())) {
                                    Game1.add(p.getName());
                                    Bukkit.broadcastMessage("Added "+ p.getName());
                                    String players = BlockHandler.visPlayers(s.getLine(2));
                                    s.setLine(2, ChatColor.LIGHT_PURPLE + players + "/20");
                                    s.update();
                                    p.sendMessage(ChatColor.GREEN + "You have been added to the "+ChatColor.DARK_AQUA + "Game 1" + ChatColor.GREEN +" queue. "+ChatColor.WHITE + "(" + Game1.size()+")");
                                } else {
                                    p.sendMessage(ChatColor.RED + "You have already joined the queue. Please wait for the game to start!");
                                }
                            } else {
                                p.sendMessage(ChatColor.RED + "That game is full.");
                            }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  7. Offline

    dillyg10

    Does it work...?
     
  8. Offline

    AGuyWhoSkis

    No
     
  9. AGuyWhoSkis
    Debug your code. Also with this line
    Code:
    if (!(Game1.size() > 2)) 
    you're checking if Game1 size is not more than 2.. is that what you're trying to do, or is it a mistake?
     
  10. Offline

    kiwhen

    If this line of code indeed does run:
    Code:
    p.sendMessage(ChatColor.GREEN + "You have been added to the "+ChatColor.DARK_AQUA + "Game 1" + ChatColor.GREEN +" queue. "+ChatColor.WHITE + "(" + Game1.size()+")");
    ... then there must be a fundemental flaw in the code that creates the List. The line that adds the player is correct, and does work if your List is properly initialized. You would actually have to go through quite a lot of work to mess this one up. Does BlockHandler.visPlayers() modify the list in any way? Do you have a reset mechanism that somehow triggers at the wrong time?

    Here is an example that works, which is essentially the same thing you're trying to do:
    Code:
    List<String> players = new ArrayList<String>();
    System.out.println("Size: " + players.size());
    players.add("kiwhen");
    players.add("AGuyWhoSkis");
    System.out.println("Size: " + players.size());
    I create a blank ArrayList with Strings in it, print the size, add you and me, and finally print the size again. The output is as expected:
    Code:
    Size: 0
    Size: 2
    Whatever the case may be, here is a few tips:
    • Don't give objects, fields and methods names that begin with a capital letter. In Java, it is common practice to name classes, interfaces and enumerators with capital letters first, and fields and objects with lower-case letters first. It is okay to use capital letters in either one; this "rule" only applies to the first letter. "Game1" should therefore be "game1", and so on.
    • In this particular case, by Java logic, you should use a Set instead of a List. Lists are generally intended for storing elements in a particular order, accessible by index. Sets, on the other hand, aren't really supposed to take the order of elements to seriously (some types do anyway). They also do not provide access via index numbers. More importantly, Sets do generally not allow duplicate elements in them, which means that you cannot store the same player twice by accident. HashSets are generally concidered to be the faster option for Sets. Initialize it like this:
      Code:
      Set<String> players = new HashSet<String>();
     
    AGuyWhoSkis and Double0negative like this.
  11. Offline

    AGuyWhoSkis

    Wow. Firstly, thanks for the tips! I will keep those in mind. To answer your questions, visPlayer is just getting the current amount of players on the sign (ex. "5/20") and changing it by one. It has no code which modifies the game1 arraylist. There is no reset for the list anywhere in the code, either.
    But I have good news! I fixed it. This was the code which was causing the problem:
    Code:
        @EventHandler(priority = EventPriority.NORMAL)
        public void RightClick(PlayerInteractEvent e) {
        ArrayList<String> Game1 = new ArrayList<String>();
        ArrayList<Player> Game2 = new ArrayList<Player>();
    Pretty much, when the player right clicked it would overwrite the ArrayLists, and then add them again!
    Thanks for all the help anyways, guys! I really appreciate it.
     
    WauloK likes this.
Thread Status:
Not open for further replies.

Share This Page