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

Chest tile

Discussion in 'Development' started by InspectorGadget, Dec 13, 2016.

  1. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Hi, I have been also working on a GUI/ Chest tile plugins. The reason of my creating this thread is how do I add functions in the Chest. Example: when someone open the tile. It shows an item, when they click it, it allows the player to get a kit once tapped and how do I cancel the Chest inventory item adding 'event' if it's an event. Thank you very much.. :) I hope I'm making sense here.
    PHP:
    <?php

    namespace GUI;

    use 
    pocketmine\block\Block;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\level\Position;
    use 
    pocketmine\level\Level;
    use 
    pocketmine\Player;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\nbt\NBT;
    use 
    pocketmine\nbt\tag\CompoundTag;
    use 
    pocketmine\nbt\tag\IntTag;
    use 
    pocketmine\nbt\tag\ListTag;
    use 
    pocketmine\nbt\tag\StringTag;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\tile\Chest;
    use 
    pocketmine\tile\Tile;

    class 
    GUI extends PluginBase implements Listener {

        public function 
    onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }
       
        public function 
    onCommand(CommandSender $senderCommand $cmd$label, array $param) {
            switch(
    strtolower($cmd->getName())) {
           
                case 
    "gui":
                    
    $this->onOpen($sender);
                    return 
    true;
                break;
           
            }
        }
       
        public function 
    onOpen(Player $player){ // Thanks to https://github.com/G******FTW/addWindow/tree/master/src/G******FTW
        
    $block Block::get(54);
        
    $player->getLevel()->setBlock(new Vector3($player->x$player->2$player->z), $blocktruetrue);
        
    $nbt = new CompoundTag("", [
          new 
    ListTag("Items", []),
          new 
    StringTag("id"Tile::CHEST),
          new 
    IntTag("x"floor($player->x)),
          new 
    IntTag("y"floor($player->y) - 2),
          new 
    IntTag("z"floor($player->z))
        ]);
        
    $nbt->Items->setTagType(NBT::TAG_Compound);
        
    $tile Tile::createTile("Chest"$player->getLevel()->getChunk($player->getX() >> 4$player->getZ() >> 4), $nbt);
        
    $i $tile->getInventory();
        
    $i->addItem(Item::get(Item::STONE01));
        
    $player->addWindow($tile->getInventory());
      }
     
      public function 
    onDisable() {
      }

    }
     
    Skullex likes this.
  2. CreeperFace

    CreeperFace Witch

    Messages:
    58
    GitHub:
    creeperface01
    do you mean InventoryTransactionEvent ?
     
  3. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Yes
     
  4. CreeperFace

    CreeperFace Witch

    Messages:
    58
    GitHub:
    creeperface01
    so cancel it if its your inventory :D
     
  5. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Okay, what about function in certain blocks? Idk how to explain it but it has function like gives the person who taps it Kit.
     
  6. CreeperFace

    CreeperFace Witch

    Messages:
    58
    GitHub:
    creeperface01
    hmm check block coordinates?
     
  7. MioTaku

    MioTaku Witch

    Messages:
    69
    GitHub:
    uselesswaifu
    Not Exactly sure what're you asking But what I do for My server is Run a task for every second and check each players inventory and see if they have a certain item in their inventory that they could only obtain from the Chest Tile
     
  8. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Its like a function inside a tile.. Its alright though
     
  9. Aviv

    Aviv Baby Zombie

    Messages:
    156
    Thats a horrible way to do it, just use events, easier & faster
    You can use InventoryOpenEvent then set the item on the $event->getInventory()
    Use InventoryTransactionEvent to check if a player took an item from the specific inventory, and cancel it then run your code
    code:
    InventoryOpenEvent:
    PHP:
    onOpen(InventoryOpenEvent $event){
      
    $inventory $event->getInventory();
      
    $chestBlock $inventory->getHolder();
      if(
    $chestBlock instanceof \pocketmine\block\Chest){//if the block is a chest
        
    if($block->getPosition() === new Position($x$y$z$level)){ // a way to check if its the chest you want, there alot of ways to check, but i dont know what is the specific way you want
          
    $inventory->addItem($item); // $item is the item you want to add
        
    }
      }
    }
    InventoryTransactionEvent:
    PHP:
    public function onTransaction(InventoryTransactionEvent $event){
        
    $transa $event->getTransaction()->getTransactions();
        
    $int $event->getTransaction()->getInventories();
        foreach(
    $transa as $t=>$value){
           foreach(
    $int as $inst) {
            
    $inst $inst->getHolder();
            if(
    $inst instanceof Player){
              
    $player $inst;
            }
            if(
    $inst instanceof Chest){
                
    $chest $inst;
            }
            
    $targetitem $value->getTargetItem();
          }
        }
        
    $block $chest->getBlock();
        if(
    $block->getPosition() === new Position($x$y$z$level)){//same concept
          
    if($targetitem->getId() === $item->getId() && $targetitem->getDamage() === $item->getDamage()){//$item is the item you want to check
            
    $event->setCancelled();
            
    //your code
          
    }
        }
      }
    }
     
    MioTaku likes this.
  10. MioTaku

    MioTaku Witch

    Messages:
    69
    GitHub:
    uselesswaifu
    Oh, Thanks I'll use 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.