1.12.2 How do teleport instead of dying

Discussion in 'Spigot Plugin Development' started by FlameFOxYT, Feb 8, 2020.

  1. I've started developing plugins 3 weeks ago,
    I was wondering how do i teleport instead of dying
    using the PlayerDeathEvent,
    If its not possible to use PlayerDeathEvent, what other ways will help me execute this task.

    why do i need this?
    Some players like to use higher versions,
    But respawning all the time / using autorespawnplus makes so much lag.
     
  2. Use the damage event, if the final damage > player's health, cancel event, then teleport them.
     
  3. Or if the answer its not ur liking(for reasons...)
    Small delays between death and force respawn,sends them back to spawn almost instantly
     
  4. Thats exactly what i dont want to do genious
     
  5. do i use the EntityDamageEvent for that?
     
  6. If you want to be offensive, please at least use correct spelling


    Yes.
     
  7. Alright so i did it but its just disabling damage to players

    @EventHandler
    public void onLastDamage(EntityDamageEvent e) {

    if (e.getEntity() instanceof Player) {

    Player p = (Player) e.getEntity();
    if (p.getLastDamage() >= p.getHealth()) {

    e.setCancelled(true);

    }

    }

    }
     
  8. do you not want to teleport the player?
     
  9. Ye ill add it later, its not the point, the point is its disabling damage no matter how much hp you have or no matter how much damage the entity/player dealt
     
  10. event.getFinalDamage() >= p.getHealth
     
  11. I tried this but its not working

    @EventHandler
    public void onLastDamage(EntityDamageEvent e) {

    if (e.getEntity() instanceof Player) {
    if (e.getEntity().getLastDamageCause() instanceof Entity) {
    Player p = (Player) e.getEntity();
    Entity k = (Entity) p.getLastDamageCause();
    if (e.getFinalDamage() >= p.getHealth()) {
    e.setCancelled(true);

    }
    }

    }

    }
     
  12. DamageCause is no instance of Entity and will thus never work.
    Code (Text):
    if (e.getEntity().getLastDamageCause() == EntityDamageEvent.DamageCause.ENTITY_ATTACK) //...
     
  13. NVM i found the solution but 1 last question

    @EventHandler
    public void onLastDamage(EntityDamageEvent e) {

    if (e.getEntity() instanceof Player) {
    Player p = (Player) e.getEntity();
    Location loc = new Location(p.getWorld(), 10, 2, 10);
    Location loc1 = new Location(p.getWorld(), -10, 2, -10);
    if (e.getFinalDamage() >= p.getHealth()) {
    e.setCancelled(true);
    }

    }
    }

    as you can see i created loc & loc1,
    how do i create an if that the player is between the two locations?
     
  14. if (e.getEntity().getLastDamageCause().equals(EntityDamageEvent.DamageCause.ENTITY_ATTACK))
     
  15. Code (Java):
    if (p.getLocation().toVector().equals(new Vector(0, 2, 0)))
    Since I don't think that that's what you mean, can you elaborate a little more?

    No since it's an enum
     
    • Like Like x 1
  16. but when i use == its making an error

    In the locations
    im trying to check where the players is between two locations,
    if he's between 10, 2, 10 & -10, 2, -10
     
  17. You should read that error, and equals does not solve this. This does:
    Code (Java):
    final EntityDamageEvent lastDamage = event.getEntity().getLastDamageCause();
    if (lastDamage != null && lastDamage.getCause() == EntityDamageEvent.DamageCause.ENTITY_ATTACK) {
               
    }
    so if he is inside a certain box?
    Code (Java):
    final Location loc = player.getLocation();
    if (loc.getX() >= -10 && loc.getX() < 10 &&
        loc.getY() == 2 &&      // bad
        loc.getZ() >= -10 && loc.getZ() < 10)
     
    • Like Like x 1
    • Useful Useful x 1
  18. Thanks
    Here's the solution i found with your help
    @EventHandler
    public void onLastDamage(EntityDamageEvent e) {

    if (e.getEntity() instanceof Player) {
    Player p = (Player) e.getEntity();
    final EntityDamageEvent lastDamage = e.getEntity().getLastDamageCause();
    final Location loc = p.getLocation();

    if (loc.getX() >= -10 && loc.getX() <= 10 &&
    loc.getY() >= 0 && loc.getY() <= 256 &&
    loc.getZ() >= -10 && loc.getZ() <= 10) {
    if (lastDamage != null && lastDamage.getCause() == EntityDamageEvent.DamageCause.ENTITY_ATTACK) {

    if (e.getFinalDamage() >= p.getHealth()) {

    e.setCancelled(true);
    p.setHealth(p.getMaxHealth());

    }
    }

    }

    }
    }