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

setItem when Inventory is full!

Discussion in 'Development' started by linuzo, Nov 20, 2017.

  1. linuzo

    linuzo Spider

    Messages:
    9
    GitHub:
    linuzo
    Looks Like i'm getting some errors on setting an item when inventory is full when a player kills another player is there a work around for this??

    This is my code

    public function onPlayerKill(PlayerDeathEvent $event){
    $player = $event->getEntity();
    $prefix = Main::pREFIX;
    if ($player instanceof Player)
    {
    $cause = $player->getLastDamageCause();
    if($cause instanceof EntityDamageByEntityEvent)
    {
    $damager = $cause->getDamager();
    if($damager instanceof Player)
    {
    $item = Item::get(Item::COOKIE);
    $item2 = Item::get(Item::DIAMOND);
    $pN = $player->getName();
    $dN = $damager->getName();
    EconomyAPI::getInstance()->addMoney($damager, 50);
    $damager->sendMessage($this->plugin->translateColors( "&", $prefix ."&eYou earned a player head and 50Kb for killing " . $pN."!"));
    $player->sendMessage($this->plugin->translateColors( "&", $prefix ."&eYou have been killed by " . $dN ."!"));
    $damager->getInventory()->setItem($damager->getInventory()->firstEmpty(), $item);
    $damager->getInventory()->setItem($damager->getInventory()->firstEmpty(), $item2);
    }
    }
     
  2. linuzo

    linuzo Spider

    Messages:
    9
    GitHub:
    linuzo
    However sometimes I guess firstEmpty cannot be found so there is an error on the console.. spams some stuff..
     
  3. Ant

    Ant Spider

    Messages:
    9
    GitHub:
    Dead
    PHP:
    array_map([$item$item2], function($item) {
         if(
    $fe $damager->getInventory()->firstEmpty()) {
               
    $damager->getInventory()->setItem($fe$item);
         } else {
               
    $damager->sendPopup(TextFormat::RED."Inventory Full");
         }
    });
    You can use foreach loop and then break out of it, if first attempt to add item failed because second won't succeed for sure.
     
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    setItem() will set the item in the slot without checking whether inventory is full (because it doesnt make sense to check whether the inventory is full or not while you're overriding an item in a slot)
    Are you confusing yourself with addItem()?
     
  5. linuzo

    linuzo Spider

    Messages:
    9
    GitHub:
    linuzo
    So I should use addItem instead then??? @Muqsit
     
  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.