Program: 117152
Project: 117152
Buggy source:
private void updateStage(){
switch (getStage()) {
case POSITION:
{
      if (getDistance(0.5,67.5,0.5) > 0.1) {
        ((WorldServer)worldObj).spawnParticle(EnumParticleTypes.END_ROD,true,posX,posY,posZ,1,0,0,0,0.01,0);
        double speed=0.1;
        Vec3D dirVec=Vec3D.getDirectionVec(new Vec3D(this),new Vec3D(0.5,67.5,0.5));
        motionX=dirVec.x * speed;
        motionY=dirVec.y * speed;
        motionZ=dirVec.z * speed;
      }
 else {
        motionX=motionY=motionZ=0;
        setPosition(0.5,67.5,0.5);
        setStage(Stage.ACQUIRE_DRAGON);
        stageTime=0;
      }
      break;
    }
case ACQUIRE_DRAGON:
{
    if (stageTime == 0) {
      if (!(worldObj.provider instanceof WorldProviderEnd)) {
        cancel();
        return;
      }
      fightManager=((WorldProviderEnd)worldObj.provider).getDragonFightManager();
      List<EntityDragon> list=worldObj.getEntities(EntityDragon.class,EntitySelectors.IS_ALIVE);
      if (fightManager.dragonKilled || list.isEmpty()) {
        cancel();
        return;
      }
      for (      EntityDragon listItem : list) {
        if (dragon == null || listItem.getDistance(0,67,0) < dragon.getDistance(0,67,0)) {
          dragon=listItem;
        }
      }
      for (      WorldGenSpikes.EndSpike genSpike : BiomeEndDecorator.getSpikesForWorld(worldObj)) {
        boolean crystalFound=false;
        for (        EntityEnderCrystal entityendercrystal : worldObj.getEntitiesWithinAABB(EntityEnderCrystal.class,genSpike.getTopBoundingBox())) {
          entityendercrystal.setBeamTarget(new BlockPos(0,65,0));
          entityendercrystal.setEntityInvulnerable(true);
          crystalFound=true;
        }
        if (!crystalFound) {
          deadCrystals.add(new BlockPos(genSpike.getCenterX() + 0.5,genSpike.getHeight() + 1,genSpike.getCenterZ() + 0.5));
        }
      }
    }
 else     if (deadCrystals.size() > 0 && stageTime > 50) {
      BlockPos pos=deadCrystals.remove(rand.nextInt(deadCrystals.size()));
      worldObj.createExplosion((Entity)null,pos.getX() + 0.5,pos.getY(),pos.getZ() + 0.5,5.0F,true);
      EntityEnderCrystal crystal=new EntityEnderCrystal(worldObj);
      crystal.setLocationAndAngles(pos.getX() + 0.5,pos.getY(),pos.getZ() + 0.5,rand.nextFloat() * 360.0F,0.0F);
      worldObj.spawnEntityInWorld(crystal);
      crystal.setEntityInvulnerable(true);
      crystal.setBeamTarget(new BlockPos(0,65,0));
      stageTime=0;
    }
    if (stageTime > 50) {
      setStage(Stage.COLLECT_SOULS);
      stageTime=0;
      return;
    }
    stageTime++;
    break;
  }
case COLLECT_SOULS:
{
  if (stageTime % 20 == 0) {
    List<EntityEnderman> list=worldObj.getEntitiesWithinAABB(EntityEnderman.class,getEntityBoundingBox().expand(300,300,300),EntitySelectors.IS_ALIVE);
    if (list.size() < 20) {
      spawnNewEnderman();
    }
    int targeting=0;
    for (    EntityEnderman enderman : list) {
      if (enderman.getDistance(0,64,0) > 60) {
        enderman.setDead();
        continue;
      }
      targeting++;
      if (targeting > 5 && enderman.getAttackTarget() == this && rand.nextInt(20) == 0) {
        EntityPlayer player=Utils.getClosestPlayer(worldObj,enderman.posX,enderman.posY,enderman.posZ,50,false);
        if (player != null) {
          enderman.setAttackTarget(player);
        }
 else {
          enderman.setAttackTarget(this);
        }
      }
 else       if (targeting > 5 && enderman.getAttackTarget() instanceof EntityPlayer) {
        continue;
      }
 else {
        enderman.setAttackTarget(this);
      }
    }
    for (int i=rand.nextInt(4); i >= 0; i--) {
      killnextEnderman();
    }
  }
  if (soulsCollected > 100) {
    stageTime=0;
    setStage(Stage.EXTRACT);
  }
  stageTime++;
  break;
}
case EXTRACT:
{
if (stageTime % 20 == 0) {
  List<EntityEnderman> list=worldObj.getEntitiesWithinAABB(EntityEnderman.class,getEntityBoundingBox().expand(300,300,300),EntitySelectors.IS_ALIVE);
  for (  EntityEnderman enderman : list) {
    enderman.setAttackTarget(this);
  }
}
double dragonDist=dragon == null ? 100 : dragon.getDistance(posX,posY,posZ);
if (dragon != null && dragonDist < 10) {
  BCEffectHandler.spawnFX(DEParticles.SOUL_EXTRACTION,worldObj,new Vec3D(dragon).add(0,2,0),new Vec3D(this),512D,3);
}
if (stageTime == 300) {
  List<Entity> list=worldObj.getEntitiesWithinAABB(Entity.class,getEntityBoundingBox().expand(300,300,300),EntitySelectors.IS_ALIVE);
  for (  Entity entity : list) {
    if (entity instanceof EntityEnderman || (entity instanceof EntityItem && ((EntityItem)entity).getEntityItem().getItem() == Items.ENDER_PEARL) || entity instanceof EntityAreaEffectCloud) {
      entity.setDead();
    }
  }
  this.worldObj.playSound((EntityPlayer)null,posX,posY,posZ,SoundEvents.ENTITY_GENERIC_EXPLODE,SoundCategory.BLOCKS,40.0F,(1.0F + (this.worldObj.rand.nextFloat() - this.worldObj.rand.nextFloat()) * 0.2F) * 0.7F);
  for (  EntityPlayer entityplayer : worldObj.playerEntities) {
    if (entityplayer.getDistance(posX,posY,posZ) < 512) {
      ((EntityPlayerMP)entityplayer).connection.sendPacket(new SPacketExplosion(posX,posY,posZ,505,Collections.<BlockPos>emptyList(),new Vec3d(0,0,0)));
    }
  }
  EntityPersistentItem entityItem=new EntityPersistentItem(worldObj,posX,posY,posZ,new ItemStack(Blocks.DRAGON_EGG));
  worldObj.spawnEntityInWorld(entityItem);
  entityItem.motionX=entityItem.motionY=entityItem.motionZ=0;
  cleanup();
  if (dragon != null) {
    dragon.getPhaseManager().setPhase(PhaseList.CHARGING_PLAYER);
  }
}
if (stageTime > 320) {
  worldObj.playSound((EntityPlayer)null,posX,posY,posZ,SoundEvents.ENTITY_ENDERMEN_STARE,SoundCategory.MASTER,10,0.7F);
  worldObj.playSound((EntityPlayer)null,posX,posY,posZ,SoundEvents.ENTITY_ENDERMEN_STARE,SoundCategory.MASTER,10,1F);
  worldObj.playSound((EntityPlayer)null,posX,posY,posZ,SoundEvents.ENTITY_ENDERMEN_STARE,SoundCategory.MASTER,10,1.3F);
  setDead();
}
if (dragon != null && (dragonDist < 10 || stageTime >= 300)) {
  stageTime++;
}
break;
}
}
}

Fixed version (ground truth):
private void updateStage(){
switch (getStage()) {
case POSITION:
{
      if (getDistance(0.5,exitPortalLocation.getY() + 0.5,0.5) > 0.1) {
        ((WorldServer)worldObj).spawnParticle(EnumParticleTypes.END_ROD,true,posX,posY,posZ,1,0,0,0,0.01,0);
        double speed=0.1;
        Vec3D dirVec=Vec3D.getDirectionVec(new Vec3D(this),new Vec3D(0.5,exitPortalLocation.getY() + 0.5,0.5));
        motionX=dirVec.x * speed;
        motionY=dirVec.y * speed;
        motionZ=dirVec.z * speed;
        stageTime++;
      }
 else {
        motionX=motionY=motionZ=0;
        setPosition(0.5,exitPortalLocation.getY() + 0.5,0.5);
        setStage(Stage.ACQUIRE_DRAGON);
        stageTime=0;
      }
      break;
    }
case ACQUIRE_DRAGON:
{
    if (stageTime == 0) {
      if (!(worldObj.provider instanceof WorldProviderEnd)) {
        cancel();
        return;
      }
      fightManager=((WorldProviderEnd)worldObj.provider).getDragonFightManager();
      List<EntityDragon> list=worldObj.getEntities(EntityDragon.class,EntitySelectors.IS_ALIVE);
      if (fightManager.dragonKilled || list.isEmpty()) {
        cancel();
        return;
      }
      for (      EntityDragon listItem : list) {
        if (dragon == null || listItem.getDistance(0,exitPortalLocation.getY() + 0.5,0) < dragon.getDistance(0,exitPortalLocation.getY() + 0.5,0)) {
          dragon=listItem;
        }
      }
      for (      WorldGenSpikes.EndSpike genSpike : BiomeEndDecorator.getSpikesForWorld(worldObj)) {
        boolean crystalFound=false;
        for (        EntityEnderCrystal entityendercrystal : worldObj.getEntitiesWithinAABB(EntityEnderCrystal.class,genSpike.getTopBoundingBox())) {
          entityendercrystal.setBeamTarget(exitPortalLocation.down(2));
          entityendercrystal.setEntityInvulnerable(true);
          crystalFound=true;
        }
        if (!crystalFound) {
          deadCrystals.add(new BlockPos(genSpike.getCenterX() + 0.5,genSpike.getHeight() + 1,genSpike.getCenterZ() + 0.5));
        }
      }
    }
 else     if (deadCrystals.size() > 0 && stageTime > 50) {
      BlockPos pos=deadCrystals.remove(rand.nextInt(deadCrystals.size()));
      worldObj.createExplosion((Entity)null,pos.getX() + 0.5,pos.getY(),pos.getZ() + 0.5,5.0F,true);
      EntityEnderCrystal crystal=new EntityEnderCrystal(worldObj);
      crystal.setLocationAndAngles(pos.getX() + 0.5,pos.getY(),pos.getZ() + 0.5,rand.nextFloat() * 360.0F,0.0F);
      worldObj.spawnEntityInWorld(crystal);
      crystal.setEntityInvulnerable(true);
      crystal.setBeamTarget(exitPortalLocation.down(2));
      stageTime=0;
    }
    if (stageTime > 50) {
      setStage(Stage.COLLECT_SOULS);
      stageTime=0;
      return;
    }
    stageTime++;
    break;
  }
case COLLECT_SOULS:
{
  if (stageTime % 20 == 0) {
    List<EntityEnderman> list=worldObj.getEntitiesWithinAABB(EntityEnderman.class,getEntityBoundingBox().expand(300,300,300),EntitySelectors.IS_ALIVE);
    if (list.size() < 20) {
      spawnNewEnderman();
    }
    int targeting=0;
    for (    EntityEnderman enderman : list) {
      if (enderman.getDistance(0,64,0) > 60) {
        enderman.setDead();
        continue;
      }
      targeting++;
      if (targeting > 5 && enderman.getAttackTarget() == this && rand.nextInt(20) == 0) {
        EntityPlayer player=Utils.getClosestPlayer(worldObj,enderman.posX,enderman.posY,enderman.posZ,50,false);
        if (player != null) {
          enderman.setAttackTarget(player);
        }
 else {
          enderman.setAttackTarget(this);
        }
      }
 else       if (targeting > 5 && enderman.getAttackTarget() instanceof EntityPlayer) {
        continue;
      }
 else {
        enderman.setAttackTarget(this);
      }
    }
    for (int i=rand.nextInt(4); i >= 0; i--) {
      killnextEnderman();
    }
  }
  if (soulsCollected > 100) {
    stageTime=0;
    setStage(Stage.EXTRACT);
  }
  stageTime++;
  break;
}
case EXTRACT:
{
if (stageTime % 20 == 0) {
  List<EntityEnderman> list=worldObj.getEntitiesWithinAABB(EntityEnderman.class,getEntityBoundingBox().expand(300,300,300),EntitySelectors.IS_ALIVE);
  for (  EntityEnderman enderman : list) {
    enderman.setAttackTarget(this);
  }
}
double dragonDist=dragon == null ? 100 : dragon.getDistance(posX,posY,posZ);
if (dragon != null && dragonDist < 10) {
  BCEffectHandler.spawnFX(DEParticles.SOUL_EXTRACTION,worldObj,new Vec3D(dragon).add(0,2,0),new Vec3D(this),512D,3);
}
if (stageTime == 300) {
  List<Entity> list=worldObj.getEntitiesWithinAABB(Entity.class,getEntityBoundingBox().expand(300,300,300),EntitySelectors.IS_ALIVE);
  for (  Entity entity : list) {
    if (entity instanceof EntityEnderman || (entity instanceof EntityItem && ((EntityItem)entity).getEntityItem().getItem() == Items.ENDER_PEARL) || entity instanceof EntityAreaEffectCloud) {
      entity.setDead();
    }
  }
  this.worldObj.playSound((EntityPlayer)null,posX,posY,posZ,SoundEvents.ENTITY_GENERIC_EXPLODE,SoundCategory.BLOCKS,40.0F,(1.0F + (this.worldObj.rand.nextFloat() - this.worldObj.rand.nextFloat()) * 0.2F) * 0.7F);
  PacketCustom.sendToAllAround(new SPacketExplosion(posX,posY,posZ,505,Collections.<BlockPos>emptyList(),new Vec3d(0,0,0)),posX,posY,posZ,512,1);
  EntityPersistentItem entityItem=new EntityPersistentItem(worldObj,posX,posY,posZ,new ItemStack(Blocks.DRAGON_EGG));
  worldObj.spawnEntityInWorld(entityItem);
  entityItem.motionX=entityItem.motionY=entityItem.motionZ=0;
  cleanup();
  if (dragon != null) {
    dragon.getPhaseManager().setPhase(PhaseList.CHARGING_PLAYER);
  }
}
if (stageTime > 320) {
  worldObj.playSound((EntityPlayer)null,posX,posY,posZ,SoundEvents.ENTITY_ENDERMEN_STARE,SoundCategory.MASTER,10,0.7F);
  worldObj.playSound((EntityPlayer)null,posX,posY,posZ,SoundEvents.ENTITY_ENDERMEN_STARE,SoundCategory.MASTER,10,1F);
  worldObj.playSound((EntityPlayer)null,posX,posY,posZ,SoundEvents.ENTITY_ENDERMEN_STARE,SoundCategory.MASTER,10,1.3F);
  setDead();
}
if (dragon != null && (dragonDist < 10 || stageTime >= 300)) {
  stageTime++;
}
break;
}
}
}