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

PMMP issue , InventoryTransactionEvent() stop being called

Discussion in 'Development' started by tungstenvm, Mar 22, 2020.

  1. tungstenvm

    tungstenvm Witch

    Messages:
    54
    if i cancelled the event when a actions' inventory is instance of PlayerInventory, the event will stop being called , it's random, after you make the problem happened, the event sometimes get called, sometimes not

    Here is the code to make the issue happens:
    PHP:
    use pocketmine\event\inventory\InventoryTransactionEvent;
    use 
    pocketmine\inventory\transaction\action\SlotChangeAction;
    use 
    pocketmine\inventory\PlayerInventory;

    public function 
    onTransaction(InventoryTransactionEvent $ev) : void{
        
    var_dump("Event called");
        
    $acts array_values($ev->getTransaction()->getActions());
        if(
    $acts[0] instanceof SlotChangeAction && $acts[1] instanceof SlotChangeAction){
          if(
    $acts[0]->getInventory() instanceof PlayerInventory || $acts[1]->getInventory() instanceof PlayerInventory){
            
    $ev->setCancelled();
          }
    }
    This issue only happens in the mc win10;
    You can place a chest with items inside on the server before running that code, then when you runs the server and the code
    So when you click the item, your cursor has the item,the console print "Event Called",when you put the item in your cursor into player inventory , the console still print "Event Called", but the item disppear both in your cursor and the inventory, for now it just the lag (if you close the inven then open again the item still there), after that, when you do again with other items, the console sometimes print, sometimes not, try to do it mutiple item , not only one

    im not sure it's just me or everyone get that or this problem is just like the item still in the cursor even the event is cancelled so can i fix this
     

    Attached Files:

  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    $event->getTransaction()->getActions() does not maintain an order. You have to foreach over all actions.
     
    HimbeersaftLP likes this.
  3. tungstenvm

    tungstenvm Witch

    Messages:
    54
    $acts can have more then 2 values but in this case that's not the point causing the bug.if u stimulate the issue $acts only returns 2 values
     
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Like I said, $event->getTransaction()->getActions() does not maintain an order.
    You are only fetching one action from the array of actions. You might as well be fetching a random action from the two (or more) actions in the array and checking if it satisfies your condition. That's how unreliable it is going to be if you aren't checking all actions.
    A foreach is unavoidable. https://github.com/Muqsit/InvMenu/blob/master/src/muqsit/invmenu/InvMenuEventHandler.php#L80

    Try before criticizing ¯\_()_/¯
     
    HimbeersaftLP 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.