How could I create a public void that when I die the player1 teleport to spawn and player 2 also. sorry for my english
Grab the players with the PlayerDeathEvent EDIT: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/PlayerDeathEvent.html
I think he want to teleport both players to spawn. There is a World#getSpawn (or something like that) you can use
If you just want the player sent to spawn when he dies, use the PlayerRespawnEvent and teleport them to your location on respawn https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/event/player/PlayerRespawnEvent.html Edit: For the other player, use the PlayerDeathEvent, get the killer and send him to spawn too
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); } } }
Yeah, but you can't teleport a dead player can you? Im not sure, but it would make sense not to be able to.
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.
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// } }
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 }
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
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... ;/