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

Bug?

Discussion in 'Development' started by SergeyIvanov, Jun 8, 2017.

  1. SergeyIvanov

    SergeyIvanov Witch

    Messages:
    59
    GitHub:
    sergeyivanov14
    Problem description.
    I use InventoryTransactionEvent and I check if player click the item with id 264, server send message to player, but when server send message is duplicated.

    How to reproduce the bug.
    Use my code:

    public function transactionEvent(\pocketmine\event\inventory\InventoryTransactionEvent $e){
    foreach($e->getTransaction()->getTransactions() as $transaction){
    foreach($e->getTransaction()->getInventories() as $inv){
    if($inv->getHolder() instanceof \pocketmine\Player) $p = $inv->getHolder();
    elseif($inv->getHolder() instanceof \pocketmine\tile\Chest) $chest = $inv->getHolder();
    if($transaction->getTargetItem()->getId() == 264){
    $p->sendMessage("message");
    }}}}
     
  2. Miste

    Miste Baby Zombie

    Messages:
    109
    GitHub:
    Misteboss
    Use ["PHP"]["/PHP"] tags please
     
  3. SergeyIvanov

    SergeyIvanov Witch

    Messages:
    59
    GitHub:
    sergeyivanov14
    PHP:
    public function transactionEvent(\pocketmine\event\inventory\InventoryTransactionEvent $e){
    foreach(
    $e->getTransaction()->getTransactions() as $transaction){
    foreach(
    $e->getTransaction()->getInventories() as $inv){
    if(
    $inv->getHolder() instanceof \pocketmine\Player$p $inv->getHolder();
    elseif(
    $inv->getHolder() instanceof \pocketmine\tile\Chest$chest $inv->getHolder();
    if(
    $transaction->getTargetItem()->getId() == 264){
    $p->sendMessage("message");
    }}}}
     
  4. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Just add break; below the $p->sendMessage("message");. I'm not sure why you foreach the inventories, but you probably have your reasons.
     
    SOFe and corytortoise like this.
  5. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    It's my understanding that when you take an item from a chest, 2 transactions are made. 1) The item is removed from the Chest, 2) The item is added to the Player's inventory. So when you foreach and check if the target item has a specific ID, your message is sent twice.
     
    Sandertv likes 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.