How do you spawn an entity?

Discussion in 'Plugin Development' started by TDS200, Apr 18, 2017.

  1. TDS200

    TDS200 New Member

    Feb 7, 2017
    18
    1
    3
    Male
    I am trying to spawn a humanoid entity that cannot move. So, I have a pretty basic question, how do you spawn an entity in Nukkit?
     
  2. Jay | Atlas

    Jay | Atlas New Member

    Mar 10, 2017
    11
    3
    3
    Male
    Code:
    Player source;
    
            FullChunk chunk = source.getLevel().getChunk((int) source.x >> 4, (int) source.z >> 4, true);
            if (!chunk.isGenerated()) {
                chunk.setGenerated();
            }
            if (!chunk.isPopulated()) {
                chunk.setPopulated();
            }
    
            CompoundTag nbt = new CompoundTag().putList(new ListTag<Tag>("Pos").add(new DoubleTag("", source.x)).add(new DoubleTag("", source.y + 0.5)).add(new DoubleTag("", source.z)))
                    .putList(new ListTag<DoubleTag>("Motion").add(new DoubleTag("", 0)).add(new DoubleTag("", 0)).add(new DoubleTag("", 0)))
                    .putList(new ListTag<FloatTag>("Rotation").add(new FloatTag("",  0))
                            .add(new FloatTag("",  0)));
    
            Entity entity =  Entity.createEntity(EntityName.class.getSimpleName(),chunk,nbt);
            entity.spawnToAll();
     
  3. TDS200

    TDS200 New Member

    Feb 7, 2017
    18
    1
    3
    Male
    Thanks so much! Although, I can't seem to get it to work. I am getting a null pointer exception on the very last line.
     
  4. TDS200

    TDS200 New Member

    Feb 7, 2017
    18
    1
    3
    Male
    Ok, I can replace the second to last line of code with this and it works with all the passive mobs.
    Code:
    Entity entity = new EntityChicken(chunk,nbt);
    entity.spawnToAll();
    But, if I replace it with this I get the error in the spoiler below.
    Code:
    Entity entity = new EntityHuman(chunk,nbt);
    entity.spawnToAll();
    [ALERT] cn.nukkit.utils.EventException
    at cn.nukkit.plugin.MethodEventExecutor.execute(MethodEventExecutor.java:34)
    at cn.nukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:56)
    at cn.nukkit.plugin.PluginManager.callEvent(PluginManager.java:553)
    at cn.nukkit.level.Level.useBreakOn(Level.java:1700)
    at cn.nukkit.Player.handleDataPacket(Player.java:2685)
    at cn.nukkit.network.Network.processPackets(Network.java:185)
    at cn.nukkit.network.Network.processBatch(Network.java:156)
    at cn.nukkit.Player.handleDataPacket(Player.java:1893)
    at cn.nukkit.network.RakNetInterface.handleEncapsulated(RakNetInterface.java:155)
    at cn.nukkit.raknet.server.ServerHandler.handlePacket(ServerHandler.java:123)
    at cn.nukkit.network.RakNetInterface.process(RakNetInterface.java:64)
    at cn.nukkit.network.Network.processInterfaces(Network.java:76)
    at cn.nukkit.Server.tick(Server.java:1010)
    at cn.nukkit.Server.tickProcessor(Server.java:808)
    at cn.nukkit.Server.start(Server.java:787)
    at cn.nukkit.Server.<init>(Server.java:472)
    at cn.nukkit.Nukkit.main(Nukkit.java:68)
    Caused by: java.lang.NullPointerException
    at cn.nukkit.entity.EntityHuman.initEntity(EntityHuman.java:97)
    at cn.nukkit.entity.Entity.init(Entity.java:372)
    at cn.nukkit.entity.Entity.<init>(Entity.java:270)
    at cn.nukkit.entity.EntityLiving.<init>(EntityLiving.java:30)
    at cn.nukkit.entity.EntityCreature.<init>(EntityCreature.java:12)
    at cn.nukkit.entity.EntityHumanType.<init>(EntityHumanType.java:26)
    at cn.nukkit.entity.EntityHuman.<init>(EntityHuman.java:59)
    at MainClass.create(MainClass.java:113)
    at MainClass.onBlockBreak(MainClass.java:85)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at cn.nukkit.plugin.MethodEventExecutor.execute(MethodEventExecutor.java:29)
    ... 16 more

    Finally, if I use this code with any entity it gives this error.
    Code:
    Entity entity = Entity.createEntity(EntityName.class.getSimpleName(),chunk,nbt);
            entity.spawnToAll();
    [ALERT] cn.nukkit.utils.EventException
    at cn.nukkit.plugin.MethodEventExecutor.execute(MethodEventExecutor.java:34)
    at cn.nukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:56)
    at cn.nukkit.plugin.PluginManager.callEvent(PluginManager.java:553)
    at cn.nukkit.level.Level.useBreakOn(Level.java:1700)
    at cn.nukkit.Player.handleDataPacket(Player.java:2685)
    at cn.nukkit.network.Network.processPackets(Network.java:185)
    at cn.nukkit.network.Network.processBatch(Network.java:156)
    at cn.nukkit.Player.handleDataPacket(Player.java:1893)
    at cn.nukkit.network.RakNetInterface.handleEncapsulated(RakNetInterface.java:155)
    at cn.nukkit.raknet.server.ServerHandler.handlePacket(ServerHandler.java:123)
    at cn.nukkit.network.RakNetInterface.process(RakNetInterface.java:66)
    at cn.nukkit.network.Network.processInterfaces(Network.java:76)
    at cn.nukkit.Server.tick(Server.java:1010)
    at cn.nukkit.Server.tickProcessor(Server.java:808)
    at cn.nukkit.Server.start(Server.java:787)
    at cn.nukkit.Server.<init>(Server.java:472)
    at cn.nukkit.Nukkit.main(Nukkit.java:68)
    Caused by: java.lang.NullPointerException
    at MainClass.create(MainClass.java:114)
    at MainClass.onBlockBreak(MainClass.java:85)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at cn.nukkit.plugin.MethodEventExecutor.execute(MethodEventExecutor.java:29)
    ... 16 more