Solved Cancel Death Event and get Killer

Discussion in 'Plugin Development' started by Domi381, Apr 6, 2013.

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

    Domi381

    Hey Guys,
    I am programming a plugin for a minigame. When someone is killed i want him to be teleported back to the Lobby without the respawn-screen (cancel Death Event?) and i want to get his killer for rewarding him. I don't know what kind of Events I must use (PlayerDeathEvent, EntityDamageEvent, ...) and how to cancel the death.

    Domi381
     
  2. Offline

    chasechocolate

    You can't cancel PlayerDeathEvent. You would need to listen for EntityDamageByEntityEvent, do player checks, and check if the player.getHealth() - event.getDamage() < 1, cancel the event and then teleport them.
     
    KingOfTheEast01 likes this.
  3. Offline

    Domi381

  4. Offline

    Compressions

    chasechocolate Just curious. How do you cancel the death event? I can't find a setCancelled boolean on PlayerDeathEvent.
    Domi381 Will you post your code?
     
  5. Offline

    chasechocolate

    Oh lol I meant you *can't cancel PlayerDeathEvent. Thanks for pointing that out :3
     
  6. Offline

    Compressions

    chasechocolate Np. I ran this code:
    Code:java
    1. @EventHandler
    2. public void onDamage(EntityDamageByEntityEvent e) {
    3. Player p = (Player) e.getEntity();
    4. if(p.getHealth() < 1) {
    5. e.setCancelled(true);
    6. p.teleport(p.getWorld().getSpawnLocation());

    But players still get the respawn screen. Do you know why?
     
  7. Offline

    chasechocolate

    Compressions You should do some checks before casting to player. But try using if(p.getHealth() - e.getDamage() < 1).
     
  8. Offline

    Compressions

  9. Offline

    chasechocolate

    Compressions sorry, I meant for it to mean subtract (should have put –).
     
  10. Offline

    Compressions

    chasechocolate It worked, but there's a stack trace. What went wrong?

    chasechocolate Okay. In investigated the error, and it seems it happens when I spawn a mob with /spawnmob. However, the error is from EntityDamageByEntityEvent from my plugin. I don't understand 0.o

    EDIT: chasechocolate Found the issue. How would I cast the player in this case? That is the problem, and this is what I have.
    Code:java
    1. Player p = (Player) e.getEntity();

    How would I fix this? Thanks :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  11. Offline

    kreashenz

    Try using, if(e.getEntity() instanceof Player){ and check if that'd work.
     
  12. Offline

    Compressions

    kreashenz Didn't work :/ I'll try again.
     
  13. Offline

    Domi381

    Compressions Thats my code, dude:

    Code:
    @EventHandler(priority = EventPriority.NORMAL)
        public void PlayerDamageReceive(EntityDamageByEntityEvent e) {
            if(e.getEntity() instanceof Player) {
                Player damaged = (Player) e.getEntity();
             
                if(e.getDamager() instanceof Player) {
                    Player damager = (Player) e.getDamager();
                 
                    if(damaged.getWorld().getName() == plugin.worldname) {
                        if((damaged.getHealth()-e.getDamage()) <= 0) {
                         
                            //Killed
                            e.setCancelled(true);
                            damaged.teleport(plugin.spawn); // <---- plugin.spawn is the spawn-location that is defined in the main class.
                            damaged.setHealth(20);
                        }
                    }
                }
            }
        }
     
    samosaara likes this.
Thread Status:
Not open for further replies.

Share This Page