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

Solved Opening Inventory

Comments in 'Plugin Development' started by aliuly, Jun 10, 2015.

Thread Status:
Not open for further replies.
  1. aliuly
    Offline

    aliuly Notable Member Plugin Developer

    Joined:
    Feb 8, 2014
    Posts:
    1,086
    Plugins:
    17
    OK, so does Opening Inventory work or not?

    I have this in my code:

    PHP:
    $player->getInventory()->open($player)
    Where $player is an instance of pocketmine\Player. So, if this is in a command and I execute this from a command, then it should open the player's inventory. But it doesn't.
    Last edited: Jun 10, 2015
    Creeperface likes this.
  2. aliuly
    Offline

    aliuly Notable Member Plugin Developer

    Joined:
    Feb 8, 2014
    Posts:
    1,086
    Plugins:
    17
    The complete plugin is here
    Creeperface likes this.
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    But does it work if the Inventory is a ContainerInventory instead of a PlayerInventory?
    iksaku likes this.
  4. aliuly
    Offline

    aliuly Notable Member Plugin Developer

    Joined:
    Feb 8, 2014
    Posts:
    1,086
    Plugins:
    17
    I have also tried it with opening Chest inventory. I will write another gist later.
    PEMapModder likes this.
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    It is sensible, though, that it doesn't work with player inventories.
  6. aliuly
    Offline

    aliuly Notable Member Plugin Developer

    Joined:
    Feb 8, 2014
    Posts:
    1,086
    Plugins:
    17
    OK, so this gist adds a openchest command that will try to open the inventory of the closest chest. So ChestInventory is a sub-class of ContainerInventory. Still doesn't work.
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Oh I found the code. It should be $Player->addWindow($Inventory). I doubt if it works with PlayerInventory, but let's test everything :)
    Creeperface likes this.
  8. GamesLoad
    Offline

    GamesLoad Active Member

    Joined:
    Jun 28, 2014
    Posts:
    106
    Minecraft User:
    GamesLoad
    hello but this plugin in practice opens a particular inventory or default?
  9. aliuly
    Offline

    aliuly Notable Member Plugin Developer

    Joined:
    Feb 8, 2014
    Posts:
    1,086
    Plugins:
    17
    Ahh.. success. I updated the gists. It is also here.

    Like you said, the PlayerInventory did not open. On the other hand, the ChestInventory did work. So the gist creates a command called openchest that will open the closest chest.

    Thanks @PEMapModder for knowing everything...
  10. iksaku
    Offline

    iksaku Notable Member Plugin Developer

    Joined:
    Sep 2, 2013
    Posts:
    1,132
    Plugins:
    4
    Minecraft User:
    iksaku
    But, does it open "In player's" inventory? Or as a separated window?
  11. aliuly
    Offline

    aliuly Notable Member Plugin Developer

    Joined:
    Feb 8, 2014
    Posts:
    1,086
    Plugins:
    17
    Opens the chests as if you had tapped it.
  12. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    Cool! So you can access a chest far away!
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Or a CustomInventory (a chest that doesn't Minecraft-geologically exist)
    TigerHix likes this.
  14. aliuly
    Offline

    aliuly Notable Member Plugin Developer

    Joined:
    Feb 8, 2014
    Posts:
    1,086
    Plugins:
    17
    I tried opening non-existent chests, but that didn't seem to work.
  15. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Code?
  16. aliuly
    Offline

    aliuly Notable Member Plugin Developer

    Joined:
    Feb 8, 2014
    Posts:
    1,086
    Plugins:
    17
    The main issue is what to use for the InventoryHolder. I could only get it to work if I provide a tile\Chest there.

    This is my code:
    PHP:
    /**
     * Test opening inventories
     *
     * @name OpenTrader
     * @main aliuly\trader\OpenTrader
     * @version 1.0.0
     * @api 1.12.0
     * @description Test another open inventory plugin
     * @author aliuly
     */


    namespace aliuly\trader{
       use 
    pocketmine\plugin\PluginBase;
       use 
    pocketmine\command\CommandExecutor;
       use 
    pocketmine\command\ConsoleCommandSender;
       use 
    pocketmine\command\CommandSender;
       use 
    pocketmine\command\Command;
       use 
    pocketmine\Player;
       use 
    pocketmine\command\PluginCommand;
       use 
    pocketmine\tile\Chest;
       use 
    pocketmine\inventory\CustomInventory;
       use 
    pocketmine\inventory\ContainerInventory;
       use 
    pocketmine\inventory\InventoryType;
       use 
    pocketmine\inventory\InventoryHolder;

       class 
    TraderInventory extends CustomInventory {
         public function 
    __construct($holder) {
           
    parent::__construct($holder,InventoryType::get(InventoryType::CHEST));
         }
       }

       class 
    TraderStore implements InventoryHolder {
         protected 
    $inventory;
         public function 
    getInventory() { return $this->inventory; }
         public function 
    __construct() {
           
    $this->inventory = new TraderInventory($this);
         }
       }

       class 
    OpenTrader extends PluginBase implements CommandExecutor{
         public function 
    onEnable(){
           
    $cmd = new PluginCommand("trade",$this);
           
    $cmd->setDescription("Trade");
           
    $cmd->setUsage("/trade");
           
    $cmd->setExecutor($this);
           
    $this->getServer()->getCommandMap()->register($this->getDescription()->getName(),$cmd);
         }
         public function 
    onCommand(CommandSender $senderCommand $cmd$label, array $args) {
           if (
    $sender instanceof Player) {
             
    $sender->sendMessage("Opening store");
             
    $store = new TraderStore();
             
    $sender->addWindow($store->getInventory());
             return 
    true;
           }
           
    $sender->sendMessage("Run in-game only");
           return 
    true;
         }
       }
    }

  17. iksaku
    Offline

    iksaku Notable Member Plugin Developer

    Joined:
    Sep 2, 2013
    Posts:
    1,132
    Plugins:
    4
    Minecraft User:
    iksaku
    I'm following this method to get virtual chests... trying to trick the tiles system to use fake Chest tiles :p
  18. iksaku
    Offline

    iksaku Notable Member Plugin Developer

    Joined:
    Sep 2, 2013
    Posts:
    1,132
    Plugins:
    4
    Minecraft User:
    iksaku
    Here's my work so far... This creates a "fake Chest Tile" at coords [0, 0, 0] and use that tile for the custom ChestInventory... You need to run the "/virtual" command to reach it ;)

    The current code is kinda buggy... To get it work you will need someone to got at "chest's" coords and break the block in place, after that you will see a "ghost" chest and you're ready to go :3

    ahoj_truhliku, PEMapModder and aliuly like this.
  19. ahoj_truhliku
    Offline

    ahoj_truhliku Banned

    Joined:
    Jul 9, 2015
    Posts:
    89
    Minecraft User:
    Creeperface001
    so i can´t openWindow without chest or something?
  20. ahoj_truhliku
    Offline

    ahoj_truhliku Banned

    Joined:
    Jul 9, 2015
    Posts:
    89
    Minecraft User:
    Creeperface001
    why this doesn´t work?
    PHP:
    class ShopManager{
      
        public 
    $plugin;
        public 
    $players = [];
      
        private 
    $fakeChest null;
        private 
    $virtualChest null;
      
        private 
    $items;
      
        public function 
    __construct(Arena $plugin) {
            
    $this->plugin $plugin;
            
    $this->items = [Item::get(2421), Item::get(101)];
            
    $this->virtualChest();
        }
      
        public function 
    fakeChestTile(){
            if(
    $this->fakeChest === null){
                
    $this->fakeChest = new Chest($this->plugin->plugin->getServer()->getDefaultLevel()->getChunk(>> 40>> 4), new Compound("VirtualChest", [
                    new 
    Enum("Items", []),
                    new 
    String("id"Tile::CHEST),
                    new 
    Int("x"0),
                    new 
    Int("y"0),
                    new 
    Int("z"0)
                ]));
            }
            return 
    $this->fakeChest;
        }
          
        public function 
    virtualChest(){
            if(
    $this->virtualChest === null){
                
    $this->virtualChest = new ChestInventory($this->fakeChestTile());
            }
            return 
    $this->virtualChest;
        }
      
        public function 
    openShop(Player $p){
            
    $this->players[strtolower($p->getName())]['shopping'] = true;
            
    $p->addWindow($this->virtualChest());
        }
    }
    xionbig and Creeperface like this.
Thread Status:
Not open for further replies.

Share This Page

Advertisement