Solved [NMS] Custom Entity Not Working for manually spawned mobs

Discussion in 'Plugin Development' started by MoeMix, Mar 15, 2014.

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

    MoeMix

    Hey bukkit! So I have a custom entity that I have already created using NMS code
    Code:java
    1. public class CustomEntityPig extends EntityPig{
    2.  
    3. public CustomEntityPig(World world) {
    4. super(world);
    5. }
    6.  
    7. @Override
    8. public void aD(){
    9. double speed = 5.3D;
    10. super.aD();
    11. this.getAttributeInstance(GenericAttributes.d).setValue(speed);
    12. }
    13. @Override
    14. public void e(float sideMot, float forMot) {
    15. if (this.passenger == null || !(this.passenger instanceof EntityHuman)) {
    16. super.e(sideMot, forMot);
    17. this.X = 0.5F;
    18. return;
    19. }
    20.  
    21. EntityHuman human = (EntityHuman) this.passenger;
    22. if (human.getBukkitEntity() != Bukkit.getPlayerExact("MoeMix").getPlayer()) {
    23. // Same as before
    24. super.e(sideMot, forMot);
    25. this.X = 0.5F;
    26. return;
    27. }
    28.  
    29. this.lastYaw = this.yaw = this.passenger.yaw;
    30. this.pitch = this.passenger.pitch * 0.5F;
    31.  
    32. this.b(this.yaw, this.pitch);
    33. this.aP = this.aN = this.yaw;
    34.  
    35. this.X = 1.0F;
    36.  
    37. sideMot = ((EntityLiving) this.passenger).be * 0.5F;
    38. forMot = ((EntityLiving) this.passenger).bf;
    39.  
    40. if (forMot <= 0.0F) {
    41. forMot *= 0.25F;
    42. }
    43. sideMot *= 0.75F;
    44.  
    45. float speed = 0.35F;
    46. this.i(speed);
    47. super.e(sideMot, forMot);
    48.  
    49. }
    50.  
    51.  
    52. }
    53.  


    I have also created a method to spawn my entity and I used it in my onEnable(). Therefore, the entities are registered....
    Code:java
    1. public static void registerEntities() {
    2. for (CustomMobs entity : values())
    3. a(entity.getCustomClass(), entity.getName(), entity.getID());
    4.  
    5.  
    6. BiomeBase[] biomes;
    7. try {
    8. biomes = (BiomeBase[]) getPrivateStatic(BiomeBase.class, "biomes");
    9. } catch (Exception exc) {
    10.  
    11. return;
    12. }
    13. for (BiomeBase biomeBase : biomes) {
    14. if (biomeBase == null)
    15. break;
    16.  
    17.  
    18. for (String field : new String[] { "as", "at", "au", "av" })
    19. try {
    20. Field list = BiomeBase.class.getDeclaredField(field);
    21. list.setAccessible(true);
    22. @SuppressWarnings("unchecked")
    23. List<BiomeMeta> mobList = (List<BiomeMeta>) list.get(biomeBase);
    24.  
    25.  
    26. for (BiomeMeta meta : mobList)
    27. for (CustomMobs entity : values())
    28. if (entity.getNMSClass().equals(meta.b))
    29. meta.b = entity.getCustomClass();
    30. } catch (Exception e) {
    31. e.printStackTrace();
    32. }
    33. }
    34. }


    This code works! However, it only works for naturally spawned entities. If I did /spawnmob pig 1, my custom class would not work for it. I only modified speed for my entity. Someone please HALP!
     
  2. Offline

    ShadowLAX

    MoeMix Of course if you spawned a chicken it wouldn't work... You extended the custom entity to be a pig. Unless pigs are now chickens, and minecraft just took a major plot twist...
     
  3. Offline

    MoeMix

    ShadowLAX actually I meant to say pig haha. Fixed it.
     
  4. Offline

    ShadowLAX

    MoeMix Try overriding the spawning of a natural entity with the command and spawn your custom entity instead.
     
  5. Offline

    MoeMix

    What's the NMS code for spawning an entity?
     
  6. Offline

    ShadowLAX

    MoeMix
    Code:java
    1. //p is net.minecraft.server.v1.7_R1.EntityHuman import,
    2. p.getWorld().addEntity(new CustomEntityPig(p.getWorld()).setLocation(x,y,z,yaw,pitch);
     
  7. Offline

    MoeMix

    ShadowLAX would that go in my onEnable or in my registerEntities() method?
     
  8. Offline

    ShadowLAX

    MoeMix Neither. It's for overriding a command that is supposed to spawn a regular pig. So, back to your example: Say someone did /spawnmob. You would have to make a onCommand() that has the @Override annotation, and spawn the custom entity instead.
     
  9. Offline

    MoeMix

    ShadowLAX I'm having trouble with the last part. The setLocation part. How do I get the location of the EntityHuman?
     
  10. Offline

    Garris0n

    Easy way would be human.getBukkitEntity().getLocation().
     
  11. Offline

    ShadowLAX

    MoeMix Use this instead, I noticed an error using what I gave you;
    Code:java
    1. //p is net.minecraft.server.v1.7_R1.EntityHuman import,
    2. CustomEntityPig custompig = new CustomEntityPig(p.getWorld());
    3. p.getWorld().addEntity(custompig);
    4. customz.setPosition(p.locX, p.locY, p.locZ);
     
  12. Offline

    MoeMix

    lol where did you get that customz?

    should it be custompig? seems reasonable

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

    ShadowLAX

    MoeMix Sorry, my bad; Forgot to change the variable there. Just change it to custompig.
     
  14. Offline

    MoeMix

    ShadowLAX Garris0n ok last part, is there a possible way to just set all the naturally spawnned mobs to use my custom class?
     
  15. Offline

    Garris0n

    All mobs, or all mobs of the type you register it as? If you mean all mobs, listen to the CreatureSpawnEvent and cancel then spawn your mob.
     
  16. Offline

    MoeMix

    ShadowLAX Garris0n For the variable "p" how did you import it?
    I already have this in my imports:
    Code:java
    1. import net.minecraft.server.v1_7_R1.EntityHuman;
     
  17. Offline

    Garris0n

    He intended that to be a player EntityHuman. It could be any location and world you like.
     
  18. Offline

    MoeMix

    Ok then how would I get it? Like when I want to add the entity I have to do world.addEntity(blahblahblah) but how do I get that "world"?
     
  19. Offline

    Garris0n

    The world has to be an NMS world. It can be retrieved like this:
    Code:java
    1. (nmsworldpackage).World world = ((CraftWorld) bukkitWorld).getHandle();


    The 'p' variable he was using was supposed to be an EntityHuman, as he specified. (Ignore my last post).
     
  20. Offline

    MoeMix

    Garris0n is bukkitWorld org.bukkit.World? I'm getting errors with that
     
  21. Offline

    Garris0n

    Yes, bukkitWorld is a bukkit world.
     
  22. Offline

    MoeMix

    Garris0n@ShadowLAX, I'm getting errors in line 4.... it's a red underline under org.bukkit.world
    Code:java
    1. @EventHandler
    2. public void spawn(CreatureSpawnEvent e){
    3. e.setCancelled(true);
    4. net.minecraft.server.v1_7_R1.World world = ((CraftWorld) org.bukkit.World).getHandle();
    5. CustomEntityPig custompig = new CustomEntityPig(world);
    6. world.addEntity(custompig);
    7. custompig.setPosition(e.getLocation().getX(), e.getLocation().getY(), e.getLocation().getZ());
    8.  
    9. }
     
  23. Offline

    Garris0n

    MoeMix org.bukkit.World is not a variable, it is the type. You need an actual world variable there too...
     
  24. Offline

    MoeMix

    oh so what should I set the variable equal to?
    Code:java
    1. org.bukkit.World w = ?
     
  25. Offline

    Garris0n

    I don't know, what world are you spawning it in? I can't code the entire plugin for you, if I did it wouldn't be your plugin. You should understand some basic Java before using Bukkit, and quite a bit about Bukkit before using internal server code... will you know what to do next update when everything breaks?
     
  26. Offline

    MoeMix

    Yeah, I'll just look at the classes and see which methods have been changed due to all the obfuscation. So I set the world to the world of the creature spawning. Thanks Garris0n for helping me out through all of this. I know it was long but you put up with all my questions :D! Thanks again!
     
  27. Offline

    InfamousSheep

    Did you find a way to spawn custom entities? I'm currently trying to figure that out after messing around with NMS
     
  28. Offline

    MoeMix

    Yeah I have. Basically, define the bukkit world, then define the nms world using by casting the bukkit world to it. then spawn the entity in that nms world
     
  29. Offline

    Garris0n

    The general "spawn procedure" is something like the following:
    Code:
    [nms package].World nmsWorld = ((CraftWorld) yourBukkitWorldHere).getHandle();
    YourCustomEntityHere entity = new YourCustomEntityHere(nmsWorld, OtherArgumentsHere); //Generally you design your custom entity to accept an nms world to call the superconstructor.
    entity.setPosition(PositionGoesHere);
    nmsWorld.addEntity(entity, OptionalSpawnReason);
     
Thread Status:
Not open for further replies.

Share This Page