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

Solved Clear mobs on shut down

Discussion in 'Development' started by minijaham, Jun 14, 2021.

  1. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    No.. :(
     
  2. Axon

    Axon Zombie

    Messages:
    276
    Why don't we run a schedule repeating task to clear all entities, like every 5 minutes?
    Then also clear them all when with the custom stop command.
     
    Agent likes this.
  3. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    Unfortunately.. it is something that I can't have it clearing with a term :(
     
  4. Axon

    Axon Zombie

    Messages:
    276
    Here's something I managed to pull together with my wild imagination lol.
    I haven't tested it and it's just theoretical. It may or may not work.
    PHP:
    // public $Coordinates = [];
    // ----------
    // PlayerQuitEvent
    // Params: $event
    if($event->getQuitMessage() != "Server Closed"){return;} // Returns the event if the server isn't closing
    array_push($this->Coordinates$event->getPlayer()->getX()."::".$event->getPlayer()->getZ()."::".$event->getPlayer()->getLevelNonNull()->getName()); // Self-explained, just pushed the data into the code


    // OnDisable
    // Params: NULL
    // Extra notes: Let's hope you don't reload the server.
    $i 0;

    foreach(
    $this->getServer()->getLevels() as $Slevel) {
        foreach(
    $this->Coordinates as $PlayerCoord){
            
    // Create the 3 variables
            
    $data explode("::",$PlayerCoord);
            
    $x $data[0];
            
    $z $data[1];
            
    $level $data[2];
            if(
    $level->getName() != $Slevel){return;}
            
    // Load the chunks
            
    $x $player->getX() >> 4// take 'X' chunk coordinate
            
    $z $player->getZ() >> 4// take 'Z' chunk coordinate
            
    $radius 15;
            for (
    $chunkX = -$radius$chunkX <= $radius$chunkX++){
                for (
    $chunkZ = -$radius$chunkZ <= $radius$chunkZ++){
                    
    // if distance <= raduis than load chunk
                    
    if (sqrt($chunkX*$chunkX $chunkZ*$chunkZ) <= $radius$Slevel->loadChunk($chunkX $x$chunkZ $z);
                }
            }
            foreach(
    $Slevel->getEntities() as $entity) {
                if(
    $entity instanceof Animal || $entity instanceof Monster) {
                    
    $entity->close();
                    
    $i++;
                }
            }
        }
       
    }


    This may be performance heavy, but no players will be online to experience the lag.
     
    Last edited: Jun 15, 2021
    Agent, Mcbeany and minijaham like this.
  5. Axon

    Axon Zombie

    Messages:
    276
    @minijaham
    And I'll just ping you even though you have already received an notification/email because you are watching this thread.
     
    Agent and minijaham like this.
  6. Axon

    Axon Zombie

    Messages:
    276
    And sadly, there is no way to detect a forcefully crash or abort.
    It's like telling your arduino board to save all the files when the battery is pulled out. It can't.

    I would update the server and plugins to prevent any aborts.
     
    Agent and minijaham like this.
  7. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    Very interesting and a great idea. I actually did manage to come up with a way.
    I should've told you this earlier(I'm extremely sorry :() but the plugin I'm currently making is a simple Pets plugin.
    I simply stored the entity id on spawn under the pet owner's name and removed it on join of the player. I don't know why but it is a bit funky though. At least it works!

    Thank you for your help mate!
     
    Axon likes this.
  8. Axon

    Axon Zombie

    Messages:
    276
    Anytime!
    And don't be sorry, I learnt about chunks today :D
     
    Agent and minijaham like this.
  9. Primus

    Primus Zombie Pigman

    Messages:
    749
    There should be an event that gets called whenever an entity is spawned into the world whether it's a new entity or old. Basic idea is to remove the old entity right when it gets placed and keep the new entities. Hint Entity class has property $ticksLived.

    I know this is solved, I just realised that I didn't post my reply yestarday.
     
    Axon and minijaham like this.
  10. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    I was going to have it only work with entities that are purposely spawned in! :D
    I actually did use EntitySpawnEvent to make the solution!
     
    Primus likes this.
  11. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    Yep, I've removed the solved tag once again, and probably will be the last time.

    I'll get to the main point. My solution = Very bad.

    So what I wasn't aware of was that the entity ids actually change upon the restart of the server, so it doesn't remove the proper entity.

    This is my last question because I couldn't find this anywhere. How do you set a certain NBT tag to entities and store it? (Or is this thinking too complicated?)
    How do you remove entities even if the chunk isn't loaded(maybe with entity ID?)?

    I want to clear EVERY ENTITY when either:
    Server shutdown
    Server start
    First player join

    Will it be possible?
     
    Last edited: Jun 15, 2021
  12. Axon

    Axon Zombie

    Messages:
    276
    The solution I showed could work.
    Considering you may create the entity like this or similar to this
    PHP:
    $nbt = new CompoundTag;
    $nbt->setTag( new ListTag("Pos", [
        new 
    DoubleTag(""$player->getX()),
        new 
    DoubleTag(""$player->getY()),
        new 
    DoubleTag(""$player->getZ())
    ]));
    $nbt->setTag( new ListTag("Motion", [
        new 
    DoubleTag(""0),
        new 
    DoubleTag(""0),
        new 
    DoubleTag(""0)
    ]));
    $nbt->SetTag( new ListTag("Rotation", [
        new 
    FloatTag(""$player->getYaw()),
        new 
    FloatTag(""$player->getPitch())
    ]));
    $nbt->setTag( new ShortTag("Health"20));
    $nbt->setTag( new CompoundTag("Skin", [
         
    "Data" => new StringTag("Data"$player->getSkin()->getSkinData()),
         
    "Name" => new StringTag("Name"$player->getSkin()->getSkinId())]));

    $human Entity::createEntity("Human"$player->getLevel(), $nbt);
    $human->setNameTag($player->getName());
    $human->setNameTagVisible(true);
    $human->spawnTo($player);
    You can add the NBT data to the CompoundTag like this
    PHP:
    $nbt->setTag( new IntTag("IsPet"1)); // 1 for yes and 0 for no
    This may not work :)
     
    Agent and minijaham like this.
  13. Primus

    Primus Zombie Pigman

    Messages:
    749
    Or don't save the entities at all?
     
    minijaham and Axon like this.
  14. Axon

    Axon Zombie

    Messages:
    276
    Good point SetCanSaveWithChunk(false)
     
    Agent, minijaham and Primus like this.
  15. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    I actually did find out about that! Had to dump the entity and scrolled almost to the bottom to know that it exists.
    I'll try it and get back if something goes wrong once again..
     
  16. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    oh BOY IT works.
    Thank you guys so much for sticking with me until I...solved this with just one line lol

    For those who might have the same question:
    PHP:
           $nbt = new CompoundTag('', [
                
    'Pos' => new ListTag('Pos', [
                    new 
    DoubleTag(''$sender->getX()),
                    new 
    DoubleTag(''$sender->getY()),
                    new 
    DoubleTag(''$sender->getZ())
                ]),
                
    'Motion' => new ListTag('Motion', [
                    new 
    DoubleTag(''0),
                    new 
    DoubleTag(''0),
                    new 
    DoubleTag(''0)
                ]),
                
    'Rotation' => new ListTag('Rotation', [
                    new 
    FloatTag(''lcg_value() * 360),
                    new 
    FloatTag(''0)
                ]),
            ]);
            
    $entity Entity::createEntity("Chicken"$level$nbt);
            
    $entity->setCanSaveWithChunk(false); // This will despawn the entity on server restart
            
    $entity->spawnToAll();
     
  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.