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

Open Chest Inventory

Discussion in 'Development' started by Denzel Code, Sep 5, 2017.

  1. Denzel Code

    Denzel Code Spider

    Messages:
    10
    GitHub:
    denzelcode
    How i can open a chest inventory

    Code:
    <?php
    
        /*
         * Plugin created by Denzel Code
         */
    
        namespace ChestCommands\Commands;
        
        // Plugin
        use pocketmine\utils\TextFormat as Color;
        use ChestCommands\Main;
        // Commands
        use pocketmine\command\{Command, CommandSender};
        // Player
        use pocketmine\Player;
        use pocketmine\entity\Entity;
        // Vector
        use pocketmine\math\Vector3;
        // Level
        use pocketmine\level\Level;
        // Particles
        use pocketmine\level\particle\FloatingTextParticle;
        // Tiles
        use pocketmine\tile\Tile;
        use pocketmine\tile\Sign;
        // Nbt
        use pocketmine\nbt\NBT;
        use pocketmine\nbt\tag\StringTag;
        use pocketmine\nbt\tag\IntTag;
        use pocketmine\nbt\tag\ListTag;
        use pocketmine\nbt\tag\CompoundTag;
        
        /**
         * Main Class
         *
         * @author Denzel Code
         */
        class ChestCommands extends Command {
            
            private $main;
            
            public function __construct(Main $plugin) {
                $this->main = $plugin;
                
                parent::__construct('chestcommands', 'ChestCommand Command', '/chestcommands <argument>', [
                    'cc',
                    'ccmd'
                ]);
            }
            
            public function getMain() {
                return $this->main;
            }
    
            public function execute(CommandSender $sender, $commandLabel, array $args) {
                
                if ($sender instanceof Player) {
                    $position = [
                        $sender->getX(), $sender->getY(), $sender->getZ()
                    ];
                    
                    $level = $sender->getLevel();
                    
                    // $position = new Vector3($position[0], $position[1], $position[2]);
                    
                    $nbt = new CompoundTag("", [
                        new ListTag("Items", []),
                        new StringTag("id", Tile::CHEST),
                        new StringTag("CustomName", "Insert Custom Name Here"),
                        new IntTag("x", $position[0]),
                        new IntTag("y", $position[1]),
                        new IntTag("z", $position[2])
                    ]);
                    
                    $nbt->Items->setTagType(NBT::TAG_Compound);
                    
                    $chest = Tile::createTile(Tile::CHEST, $level, $nbt);
                    
                    if ($chest instanceof \pocketmine\tile\Chest) {
                        $sender->addWindow($chest->getInventory());
                    } else {
                        $sender->sendMessage("cant");
                    }
                    
                    $sender->sendMessage("Testing");
                }
            }
        }
    
    ?>
     
  2. Karanpatel567

    Karanpatel567 Baby Zombie

    Messages:
    115
    GitHub:
    Karanpatel567
    What do you mean, you could do
    $addWindow($player->getInventory());
    Or do you want it to open a chest inventory?
     
    KYUMA likes this.
  3. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    PHP:
                if ($sender instanceof Player) {
                    
    $position = [$sender->getX() - 2$sender->getY() - 2$sender->getZ() - 2]; // two blocks under the player so it isn't seen
                    
    $level $sender->getLevel();
                    
    $nbt = new CompoundTag("", [
                        new 
    ListTag("Items", []),
                        new 
    StringTag("id"Tile::CHEST),
                        new 
    StringTag("CustomName""Insert Custom Name Here"),
                        new 
    IntTag("x"$position[0]),
                        new 
    IntTag("y"$position[1]),
                        new 
    IntTag("z"$position[2])
                    ]);
                    
    $nbt->Items->setTagType(NBT::TAG_Compound);

                    
    $chest Tile::createTile(Tile::CHEST$level$nbt);

                    if (
    $chest instanceof \pocketmine\tile\Chest) {
                        
    $sender->addWindow($chest->getInventory());
                    } else {
                        
    $sender->sendMessage("Failed to create Chest Tile");
                    }
                    
    $sender->sendMessage("End of Command");
                }
     
  4. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    $addWindow isn't a function, it's a variable...

    $player->addWindow($chest->getInventory());

    $chest is ChestTile
     
  5. Karanpatel567

    Karanpatel567 Baby Zombie

    Messages:
    115
    GitHub:
    Karanpatel567
    Yes i know, but I never said that $addWindow was a function and also didn't use $chest so really i don't get your point.
     
  6. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    I already mentioned that $chest is a "chest tile", read my full post instead of replying with excitement
     
  7. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    also $addWindow($player->getInventory ()); doesn't make sense
     
  8. Denzel Code

    Denzel Code Spider

    Messages:
    10
    GitHub:
    denzelcode
    That don't open the chest, only sound the chest open sound, and any more
     
  9. Karanpatel567

    Karanpatel567 Baby Zombie

    Messages:
    115
    GitHub:
    Karanpatel567
    Well, I have recently created a plugins you are trying to make.
     
  10. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    Who cares? lol
     
  11. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    499
    GitHub:
    xXNiceYT
    I do (Not)
     
  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.