Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to get the player on InventoryTransactionEvent?

Comments in 'Plugin Development' started by MyNameIsTriXz, Dec 26, 2015.

  1. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    How am I doing that?
  2. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    PHP:
    public function onInvTrans(InventoryTransactionEvent $event){
    $trans $event->getTransaction()->getTransactions();
            
    $int $event->getTransaction()->getInventories();
            foreach (
    $trans as $t) {
                foreach (
    $int as $chest) {
                    if (!(
    $chest instanceof ChestInventory) && !($chest instanceof PlayerInventory)) {
                        return;
                    }
                    
    $chest $chest->getHolder();
                    if (
    $chest instanceof Player) {
                        
    $player $chest;
                    }
                }
            }
    }
  3. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    It returns "Call to a member function on null on getName())"
  4. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Code?
  5. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    PHP:
    <?php

    namespace InventoryCommand;

    use 
    pocketmine\block\Block;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\level\Level;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\event\player\PlayerJoinEvent;
    use 
    pocketmine\command\ConsoleCommandSender;
    use 
    pocketmine\inventory\ChestInventory;
    use 
    pocketmine\inventory\PlayerInventory;
    use 
    pocketmine\event\server\ServerCommandEvent;
    use 
    SimpleAuth\event\PlayerAuthenticateEvent;
    use 
    pocketmine\event\player\PlayerItemHeldEvent;
    use 
    pocketmine\event\inventory\InventoryTransactionEvent;
    use 
    pocketmine\player\Player;
    class 
    Main extends PluginBase implements Listener{
        private 
    $auth;
        public 
    $playerinv;
        public 
    $player;
        public 
    $chestinv;
        public 
    $chest;

        public function 
    onEnable(){
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
    }
        public function 
    onDisable(){
            
    $this->getLogger()->info(TextFormat::RED "Chest Disabled!");
        }

      


    public function 
    InventoryTransfer(InventoryTransactionEvent $event){
            
    $trans $event->getTransaction()->getTransactions();
            
    $invs $event->getTransaction()->getInventories();
            foreach(
    $trans as $t){
                foreach(
    $invs as $inv){
            
                    if(
    $inv instanceof ChestInventory){
               
    $this->chest $inv->getHolder();
              
    $this->chestinv $inv;
                      }

        
               
    $inv $inv->getHolder();
                    if(
    $inv instanceof Player) {
                        
    $this->player $inv;
                    }
          

              
                
                        
                  
                
    $item $t->getTargetItem();
                
    $item2 $t->getSourceItem();
                if(
    $item->getId() === 0){
                    
    $item $item2;
                }
                
    $ChestX $this->chest->getX();
                
    $ChestY $this->chest->getY();
                
    $ChestZ $this->chest->getZ();
                
    $ChestLevel $this->chest->getBlock()->getLevel()->getName();       
                
    $name $item->getName();
                
    $id $item->getId();
                
    $damage $item->getDamage();
                
    $count $item->getCount();

                
    var_dump($name.":".$damage." ".$count." at ".$ChestX." ".$ChestY." ".$ChestZ." ".$ChestLevel." "$this->player->getName());

                }
            }
        }




  6. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Mind pointing out the line?
  7. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    It is just a little plugin....
  8. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    Did you even read the thread name before criticizing other people? There is no code!!!
  9. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    Then how are you getting an error? o_O
    applqpak and CraftYourBukkit like this.
  10. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    When I want to dump a text where $this->player->getName() is included
  11. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Understand the code and your error will be fixed.
  12. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Because you don't simply put the code into your plugin. You have to check that the holder is a player. @CraftYourBukkit's code already does it, but you are putting your code out of the checked part. If you still don't understand, learn PHP. We are not here to teach you the basics of PHP. This is the PocketMine Forums, not the PHP Forums.
  13. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    I did if($player instanceof Player){ then it cant be dumped
  14. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    if($chest instanceof Player){
    Doesnt even get executed...

Share This Page

Advertisement