Solved Getting the last player in an ArrayList

Discussion in 'Plugin Development' started by Xyplo, Jul 6, 2014.

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

    Xyplo

    I need to get the ONLY player that is inside of an ArrayList. The catch is that I need to cast a player to the only player that is in that ArrayList. Perhaps my code can explain better..


    Code:
    if(Lists.gamePlayers.size() == 1)
     
            {
     
                Player pWhoWon = (Player) Lists.gamePlayers.; //So this is where I'm confused..
     
                Bukkit.broadcastMessage(pr.prefix1 + ChatColor.AQUA + "Deathmatch has ended! The winner was: " + ChatColor.GOLD + pWhoWon.getName());
     
            } else Bukkit.broadcastMessage("I don't know what to do: " + Lists.gamePlayers.size()); // Ignore this, it's for when I add a draw system.
    Basically, how can I cast a player to the last player in an ArrayList?
     
  2. Offline

    1Rogue

    If there's only one player in the list:

    Code:java
    1. Player p = Lists.gamePlayers.iterator().next();


    If you need the last one on a type of arraylist:

    Code:java
    1. Player p = Lists.gamePlayers.get(Lists.gamePlayers.size() - 1);


    No idea what or why you're casting though.
     
  3. This should do it:
    <<syntax=java>>
    if(Lists.gamePlayers.size() == 1)

    {

    Player pWhoWon = (Player) Lists.gamePlayers.get(0); //So this is where I'm confused.. //Just get the first player. The index is 0

    Bukkit.broadcastMessage(pr.prefix1 + ChatColor.AQUA + "Deathmatch has ended! The winner was: " + ChatColor.GOLD + pWhoWon.getName());

    } else Bukkit.broadcastMessage("I don't know what to do: " + Lists.gamePlayers.size()); // Ignore this, it's for when I add a draw system.
    <</syntax>>
     
  4. Offline

    Xyplo

    1Rogue Thanks, it worked fine!
    I'm basically getting the player who won in my SurvivalGames plugin. Every player is in an arraylist and taken out when they quit/die. I also have no idea why I was casting XD

    SOLVED
     
Thread Status:
Not open for further replies.

Share This Page