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

What is the easiest way to generate chests with full inventories automatically in a world?

Discussion in 'Development' started by jasonwynn10, Jan 1, 2017.

  1. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    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.
     
    Last edited: Jan 2, 2017
  2. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Can you share the error, and your attempt?
     
    applqpak likes this.
  3. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    The generator I use is here:
    PHP:
    <?php
    namespace 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 $levelRandom $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 == && abs($chunkZ) % 20 == 0) {
                for (
    $x 0$x 7$x++) {
                    for (
    $z 0$z 7$z++) {
                        
    $chunk->setBlock($x61$zBlock::DIRT);
                        
    $chunk->setBlock($x62$zBlock::DIRT);
                        
    $chunk->setBlock($x63$zBlock::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 == && abs($chunkZ) % 20 == 0) { //works as a filter to only use specific chunks 320 blocks apart
                
    Tree::growTree($this->level$chunkX 16 364$chunkZ 16 3$this->randomSapling::OAK);
                
    $c->setBlock($chunkX 16 264$chunkZ 16 3Block::CHEST,2);
            }
            
    $c->setChanged();
        }
        
    /**
         * Return BasicIsland spawn
         *
         * @return Vector3
         */
        
    public function getSpawn() {
            return new 
    Vector3(0650);
        }
    }
    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
     
  4. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    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
     
  5. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    NBT stuff like what? Can I have an example?
     
  6. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    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.
     
    jasonwynn10 and Matthew like this.
  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.