Solved Check If One Player Is Left

Discussion in 'Plugin Development' started by SuperboyDev, Oct 9, 2015.

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

    SuperboyDev

    The title says it all. I'm making a plugin which checks if only one player is left. My complication is that I store the players who have died in a hashmap, and they become a spectator. Si they are still online on the server. How do I check if only one player is left alive?
     
  2. Offline

    MarinD99

    You can check this in the a pretty simple fashion.
    Code:
    for(int i : Bukkit.getOnlinePlayers().size) {
    if(i == 0) {
    // code here
    }
    }
    As for the spectator part - I'd suggest creating two values. One for players and one for spectators. Upon each death (I'm assuming you're making a minigame) add a player to the spectator value. From here, you can play around, such as; adding invisibility, making them unable to break blocks, give them the ability to fly etc.
     
  3. Offline

    Scimiguy

    What are you storing in the hashmap?

    Why not store the dead players in a List, and compare that list.size() with the getOnlinePlayers().size() ?

    If the difference between the two is 2 or more, then you still have multiple players alive.
    To get the player, just run a loop for each of the Players in one of the list, removing them from the other list until the last one remains
     
  4. Offline

    SuperboyDev

    That's what I've done @Scimiguy But after that, i cant figure out how to stop the game once theres one player left. Like, it will like say, "<Player> has won the game!" and stop the server
     
  5. Offline

    Protophite

    @SuperboyDev Give us the class you are having problems with,.
     
  6. Offline

    Scimiguy

    Well it would depend on how your game is set up, programmatically.

    To get the last player, just loop through the list using a foreach from the Bukkit.getOnlinePlayers() collection. Remove them from the list, and if a player isn't in your list, then they're the surviving one.
    @SuperboyDev
     
  7. Offline

    SuperboyDev

    @MarinD99 I meant list. Sorry :p My list stores the data of people who have died. So on player death, it adds them to the list. Once the player respawns, it sets them as a spectator. I want to find out how to check that the last player left is not in this list, or do I create a list called alive?

    @Scimiguy Is it this?
    Code:
    for(Player p : Bukkit.getServer().getOnlinePlayers()) {
    List<?> alive = new ArrayList<?>
    }
    Also, how to check if a player is not in a list?
     
  8. Offline

    Zombie_Striker

    @SuperboyDev
    If you store the DEAD in a list, than check if (OnlinePlayers.size - Dead.size == 1) to see if there is one player or (onlinesize - Deadsize <= 1) to see if there is less than or equal to one player alive.

    Also make sure that thal players in the DEAD lists are online. If one of them gets removed, then it could cause a glitch.
     
  9. Offline

    MarinD99

    Code:
    if(!yourList.contains(p));
     
  10. Offline

    SuperboyDev

    @Zombie_Striker @MarinD99 This is my code. In what event should I put it? I think PlayerDeathEvent.
    Code:
    for (Player p : Bukkit.getServer().getOnlinePlayers()) {
                if (!dead.contains(p)
                        && (Bukkit.getServer().getOnlinePlayers().size()
                                - dead.size() == 1)) {
                    p.sendMessage(ChatColor.GREEN
                            + getConfig().getString("playerwinmessage"));
                }
            }
    EDIT: When I put it in the player death event, it seem to work. Any ideas?
     
    Last edited: Oct 11, 2015
  11. Offline

    Scimiguy

  12. Offline

    caderape

    @SuperboyDev why not playing with the GameMode ?
    I guess if they are spectator, they got the gamemode spectator
    So loops players in the world, skip players in gamemode spectators and u get the number of player alive
     
  13. Offline

    Scimiguy

    @caderape there's no need to do that with the way he's currently doing it.
     
  14. Offline

    SuperboyDev

    @caderape Mine doesn't change gamemode. What my question is, what if the player quits instead of dies? Do I keep the code in both?
    @Scimiguy
     
  15. Offline

    Scimiguy

    @SuperboyDev

    You mean if there's 2 people left and one leaves?
    You should be able to use the same code for both, sure

    Just make sure you're using the same Dead list for both EventHandlers
    You can use the PlayerQuitEvent
     
  16. Offline

    SuperboyDev

Thread Status:
Not open for further replies.

Share This Page