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

Giving an item when killing a mob

Discussion in 'Development' started by Astro, Aug 21, 2018.

  1. Astro

    Astro Slime

    Messages:
    78
    GitHub:
    RealAstro
    Is there a way to give a player an item when killing a mob or a monster?
     
  2. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    EntityDamageByEntityEvent

    Check if the health of the damaged entity minus the final health is <= 0

    If so, check if the damager is a player. Then $event->getDamager()->getInventory()->addItem(Item::get(3, 0, 1));
     
  3. Astro

    Astro Slime

    Messages:
    78
    GitHub:
    RealAstro
    can you please give me the full if statements?
     
  4. Astro

    Astro Slime

    Messages:
    78
    GitHub:
    RealAstro
    because i'm tryna make it when you kill a zombie you get a certain reward and when killing another mob you get another reward
     
  5. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Have you listened to events before? If so, listen to EntityDeathEvent and check if the entity is a zombie. If it is, check if Zombie->getLastDamageCause() is an instanceof EntityDamageByEntityEvent and whether the damager is an instanceof Player.
     
  6. Astro

    Astro Slime

    Messages:
    78
    GitHub:
    RealAstro
  7. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Btw, don't forget to import the correct class namespaces. PHP doesn't throw errors when a class doesn't exist while doing an instanceof check for object instanceof class.
     
  8. Astro

    Astro Slime

    Messages:
    78
    GitHub:
    RealAstro
    correct me if i'm wrong

    if($cause instanceof EntityDeathEvent){

    $damager = $cause->getDamager();

    if($damager instanceof Player){
     
  9. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    if($cause instanceof EntityDamageByEntityEvent){

    and $cause is an instance of EntityDamageEvent
     
  10. Astro

    Astro Slime

    Messages:
    78
    GitHub:
    RealAstro
    public function onDeath(EntityDeathEvent $ev){

    $cause = $player->getLastDamageCause();

    if($cause instanceof EntityDamageByEntityEvent){

    $damager = IronGolem->getLastDamageCause()

    if($damager instanceof Player){

    $inv = $damager->getInventory();
    $IGHead = Item::get(397,0,1);
    $name = $player->getName();
    $IGHead->setCustomName(TF:: DARK_PURPLE . "Iron Golem Head"));
    $inv->addItem($IGead);
     
  11. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    You missed a lot of } and $player is undefined

    EDIT: Use the insert code button and select the PHP so the code would be readable by us.
     
  12. Astro

    Astro Slime

    Messages:
    78
    GitHub:
    RealAstro
    i know i just wanted to know if that part of the code is correct i have $player defined and } added
     
  13. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    Oh, it isn't the full code then. It may work.
     
  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.