How can I grab a winner?

Discussion in 'Spigot Plugin Development' started by Vrsd12, May 6, 2017.

  1. How could I create a public void that when I die the player1 teleport to spawn and player 2 also.
    sorry for my english
     
  2. Player p = event.getEntity()
    Player p2 evento.getEntity().getKiller()
    ????
     
  3. Test it out! Find what works best!
     
  4. Gianluca

    Gianluca Retired Resource Staff
    Retired Patron

    If you want to "force" respawn a player when they die, take a look at Player.Spigot()
     
  5. I think he want to teleport both players to spawn. There is a World#getSpawn (or something like that) you can use
     
    • Like Like x 1
  6. Set their health to 20 on death event, then teleport them to the world spawn.
     
  7. That would only work I believe if they cancel the death event


    Sent from my iPhone using Tapatalk
     
  8. They don't need to cancel it.
    Beware, spoonfeed incoming.
    Code (Text):
        @EventHandler
        public void onPlayerDeathEvent(PlayerDeathEvent e) {
            Player p = e.getEntity();
            if (p.isDead()) {
                p.setHealth(20);
                }
            }
        }
     
  9. Yeah, but you can't teleport a dead player can you? Im not sure, but it would make sense not to be able to.
     
  10. Put the teleport code after p.setHealth(20);
     
  11. I'm not saying you can't code it in, but will it work if you do?


    Sent from my iPhone using Tapatalk
     
  12. Try listening to an EntityDamageByEntityEvent, test if the event's final damage exceeds player 1's health, and if so, cancel it and teleport player 1 and the entity who caused the damage then.
     
  13. Thank you all now I notice that I do !!
     
  14. How about something like that:

    @EventHandler
    public void onPlayerDeathEvent(PlayerDeathEvent e) {
    Player p = e.getEntity();
    p.teleport()//spawn//
    }
    }

    @EventHandler
    public void onDamage(EntityDamageByEntityEvent e) {
    Player p = e.getEntity().getKiller();
    p.teleport()//spawn//
    }
    }
     
  15. Moreso something along these lines:

    Code (Text):
    public void onDamage(EntityDamageByEntityEvent e) {
        if (!(e.getEntity() instanceof Player) || (!(e.getDamager() instanceof Player)) return;
        Player damaged = (Player) e.getEntity();
        Player damager = (Player) e.getDamager();

       //pseudo code
        if damaged's health is less than getDamage //means the player will die
        cancel the event
        set both players health to 20
        teleport them both to a location //spawn
    }
     
    • Like Like x 3
  16. Frist , on PlayerDeathEvent , you cannot tp a player when they not respawning..
    Second, on EntityDamageByEntityEvent , you just tp them when they hit player...
    Code (Text):
    @EventHandler
        public void onPlayerDeath(PlayerDeathEvent e) {
            //create spawn point
            Location spawn = new Location(Bukkit.getServer().getWorld("World"), X, Y, Z);
            //skip the respawn button
            ((CraftPlayer) e.getEntity()).getHandle().playerConnection.a(new PacketPlayInClientCommand(EnumClientCommand.PERFORM_RESPAWN));
            //TP the player who dead back to spawn
            e.getEntity().teleport(spawn);
           
            //TP the player who kill someone to spawn
            e.getEntity().getKiller().teleport(spawn);
        }
    That should work fine
     
  17. You don't need NMS at ALL to achieve this... @pvprealm 's solution is the perfect one.
     
  18. You don't tp them when they get hit by the player, you tp them when their health would drop below 0, i.e they would die. Read the code... ;/