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

Solved Help with chest inv

Discussion in 'Development' started by Defications2po, Jun 24, 2017.

  1. Defications2po

    Defications2po Witch

    Messages:
    51
    So i know how to make a chest inv. But i tried to add a item but it does not work heres my code.
    PHP:
    public function sendInventory (Player $player) {
            
    $nbt = new CompoundTag "", [
                new 
    StringTag "id"Tile::CHEST ),
                new 
    IntTag "Inventory"),
                new 
    IntTag "x", ( int ) $player->getX () ),
                new 
    IntTag "y", ( int ) $player->getY () ),
                new 
    IntTag "z", ( int ) $player->getZ () )
            ] );
            
    /** @var Chest $tile */
            
    $tile Tile::createTile "Chest"$player->getLevel (), $nbt );
            
    $block Block::get Block::CHEST );
            
    $block->= ( int ) $tile->x;
            
    $block->= ( int ) $tile->y;
            
    $block->= ( int ) $tile->z;
            
    $block->level $tile->getLevel ();
            
    $block->level->sendBlocks ( [
                
    $player
            
    ], [
                
    $block
            
    ] );
            if (
    $tile instanceof Chest) {
            
    // Items
                
    $i $tile->getInventory();
                
    $i->addItem(Item::get(Item::STONE01));
            }
            
    $player->addWindow($tile->getInventory());
        }
    Can you please help me.
     
  2. Defications2po

    Defications2po Witch

    Messages:
    51
  3. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    $player->addWindow($i);
     
  4. Defications2po

    Defications2po Witch

    Messages:
    51
    mk let me try
     
  5. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    OFF TOPIC: why do you check if $tile is a instanceof Chest? your creating it so i suppose its a chest already
     
  6. Marabou

    Marabou Baby Zombie

    Messages:
    137
    GitHub:
    wiligangster
    *facepalm*
    He can not put anything in his inventory.
    Do you believe in seeing chest on the createTile () function that its will work? If it does not put instanceof a Chest it considered it as a Sign or everything that comes in the function Tile.php.
     
  7. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    i never used instanceof on my gui send ...
     
  8. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    The instanceof check is useless, yeah.
     
  9. Marabou

    Marabou Baby Zombie

    Messages:
    137
    GitHub:
    wiligangster
    Nope
     
  10. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    The instanceof check is indeed useless, but I sometimes do it too to silence my IDE. Even PHPStorm isn't smart enough to understand you're making a chest tile there. You could just add a doc above it too, though.
     
  11. Marabou

    Marabou Baby Zombie

    Messages:
    137
    GitHub:
    wiligangster
    Hmmm ok
     
  12. Defications2po

    Defications2po Witch

    Messages:
    51
    Still does not work:(
     
  13. BEcraft

    BEcraft Slime

    Messages:
    79
    GitHub:
    BEcraft
    Can you show us what classes are you using?
     
  14. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Any errors? Remove the variable declaration and the instanceof check in the ending. It's kinda confusing.
    PHP:
    //REPLACE THIS:
            
    if ($tile instanceof Chest) {
            
    // Items
                
    $i $tile->getInventory();
                
    $i->addItem(Item::get(Item::STONE01));
            }
            
    $player->addWindow($tile->getInventory());
    //WITH THIS
            
    $tile->getInventory()->addItem(Item::get(Item::STONE));
            
    $player->addWindow($tile->getInventory());
    That might barely be the problem. I believe the problem is you trying to create the chest where the PLAYER STANDS.
    According to your code, the chest would be created right where the player is standing, and that would force the player to move a little away from the chest due to bounding box collision. Try creating the chest 2 blocks above the player, so it's right above the player's head.
    PHP:
    new IntTag("y", (int) $player->getY() + 2),
     
  15. Defications2po

    Defications2po Witch

    Messages:
    51
    Now i see why they call Muqsit the great code:pr. Thank you so much everyone.
    And god luck on your servers Muqsit.
     
    Muqsit likes this.
  16. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Didn't expect someone to wish me god luck :p
    Thank you, but I am not a "great coder". There are many other people better than me in programming stuff. I'm still learning and have no idea about more than a 100 topics that a person having an year of experience in the development field should know of.
     
  17. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    lucky xD
     
    xXNiceAssassinlo YT likes 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.