Hello, Im having trouble disabling specific item on dead here is my code. $drops = $event->getDrops(); if (($key = array_search( Item::get(Item:IRT, 0, 1), $drops)) !== false) { unset($drops[$key]); $event->setDeathMessage("please work"); } $event->setDrops($drops); Im still new to php and i i cant find a function that can remove item on drop thats why i want to remove an array using key but it just dont work.
PHP: $player = $event->getPlayer(); fix your code: PHP: $player->setDeathMessage("You died"); PHP: $player->setDrops($drops);
That won't work, the problem is that you can't compare item objects with the equals operator, so array search won't work.
$event->setDeathMessage("please work"); i just used this command for debugging lol. What i want to do is to specific item drop on death.
Im using playerdeathevent $drops = $event->getDrops(); // get array drop item[] if (($key = array_search( Item::get(Item: DIRT, 0, 1), $drops)) !== false) { unset($drops[$key]); //what i want to do is remove an array value } $event->setDrops($drops); //back the drops with removed value
You can use array_filter: PHP: public function onDeath(PlayerDeathEvent $event) { $drops = $event->getDrops(); // Remove all dirt from the drops $drops = array_filter($drops, function (Item $item) { return $item->getId() !== Item::DIRT; }); $event->setDrops($drops); }
Thanks for that! Finally someone that understand. So array filter is what im missing. Tho i still dont know why mine dont work. Maybe because its object.
array_filter goes through each element in the array, executes the function given as the second parameter, and keeps only the elements where the function returns true. array_search goes through the array until it encounter an element that equals the element given as the first parameter. In php, objects equal (with unstrict comparison, aka ==) when all properties are the same. In your case, this would only be able to detect a stack with only one dirt block. That's why my code only compares the ID in the function I give to array_filter.