Solved Keeping an entity from being removed.

Discussion in 'Plugin Development' started by CoderMusgrove, Jan 28, 2014.

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

    CoderMusgrove

    So I have an entity that I would like to keep on my server, even if it's out of view for all players or if no players are online, and I have searched up on it. In conclusion, the option that I see left is reflection in nms. I am new to both of those.

    So I started to create my reflection method, which what I want it to do is override the remove() method inside of CraftEntity, but I don't know what to do beyond taking in account the Method.
    Code:
    try {
        Method removeMethod = CraftEntity.class.getMethod("remove");
        removeMethod.setAccessible(true);
    } catch (Exception e) {
        e.printStackTrace();
    }
    I've been searching this for quite a while, and people mostly say to just extend the class and then override the method, but that's not an option at the moment..

    From this point, how would I override the method? What I plan to do is since I have a List of Entities, I would say "if this entity is not in the list, then you can remove."

    If anyone else knows of a better way, please tell me. :D
     
  2. Offline

    Jogy34

    Changing out a method with reflection is extremely tricky. I think it's possible but I'm not completely sure and either way I have no idea how you would go about doing it.

    Something that may actually work better than creating a full custom entity for what you want to do would be to create your own craftbukkit based entity. This can be done by just extending from the Craft[ENTITY_TYPE] class and then overriding the remove() method and replacing the bukkitEntity in the nms entity with your new entity (through reflection) when you add an entity to your list. In order to be able to extend off of a craftbukkit entity you have to use a modified version of craftbukkit to get around the ambiguous double/int health methods. I made a thread a while back when I was trying to do this, you can find that here, it should give you a good explanation of what to do. If you have any questions on that, post them here and not on that thread because it's fairly old.
     
  3. Offline

    CoderMusgrove

    Solved. Jogy34 I was able to keep the entity from despawning after I gave it some thought, I used a ChunkUnloadEvent and said if the specific entity/entities are in that chunk, don't unload. Thank you for your help anyways.
     
Thread Status:
Not open for further replies.

Share This Page