Extending NMS entities

Discussion in 'Spigot Plugin Development' started by Marcely99, Jun 2, 2017.

  1. Hey,
    I am currently trying to modify some things in entities via nms.
    For that I am trying to override a specific function.
    My code works perfectly if I am not overriding anything, but if I create a new class which is extending the nms entity class, the entity is kinda not existing but existing. It's not visible but it has sounds (log out/log in has no effects). Has anyone experience with stuff like that? Also please don't write how to spawn an entity in bukkit THANKS
  2. Did you register your Custom Entity?
    • Informative Informative x 1
  3. I think you have to set the visibility by hand
  4. What do you mean by that? Like sending packets etc.?
  5. I thought there was a simple method for it. I havent been workinc with nms in the newer versions of spigot though.
    • Agree Agree x 1
  6. what do u mean not overriding anything?

    any methods in the entity object?

    post your code. they appear invisible because you have not registered them into the server.
  7. How are you spawning your custom entity?

    If you are using packets then when you log out and log back in, then it will disappear. What you need to do is take your extended NMS entity and call addEntity(); on an NMS world. For example: ((CraftWorld)Bukkit.getWorld("world")).getHandle().addEntity(new YourCustomEntity());
    (There may be more arguments cause I am on my phone and I can't check NMS source right now).
  8. Again:
    Code (Text):
            final EntitySlime nmsSlime = new EntitySlime(worldServer);
            nmsSlime.setLocation(player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ(), 0, 0);
            ((CraftWorld) player.getWorld()).addEntity(nmsSlime, SpawnReason.CUSTOM);
    is working
    Code (Text):
            final EntitySlime nmsSlime = new EntitySlime(worldServer){
            nmsSlime.setLocation(player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ(), 0, 0);
            ((CraftWorld) player.getWorld()).addEntity(nmsSlime, SpawnReason.CUSTOM);
    is not working
  9. You've to register the custom entity to be shown to the client.

    Edit: And why do you use a anonymous inner class?
    #9 ysl3000, Jun 3, 2017
    Last edited: Jun 3, 2017
  10. You aren't registering the class. You're adding an entity to the world.

    Code (Text):
       private static void addToMaps(Class clazz, String name, int id)
            //getPrivateField is the method from above.
            //Remove the lines with // in front of them if you want to override default entities (You'd have to remove the default entity from the map first though).
            ((Map)getPrivateField("c", net.minecraft.server.v1_7_R4.EntityTypes.class, null)).put(name, clazz);
            ((Map)getPrivateField("d", net.minecraft.server.v1_7_R4.EntityTypes.class, null)).put(clazz, name);
            //((Map)getPrivateField("e", net.minecraft.server.v1_7_R4.EntityTypes.class, null)).put(Integer.valueOf(id), clazz);
            ((Map)getPrivateField("f", net.minecraft.server.v1_7_R4.EntityTypes.class, null)).put(clazz, Integer.valueOf(id));
            //((Map)getPrivateField("g", net.minecraft.server.v1_7_R4.EntityTypes.class, null)).put(name, Integer.valueOf(id));
    This is the code to register a custom entity. 1.7


    You need to update your fieldnames to the current mc version.
  11. Dude it wouldn't work completly if I wouldn't register it so please stop.
    Just read what I wrote: it's working but if I am using the inner classes it's not working
  12. In your first example you only instantiating an entity.

    Learn Java before using the Bukit/Spigot-API or NMS.

    you have no fucking clue what your doing.
    #12 ysl3000, Jun 3, 2017
    Last edited: Jun 3, 2017
  13. Then don't use the anonymous class?
  14. Thanks for helping me a lot.
    #14 Marcely99, Jun 3, 2017
    Last edited: Jun 3, 2017
  15. No need to be hostile. You are actually decreasing the chance that you will get the help.
    • Agree Agree x 2
  16. It doesn't matter how long you have been doing anything if you are acting like an impatient child.
    And @ysl3000 is totally right by the way, you are only adding the modified entity instance to the world instead of registering it on the server. My guess is that the client has no idea what to do with that. Extend the class properly and register it in NMS (edit: to be exact, the EntityTypes object).
    #16 Quasindro, Jun 3, 2017
    Last edited: Jun 3, 2017
    • Winner Winner x 1
  17. This is maybe true but it doesn't explain why it disappears when adding the inner class. Its working perfectly without the inner class.
  18. You defined an inner class -> an custom class of an entity that is not registered into EntityTypes class. Client and Server Doesn't know how to handle the custom entity. Nobody knows how it should look like.
    • Like Like x 1
  19. Oh I thought you ment like to add the entity to the world lol. I am sorry but maybe you understand the anger I had because I thought you are writing me something I already wrote
  20. no. there is a difference.

    right now, you are currently 'adding' / 'spawning' the entity into the world, however, it will not appear because you have not defined it as an entitytype or registered it as a valid entity on the server that it'll recognize.

    example and tutorial, of many, can be found here: