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.
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: $eventif($event->getQuitMessage() != "Server Closed"){return;} // Returns the event if the server isn't closingarray_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.
@minijaham And I'll just ping you even though you have already received an notification/email because you are watching this thread.
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.
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!
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.
I was going to have it only work with entities that are purposely spawned in! I actually did use EntitySpawnEvent to make the solution!
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?
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
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..
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();