Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Spawn Chest with Contents Glitch (Bug?)

Comments in 'Plugin Development' started by TruDan, Oct 27, 2014.

  1. TruDan
    Offline

    TruDan New Member

    Joined:
    Oct 24, 2014
    Posts:
    8
    Minecraft User:
    TruDan
    Ok, so i spawn a chest with the following code:

    PHP:
    if ($block == 54) {
        
    // Populate Chest
        
    $items = array();
        for (
    $x 0$x 27$x++) {
            
    $chance rand(09);
            if (
    $chance 3) { // 30% chance of spawning something
                
    $items[$x] = $this->getRandomFeastItem();
            }
        }

        
    $nbt = new Compound(false, array(
            new 
    Enum("Items", []),
            new 
    String("id"Tile::CHEST),
            new 
    Int("x"$xPos $xRel),
            new 
    Int("y"$yPos $yRel),
            new 
    Int("z"$zPos $zRel)
        ));
        
    $nbt->Items->setTagType(NBT::TAG_Compound);
        
    $chest = new \pocketmine\tile\Chest($level->getChunk(($xPos $xRel) >> 4, ($zPos $xRel) >> 4), $nbt);
        
    $level->addTile($chest);

        
    $pos = new \pocketmine\level\Position($chest->getFloorX(), $chest->getFloorY(), $chest->getFloorZ(), $chest->getLevel());
        
    $task = new FillChestTask($pos$items);
        
    TruHG::getInstance()->getServer()->getScheduler()->scheduleDelayedTask($task1);

        
    $chestX $xPos $xRel;
        
    $chestY $yPos $yRel;
        
    $chestZ $zPos $zRel;
    }
    All i've had to go by is existing plugin sources and searching the pocketmine docs. The FillChestTask is as follows:

    PHP:
    class FillChestTask extends PluginTask {

        
    /**
         *
         * @var Position Position
         */
        
    private $pos;
       
        
    /**
         *
         * @var Item[] Items
         */
        
    private $items;
       
        public function 
    __construct($pos$items) {
            
    parent::__construct(\TruDan\TruHG\TruHG::getInstance());
            
    $this->pos $pos;
            
    $this->items $items;
        }
       
        public function 
    onRun($tick) {
            
    $tile $this->pos->getLevel()->getTile($this->pos);
           
            if(
    $tile instanceof Chest) {
                
    $inv $tile->getRealInventory();
                foreach(
    $this->items as $slot => $item) {
                    
    $inv->setItem($slot$item);
                }
            }
           
            \
    TruDan\TruHG\TruHG::getInstance()->getLogger()->info("Filled Chest.");
        }

    }
    I first tried this without the delayed task, but instead just straight away. Yet it has the same results as with the delayed task.

    However, when it spawns in, the inventory is displayed on the left hand side (where player inventory should be), and not the right.

    Any Ideas, I'm not sure if this is some kind of bug with chests, or if i have missed something?
    LDX likes this.
  2. TruDan
    Offline

    TruDan New Member

    Joined:
    Oct 24, 2014
    Posts:
    8
    Minecraft User:
    TruDan
    Anyone?
    LDX likes this.
  3. LDX
    Offline

    LDX Notable Member Plugin Developer

    Joined:
    Oct 2, 2013
    Posts:
    1,434
    Plugins:
    14
    I wouldn't use a FillChestTask, I would get the tile entity and edit it's contents.
  4. TruDan
    Offline

    TruDan New Member

    Joined:
    Oct 24, 2014
    Posts:
    8
    Minecraft User:
    TruDan
    I tried it without the task, and doing it directly, which produced the same results. I also tried pre-defining the items in the Items enum. Also no luck. =/
  5. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,201
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    How about looking at ChestRefill plugin by @Falk?
    LDX likes this.
  6. TruDan
    Offline

    TruDan New Member

    Joined:
    Oct 24, 2014
    Posts:
    8
    Minecraft User:
    TruDan
    I looked there also, thats actually where i got the idea to do a fill task from.

    The strange thing is, it appears to put the contents there, because when you open the chest it shows the inventory, its just showing on the players side rather than the chests side.

Share This Page

Advertisement