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

Help! InventoryTransactionEvent

Discussion in 'Development' started by TheStepKla, Mar 5, 2020.

  1. TheStepKla

    TheStepKla Silverfish

    Messages:
    20
    I want to click to item for use command and block move item to player inventory
    mycode
    Code:
    <?php
    namespace KRTStduio;
    
    use pocketmine\command\Command;
    use pocketmine\command\CommandSender;
    use pocketmine\inventory\ChestInventory;
    use pocketmine\item\ItemIds;
    use pocketmine\nbt\NBT;
    use pocketmine\nbt\tag\ByteTag;
    use pocketmine\nbt\tag\CompoundTag;
    use pocketmine\nbt\tag\DoubleTag;
    use pocketmine\nbt\tag\FloatTag;
    use pocketmine\nbt\tag\IntTag;
    use pocketmine\nbt\tag\ListTag;
    use pocketmine\nbt\tag\ShortTag;
    use pocketmine\nbt\tag\StringTag;
    use pocketmine\Player;
    use pocketmine\plugin\PluginBase;
    use pocketmine\tile\Chest;
    use pocketmine\tile\Sign;
    use pocketmine\tile\Tile;
    use pocketmine\item\Item;
    use pocketmine\event\Listener;
    use pocketmine\Server;
    use pocketmine\level\Level;
    use pocketmine\math\Vector3;
    use pocketmine\event\inventory\InventoryTransactionEvent;
    
    class Main extends PluginBase implements Listener {
    
    private $listener;
        
        public function onEnable(){
            $this->getLogger()->info("Enable");
            $this->getServer()->getPluginManager()->registerEvents($this, $this);
        }
        
        public function onCommand(CommandSender $sender, Command $cmd, $label, array $args) {
            if($cmd->getName() == "testgui"){
                $this->openShop($sender);
                return true;           
            }
        }
        
        public function openShop(Player $player){
    
            $chestBlock = new \pocketmine\block\Chest();
            $player->getLevel()->setBlock(new Vector3($player->getX(), $player->getY() - 4, $player->getZ()), $chestBlock, true, true);
    
            $nbt = new CompoundTag("", [
                new ListTag("Items", []),
                new StringTag("id", Tile::CHEST),
                new IntTag("x", $player->getX()),
                new IntTag("y", $player->getY() - 4),
                new IntTag("z", $player->getZ())
            ]);
            $nbt->Items->setTagType(NBT::TAG_Compound);
            $tile = Tile::createTile("Chest", $player->getLevel($player->getX() >> 4, $player->getZ() >> 4), $nbt);
            if($tile instanceof Chest) {
                $tile->getInventory()->clearAll();
                $tile->getInventory()->setItem(1,Item::get(145,0,1));
                $tile->getInventory()->setItem(2,Item::get(135,0,1));
                $tile->getInventory()->setItem(3,Item::get(45,0,1));
                $tile->getInventory()->setItem(4,Item::get(345,0,1));
                $tile->getInventory()->setItem(5,Item::get(320,0,1));
                $tile->getInventory()->setItem($tile->getInventory()->getSize()-1, Item::get(Item::WOOL, 14, 1));
                $player->addWindow($tile->getInventory());
            }
        }
        
        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();
          }
        }
        if($targetitem->getId() === 145){//$item is the item you want to check
            $event->setCancelled();
            $player->addTitle('§f•§e§f•');
        }
        if($targetitem->getId() === 135){//$item is the item you want to check
            $event->setCancelled();
            $player->addTitle('§f•§e§f•');
        }
        if($targetitem->getId() === 45){//$item is the item you want to check
            $event->setCancelled();
            $player->addTitle('§f•§e§f•');
        }
        if($targetitem->getId() === 345){//$item is the item you want to check
            $event->setCancelled();
           $player->addTitle('§f•§e§f•');
        }
        if($targetitem->getId() === 320){//$item is the item you want to check
            $event->setCancelled();
            $player->addTitle('§f•§e§f•');
        }
        }
    }
    you help me
     
  2. Nesqiik

    Nesqiik Spider

    Messages:
    6
    GitHub:
    kskaneki5
  3. TheStepKla

    TheStepKla Silverfish

    Messages:
    20
  4. TheStepKla

    TheStepKla Silverfish

    Messages:
    20
  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.