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

Remove NBT

Discussion in 'Development' started by ethaniccc, Apr 4, 2020.

  1. ethaniccc

    ethaniccc Baby Zombie

    Messages:
    189
    GitHub:
    ethaniccc
    So in a LobbyCore plugin I'm making, I made it so when the plugin enables, it creates a NBT like so:
    PHP:
    $querypvp PMQuery::query("ip"30000);
            
    $kitpvp = (int) $querypvp['Players'];
            
    $nbt Entity::createBaseNBT(new Vector3(0.58618.5));
            
    $npc1 = new Villager($level$nbt);
            
    $npc1->setNameTag(TextFormat::BOLD TextFormat::GOLD "KitPvP\n" TextFormat::GREEN "Online: " $kitpvp);
            
    $npc1->setNameTagVisible(true);
            
    $npc1->lookAt(new Vector3(0860));
            
    $npc1->spawnToAll();
    The problem with this is that it will always spawn the Entity whenever it's enabled, making multiple entities. I tried the following (I started to use everything since I started getting annoyed lol):
    PHP:
    foreach($level->getEntities() as $entity){
                
    $entity->flagForDespawn();
                
    $entity->setHealth(0);
                
    $entity->kill();
                
    $entity->despawnFromAll();
                
    $entity->close();
            }
    Can anyone help me?
    Thanks in advance!
     
  2. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    so the code is in onEnable() function?
     
  3. ethaniccc

    ethaniccc Baby Zombie

    Messages:
    189
    GitHub:
    ethaniccc
    Yes
     
  4. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    Isn't that in some loop?
     
    ethaniccc likes this.
  5. ethaniccc

    ethaniccc Baby Zombie

    Messages:
    189
    GitHub:
    ethaniccc
    Ok, so basiclly, the first time the plugin is enabled, it creates the Entity and puts it in the position. That's why when it enables again, I want it to remove all the entities that are currently in place, and then make the same entity again, that way, there is only one entity.
     
  6. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    Im guessing the entity was saved in the chunks, to prevent that simply
    PHP:
    $entity->setCanSaveWithChunk(false);
    Note that if the chunk is not a spawn chunk and was unloaded, the entity will disappear
    Another way will be to save the entity somewhere in the plugin and remove the entity onDisable
     
    Muqsit and ethaniccc like this.
  7. ethaniccc

    ethaniccc Baby Zombie

    Messages:
    189
    GitHub:
    ethaniccc
    Thanks :D
     
  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.