Solved Searching Event for entity remove when far away

Discussion in 'Plugin Development' started by Shmobi, Jun 24, 2014.

Thread Status:
Not open for further replies.
  1. Hey buddys,
    i made a plugin which changes life of mobs. all mobs which are modified are listed in my arraylist. and all mobs which are modified have an animation, which is shown all few seconds at thier position. if such a mob dies or gets removed, it needs to be removed out of my arraylist and the animation must stop.
    in case of death no problem, entitydeathevent -> check if its one of my mobs -> if yes, remove it and stop animation

    but if it is removed, because the distance between the next player to it and it self is to big, there is no entitydeathevent. what i want is an event, which is thrown if a mob is removed in case of a to big distance to the next player.

    (just to say it, i wont tell the mob to not getting removed)

    thanks for help :)
     
  2. Shmobi I... I don't understand. But there's no event for when an "entity moves too far away" because that's just subjective and pointless. What's it meant to be too far away from? And you can check that yourself with the relevant methods in entity. See the JavaDocs.
     
  3. Offline

    Gater12

    AdamQpzm
    I think he means when the Entity is despawned?
     
  4. Offline

    _LB

    I'd like to know about this too. There is a CreatureSpawnEvent but no CreatureDespawnEvent or similar. There's an ItemDespawnEvent, but mobs aren't items.
     
  5. Gater12 Ah, that makes more sense.

    _LB Shmobi Maybe check when the chunk unloads? That would (temporarily) despawn entities & happens when a Player moves too far.
     
  6. Gater12 AdamQpzm yes Gater, that´s what i meant. if a mob dissociates to far from the nearest player bukkit will make the mob despawn. thats because bukkit dont want to have save a few thousand mobs in a list. if you would save every one you would get that much. that´s why they despawn if they arent near to anybody. so bukkit just must save all mobs arround the players.

    my problem is, if a mob despawns, i dont know which event is thrown, if there is one thrown. i need to remove the mob from my list if it despawns or dies. death is no problem because there is the entitydeathevent, but there is no entitydespawnevent, is it? that´s what i would need

    another way would be to forbid every mob which is in my list to despawn (livingEntity.setRemoveWhenFarAway(false);) but then i would have to list way to much mobs. so i wont do this. i want to remove them if they get removed from bukkit because of a to high distance to the next player

    did you get it now?
     
  7. Offline

    Gater12

    Shmobi
    Maybe ChunkUnloadEvent?
     
  8. AdamQpzm that would be just a way to check for a chunk, but im not quiet sure if the mob will despawn earlier or at another timepoint. i want to keep it in my list as long as it exists and no moment shorter or longer

    Gater12
    Gater12 AdamQpzm what if a entity is removed by another plugin? you can remove one by simply using entity.remove(). looking for a chunkunload would be just one case of a few where a entity can be removed/despawn. so there should be a way to cover all of the cases with an event, at least i hope so....

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  9. Shmobi Hmm. I might be remembering it incorrectly, and I don't have the time to look through the source, but remove() might just kill the entity and fire an EntityDeathEvent. Or it might not.

    As for another solution, you could try keeping a list of entities and, every so often, checking isValid()
     
  10. Offline

    RawCode

    they die on unload...
     
  11. RawCode unload dont cares. There are enough ways they despawn. entity.remove() for example. i need a way to cover all cases they can despawn.
    and i dont they they die. how did you get to this? is there any reference you know this from or do you just think they are dieing? because by dieing they would drop items, and items would keep laying there until they rlly despawn or getting found. that would mean that you can find everytime you reload a chunk the items of every mob which was inside (except for the itemdrop is cancelled by unloading, but why should they make so much work if they could just use remove()?)
     
  12. Offline

    RawCode

    Shmobi
    i opened source code of cbukkit and found method that despawn mobs.

    i will ask you to show code used to play animation, becouse i just dont see any issues with task, API have method to check is entity dead or not.
     
  13. RawCode my Plugin is doing following steps:
    1. listen to CreatureSpawnEvent
    2. if (entity == instanceof creeper) moblist.add(entity); moblist.startAnimation(entity);
    ... Between step 2 and 3 the entity is alive and can do whatever it wants. And while it is living, the animation will keep playing...
    3. listen to EntityDeathEvent
    4. if(moblist.contains(entity)) moblist.stopAnimation(entity); moblist.remove(entity);

    That´s all my Plugin is doing until yet. But if an Entity does despawn instead of die, there is no EntityDeathEvent thrown and i cant stop the animation. It will keep playing at the location the entity despawned

    5. listen to EntityDespawnEvent
    6. if(moblist.contains(entity)) moblist.stopAnimation(entity); moblist.remove(entity);

    step 5 and 6 would be the thow steps i miss
     
  14. Offline

    RawCode

    show code used to play animation ONLY.

    nothing else matters. Shmobi
     
  15. sure, you could replace "true" with !entity.isDead(), but this would only stop the animation. it wouldn´t give me the possibility to remove the mob from my list!
    Code:java
    1. new Thread(){
    2. public void run(){
    3. while(true){
    4. entity.getWorld().playEffect(entity.getLocation(), Effect.MOBSPAWNER_FLAMES, 10);
    5. try{
    6. Thread.sleep(1000);
    7. }catch(Exception ignore){
    8. }
    9. }
    10. }
    11. }
     
  16. Offline

    RawCode

    and how exactly it prevents you from cleaning list?
     
  17. RawCode there is a method i made called removeEntity() in my listclass. the thread for the animation is in my mobclass. the removeEntity() method stops the animaiton and removes the entity from the list. if i call this one the entity will be removed from the list and the animation stops, all i want. now i need a event which is fired when a livingentity gets removed.

    so i cant call removeEntity() in the animationthread because removeEntity() is a method of my list and not of the mob.

    imagine there is a creeper which is added to my list. ingame you can see the creeper with the animation. in my code the animation is running in a thead, as you can so. my removeEntity() method stops this thread so the animation wont be played anymore and after that it removes the mob from my list.

    there are 2 cases i need to stop the animation and remove a mob from my list
    - mob dies
    - mob gets removed

    mob dies is done by calling removeEntity() if a entitydeathevent is called, but how to cover the case when a mob is removed? thats what i am asking for.

    RawCode btw can be that thier isDead flag is set to false if a chunk unload, but they dont fire a entitydeathevent. thats all i said, i never said their flag is not set

    well, i wrote my own event for this... works fine everything is done

    how do i close a topic? i ve no idea xD

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

    Gater12

    Shmobi
    Mark it as solved in Thread tools. If you want to lock it, ask a moderator. Or report your own post saying you want it locked.
     
  19. Offline

    RawCode

    you shoud post solution in order to help other users who may hit similar issue and will find this thread with search tools Shmobi
     
  20. Offline

    _LB

    Seriously, how are we supposed to know when to remove entities from our maps and lists when they despawn if there's no way to tell that they've despawned?
     
  21. Offline

    RawCode

    there is way to tell is entity despawned or not, read javadocs\source and you will find it without any issues.
     
  22. Offline

    _LB

    RawCode could you give a class name? I've searched through the API thoroughly and used Google too.
     
  23. Offline

    krisdestruction

Thread Status:
Not open for further replies.

Share This Page