Cancelling Death Event

Discussion in 'Plugin Development' started by magicced01, Mar 25, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    magicced01

    Is there a way to cancel a PlayerDeathEvent in Bukkit? I can't use event.setCanceled(true)


    Code:
        public void onDamage(EntityDamageByEntityEvent e)
        {
           
            if( e.getEntity() instanceof Player)
            {
                 Player p = (Player) e.getEntity();
            if(p.getHealth() - e.getDamage() < 1)
             {
                e.setCancelled(true);
                p.performCommand(MyClasses.PlayerClassCache.get(e.getEntity().getName()));
                p.teleport(p.getWorld().getSpawnLocation());
              
                p.sendMessage("Du wurdest von "+e.getDamager().getName()+ " getötet" );
                Bukkit.broadcastMessage("§6"+e.getEntity().getName()+" §fwurde von "+ "§c"+e.getDamager().getName()+" §fgetötet" );
             }
          }
        }
    this is the event im using. Im writing something like a PvP Classes Plugin, but my Problem is, one of my classes has a power 5 bow. If he now hits someone with enchanted diamond armor, normally the diamond Class wouldnt take really much Damage. But instead he is oneshot. Thats probably because i substract the Damage dealt from the players health and the armor is being ignored. Does anyone of you know a better way of how to solve my problem?

    Ced
     
  2. Offline

    Funergy

    @magicced01
    e.getPlayer().setHealth(e.getPlayer().getMaxHealth());

    EDIT: Make sure to have Bukkit in your buildpath not CraftBukkit
     
  3. Offline

    magicced01

    That doesnt work since the event doesn't get cancelled so you still die
     
  4. Offline

    DemKazoo

    You can't cancel the death of a player unless you use packets, if you would like to use packets I'd recommend to use ProtocolLib, if you don't feel like it you could cancel the entity damage event if the players health is below the amount you'd want it to be, example:


    Code:
    @EventHandler
    public void kill(EntityDamageByEnityEvent e) {
      if(e.getEntity().getHealth()  < 5 ) {
         e.getEntity().setHealth(20D);
      }
      else if(!e.getEntity().getHealth()  < 5 ) {
      return;
    }
     
    //getHealth() will be undefined for e.getEntity() so you would've to cast Player to it
    Please note, I don't know if those code is 100% functioning because I wrote this without IDE and their might be syntax errors, if so don't bash on me and just tell me where the issue is located at.
     
  5. Offline

    Konato_K

    @DemKazoo This won't work because the damage is not applied yet, which means the player it's alive and their health won't be 0


    @magicced01 The best way it's to use the EntityDamageEvent and check if their current health minus the final damage of the event will be less or equal to 0.
     
  6. Offline

    DemKazoo

    @Konato_K If I'am correct, the event only applies when the entity is damaged by another entity, so yes the damage does get applied, thats where the event is for.
     
  7. Offline

    Konato_K

    @DemKazoo That's EntityDamageByEntityEvent, EntityDamageEvent gets called whenever an entity gets damage (regardless of the source), and yes, the damage is applied, but it's applief after the event goes through all the event handlers.
     
    DemKazoo and teej107 like this.
  8. Offline

    DemKazoo

    @Konato_K Oh deng, I didn't read properly, never saw it was EntityDamageEvent. Thanks for the heads up ;P
     
Thread Status:
Not open for further replies.

Share This Page