1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Cannot create entities

Discussion in 'Development' started by ZenixPlayzMC, Apr 24, 2019.

  1. ZenixPlayzMC

    ZenixPlayzMC Silverfish

    Messages:
    23
    Hi! My name is ZenixPlayzMC. So I have this error when i create a entity on a world. The world and its chunks is fully loaded though. This is the error:
    Code:
    [11:52:33] [Server thread/CRITICAL]: InvalidStateException "Cannot create entities in unloaded chunks" (EXCEPTION) in "src/pocketmine/entity/Entity" at line 561
    
    This is the code on creating the entity:
    PHP:
                          $entity Entity::createEntity("Zombie"$player->getLevel(), $nbt);
                          
    $entity->setMaxHealth(500);
                          
    $entity->setHealth(500);
                          
    $entity->setScale(1);
                          
    $entity->getArmorInventory()->setHelmet(Item::get(310,0,1));
                          
    $entity->getArmorInventory()->setChestplate(Item::get(311,0,1));
                          
    $entity->getArmorInventory()->setLeggings(Item::get(312,0,1));
                          
    $entity->getArmorInventory()->setBoots(Item::get(313,0,1));
                          
    $entity->setNameTag(c::LIGHT_PURPLE c::BOLD "Test Boss" "\n" c::AQUA "HP " c::GRAY $entity->getHealth() . c::GREEN "/" c::GRAY $entity->getMaxHealth());
                          
    $entity->spawnToAll();
     
  2. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    Are you sure the chunk at whatever position you put in $nbt is loaded? It does not seem to be
    Can you show your $nbt?
     
  3. iCirgio

    iCirgio Slime

    Messages:
    92
    GitHub:
    lolnova
    What's $nbt defined as?
     
  4. ZenixPlayzMC

    ZenixPlayzMC Silverfish

    Messages:
    23
    Sorry for late reply. This is my $nbt:
    PHP:
                    $nbt = new CompoundTag("", [
                        
    "Pos" => new ListTag("Pos", [
                         new 
    DoubleTag(""$block->x),
                         new 
    DoubleTag(""$block->2),
                         new 
    DoubleTag(""$block->z)
                        ]),
                        
    "Motion" => new ListTag("Motion", [
                        new 
    DoubleTag(""0),
                        new 
    DoubleTag(""0),
                        new 
    DoubleTag(""0)
                        ]),
                        
    "Rotation" => new ListTag("Rotation", [
                          new 
    FloatTag(""mt_rand() / mt_getrandmax() * 360),
                          new 
    FloatTag(""0)
                        ]),
                        ]);
     
  5. ZenixPlayzMC

    ZenixPlayzMC Silverfish

    Messages:
    23
  6. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    Just to make sure that the chunk is loaded you can add this before Entity::createEntity
    PHP:
    $player->getLevel()->loadChunk($block->>> 4,$block->>> 4);
     
  7. ZenixPlayzMC

    ZenixPlayzMC Silverfish

    Messages:
    23
  8. ZenixPlayzMC

    ZenixPlayzMC Silverfish

    Messages:
    23
    The zombie entity doesnt spawn on the chunks and it doesnt send errors. Why is that?
     
  9. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    It probably isnt spawning at where you expect it to be, judging by the chunks not being loaded previously
    How did you define $block? Is it near the player?
     
  10. ZenixPlayzMC

    ZenixPlayzMC Silverfish

    Messages:
    23
    Wait lemme check
     
  11. ZenixPlayzMC

    ZenixPlayzMC Silverfish

    Messages:
    23
    PHP:
    $block $event->getBlock();
    And the event is PlayerInteractEvent.
     
  12. ZenixPlayzMC

    ZenixPlayzMC Silverfish

    Messages:
    23
  13. KielKing

    KielKing Zombie

    Messages:
    245
    GitHub:
    kielking
    you should create NBT from the function createBaseNBT which should be called on child classes
    PHP:
    $nbt Zombie::createBaseNBT($block->add(02));
     
  14. ZenixPlayzMC

    ZenixPlayzMC Silverfish

    Messages:
    23
    OOF Idk how to do that. Could you explain to me? I just know basic nbt.
     
  15. KielKing

    KielKing Zombie

    Messages:
    245
    GitHub:
    kielking
    literally just paste the code, and see if it has changed or not, it seems like you're interacting with a block in a chunk that has not loaded yet
     
  16. ZenixPlayzMC

    ZenixPlayzMC Silverfish

    Messages:
    23
    where should i paste it?
    So i should replace this code:
    PHP:
                    $nbt = new CompoundTag("", [
                        
    "Pos" => new ListTag("Pos", [
                         new 
    DoubleTag(""$block->x),
                         new 
    DoubleTag(""$block->2),
                         new 
    DoubleTag(""$block->z)
                        ]),
                        
    "Motion" => new ListTag("Motion", [
                        new 
    DoubleTag(""0),
                        new 
    DoubleTag(""0),
                        new 
    DoubleTag(""0)
                        ]),
                        
    "Rotation" => new ListTag("Rotation", [
                          new 
    FloatTag(""mt_rand() / mt_getrandmax() * 360),
                          new 
    FloatTag(""0)
                        ]),
                        ]);
    with:
    PHP:
    $nbt Zombie::createBaseNBT($block->add(02));
     
  17. KielKing

    KielKing Zombie

    Messages:
    245
    GitHub:
    kielking
    yes
     
  18. ZenixPlayzMC

    ZenixPlayzMC Silverfish

    Messages:
    23
    How will I set my rotation and motion then?
     
  19. ZenixPlayzMC

    ZenixPlayzMC Silverfish

    Messages:
    23
    nvm
     
  20. ZenixPlayzMC

    ZenixPlayzMC Silverfish

    Messages:
    23
    it summons 2 entities
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.