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

Solved BedWars Shop

Discussion in 'Development' started by SkyZone, Feb 4, 2018.

  1. SkyZone

    SkyZone Slime

    Messages:
    95
    How to create the listener, so it works?
    I tried several ways to create the InventoryTransactionEvent, but everytime it only worked for windows 10 players...
    This is my code:
    PHP:
    public function onInventoryTransaction(InventoryTransactionEvent $event) {
            
    $trans $event->getTransaction()->getActions();

            foreach(
    $trans as $t) {

                
    $inv $t->getInventory();

                if(
    $inv instanceof ChestInventory) {

                    foreach(
    $inv->getViewers() as $assumed) {

                        if(
    $assumed instanceof Player) {

                            
    $p $assumed;
                            
    $cinv $inv;
                            
    $action $t;
                            break;
                        }

                    }

                }

                if(
    $inv == null) return;
                
    $e->setCancelled();
                
    $item $action->getTargetItem();

                if(
    $item->getName() === "Test") {

                    
    $sender->sendMessage("Whats this");

                }

            }

        }
     
  2. TwistedAsylumMC

    TwistedAsylumMC Slime

    Messages:
    96
    GitHub:
    twistedasylummc
    transaction event is different for each OS
     
  3. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    See how InvMenu does it
    https://github.com/Muqsit/InvMenu/b...431c/src/muqsit/invmenu/EventListener.php#L60

    $inventoryAction->getSourceItem() is the item the player clicks in the chest inventory and $playerAction->getSourceItem() is the item the player puts in the chest inventory.

    Also, note.
    The code you're using is a very old code I wrote for ChestShop, it's only compatible with ALPHA7 or ALPHA9.
     
  5. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    I had same bug, there are problem with two actions, at the android works first, and at the windows works second. You can get functional action by target item id.

    PHP:
    public function onInventoryTransaction(InventoryTransactionEvent $event) {
            
    $trans $event->getTransaction()->getActions();

            foreach(
    $trans as $t) {

                
    $inv $t->getInventory();

                if(
    $inv instanceof ChestInventory) {

                    foreach(
    $inv->getViewers() as $assumed) {

                        if(
    $assumed instanceof Player && $t->getTargetItem()->getId() !== 0) {

                            
    $p $assumed;
                            
    $cinv $inv;
                            
    $action $t;
                            break;
                        }

                    }

                }

                if(
    $inv == null) return;
              
                
    $event->setCancelled();
                
    $item $action->getTargetItem();

                if(
    $item->getName() === "Test") {

                    
    $event->getPlayer()->sendMessage("Whats this");

                }

            }
        }
     
    Last edited: Feb 5, 2018
  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.