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

Bug? Fix?

Discussion in 'Development' started by LucGamesDE, Mar 15, 2017.

  1. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    If I despawn entities, they don't despawn correctly :/
    They only despawn for the player who joinen first. All other players see them.

    My code:
    PHP:
    foreach ($player->getLevel()->getEntities() as $entity) {
    if(!
    $entity instanceof Player) {
    $entity->close();
    // kill() makes no difference
    }
    }
     
  2. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    Try despawnFromAll().
     
  3. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    I already tried that. But it isn't working
     
  4. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    Try a different way:
    PHP:
    foreach($player->level->getEntities() as $entity){
    if(
    $entity instanceof Player) continue;
    $entity->close();
    }
     
  5. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    No. Doesn't work
     
  6. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    Its something with the despawn. I tried this to kill all entity (also players). And the mobs don't despawn
     
  7. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    I tested something around. It takes only Player(1) as $event->getPlayer(). Has someone an Idea how to fix that?
     
  8. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    Are you using PlayerJoinEvent to run the code?
     
  9. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    I tried it in different events. With two players
     
    TheDiamondYT likes this.
  10. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    Schedule a delayed task to run 5+ seconds later after plugin startup. Then in the task run this:

    PHP:
    $level $this->yourPluginBaseClass->getServer()->getLevelByName("level name here");

    foreach(
    $level->getEntities() as $entity){
       if(!(
    $entity instanceof \pocketmine\entity\Human)){
          
    $entity->close();
       }
    }
     
  11. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    If I kill an entity with a sword, it despawns normally. What method do they use there?
     
  12. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    They use the attack() method, inside attack() they use the setHealth() method.
     
  13. Aviv

    Aviv Baby Zombie

    Messages:
    156
    why dont you just disable EntitySpawnEvent? its less lagg
    PHP:
    public function onEntitySpawn(EntitySpawnEvent $event){
     if(
    $event->getEntity() instanceof \pocketmine\Player) return;
     
    $event->setCancelled(); // you can check to disable at ceratin worlds too!
    }
     
    Last edited: Mar 16, 2017
  14. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    Continue is only used in loops and this is useless if there are already entities in the world.
     
    Muqsit and Aviv like this.
  15. Aviv

    Aviv Baby Zombie

    Messages:
    156
    Sorry
    My bad :D
     
  16. Aviv

    Aviv Baby Zombie

    Messages:
    156
    But if he wants to run a piece of code that removes entities every 1 second it will reduce the performence, meanwhile you can run this code once to remove all entities and then disable the event :p
     
  17. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    Wtf I don't want to disable mobs. Read the thread lol
     
  18. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    That is only if you run a repeating task, but a delayed task won't hurt. In fact repeating tasks have the best performance.
     
  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.