Solved Adding a player to an ArrayLists

Discussion in 'Plugin Development' started by Munnzeh, Aug 31, 2015.

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

    Munnzeh

    I have recently got back into Bukkit plugin development and Minecraft in general, now after about a year break my coding knowledge is more than rusty.

    What I'm trying to achieve is one player join, it will add the player to one of four arraylists, firstly it will check if the first list is empty, if so add them, if not add them to the second one and so on, now for some reason it only works up until the third one, when a third player joins it just adds them to the second array list.

    Any help would be appreciated.

    Code:
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            e.setJoinMessage(ChatColor.RESET + p.getPlayerListName() + ChatColor.DARK_GREEN + " has joined the game. (" + ChatColor.RED + Bukkit.getOnlinePlayers().size() +"/25" + ChatColor.DARK_GREEN + ")");
       
           
                    //Players
                if(ar.Player1M.size() == 0) {
                    ar.Player1M.add(p.getName());
                    p.sendMessage(ChatColor.RED + "You are Player 1!");
                }
                else if(ar.Player1M.size() == 1) {
                    ar.Player1F.add(p.getName());
                    p.sendMessage(ChatColor.RED + "You are Player 2!");
                }
                else if(ar.Player1F.size() == 1){
                    ar.Player2M.add(p.getName());
                    p.sendMessage(ChatColor.RED + "You are Player 3!");
                }
                else if(ar.Player2M.size() == 1){
                    ar.Player2F.add(p.getName());
                    p.sendMessage(ChatColor.RED + "You are Player 4");
                }
                 
    
            
     
  2. Offline

    Oxyorum

    @Munnzeh I'm not exactly sure what you are trying to do here. Do you want this to happen randomly, or do you want all players evenly split up between the lists?

    In your example, you are using the same condition for the second, third and fourth if statements, so its natural that all players after the first player get added to the second arraylist. The condition is the same for the second, third and fourth statements, so the code under the first of these is the one that gets executed.

    Edit: For clarity.
     
    Last edited: Aug 31, 2015
  3. Offline

    Munnzeh

    @Oxyorum In the first condition it checks if the list is empty, the second checks if its full, is that what's causing the issue?

    EDIT: Ohhh, I understand you, can you think of a work around?
     
  4. Offline

    AdobeGFX

    try checking if the array you are adding the player to is empty aswell
     
  5. Offline

    Oxyorum

    @Munnzeh Look at the edit to my previous post. And no, technically that is not an issue. That code runs just fine.

    EDIT: Saw your edit. :) I can't help you more until you tell me what you are intending to do with this. Do you want all players split between your four lists, or random addition into one of the four lists?
     
  6. Offline

    Munnzeh

    Code:
     else if(ar.Player1F.size() == 1 && ar.Player2M() == 0){
                    ar.Player2M.add(p.getName());
                    p.sendMessage(ChatColor.RED + "You are Player 3");
                }
    I tried this but no luck.

    EDIT: Oh, Yes this actually worked perfectly, it was just because I added it on the wrong line of code, thanks all.

    I wanted each player split between the four lists, but It's working now anyway, thankyou!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  7. Offline

    xMrPoi

    @Munnzeh this will only work for 4 players though.

    To have this work with any amount of players, you'd need to check the size of each List and get the list with the lowest size. Add the player to that list and when they leave, remove them.
     
  8. Offline

    Gonmarte

  9. Offline

    xMrPoi

    Just because he's having trouble with a plugin, doesn't mean he doesn't know Java. He even stated that he was getting back into it. His code is fine apart from the sizes he's comparing. Nothing here shows that he has a lack of knowledge of Java lol.
     
  10. Offline

    Gonmarte

    @xMrPoi Sorry, im not saying im a expert in java ( cuz IM NOT ) but if he learnt java he would now how to add a player in an array list
     
  11. Offline

    Keubix

    Dude, just stop. The no one just knows the bukkit api. You have to learn it as you get more into it. I have been using the bukkit api for 4 months now, and I still hit things I never knew how to do with bukkit. So just stop @Gonmarte
     
  12. Offline

    Gonmarte

    @Keubix I never start i was just giving a advice. I dont know why are u getting ur nose in this conversation.
     
  13. Offline

    xMrPoi

    Did you read the thread? He was adding people to lists just fine. The part he was getting tripped up on was comparing the sizes.
     
Thread Status:
Not open for further replies.

Share This Page