Finding specific mobs killed

Discussion in 'Plugin Development' started by KaiPol, Nov 20, 2013.

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

    KaiPol

    So I'm trying to make a /stats command and I want to add how many pigmen they killed so how exactly would I do this?
    Code:
            if(commandLabel.equalsIgnoreCase("stats")){
                Player player = (Player) sender;
                int kills;
                for(EntityType mob : EntityType.PIG_ZOMBIE)
                {
                }
                player.sendMessage(ChatColor.DARK_GREEN + "---------------------------");
                player.sendMessage(ChatColor.BLUE + "" + ChatColor.ITALIC + "Statistics:");
                player.sendMessage(ChatColor.BLUE + "Pigmans killed:" + kills);
                player.sendMessage(ChatColor.DARK_GREEN + "---------------------------");
            }
    It gives me an error at EntityType.PIG_ZOMBIE I don't know what I'm doing wrong /:
     
  2. Offline

    Commander9292

    KaiPol The hell is that code? Why are you looping through An entity then doing nothing with it?

    You need to use an if statement to check if it's a Zombie pigman, then add to kills.
     
  3. Offline

    KaiPol

    Yeah I messed up a little .-. had something else going through my head
     
  4. Offline

    Commander9292

    Lol, I can see that...
     
  5. Offline

    maxben34

    KaiPol
    You need to check if(args.equals(0)){
    put your command in here and return true (or false).
    }
    You have a for loop that doesn't do anything to what it iterates through.
    You create a variable that will get printed, but you don't write anything for how the kills stat will increase.
    You should have on EntityHitByEntityEvent, check for player hitting a pigman. If the pigman's hearts are at 0 then kills++.
    The code above is really all over the place and doesn't make any sense tbh.
     
    Commander9292 likes this.
  6. Offline

    KaiPol

    So I've messed around with it a bit and I come up with this
    Code:
        @EventHandler
        public void onDeath(EntityDamageByEntityEvent event){
            int kills = 0;
            Entity entity = event.getEntity();
            Player player = (Player) entity;
            if(event.getEntity() instanceof PigZombie) {
                if (entity.isValid()) {
                    return;
                } else if(entity.isDead()) {
                    kills++;
                    player.sendMessage("You now have: " + kills + "Zombie kills");
                }
            }
    I get an error and it doesn't work in-game. One thing I've noticed is that EntityHitByEntityEvent doesn't work, so I changed it to EntityDamageByEntityEvent because that's the same thing(I think .-.). This is coming straight from my head so I'm probably doing something stupid here.(This is just a test to get "kills" working)
     
  7. Offline

    Windy Day

    Well, first of all, showing us the error is always helpful and learning to read your own stack traces can solve many of your problems quickly. Moving on, you set kills to 0 every event. So the most it will ever say you have killed at one time is one. Also, you should check if the Entity entity is a player before you cast them to a player. Finally, you are getting the damaged and casting it to a player (unsafely as I said above) then getting it again and checking if it is a PigZombie. Instead you should have something along these lines:
    Code:
            if (event.getEntity() instanceof PigZombie)  {// gets the the damaged and checks if it is a pigzombie
                if (event.getDamager() instanceof Player) { //gets the damager checks if they are a player
                    Player damager = (Player)event.getDamager(); //casts damager to player now that we know they are a player
    //continue checking if zombie is dead etc etc
     
    KaiPol likes this.
  8. Offline

    KaiPol

    When I try to not set kills as 0 it gives me an error when I try kills++;

    Here's an image of the error(for ingame):
    [​IMG]
    I'm thinking my death check might be wrong, it also says entity.PigZombie cannot be cast to org.bukkit.entity.Player or something like that so maybe I messed up calling the player.

    Windy Day
     
  9. Offline

    Windy Day

    Yup, as I said in my last reply, your casts are set up wrong. But good job identifying that one of the problems lays there. So lets start by fixing this, EntityDamageByEntityEvent contains two different entities that you can get, the entity being damaged and the entity damaging the other.
    To retrieve the entity being damaged simply use:
    Code:
    event.getEntity();
    To retrieve the entity the is the damager use:
    Code:
    event.getDamager();
    In you code, you only use event.getEntity(); so you are only retrieving the damaged entity. So the error you are getting is from a entity the is being damaged but isn't a player. You get this error because of these lines:
    Code:
            Entity entity = event.getEntity();
            Player player = (Player) entity;
    You get the damaged then without checking the type, cast it to a player. So when you hit a pigzombie, you will get an error saying you can't cast it to a player like you said you are getting.

    How do you fix this?

    What you want to do is :
    Code:
    if (event.getDamager() instanceof Player) {
        Player player = (Player)event.getDamager();
    }
    Then continue your code checking if the damaged entity (which you are retrieving correctly) is a pigzombie and so on.

    Once you get that fixed and you have no errors you will need to change how you are counting the kills, but that is an easy fix and should be addressed after you have your event repaired.
     
    KaiPol likes this.
  10. Offline

    KaiPol

    Okay this sorta worked, but now it gives me the message every time I hit them, and the "entity.isDead()" doesn't work I think. Also as you said it kept giving me 1.
    Windy Day
     
  11. Offline

    Windy Day

    Ok, can I see what you have now? Entity.isDead() should work, but you can also check if the health is equal to or less than zero and accomplish the same thing.
     
  12. Offline

    maxben34

    I don't know if you've done this, but you can save the UUID of any entity (An id that is given to every entity on the server). If a spawned mob with a specific UUID is killed then kills++

    KaiPol
     
  13. Offline

    KaiPol

    Code:
        @EventHandler
        public void onDeath(EntityDamageByEntityEvent event){
            int kills = 0;
            Entity entity = event.getEntity();
            if (event.getDamager() instanceof Player) {
                Player damager = (Player)event.getDamager();
            if (event.getEntity() instanceof PigZombie)  {
     
                        kills++;
                        damager.sendMessage(ChatColor.LIGHT_PURPLE +  "You now have: " + kills);
                    }
            }
    this makes it so that it gives me the message every time I hit

    Code:
            if (event.getEntity() instanceof PigZombie)  {
                if(entity.isDead()){
                        kills++;
                        damager.sendMessage(ChatColor.LIGHT_PURPLE +  "You now have: " + kills);
                    }
            }
    This(With the entity.isDead) does nothing. I've done some research and I can't seem to get entity.getHealth(); to work, I've tried to use it as an integer and a double(because I saw it was changed recently).
    I'll look into this some more, would I have to code this in the area where I spawned the pigzombie?
     
Thread Status:
Not open for further replies.

Share This Page