What is the easiest way to generate chests with full inventories automatically in a world? I have tried repeatedly to accomplish this, but I keep receiving an error when the Tile is created.
The generator I use is here: Spoiler: Usable SkyblockGenerator PHP: <?phpnamespace SkyBlock;use pocketmine\level\generator\biome\Biome;use pocketmine\level\generator\Generator;use pocketmine\block\Block;use pocketmine\block\Sapling;use pocketmine\level\ChunkManager;use pocketmine\level\generator\object\Tree;use pocketmine\math\Vector3;use pocketmine\utils\Random;class SkyBlockGenerator extends Generator { /** @var array */ private $settings; /** @var string */ private $name = "island"; /** @var ChunkManager */ private $level; /** @var Random */ private $random; /** * BasicIsland constructor. * * @param array $settings */ public function __construct(array $settings = []) { $this->settings = $settings; } /** * Initialize BasicIsland * * @param ChunkManager $level * @param Random $random */ public function init(ChunkManager $level, Random $random) { $this->level = $level; $this->random = $random; $this->name = "island"; } /** * Return generator name * * @return string */ public function getName() { return $this->name; } public function getSettings() { return $this->settings; } public function generateChunk($chunkX, $chunkZ) { $chunk = $this->level->getChunk($chunkX, $chunkZ); $chunk->setGenerated(); if (abs($chunkX) % 20 == 0 && abs($chunkZ) % 20 == 0) { for ($x = 0; $x < 7; $x++) { for ($z = 0; $z < 7; $z++) { $chunk->setBlock($x, 61, $z, Block::DIRT); $chunk->setBlock($x, 62, $z, Block::DIRT); $chunk->setBlock($x, 63, $z, Block::GRASS); } } $chunk->setX($chunkX); $chunk->setZ($chunkZ); $this->level->setChunk($chunkX, $chunkZ, $chunk); } } public function populateChunk($chunkX, $chunkZ) { $c = $this->level->getChunk($chunkX, $chunkZ); for ($x = 0; $x < 7; $x++) { //use 7 x 7 as Island size within chunks for ($z = 0; $z < 7; $z++) { $c->setBiomeId($x,$z,Biome::PLAINS); // TODO change biome color } } if (abs($chunkX) % 20 == 0 && abs($chunkZ) % 20 == 0) { //works as a filter to only use specific chunks 320 blocks apart Tree::growTree($this->level, $chunkX * 16 + 3, 64, $chunkZ * 16 + 3, $this->random, Sapling::OAK); $c->setBlock($chunkX * 16 + 2, 64, $chunkZ * 16 + 3, Block::CHEST,2); } $c->setChanged(); } /** * Return BasicIsland spawn * * @return Vector3 */ public function getSpawn() { return new Vector3(0, 65, 0); }} and the error is here: Code: [Asynchronous Worker #1 thread/CRITICAL]: Error: "Call to a member function getLevel() on null" (EXCEPTION) in "/src/pocketmine/tile/Tile" at line 124
It's very hard to create chests with content from a generator, because it is running in a thread. You'd need to use NBT stuff
The entire chunk needs to be serialised to be able to passed across threads anyway, tiles included. Threading in PHP is complete sh*t. You can't pass objects across threads, you need to serialise the data and have some way of recreating the object on the other thread. This means you can't use Entities, Levels, Players, the Server class or any other PocketMine API method that has reference to any other object on the main thread.