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

Solved Simulate Death

Discussion in 'Development' started by David Flash, Aug 28, 2020.

  1. David Flash

    David Flash Spider Jockey

    Messages:
    26
    GitHub:
    davidflashcz
    Hey! I have been trying to Simulate Death Event in the reason to skip the death screen and make my actions after the death. Is there a way to do such a thing? I know I am not able to directly cancel the EntityDeathEvent.

    My Code:
    PHP:
    public function onDamage(EntityDamageEvent $event){
          
    $player $event->getEntity();
        if(
    $player instanceof Player){
            if(
    $event->getCause() === EntityDamageEvent::CAUSE_ENTITY_ATTACK){
            if(
    $event->getFinalDamage() >= $player->getHealth()){
                
    $event->setCancelled();
                
    $player->teleport($player->getLevel()->getSafeSpawn());
              }
            }
       }
    }
    Now, the code works, but I want it to act like the player actually died.
     
  2. Taco

    Taco Spider Jockey

    Messages:
    41
    GitHub:
    taconoburrito
    clear the players inventory
    set health and food to 20
    remove all effects
    and call playerdeathevent $this->getServer()->getPluginManager()->callEvent(new PlayerDeathEvent($player, [], ""));
     
  3. David Flash

    David Flash Spider Jockey

    Messages:
    26
    GitHub:
    davidflashcz
    Thanks :) Here is the code:
    PHP:
    public function onDamage(EntityDamageEvent $event){
          
    $player $event->getEntity();
        if(
    $player instanceof Player){
            if(
    $event->getCause() === EntityDamageEvent::CAUSE_ENTITY_ATTACK){
            if(
    $event->getFinalDamage() >= $player->getHealth()){
                
    $event->setCancelled();
                
    $player->teleport($player->getLevel()->getSafeSpawn());
                
    $this->getServer()->getPluginManager()->callEvent(new EntityDeathEvent($player, [], 1));
              }
            }
       }
    }
     
  4. Taco

    Taco Spider Jockey

    Messages:
    41
    GitHub:
    taconoburrito
    sure :)
     
  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.