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

Disable specific item drop on dead

Discussion in 'Development' started by WhoAreMe, May 1, 2020.

  1. WhoAreMe

    WhoAreMe Witch

    Messages:
    55
    Hello, Im having trouble disabling specific item on dead here is my code.
    $drops = $event->getDrops();

    if (($key = array_search( Item::get(Item::DIRT, 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.
     
  2. WhoAreMe

    WhoAreMe Witch

    Messages:
    55
    no response?
     
  3. HiToLaKhanh

    HiToLaKhanh Spider Jockey

    Messages:
    25
    GitHub:
    HiToLaKhanh
    get player first
     
  4. HiToLaKhanh

    HiToLaKhanh Spider Jockey

    Messages:
    25
    GitHub:
    HiToLaKhanh
    PHP:
    $player $event->getPlayer();
    fix your code:
    PHP:
    $player->setDeathMessage("You died");
    PHP:
    $player->setDrops($drops);
     
  5. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    That won't work, the problem is that you can't compare item objects with the equals operator, so array search won't work.
     
    GamakCZ likes this.
  6. WhoAreMe

    WhoAreMe Witch

    Messages:
    55
    $event->setDeathMessage("please work");
    i just used this command for debugging lol.
    What i want to do is to specific item drop on death.
     
  7. WhoAreMe

    WhoAreMe Witch

    Messages:
    55
    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
     
  8. HiToLaKhanh

    HiToLaKhanh Spider Jockey

    Messages:
    25
    GitHub:
    HiToLaKhanh
    ya, i really don't know what's he write
     
  9. WhoAreMe

    WhoAreMe Witch

    Messages:
    55
    Then how do i do it. Im new to php. Can u give me a code that can remove value from object in array?
     
  10. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    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);
     }
     
    ethaniccc, WhoAreMe and GamakCZ like this.
  11. WhoAreMe

    WhoAreMe Witch

    Messages:
    55
    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.
     
  12. WhoAreMe

    WhoAreMe Witch

    Messages:
    55
    I look at array filter and i dont understand how this thing work. Can u explain it to me a noob guy
     
  13. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    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.
     
    WhoAreMe likes this.
  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.