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:REFIX; 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:IAMOND); $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); } }
However sometimes I guess firstEmpty cannot be found so there is an error on the console.. spams some stuff..
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.
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()?