Solved If List<LivingEntity> contains specific mob?

Discussion in 'Plugin Development' started by Thej0y, May 20, 2013.

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

    Thej0y

    Hi, i'm trying to do a simple command that will return if the EnderDragon is dead.
    Code:
    World world = getServer().getWorld("world_the_end");
    List<LivingEntity> livemobs = world.getLivingEntities();
      if(livemobs.contains("EnderDragon")){
          sender.sendMessage("The EnderDragon is still alive!")
      }else{
          sender.sendMessage("The EnderDragon dead.")
      }
    
    Will this work? Is there a way to target a specific entity or we just use its class name(as i did)? Or maybe Listing entities is a bad idea? Once again, I rely on your pro tips! XD

    Thanks :)
     
  2. Offline

    afistofirony

    Nope. You could try:

    Code:
    public boolean hasEnderDragon(){
        World world = getServer().getWorld("world_the_end");
        if(world == null) return false; // Return false if there is no End world
        else {
            List<LivingEntity> livemobs = world.getLivingEntities();
            for (LivingEntity e : livemobs) {
                if (e instanceof EnderDragon) return true;
            }
      } return false;
    }
    This will return if there is an Ender dragon in world_the_end. Now you can use:

    Code:
    sender.sendMessage("The ender dragon is " + (hasEnderDragon() ? "still alive!" : "dead."));
     
  3. Offline

    Thej0y

    Thanks :)
     
Thread Status:
Not open for further replies.

Share This Page