The source of this can be a Player, entities, mobs, or even hoppers in the future!
Inheritance: extends pocketmine\event\Event, implements pocketmine\event\Cancellable
 public function onTransaction(InventoryTransactionEvent $ev)
 {
     echo __METHOD__ . "," . __LINE__ . "\n";
     //##DEBUG
     $tg = $ev->getTransaction();
     $pl = null;
     $ti = null;
     foreach ($tg->getInventories() as $i) {
         echo "INV " . get_class($i) . "\n";
         //##DEBUG
         if ($i instanceof PlayerInventory) {
             $pl = $i->getHolder();
         }
         if ($i instanceof TraderInventory) {
             $ti = $i;
         }
     }
     if ($ti == null) {
         return;
     }
     // This does not involve us!
     if ($pl == null) {
         $this->owner->getLogger()->error(mc::_("Unable to identify player in inventory transaction"));
         return;
     }
     echo __METHOD__ . "," . __LINE__ . "\n";
     //##DEBUG
     echo "PLAYER " . $pl->getName() . " is buying\n";
     //##DEBUG
     // Calculate total $$
     $xx = $this->getState("trade-inv", $pl, null);
     if ($xx == null) {
         return;
     }
     // This is a normal Chest transaction...
     echo "FROM SHOP " . $xx["shop"] . "\n";
     //##DEBUG
     $added = [];
     foreach ($tg->getTransactions() as $t) {
         if ($t->getInventory() instanceof PlayerInventory) {
             // Handling PlayerInventory changes...
             foreach ($this->playerInvTransaction($t) as $nt) {
                 $added[] = $nt;
             }
             continue;
         }
         foreach ($this->traderInvTransaction($t) as $nt) {
             $added[] = $nt;
         }
     }
     echo __METHOD__ . "," . __LINE__ . "\n";
     //##DEBUG
     // Test if the transaction is valid...
     // Make a copy of current inventory
     $tsinv = [];
     foreach ($pl->getInventory()->getContents() as $slot => $item) {
         if ($item->getId() == Item::AIR) {
             continue;
         }
         $tsinv[$slot] = [implode(":", [$item->getId(), $item->getDamage()]), $item->getCount()];
     }
     //var_dump($tsinv);//##DEBUG
     echo __METHOD__ . "," . __LINE__ . "\n";
     //##DEBUG
     //print_r($tsinv);//##DEBUG
     // Apply transactions to copy
     foreach ([$tg->getTransactions(), $added] as &$tset) {
         foreach ($tset as &$nt) {
             if ($nt->getInventory() instanceof PlayerInventory) {
                 $item = clone $nt->getTargetItem();
                 $slot = $nt->getSlot();
                 if ($item->getId() == Item::AIR) {
                     if (isset($tsinv[$slot])) {
                         unset($tsinv[$slot]);
                     }
                 } else {
                     $tsinv[$slot] = [implode(":", [$item->getId(), $item->getDamage()]), $item->getCount()];
                 }
             }
         }
     }
     echo __METHOD__ . "," . __LINE__ . "\n";
     //##DEBUG
     //		print_r($tsinv);//##DEBUG
     echo $pl->getName() . " has " . $xx["money"] . "G\n";
     //##DEBUG
     $total = 0;
     var_dump($xx["money"]);
     //##DEBUG
     var_dump($tsinv);
     //##DEBUG
     foreach ($tsinv as $slot => $item) {
         list($idmeta, $cnt) = $item;
         echo "slot={$slot} idmeta={$idmeta} cnt={$cnt}\n";
         //##DEBUG
         if (!isset($this->keepers[$xx["shop"]]["items"][$idmeta])) {
             $this->shopMsg($pl, $xx["shop"], "inventory-error");
             $ev->setCancelled();
             return;
         }
         list($i, $price) = $this->keepers[$xx["shop"]]["items"][$idmeta];
         echo "-{$idmeta} - {$cnt}/" . $i->getCount() . " *  {$price} ..." . round($cnt / $i->getCount()) * $price . "\n";
         //##DEBUG
         $total += round($cnt / $i->getCount()) * $price;
     }
     echo "TOTAL={$total}\n";
     //##DEBUG
     if ($total > $xx["money"]) {
         $this->shopMsg($pl, $xx["shop"], "not-enough-g", $total, $xx["money"]);
         $ev->setCancelled();
         return;
     }
     echo __METHOD__ . "," . __LINE__ . "\n";
     //##DEBUG
     foreach ($added as $nt) {
         $tg->addTransaction($nt);
     }
     // Make sure inventory is properly synced
     foreach ($tg->getInventories() as $i) {
         $this->owner->getServer()->getScheduler()->scheduleDelayedTask(new PluginCallbackTask($this->owner, [$i, "sendContents"], [$pl]), 5);
         $this->owner->getServer()->getScheduler()->scheduleDelayedTask(new PluginCallbackTask($this->owner, [$i, "sendContents"], [$pl]), 10);
         $this->owner->getServer()->getScheduler()->scheduleDelayedTask(new PluginCallbackTask($this->owner, [$i, "sendContents"], [$pl]), 15);
     }
 }
 public function execute()
 {
     if ($this->hasExecuted() or !$this->canExecute()) {
         return false;
     }
     Server::getInstance()->getPluginManager()->callEvent($ev = new InventoryTransactionEvent($this));
     if ($ev->isCancelled()) {
         foreach ($this->inventories as $inventory) {
             if ($inventory instanceof PlayerInventory) {
                 $inventory->sendArmorContents($this->getSource());
             }
             $inventory->sendContents($this->getSource());
         }
         return false;
     }
     foreach ($this->transactions as $transaction) {
         $transaction->getInventory()->setItem($transaction->getSlot(), $transaction->getTargetItem());
     }
     $this->hasExecuted = true;
     return true;
 }
 public function onTransaction(InventoryTransactionEvent $event)
 {
     foreach ($event->getTransaction()->getInventories() as $inv) {
         if ($inv instanceof PlayerInventory) {
             $player = $inv->getHolder();
             if (!$player instanceof Player) {
                 return;
             }
             $session = $this->main->getSession($player);
             if ($session === null) {
                 $event->setCancelled();
                 return;
             }
             if ($session->onTransaction($event) === false) {
                 $event->setCancelled();
             }
             return;
         }
     }
 }
 public function onTransaction(InventoryTransactionEvent $event)
 {
     $Transaction = $event->getTransaction();
     $Player = null;
     $BuyingTile = null;
     $BuyingInv = null;
     foreach ($Transaction->getInventories() as $inv) {
         if ($inv instanceof PlayerInventory) {
             $Player = $inv->getHolder();
         } elseif ($inv instanceof BuyingInventory || $inv instanceof ChestInventory) {
             $BuyingInv = $inv;
         }
     }
     if (!$Player || !$BuyingInv) {
         return;
     }
     if ($this->plugin->getState("buying_chest", $Player, null) == null) {
         return;
     }
     $Level = $Player->getLevel();
     $BuyingTile = $BuyingInv->getHolder();
     $added = [];
     foreach ($Transaction->getTransactions() as $t) {
         foreach ($this->traderInvTransaction($t) as $nt) {
             $added[] = $nt;
         }
     }
     $event->setCancelled(true);
     $X = $BuyingTile->getX();
     $Y = $BuyingTile->getY();
     $Z = $BuyingTile->getZ();
     $Slot = $added[0]->getSlot();
     $SourceItem = $added[0]->getSourceItem();
     $TargetItem = $added[0]->getTargetItem();
     switch ($this->plugin->getState("buying_type", $Player, null)) {
         case 0:
             if ($Y == 0) {
                 if ($Level->getBlockIdAt($X, $Slot + 1, $Z) != 54) {
                     $Level->setBlock(new Vector3($X, $Slot + 1, $Z), Block::get(54), true, true);
                     $chest = new Chest($Level->getChunk($X >> 4, $Z >> 4, true), new Compound(false, array(new Int("x", $X), new Int("y", $Y + 1), new Int("z", $Z), new String("id", Tile::CHEST))), $this->plugin);
                     $Level->addTile($chest);
                 } else {
                     $chest = $Level->getTile(new Vector3($X, $Y + 1, $Z));
                 }
                 if ($Level->getBlockIdAt($X, $Slot + 2, $Z) != 54) {
                     $Level->setBlock(new Vector3($X, $Slot + 2, $Z), Block::get(0), true, true);
                 }
                 $chest = new BuyingInventory($chest, $Player);
                 $contents = [];
                 foreach ($this->plugin->buys_Values[$Slot][1] as $j => $buy) {
                     $contents[] = Item::get($buy[1], $buy[2], $buy[3]);
                 }
                 $chest->setContents($contents);
                 $this->plugin->setState("buying_chest", $Player, $chest);
                 $this->plugin->setState("buying_menu", $Player, $Slot);
                 $Player->addWindow($chest);
             } else {
                 $this->plugin->game->Buy($Player, $this->plugin->getState("buying_menu", $Player, -1), $Slot);
             }
             break;
         case 1:
             $this->plugin->SelectTeam($Player, $Slot - 1, $TargetItem);
             break;
         case 2:
             $Item = $BuyingInv->getItem($Slot);
             if (($i = array_search($Item->getId(), [336, 265, 266])) !== false && ($t = $this->plugin->spawner_gives[["b", "i", "g"][$i]])) {
                 if (!isset($this->plugin->game->PopupInfo2->PlayersData[strtolower($Player->getName())][1])) {
                     $this->plugin->game->PopupInfo2->PlayersData[strtolower($Player->getName())][0] = TextFormat::GREEN;
                     $this->plugin->game->PopupInfo2->PlayersData[strtolower($Player->getName())][1] = 0;
                 }
                 $this->plugin->game->PopupInfo2->PlayersData[strtolower($Player->getName())][1] += $Item->getCount() * $t;
                 $BuyingInv->clear($Slot);
             } else {
                 $event->setCancelled(false);
             }
             break;
     }
 }
 public function onTransaction(InventoryTransactionEvent $event)
 {
     if (!$this->isPlaying()) {
         /** @var ChestInventory|null $chest */
         $chest = null;
         foreach ($event->getTransaction()->getInventories() as $inv) {
             if ($inv instanceof ChestInventory) {
                 $chest = $inv;
             }
         }
         if ($chest !== null) {
             $chest->close($this->getPlayer());
         }
         return false;
     }
     return true;
 }
Exemple #6
0
 public function onTransaction(InventoryTransactionEvent $event)
 {
     $trans = $event->getTransaction()->getTransactions();
     $inv = $event->getTransaction()->getInventories();
     $player = null;
     $chestBlock = null;
     foreach ($trans as $t) {
         foreach ($inv as $inventory) {
             $chest = $inventory->getHolder();
             if ($chest instanceof Chest) {
                 $chestBlock = $chest->getBlock();
                 $transaction = $t;
             }
             if ($chest instanceof Player) {
                 $player = $chest;
             }
         }
     }
     if ($player != null && $chestBlock != null && isset($transaction)) {
         if ($this->inArena($player)) {
             $config = new Config($this->getDataFolder() . "shop.yml", Config::YAML);
             $all = $config->get("Shop");
             /*
             if(in_array($transaction->getTargetItem()->getId(), $all)){
                 $this->isShopping[$player->getName()] = "ja";
             }
             */
             $arena = $this->getArena($player);
             $chestTile = $player->getLevel()->getTile($chestBlock);
             if ($chestTile instanceof Chest) {
                 $TargetItemID = $transaction->getTargetItem()->getId();
                 $TargetItemDamage = $transaction->getTargetItem()->getDamage();
                 $TargetItem = $transaction->getTargetItem();
                 $inventoryTrans = $chestTile->getInventory();
                 if ($this->isShopping[$player->getName()] != "ja") {
                     $zahl = 0;
                     for ($i = 0; $i < count($all); $i += 2) {
                         if ($TargetItemID == $all[$i]) {
                             $zahl++;
                         }
                     }
                     if ($zahl == count($all)) {
                         $this->isShopping[$player->getName()] = "ja";
                     }
                 }
                 if ($this->isShopping[$player->getName()] != "ja") {
                     $secondslot = $inventoryTrans->getItem(1)->getId();
                     if ($secondslot == 384) {
                         $this->isShopping[$player->getName()] = "ja";
                     }
                 }
                 if ($this->isShopping[$player->getName()] == "ja") {
                     if ($TargetItemID == Item::WOOL && $TargetItemDamage == 14) {
                         $event->setCancelled(true);
                         $config = new Config($this->getDataFolder() . "shop.yml", Config::YAML);
                         $all = $config->get("Shop");
                         $chestTile->getInventory()->clearAll();
                         for ($i = 0; $i < count($all); $i = $i + 2) {
                             $slot = $i / 2;
                             $chestTile->getInventory()->setItem($slot, Item::get($all[$i], 0, 1));
                         }
                     }
                     $TransactionSlot = 0;
                     for ($i = 0; $i < $inventoryTrans->getSize(); $i++) {
                         if ($inventoryTrans->getItem($i)->getId() == $TargetItemID) {
                             $TransactionSlot = $i;
                             break;
                         }
                     }
                     $secondslot = $inventoryTrans->getItem(1)->getId();
                     if ($TransactionSlot % 2 != 0 && $secondslot == 384) {
                         $event->setCancelled(true);
                     }
                     if ($TargetItemID == 384) {
                         $event->setCancelled(true);
                     }
                     if ($TransactionSlot % 2 == 0 && $secondslot == 384) {
                         $Kosten = $inventoryTrans->getItem($TransactionSlot + 1)->getCount();
                         $yourmoney = $player->getExpLevel();
                         if ($yourmoney >= $Kosten) {
                             $money = $yourmoney - $Kosten;
                             $player->setExpLevel($money);
                             $player->getInventory()->addItem(Item::get($inventoryTrans->getItem($TransactionSlot)->getId(), $inventoryTrans->getItem($TransactionSlot)->getDamage(), $inventoryTrans->getItem($TransactionSlot)->getCount()));
                         }
                         $event->setCancelled(true);
                     }
                     if ($secondslot != 384) {
                         $event->setCancelled(true);
                         $config = new Config($this->getDataFolder() . "shop.yml", Config::YAML);
                         $all = $config->get("Shop");
                         for ($i = 0; $i < count($all); $i += 2) {
                             if ($TargetItemID == $all[$i]) {
                                 $chestTile->getInventory()->clearAll();
                                 $suball = $all[$i + 1];
                                 $slot = 0;
                                 for ($j = 0; $j < count($suball); $j++) {
                                     $chestTile->getInventory()->setItem($slot, Item::get($suball[$j][0], 0, $suball[$j][1]));
                                     $slot++;
                                     $chestTile->getInventory()->setItem($slot, Item::get($suball[$j][2], 0, $suball[$j][3]));
                                     $slot++;
                                 }
                                 break;
                             }
                         }
                         $chestTile->getInventory()->setItem($chestTile->getInventory()->getSize() - 1, Item::get(Item::WOOL, 14, 1));
                     }
                 }
             }
         }
     }
 }
 public function execute()
 {
     /** @var Transaction[] */
     $failed = [];
     while (!$this->transactionsToRetry->isEmpty()) {
         //Some failed transactions are waiting from the previous execution to be retried
         $this->transactionQueue->enqueue($this->transactionsToRetry->dequeue());
     }
     if (!$this->transactionQueue->isEmpty()) {
         $this->player->getServer()->getPluginManager()->callEvent($ev = new InventoryTransactionEvent($this));
     } else {
         return;
     }
     while (!$this->transactionQueue->isEmpty()) {
         $transaction = $this->transactionQueue->dequeue();
         if ($ev->isCancelled()) {
             $this->transactionCount -= 1;
             $transaction->sendSlotUpdate($this->player);
             //Send update back to client for cancelled transaction
             unset($this->inventories[spl_object_hash($transaction)]);
             continue;
         } elseif (!$transaction->execute($this->player)) {
             $transaction->addFailure();
             if ($transaction->getFailures() >= self::DEFAULT_ALLOWED_RETRIES) {
                 /* Transaction failed completely after several retries, hold onto it to send a slot update */
                 $this->transactionCount -= 1;
                 $failed[] = $transaction;
             } else {
                 /* Add the transaction to the back of the queue to be retried on the next tick */
                 $this->transactionsToRetry->enqueue($transaction);
             }
             continue;
         }
         $this->transactionCount -= 1;
         $transaction->setSuccess();
         $transaction->sendSlotUpdate($this->player);
         unset($this->inventories[spl_object_hash($transaction)]);
     }
     foreach ($failed as $f) {
         $f->sendSlotUpdate($this->player);
         unset($this->inventories[spl_object_hash($f)]);
     }
 }