This is a puzzler.

Discussion in 'Plugin Development' started by user_43347, Jun 4, 2013.

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

    user_43347

    So I have a collection of entities stored in an array (I also tried storing entity IDs and locating the entity afterwards) that need to be killed when the round ends. I did a simple for loop of the spawned entities and called remove, however I noticed none of them were removed. I also tried damaging the entity with all it's remaining health, no luck. I then printed out a true/false if the entity was found in the world, and all printing false.

    TL;DR I have an array of entities I need to kill that exist, but aren't in the world, any help?
     
  2. Just store the UUID of the entity? Also I don't really understand what you are trying to do since you'er explaining it very unclear...
     
  3. Offline

    Shinxs

    try this:
    Code:
    for(Entity e : <your entity list>) {
        e.getWorld().getEntities().remove(e);
    }    
     
  4. Offline

    user_43347

    Shinxs That's just another way of doing what I tried, and no it didn't work :/
    CaptainBern I'm trying to kill a list of entities, what's unclear about that?
     
  5. Offline

    Shinxs

    did you do a check if they were still alive
     
  6. Offline

    Hoolean

    nkrecklow

    Please may we just see your code? It's probably a wee slip-up somewhere :)
     
  7. Offline

    ZeusAllMighty11

    Shinxs
    That is exactly what he said and he said it didn't work...



    Make sure that you don't have any plugins interfering with entity death
     
  8. Offline

    Hoolean

    TheGreenGamerHD

    He also said that he had tried using damage; also that wouldn't work as if it's say a Wither it's health will be more than 20 :p
     
  9. Offline

    user_43347

    TheGreenGamerHD Tried it, the problem is they say they are in the world, but the world say they aren't. It's very odd.

    Hoolean I'm doing a 1002939 check, it's under a license by the server I'm developing it for so publishing any code is sort of a stretch, let me see what I can do.

    microgeek Already tried that.
     
  10. Offline

    microgeek

    Storing an Entity's instance can lead to memory leaks. Store the UUID or entity ID in the collection and use a method like this to retrive the instance:
    Code:
        public static Entity getEntityByID(World world, int id) {
            //You can replace this with the UUID if you want
            for(Entity e : world.getEntities()) {
                if(e.getEntityId() == id) return e;
            }
            return null;
        }
    We can't really help with the other issue you're having if you don't provide code.

    EDIT: dupe'd
     
  11. Offline

    Hoolean

     
    user_43347 likes this.
  12. Offline

    user_43347

    I can't publish *much* code, but here's how it goes:
    1. Entity is spawned. (We only want certain entities).
    2. Added to a HashMap with it's type and entity id. (Verified working).
    3. Method is called to kill entity.
      1. Loop through entity IDs (verified working).
      2. Find entity in world with matching ID (it fails here, no entities have a matching id).
      3. Remove entity if not null. (They are all null, see above).
    I have it printing if the entity is null:
    Show Spoiler
    15:50:57 [INFO] Failed to find entity!
    15:50:57 [INFO] Removed: 155784
    15:50:57 [INFO] Failed to find entity!
    15:50:57 [INFO] Removed: 155781
    15:50:57 [INFO] Failed to find entity!
    15:50:57 [INFO] Removed: 155780
    15:50:57 [INFO] Failed to find entity!
    15:50:57 [INFO] Removed: 155783
    15:50:57 [INFO] Failed to find entity!
    15:50:57 [INFO] Removed: 155782
    15:50:57 [INFO] Failed to find entity!
    The removed message is when we try to remove it's ID, and the error is when it's null.


    So we have all the IDs and everything properly saved, I just can't find the matching entity in the world.
     
  13. Offline

    Hoolean

    nkrecklow

    Well that's mighty odd!

    Are you storing their ID or their UID (Unique Entity ID)?
     
  14. Offline

    user_43347

    Tried both, neither manage to find the corresponding entity in the world. Which is odd because the entities certainly exist, as I can see/interact with them.
     
  15. Offline

    Hoolean

    Have you been checking the right world? :)

    Also, try making a simple plugin that prints an entities UUID when interacted with to see if it matches any of the ones stored!
     
  16. Offline

    macguy8

    Pretty sure you're doing world.getEntities().remove(e);. That's removing it from a immutable list, not de-spawning it. You'd want to call e.remove()
     
  17. Offline

    user_43347

    No, I'm calling entity.remove.

    Hoolean Just tried it, found an entity with a matching ID by hitting around, but it couldn't find the matching entity in the world when the match ended.

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

    Hoolean

    nkrecklow

    That really is queer!

    If you're certain it's not your code it may be a CraftBukkit/Bukkit bug and you may wish to report it at leaky.bukkit.org :(
     
  19. Offline

    user_43347

    Hoolean Having a few other developers from the server look it over, make sure I'm not going insane. If it is a CraftBukkit bug, I'll be sure to report it, thanks for the help.
     
    Hoolean likes this.
  20. Offline

    cMan_

    Haven't used java in a while, but I think you should try removing the entities through craftbukkit? I remembered I encountered something like this with one of my plugins. I'll try to remember my solution.
     
  21. Offline

    user_43347

    Tried that, world.getLivingEntities().size() returns 0. This is very weird.
     
  22. Offline

    Jnorr44

    nkrecklow You would most likely get better results off stackoverflow. I will try and look through CB stuff real quick. The removal may be prevented in some cases.

    EDIT: It seems that the CB code just sets "dead" in nms Entity to true, looking deeper.
     
  23. Offline

    user_43347

    Thanks for the help, but I think I've tracked down the issue. I changed it to store instances and print out whether it's a valid entity, all which return false. And seeing that all are alive the only other cause according to the Javadocs is that is was despawned. How can I prevent entities from being despawned until I can remove them when the last player leaves a world?

    Jnorr44 I think I tracked it down to despawning the entities because it's called when the last person leaves the match (which is in it's own world). See above :p (also thanks for the help).

    Thread is changing directions: How can I prevent an entity from despawning when the last person leaves the world?

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

    Jnorr44

    Well, I got as far as possible on mob death in NMS, it led me here, and IWorldAccess is in mc-dev not CB nms.
    Code:
        protected void b(Entity entity) {
            for (int i = 0; i < this.u.size(); ++i) {
                ((IWorldAccess) this.u.get(i)).b(entity);
            }
     
            entity.valid = false; // CraftBukkit
        }
    Now that the thread is changing direction...

    In the next version you might be able to just use the new mob tag system to tag it. For now, try Entity.setRemoveWhenFarAway(false). There may be some other stuff in CB that I can look through if you want. There may be an NMS Entity.setStatic(true) or something along those lines. Just let me know if my current suggestion doesn't work.
     
    microgeek likes this.
  25. Offline

    user_43347

    Jnorr44 I need to do more testing, but I think setRemoveWhenFarAway is working, I didn't think that function would cover this, thanks!
     
  26. Offline

    Jnorr44

  27. Offline

    user_43347

  28. Offline

    Jnorr44

    Alright, I'll look into some CB and NMS stuff.
     
  29. Offline

    user_43347

    Okay, here's another clue. When I die and get teleported back the main world, the match ends and removes the entities just fine. However, when I quit the game and the match auto ends it doesn't remove them okay. Pointing in the direction of despawn issues because when I die I respawn so the world doesn't despawn the entities, but in leaving it can.
     
  30. Offline

    Jnorr44

    This is going to be really hard. I am noticing that all of the death calls that I see require the world to not be static... so... you could get the NMS entity and set it's world to static... That does require the use of NMS through, and I'm not sure you want to deal with all that.

    Another solution may be setting the chunk loaded and keeping it that way as long as the entity is in it. You would probably just run a scheduler and make sure the entity has a loaded chunk. If the server is stopped though, and the world is not up, this won't work. The main advantage to this is that it won't require NMS or CB.

    EDIT: After reading your latest post, my first solution is sounding much better, since when you leave a world it may change static to false.
     
Thread Status:
Not open for further replies.

Share This Page