Create and Spawn Custom Entity?

Discussion in 'Plugin Development' started by NerdsWBNerds, May 19, 2014.

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

    NerdsWBNerds

    How can I create and spawn a custom entity? I've found the tutorials showing how to change ALL spawning/spawned entities to a new custom entity, but how can I just create a single custom entity, lets say 'CustomCow', and spawn that custom entity in whenever I want, instead of replacing all cows with this new custom entity?
     
  2. Offline

    caseif

    I haven't done it myself (and in fact I haven't seen mention of the topic in months), but IIRC, you'll need to create a new class called "CustomCow," have it extend either LivingEntity or Cow, and then create an instance of it and add it to the world with World#getHandle().addEntity(Entity).
     
  3. Offline

    tylersyme


    I had a hard time figuring out this same problem as well, but after hours of 'dinking' around I got it working

    It basically involves actually injecting a new entity type right into bukkit rather than replacing it, I do have to give credit to.. well I don't remember who it was anymore but he was a big help with some of this :p

    First, these are the two methods that are used to inject a new entity type

    Code:java
    1. protected static Field mapStringToClassField, mapClassToStringField, mapClassToIdField, mapStringToIdField;
    2. //protected static Field mapIdToClassField;
    3.  
    4. static
    5. {
    6. try
    7. {
    8. mapStringToClassField = net.minecraft.server.v1_7_R2.EntityTypes.class.getDeclaredField("c");
    9. mapClassToStringField = net.minecraft.server.v1_7_R2.EntityTypes.class.getDeclaredField("d");
    10. //mapIdtoClassField = net.minecraft.server.v1_7_R1.EntityTypes.class.getDeclaredField("e");
    11. mapClassToIdField = net.minecraft.server.v1_7_R2.EntityTypes.class.getDeclaredField("f");
    12. mapStringToIdField = net.minecraft.server.v1_7_R2.EntityTypes.class.getDeclaredField("g");
    13.  
    14. mapStringToClassField.setAccessible(true);
    15. mapClassToStringField.setAccessible(true);
    16. //mapIdToClassField.setAccessible(true);
    17. mapClassToIdField.setAccessible(true);
    18. mapStringToIdField.setAccessible(true);
    19. }
    20. catch(Exception e) {e.printStackTrace();}
    21. }
    22.  
    23. @SuppressWarnings({ "rawtypes", "unchecked" })
    24. protected static void addCustomEntity(Class entityClass, String name, int id)
    25. {
    26. if (mapStringToClassField == null || mapStringToIdField == null || mapClassToStringField == null || mapClassToIdField == null)
    27. {
    28. return;
    29. }
    30. else
    31. {
    32. try
    33. {
    34. Map mapStringToClass = (Map) mapStringToClassField.get(null);
    35. Map mapStringToId = (Map) mapStringToIdField.get(null);
    36. Map mapClasstoString = (Map) mapClassToStringField.get(null);
    37. Map mapClassToId = (Map) mapClassToIdField.get(null);
    38.  
    39. mapStringToClass.put(name, entityClass);
    40. mapStringToId.put(name, Integer.valueOf(id));
    41. mapClasstoString.put(entityClass, name);
    42. mapClassToId.put(entityClass, Integer.valueOf(id));
    43.  
    44. mapStringToClassField.set(null, mapStringToClass);
    45. mapStringToIdField.set(null, mapStringToId);
    46. mapClassToStringField.set(null, mapClasstoString);
    47. mapClassToIdField.set(null, mapClassToId);
    48. }
    49. catch (Exception e)
    50. {
    51. e.printStackTrace();
    52. }
    53. }
    54. }


    I personally inserted that code directly into my main class, as far as I'm aware this works with all 1.7 versions

    That's pretty much the bulk of the NMS stuff, the rest is creating the entities

    Just as an example use though:
    Code:java
    1. public void onLoad()
    2. {
    3. for (Entity en : Bukkit.getWorld("world").getEntities()) {
    4. if (en.getType() != EntityType.PLAYER) {
    5. en.remove();
    6. }
    7. }
    8. addCustomEntity(CustomCreeper.class, "CreeperCow", 50);
    9. }


    The integer in that last parameter stands for the skin that the entity will possess. 50 is the entity ID of a creeper, which means that when this entity is spawned in, it will take the form of a creeper but will behave however you've decided

    As for the removing of all entities, that is to prevent a crash, although I don't recall under what conditions the crash occurs. I'm sure there's a better of preventing whatever crash happens but I haven't looked into it :)

    In the end, this will not replace any existing entities neither will the server spawn them in of its own accord which is really nice.

    After all the digging you've done you may already know how to spawn in the custom entities after having injected them into bukkit, so I'll just leave it at that for now! Tagh me if you have any more questions I guess
     
Thread Status:
Not open for further replies.

Share This Page