I'm foreaching a level's tiles and checking if they are an instanceof Chest but when I var_dump("chest") after the check, I get less strings then there are chests in the level. Why is this happening?
My guess is that Level.php only stores Tiles that are in loaded chunks, since almost everything is handled by each chunk. I may very well be wrong. What are you trying to do with each tile?
Would TreasureChest work? I'm not sure about how to resolve your problem, though somebody else might.
Using that plugin without any modifications still gives me that problem. It's the one I was modifying
Maybe you haven’t load Level or get all levels Try it but not sure if it will work PHP: use pocketmine\tile\Chest;foreach($this->getServer()->getLevels() as $level){foreach($level->getTiles() as $tile){if($tile instanceof Chest){//stuff}}}
You can't refill ALL the chests in a level, but you can try these methods: Write a list of coordinates where you've put the chests at, and loop through those coordinates. Use chunk loaders. I'd recommend going with the second option. A ChunkLoader keeps the chunk loaded until it exists. Players for example, are a type of chunk loaders. Add chunk loaders across your map, then remove them when the game is over (if you don't remove them, the chunks will stay loaded until the level unloads, which is generally during server shutdown). To create a ChunkLoader class, create a class that implements the ChunkLoader interface. PHP: <?phpclass MyChunkLoader implements ChunkLoader{} Don't forget — this is an interface, you will need to fill in the required class details. Once you do that, it's fairly simple to add and remove them. PHP: /** @var ChunkLoader $loader *//** @var Vector3 $pos */$chunkX = $pos->getFloorX() >> 4;$chunkZ = $pos->getFloorZ() >> 4;//Add ChunkLoader:$level->registerChunkLoader($loader, $chunkX, $chunkZ);//Remove ChunkLoader:$level->unregisterChunkLoader($loader, $chunkX, $chunkZ);
PHP: /** * @param ChunkLoadEvent $event */ public function onLoadChunk(ChunkLoadEvent $event) : void{ $chunk = $event->getChunk(); foreach($chunk->getTiles() as $tile) { if ($tile instanceof Chest) { if(!in_array($tile->getId(),$this->filledTiles)) { $this->filledTiles[] = $tile->getId(); if ($tile->getLevel()->getId() == $tile->getLevel()->getId()) { $tile->getInventory()->clearAll(); $slotToFill = 0; for ($i = 0; $i <= 7; $i++) { $items = [Item::get(Item::IRON_SWORD), Item::get(Item::IRON_BOOTS)]; $slotToFill++; $tile->getInventory()->setItem($slotToFill, $a); } } } } } }