Solved Adding money onDeath

Discussion in 'Development' started by Clik, Jul 8, 2019.

  1. Clik

    Clik Silverfish

    Messages:
    16
    GitHub:
    legofan48737
    So, im trying to add money when a player dies ($10 specifically), using EconomyAPI. My code is
    PHP:
    public function onDeath(PlayerDeathEvent $event) {
            
    $player $event->getPlayer();
            
    $name $player->getName();
            
    $event->setDeathMessage("§0• §7[§4X§7]§f " $name);
            
    $cause $event->getEntity()->getLastDamageCause(); // Thanks IZeoGamer !
            
    if ($cause->getCause() != 1) return; //not killed by entity
            
    if (!$cause instanceof EntityDamageByEntityEvent) return; //double check
            
    if ($cause->getDamager() instanceof Player) {
                
    $killer $cause->getDamager();
                
    $this->eco $this->getServer()->getPluginManager()->getPlugin("EconomyAPI");
                
    $this->eco->addMoney($killer->getName(), 10);
            }
        }
    I added the listener, and the Death Message works fine.
    Full code is at https://github.com/LegoFan48737/OrionListeners/blob/master/src/Clik/OrionListeners/Main.php
    am i misunderstanding how to use EconomyAPIs methods?
     
  2. iCirgio

    iCirgio Slime

    Messages:
    87
    GitHub:
    lolnova
    try this
    PHP:
    public function onDeath(PlayerDeathEvent $event) {
            
    $player $event->getPlayer();
            
    $name $player->getName();
            
    $event->setDeathMessage("§0• §7[§4X§7]§f " $name);
            
    $cause $event->getEntity()->getLastDamageCause();
            if (
    $cause instanceof EntityDamageByEntityEvent){
                if (
    $cause->getDamager() instanceof Player) {
                    
    $killer $cause->getDamager();
                    
    $this->eco $this->getServer()->getPluginManager()->getPlugin("EconomyAPI");
                    
    $this->eco->addMoney($killer10);
            }
        }
    }
     
  3. Clik

    Clik Silverfish

    Messages:
    16
    GitHub:
    legofan48737
    Tried that, still doesn't work @iCirgio
     
  4. KielKing

    KielKing Zombie

    Messages:
    251
    GitHub:
    kielking
    try this, and make sure you have all your imports
    PHP:
    /**
    * @param PlayerDeathEvent $event
    * @throws \ReflectionException
    * @priority HIGHEST
    * @ignoreCancelled TRUE
    */
    public function onDeath(PlayerDeathEvent $event): void{
        
    $player $event->getPlayer();
        
    $name $player->getName();
        
    $event->setDeathMessage("§0• §7[§4X§7]§f " $name);
        
    $cause $player->getLastDamageCause();
        if(
    $cause instanceof EntityDamageByEntityEvent){
            
    $damager $cause->getDamager();
            if(
    $damager instanceof Player){
                
    EconomyAPI::getInstance()->addMoney($damager10);
            }
        }
    }
     

Share This Page

  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.