How to detect damage to a spawned entity

Discussion in 'Plugin Development' started by kevinspl2000, Oct 30, 2013.

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

    kevinspl2000

    Okay so I spawned an entity and I want to do something if the entity died.
    I spawned a pigman like this:
    Code:java
    1. world.spawnEntity(loc, EntityType.PIG_ZOMBIE);

    just what do I do after?
    EDIT: I also want something to drop when the entity dies like for example an apple
     
  2. You want to store that spawnEntity's ID
    so
    Code:java
    1.  
    2. int id = world.spawnEntity(loc, EntityType.PIG_ZOMBIE);
    3. // Store that ID somewhere and check for
    4. // EntityDamageEvent and check if the entity id is equal to the one you spawned.
     
  3. Offline

    kevinspl2000

    SupaHam
    Getting errors
    Type mismatch: cannot convert from Entity to int

    okay so this is what i have now
    Code:java
    1. Location id = world.spawnEntity(loc, EntityType.ZOMBIE).getLocation();
    2. world.spawnEntity(loc, EntityType.ZOMBIE);
    3. if (((Entity) id).isDead()) {
    4. world.dropItem(loc, (ItemStack) inventory)
    5. }

    This doesnt seem to work???

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

    Pizza371

    kevinspl2000 that, uhm.. that, do you understand yourself what you are trying to do?
    this is what it looks like to me:
    Code:java
    1. 1. get the location of 'loc' which you already have
    2. 2. spawn a zombie at pre-defined location
    3. 3. if( (THIS IS AN ENTITY) Location).isDead()) { //it isnt its a location
    4. world.dropItem(loc, (THIS IS AN ITEMSTACK) inventory) //it wont be an itemstack cuz its an invenory, you're checking to see if an entity that you just spawned has died and dropping its items if it has died
    5. }
    6. //what im trying to get across is do you actually understand anything or arte you just copying & paste off of bukkit & google? You will find it VERY hard to make a plugin without learning at least a little.
     
  5. Offline

    Ultimate_n00b

    I have no idea what pizza is doing, but what you need to do is simple.

    Store the UUID in a global list/single variable (depends if you have this multiple times).
    Then listen to the entity damage event and check if the UUID is the same as one in the list/single variable.
    Then, remove the UUID from the list (if it's a variable skip this) when he dies.
     
  6. Do you even understand what you just wrote?

    I forgot to insert the method that retrieves the entity id here's a fix:
    Code:java
    1. int id = world.spawnEntity(loc, EntityType.PIG_ZOMBIE).getEntityId();


    I'm not entirely sure if using UUID is necessary, since you aren't planning to save it, but don't quote me on that.

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

    Ultimate_n00b

    If the entity dies, his ID will be given to another entity.
     
  8. Right... but he is already going to be listening for damage and death, so it'll be useless then and he would just remove the cache like he would with UUID
     
  9. Offline

    Ultimate_n00b

    Yeah, I realized that as I posted it.
     
  10. Offline

    kevinspl2000

    The UUID didn't seem to work. So thought if I get the Entity by location I could be able to find the entity by that. And by the way I want to spawn an entity and find the spawned entity's id. Will that spawn the entity and find its Unique Id? Or will it just find an id of a random zombie

    Is anyone willing to write an example code?

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

    hellboyPS

    kevinspl2000:
    You should try to write your own code. Otherwise you won't learn it :)

    This is what you need to do:
    • Spawn the entity and store the entityId somewhere
    • Add a Listener which listens for EntityDeathEvent
    • Check if the dying entity has a entityId which you stored earlier
    • If yes, do your stuff :)
    Another way to do it would be adding MetaData to the entity which you can later on read to determine if this Entity was spawned by you. This will then also be functional even if you restart your Plugin or Server (correct me if I'm wrong on this!)
    Cheers, hellboyPS
     
    russjr08 likes this.
  12. I don't believe metadata is stored if the server disables.

    Do yourself and everyone a favour and read the JavaDocs here. Javadocs stores all documentation for most, if not all, methods in the Bukkit API.
    Your goals should be figuring out what spawnEntity(), in the class World, does and how you can retrieve the id of an entity (which I already explained above).

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

    kevinspl2000

    BUMP
    SupaHam
    I don't see any information on UUID's. Still not solved..

    And hellboyPS
    I know what to do.. I just don't know where to store the ID of the entity and check if it is dead or not

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

    fromgate

    You can use MetadataValue to store specific information "inside" the mob. When required event is occurs you can check has this mob required MetadataValue or not.

    Here is example how to store experience and money values: https://github.com/fromgate/ReActio...gate/reactions/util/RAMobSpawn.java#L142-L150
    And here is how to set extra experience drop and money reward to mob killer (handler of the EntityDeathEvent): https://github.com/fromgate/ReActions/blob/master/src/me/fromgate/reactions/RAListener.java#L98-L111
     
  15. Offline

    Coelho

    There is no need to store it's ID. Store the Entity object in a SoftSet, and see if the Set contains said Entity on death.
     
  16. Offline

    hellboyPS

    Coelho Any SoftSet implementation you can recommend?
     
  17. Offline

    kevinspl2000

    Coelho
    What the hell is a SoftSet? I am new to this..
     
  18. Offline

    Coelho

    Use Guava's.
     
  19. Offline

    hellboyPS

    Then probably stick to the entityId.
    Get the entityId with entity.getEntityId(); Store it in a ArrayList. Later, check if the ArrayList contains the said id.
    If you don't know how this works, search google for some ArrayList tutorials.
     
Thread Status:
Not open for further replies.

Share This Page