Solved How to kill a player when they reach a certain Y axis

Discussion in 'Plugin Development' started by nrs23, Sep 6, 2013.

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

    nrs23

    hi there,

    im trying to make it so that when a player reaches a certain Y they r killed.

    Ive been fiddling around with it for ages and i cant get any code to work.

    Can someone please help me?

    Cheers,
    nrs23
     
  2. Offline

    nuclearmissile

    public void playerMove(PlayerMoveEvent e){
    Player player = e.getPlayer();
    if (player.getLocation().getY() <= 3) player.setHealth(0);

    }

    This code will kill the player if they move below 3. I'm not totally sure if this works if they stand totally still though. This is the simplest solution I could think of, but you may need to use the scheduler if you want it to check every tick or every few ticks where all the players in the server are, and kill the ones below a certain level. If you need scheduler help, I'm not going to be of any use :3
     
  3. Offline

    GaaTavares

    Code:java
    1. public void playerMove(PlayerMoveEvent e){
    2. Player player = e.getPlayer();
    3. if (player.getLocation().getY() < 10){
    4. player.setHealth(0);
    5. player.sendMessage(ChatColor.RED + "WOW! DAT PICKAXE");
    6.  
    7. }
     
  4. Offline

    Squid_Boss

    That, but with an equals next to the "<", so "<="
     
  5. Offline

    nrs23

    Sweet Thanks, ill try it out.

    Worked perfectly, to be honest thats one of the nicest pieces of code I've seen in a long time.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  6. Offline

    PoisonMondo

    Here's an event that checks every 2 seconds and applies to all players (put in onEnable):

    Code:java
    1. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    2. public void run() {
    3. for(Player all : Bukkit.getServer().getOnlinePlayers()) {
    4. if(all.getLocation().getBlockY() <= 60) {
    5. all.setHealth(0);
    6. }
    7. }
    8. }
    9. }, 0L, 40L); //runs every two seconds
     
  7. Offline

    The_Doctor_123

    PoisonMondo

    Umm.. the other code is better and more efficient. It also applies to all players, if you didn't know...
     
  8. Offline

    Compressions

    PoisonMondo Bukkit has events so you don't have to perform resource-intensive schedulers.
     
Thread Status:
Not open for further replies.

Share This Page