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

Solved Error - Why?

Comments in 'Plugin Development' started by CraftYourBukkit, Nov 8, 2015.

  1. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Hey. I am using InventoryTransactionEvent for a shop, but there is an error and I don't know how to fix it.
    PHP:
    public function onTransaction(InventoryTransactionEvent $event) {
            
    $trans $event->getTransaction()->getTransactions();
            
    $int $event->getTransaction()->getInventories();
            
    $isChest false;
            
    $isPlayer false;
            
    $player;
            
    $chestNew;
            
    $realTrans;
            foreach (
    $trans as $t) {
                foreach (
    $int as $chest) {
                    if (!(
    $chest instanceof ChestInventory) && !($chest instanceof PlayerInventory)) {
                        return;
                    }
                    
    $chest $chest->getHolder();
                    if (
    $chest instanceof Chest) {
                        
    $chestNew $chest->getBlock();
                        
    $isChest true;
                        
    $realTrans $t;
                    }
                    if (
    $chest instanceof Player) {
                        
    $player $chest;
                        
    $isPlayer true;
                    }
                }
            }
            if (
    $isPlayer == true && $isChest == true) {
                
    $levelname $player->getLevel()->getFolderName();
                if (
    in_array($levelname$this->arenas)) {
                    
    $mainChest $player->getLevel()->getTile($chestNew);
                    if (
    $mainChest instanceof Chest) {
                        
    $this->getServer()->broadcastMessage("CHEST");
                        
    $itemTrans $realTrans->getTargetItem()->getId();
                        
    $realItemTrans $realTrans->getTargetItem();
                        
    $realItemTrans->setCount(1);
                        
    $inventoryTrans $mainChest->getInventory(); // Error here
                        
    if ($realItemTrans->getId() == Item::WOOL && $realItemTrans->getDamage() == 14) {
                            
    $event->setCancelled(true);
                            
    $config = new Config($this->getDataFolder() . "/config.yml"Config::YAML);
                            
    $all $config->get("buyable");
                            
    $mainChest->getInventory()->clearAll();
                            for (
    $i 0$i count($all); $i $i 2) {
                                
    $slot $i 2;
                                
    $mainChest->getInventory()->setItem($slotItem::get($all[$i], 01));
                            }
                        } else {
                            
    $slotTrans 0;
                            for (
    $i 0$i $inventoryTrans->getSize(); $i++) {
                                if (
    $inventoryTrans->getItem($i)->getId() == $itemTrans) {
                                    
    $slotTrans $i;
                                    break;
                                }
                            }
                            
    $secondslot $inventoryTrans->getItem(1)->getId();
                            if (
    $slotTrans != && ($secondslot == Item::BRICK || $secondslot == Item::IRON_INGOT || $secondslot == Item::GOLD_INGOT)) {
                                
    $event->setCancelled(true);
                            }
                            if (
    $itemTrans == Item::BRICK || $itemTrans == Item::IRON_INGOT || $itemTrans == Item::GOLD_INGOT) {
                                
    $event->setCancelled(true);
                            }
                            if (
    $slotTrans == && ($secondslot == Item::BRICK || $secondslot == Item::IRON_INGOT || $secondslot == Item::GOLD_INGOT)) {
                                
    $iCost $inventoryTrans->getItem($slotTrans 1)->getId();
                                
    $dCost $inventoryTrans->getItem($slotTrans 1)->getDamage();
                                
    $aCost $inventoryTrans->getItem($slotTrans 1)->getCount();

                                
    $aoI $player->getInventory()->all(Item::get($iCost$dCost));
                                
    $yourmoney 0;
                                foreach (
    $aoI as $currently) {
                                    
    $yourmoney += $currently->getCount();
                                }
                                if (
    $yourmoney >= $aCost) {
                                    for (
    $i 0$i $player->getInventory()->getMaxStackSize(); $i++) {
                                        if (
    $player->getInventory()->getItem($i)->getId() == $iCost) {
                                            
    $itemToRemove $player->getInventory()->getItem($i);
                                            if (
    $itemToRemove->getCount() <= 1) {
                                                
    $player->getInventory()->clear($i);
                                            } else {
                                                
    $player->getInventory()->setItem($iItem::get($iCost0$itemToRemove->getCount() - $aCost));
                                            }
                                            break;
                                        }
                                    }
                                    
    $player->getInventory()->addItem(clone Item::get($inventoryTrans->getItem($slotTrans)->getId(), $inventoryTrans->getItem($slotTrans)->getDamage(), $inventoryTrans->getItem($slotTrans)->getCount()));
                                }
                                
    $event->setCancelled(true);
                            }
                            if (
    $secondslot != Item::BRICK && $secondslot != Item::IRON_INGOT && $secondslot != Item::GOLD_INGOT) {
                                
    $event->setCancelled(true);
                                
    $config = new Config($this->getDataFolder() . "/config.yml"Config::YAML);
                                
    $all $config->get("buyable");
                                for (
    $i 0$i count($all); $i+=2) {
                                    if (
    $itemTrans == $all[$i]) {
                                        
    $mainChest->getInventory()->clearAll();
                                        
    $suball $all[$i 1];
                                        
    $slot 0;
                                        for (
    $j 0$j count($suball); $j++) {
                                            
    $mainChest->getInventory()->setItem($slotItem::get($suball[$j][0], 0$suball[$j][1]));
                                            
    $slot++;
                                            
    $mainChest->getInventory()->setItem($slotItem::get($suball[$j][2], 0$suball[$j][3]));
                                            
    $slot++;
                                        }
                                        break;
                                        return;
                                    }
                                }
                                
    $mainChest->getInventory()->setItem($mainChest->getInventory()->getSize() - 1Item::get(Item::WOOL141));
                            }
                        }
                    } else {
                        
    $this->getServer()->broadcastMessage("NO CHEST");
                    }
                }
            }
        }
    Error: Fatal error: Call to a member function getInventory() on null in /plugins/BedWars/src/BedWars/BedWars.php on line 421
  2. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    this is impossible because $mainChest is an instance of Chest so isn't null :/
  3. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    It's instanceof Chest as tile, not as block!
  4. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    i din't say it's a block , i said that $mainChest isn't NULL because it is an instanceof Chest
  5. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Well, it actually is null, that's what the error says and when debugging, it also returns false. But I don't know why :/
  6. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    He has a comment that says "Error here"... :p
  7. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Fixed the error.:3
  8. zombie_Power
    Offline

    zombie_Power Active Member

    Joined:
    Sep 7, 2015
    Posts:
    239
    Minecraft User:
    Zombie_Power
    I presume, the problem has to do with $chestNew ?
  9. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Yes, with the chest which is opened(Spawned it with another function)

Share This Page

Advertisement