Player Death Event

Discussion in 'Plugin Development' started by MyNameIsHariK, Nov 2, 2013.

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

    MyNameIsHariK

    Hey!

    I need your help with this.

    Code:
    Set<String> team = new HashSet<String>();
    Set<String> spectate = new HashSet<String>();
    If a player 'team' dies I want them removed from team and added to 'spectate' and if there is 1 player in 'team' I want it to broadcast that that player is the winner.

    Thanks for your time,
    Hari
     
  2. Offline

    ChronicNinjaz

    Here you go ;)

    Code:
    @EventHandler
    public void onDeath(PlayerDeathEvent e){
    Player p = e.getPlayer();
    if(team.contains(p.getName()){
    team.remove(p.getName();
    spectate.add(p.getName());
    }
    }
     
  3. Offline

    adam753

  4. Offline

    MyNameIsHariK

    adam753 I know how to do the listener not the rest
     
  5. Offline

    Tss1410

    what do you use to add them in "team" and "spectate"?
     
  6. Offline

    adam753

  7. Offline

    MyNameIsHariK

    adam753

    public void onDeath(PlayerDeathEvent e) {
    //code here
    }
     
  8. Offline

    ChronicNinjaz

    If you use the code I gave you... It will work just fine ;)

    @MyNameIsHariK Here is a few examples ill give you about array lists. Hope it helps.
    Code:java
    1. public void array(Player p){
    2. //Adds the player to the array lists
    3. green.add(p.getName());
    4.  
    5. //removes the player form the array list
    6. green.remove(p.getName());
    7.  
    8.  
    9. //Checks if the player is in the array list
    10. if(green.contains(p.getName())){
    11. //code here
    12. }
    13. //clears the array list
    14. green.clear();
    15.  
    16. //gets the amount of players in the array list
    17. green.size();
    18.  
    19.  
    20. //Checks if the array list is empty
    21. if(green.isEmpty()){
    22. //code here
    23. }
    24.  
    25.  
    26. //this is almost everything you will ever use array lists for ... btw just to make things clear -
    27. // (green) is the name of my array list ... you will replace (green) with the name of your -
    28. //Array list. I hope this helps!
    29.  
    30. }


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

    MyNameIsHariK

    Help please?
     
  10. Offline

    adam753

    Well what about this then?
    Code:
    Player p = e.getPlayer();
     
    if(team.contains(p.getName()) {
        team.remove(p.getName());
        spectate.add(p.getName());
    }
    
     
    kevinspl2000 likes this.
  11. Offline

    MyNameIsHariK

    How would I do the > if there is 1 player in 'team' I want it to broadcast that that player is the winner.
     
  12. Offline

    kevinspl2000

    Code:java
    1. if (team.size() == 1) {
    2. // You have to iterate through the HashSet
    3. }

    Also one more thing is I wouldn't use HashSets or HashMaps for this kind of things. You could use a hashmap if you have more than one team, but for this I would use an ArrayList :)
     
  13. Offline

    Tss1410

    if(team.size == 1 && team.contains(e.getPlayer().getName()){
    e.getPlayer() is the winner
     
  14. Offline

    MyNameIsHariK

  15. Offline

    kevinspl2000

    MyNameIsHariK
    On the last error, add another bracket, see if that solves the problem. As for the other errors, is the variable in the public void (//event e) set to the e or something else.
     
  16. Offline

    MyNameIsHariK

  17. Offline

    adam753


    On the first two errors, add extra brackets where it tells you to. I'd have thought it was obvious, but if you count the brackets with your fingers you'll find one missing each time.

    The last two errors are my mistake, sorry - the method is called getEntity(), not getPlayer().
     
  18. Offline

    MyNameIsHariK

    adam753 kevinspl2000 Just tested it and it didnt work :(

    Code:
        @EventHandler
        public void onDeath(PlayerDeathEvent e){
            Player p = e.getEntity();
         
            if(joinedplayers.contains(p.getName())) {
                joinedplayers.remove(p.getName());
                spectate.add(p.getName());
            }
         
            if(cStarted == true) {
                if(joinedplayers.size() == 1 && joinedplayers.contains(e.getEntity().getName())){
                    Bukkit.broadcastMessage(ChatColor.RED + "[" + ChatColor.GRAY + "Tnt" + ChatColor.RED + "] " + ChatColor.YELLOW + e.getEntity() + ChatColor.YELLOW + " won the game!");
                }
            }
         
            e.getDrops().clear();
            p.getInventory().setBoots(null);
            p.getInventory().setChestplate(null);
            p.getInventory().setHelmet(null);
            p.getInventory().setLeggings(null);
        }
     
  19. Offline

    Tss1410

    what is wrong?
    Error?
     
  20. Offline

    adam753

    MyNameIsHariK
    If there's only one player on the team, and when they die you remove them from the team, then what's the size of the team going to be after that? Hint: it's not 1.
     
  21. Offline

    MyNameIsHariK


    adam753 What, I am making a ffa game and there are 24 players in 'team'

    Tss1410 It dont work. We started the game, and my friend died and it didnt say i won

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

    adam753

    Use some common sense. You're removing the player from the set, then checking if the set contains the player. That will always be false.

    What I meant with my last post is that after you've removed the last player from the set, its size should be 0, not 1. You're doing things in the wrong order and/or checking things in the wrong way.
     
Thread Status:
Not open for further replies.

Share This Page